Skip to content

Commit

Permalink
src: set thread name for main thread and v8 worker
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
RafaelGSS authored and targos committed Feb 7, 2025
1 parent 3579143 commit f4b086d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
}

if (!(flags & ProcessInitializationFlags::kNoInitializeNodeV8Platform)) {
uv_thread_setname("MainThread");
per_process::v8_platform.Initialize(
static_cast<int>(per_process::cli_options->v8_thread_pool_size));
result->platform_ = per_process::v8_platform.Platform();
Expand Down
1 change: 1 addition & 0 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct PlatformWorkerData {
};

static void PlatformWorkerThread(void* data) {
uv_thread_setname("V8Worker");
std::unique_ptr<PlatformWorkerData>
worker_data(static_cast<PlatformWorkerData*>(data));

Expand Down
29 changes: 29 additions & 0 deletions test/addons/uv-thread-name/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <node.h>
#include <uv.h>
#include <v8.h>

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::String;
using v8::Value;

void GetThreadName(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
uv_thread_t thread;
char thread_name[16];
#ifdef _WIN32
/* uv_thread_self isn't defined for the main thread on Windows. */
thread = GetCurrentThread();
#else
thread = uv_thread_self();
#endif
uv_thread_getname(&thread, thread_name, sizeof(thread_name));
args.GetReturnValue().Set(
String::NewFromUtf8(isolate, thread_name).ToLocalChecked());
}

void init(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "getThreadName", GetThreadName);
}

NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, init)
9 changes: 9 additions & 0 deletions test/addons/uv-thread-name/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'binding.cc' ],
'includes': ['../common.gypi'],
}
]
}
35 changes: 35 additions & 0 deletions test/addons/uv-thread-name/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
const common = require('../../common');

if (common.isAIX) {
common.skip('AIX is not supported by libuv');
}

const assert = require('node:assert');
const { parentPort, Worker, isMainThread } = require('node:worker_threads');
const bindingPath = require.resolve(`./build/${common.buildType}/binding`);
const binding = require(bindingPath);

if (isMainThread) {
assert.strictEqual(binding.getThreadName(), 'MainThread');

const worker = new Worker(__filename);
worker.on('message', common.mustCall((data) => {
assert.strictEqual(data, 'WorkerThread');
}));
worker.on('error', common.mustNotCall());
worker.on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
}));

const namedWorker = new Worker(__filename, { name: 'NamedThread' });
namedWorker.on('message', common.mustCall((data) => {
assert.strictEqual(data, 'NamedThread');
}));
namedWorker.on('error', common.mustNotCall());
namedWorker.on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
}));
} else {
parentPort.postMessage(binding.getThreadName());
}

0 comments on commit f4b086d

Please sign in to comment.