src: set thread name for main thread and v8 worker · nodejs/node@f4b086d · GitHub
Skip to content

Commit f4b086d

Browse files
RafaelGSStargos
authored andcommitted
src: set thread name for main thread and v8 worker
PR-URL: #56416 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent 3579143 commit f4b086d

5 files changed

Lines changed: 75 additions & 0 deletions

File tree

src/node.cc

Lines changed: 1 addition & 0 deletions

src/node_platform.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct PlatformWorkerData {
2525
};
2626

2727
static void PlatformWorkerThread(void* data) {
28+
uv_thread_setname("V8Worker");
2829
std::unique_ptr<PlatformWorkerData>
2930
worker_data(static_cast<PlatformWorkerData*>(data));
3031

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <node.h>
2+
#include <uv.h>
3+
#include <v8.h>
4+
5+
using v8::FunctionCallbackInfo;
6+
using v8::Isolate;
7+
using v8::String;
8+
using v8::Value;
9+
10+
void GetThreadName(const FunctionCallbackInfo<Value>& args) {
11+
Isolate* isolate = args.GetIsolate();
12+
uv_thread_t thread;
13+
char thread_name[16];
14+
#ifdef _WIN32
15+
/* uv_thread_self isn't defined for the main thread on Windows. */
16+
thread = GetCurrentThread();
17+
#else
18+
thread = uv_thread_self();
19+
#endif
20+
uv_thread_getname(&thread, thread_name, sizeof(thread_name));
21+
args.GetReturnValue().Set(
22+
String::NewFromUtf8(isolate, thread_name).ToLocalChecked());
23+
}
24+
25+
void init(v8::Local<v8::Object> exports) {
26+
NODE_SET_METHOD(exports, "getThreadName", GetThreadName);
27+
}
28+
29+
NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, init)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': [ 'binding.cc' ],
6+
'includes': ['../common.gypi'],
7+
}
8+
]
9+
}

test/addons/uv-thread-name/test.js

Lines changed: 35 additions & 0 deletions

0 commit comments

Comments
 (0)