-
Notifications
You must be signed in to change notification settings - Fork 30.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'sources': [ 'binding.cc' ], | ||
'includes': ['../common.gypi'], | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |