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

5 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -1165,6 +1165,7 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,

11651165

}

11661166
11671167

if (!(flags & ProcessInitializationFlags::kNoInitializeNodeV8Platform)) {

1168+

uv_thread_setname("MainThread");

11681169

per_process::v8_platform.Initialize(

11691170

static_cast<int>(per_process::cli_options->v8_thread_pool_size));

11701171

result->platform_ = per_process::v8_platform.Platform();

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
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)

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+

}

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,35 @@

1+

'use strict';

2+

const common = require('../../common');

3+
4+

if (common.isAIX) {

5+

common.skip('AIX is not supported by libuv');

6+

}

7+
8+

const assert = require('node:assert');

9+

const { parentPort, Worker, isMainThread } = require('node:worker_threads');

10+

const bindingPath = require.resolve(`./build/${common.buildType}/binding`);

11+

const binding = require(bindingPath);

12+
13+

if (isMainThread) {

14+

assert.strictEqual(binding.getThreadName(), 'MainThread');

15+
16+

const worker = new Worker(__filename);

17+

worker.on('message', common.mustCall((data) => {

18+

assert.strictEqual(data, 'WorkerThread');

19+

}));

20+

worker.on('error', common.mustNotCall());

21+

worker.on('exit', common.mustCall((code) => {

22+

assert.strictEqual(code, 0);

23+

}));

24+
25+

const namedWorker = new Worker(__filename, { name: 'NamedThread' });

26+

namedWorker.on('message', common.mustCall((data) => {

27+

assert.strictEqual(data, 'NamedThread');

28+

}));

29+

namedWorker.on('error', common.mustNotCall());

30+

namedWorker.on('exit', common.mustCall((code) => {

31+

assert.strictEqual(code, 0);

32+

}));

33+

} else {

34+

parentPort.postMessage(binding.getThreadName());

35+

}