GeezerVison is showing github.com — worker: add name for worker · nodejs/node@eecd327
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/worker_threads.md +24Lines changed: 24 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -721,6 +721,17 @@ An integer identifier for the current thread. On the corresponding worker object | 721 | 721 | (if there is any), it is available as [`worker.threadId`][]. | 722 | 722 | This value is unique for each [`Worker`][] instance inside a single process. | 723 | 723 |
| | 724 | +## `worker.threadName` | | 725 | +
| | 726 | +<!-- YAML | | 727 | +added: REPLACEME | | 728 | +--> | | 729 | +
| | 730 | +* {string|null} | | 731 | +
| | 732 | +A string identifier for the current thread or null if the thread is not running. | | 733 | +On the corresponding worker object (if there is any), it is available as [`worker.threadName`][]. | | 734 | +
| 724 | 735 | ## `worker.workerData` | 725 | 736 |
| 726 | 737 | <!-- YAML | @@ -2011,6 +2022,17 @@ An integer identifier for the referenced thread. Inside the worker thread, | 2011 | 2022 | it is available as [`require('node:worker_threads').threadId`][]. | 2012 | 2023 | This value is unique for each `Worker` instance inside a single process. | 2013 | 2024 |
| | 2025 | +### `worker.threadName` | | 2026 | + | | 2027 | +<!-- YAML | | 2028 | +added: REPLACEME | | 2029 | +--> | | 2030 | + | | 2031 | +* {string|null} | | 2032 | + | | 2033 | +A string identifier for the referenced thread or null if the thread is not running. | | 2034 | +Inside the worker thread, it is available as [`require('node:worker_threads').threadName`][]. | | 2035 | + | 2014 | 2036 | ### `worker.unref()` | 2015 | 2037 |
| 2016 | 2038 | <!-- YAML | @@ -2139,6 +2161,7 @@ thread spawned will spawn another until the application crashes. | 2139 | 2161 | [`require('node:worker_threads').parentPort.postMessage()`]: #workerpostmessagevalue-transferlist | 2140 | 2162 | [`require('node:worker_threads').parentPort`]: #workerparentport | 2141 | 2163 | [`require('node:worker_threads').threadId`]: #workerthreadid | | 2164 | +[`require('node:worker_threads').threadName`]: #workerthreadname | 2142 | 2165 | [`require('node:worker_threads').workerData`]: #workerworkerdata | 2143 | 2166 | [`trace_events`]: tracing.md | 2144 | 2167 | [`v8.getHeapSnapshot()`]: v8.md#v8getheapsnapshotoptions | @@ -2149,6 +2172,7 @@ thread spawned will spawn another until the application crashes. | 2149 | 2172 | [`worker.postMessage()`]: #workerpostmessagevalue-transferlist | 2150 | 2173 | [`worker.terminate()`]: #workerterminate | 2151 | 2174 | [`worker.threadId`]: #workerthreadid_1 | | 2175 | +[`worker.threadName`]: #workerthreadname_1 | 2152 | 2176 | [async-resource-worker-pool]: async_context.md#using-asyncresource-for-a-worker-thread-pool | 2153 | 2177 | [browser `LockManager`]: https://developer.mozilla.org/en-US/docs/Web/API/LockManager | 2154 | 2178 | [browser `MessagePort`]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/worker.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,6 +72,7 @@ const { | 72 | 72 | isInternalThread, | 73 | 73 | resourceLimits: resourceLimitsRaw, | 74 | 74 | threadId, | | 75 | + threadName, | 75 | 76 | Worker: WorkerImpl, | 76 | 77 | kMaxYoungGenerationSizeMb, | 77 | 78 | kMaxOldGenerationSizeMb, | @@ -433,6 +434,12 @@ class Worker extends EventEmitter { | 433 | 434 | return this[kHandle].threadId; | 434 | 435 | } | 435 | 436 |
| | 437 | + get threadName() { | | 438 | + if (this[kHandle] === null) return null; | | 439 | + | | 440 | + return this[kHandle].threadName; | | 441 | + } | | 442 | + | 436 | 443 | get stdin() { | 437 | 444 | return this[kParentSideStdio].stdin; | 438 | 445 | } | @@ -597,6 +604,7 @@ module.exports = { | 597 | 604 | getEnvironmentData, | 598 | 605 | assignEnvironmentData, | 599 | 606 | threadId, | | 607 | + threadName, | 600 | 608 | InternalWorker, | 601 | 609 | Worker, | 602 | 610 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/worker_threads.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | setEnvironmentData, | 9 | 9 | getEnvironmentData, | 10 | 10 | threadId, | | 11 | + threadName, | 11 | 12 | Worker, | 12 | 13 | } = require('internal/worker'); | 13 | 14 |
| @@ -44,6 +45,7 @@ module.exports = { | 44 | 45 | resourceLimits, | 45 | 46 | postMessageToThread, | 46 | 47 | threadId, | | 48 | + threadName, | 47 | 49 | SHARE_ENV, | 48 | 50 | Worker, | 49 | 51 | parentPort: null, | |
Collapse file Copy file name to clipboardExpand all lines: src/api/environment.cc +21-1Lines changed: 21 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -414,6 +414,25 @@ Environment* CreateEnvironment( | 414 | 414 | EnvironmentFlags::Flags flags, | 415 | 415 | ThreadId thread_id, | 416 | 416 | std::unique_ptr<InspectorParentHandle> inspector_parent_handle) { | | 417 | + return CreateEnvironment(isolate_data, | | 418 | + context, | | 419 | + args, | | 420 | + exec_args, | | 421 | + flags, | | 422 | + thread_id, | | 423 | + std::move(inspector_parent_handle), | | 424 | + {}); | | 425 | +} | | 426 | + | | 427 | +Environment* CreateEnvironment( | | 428 | + IsolateData* isolate_data, | | 429 | + Local<Context> context, | | 430 | + const std::vector<std::string>& args, | | 431 | + const std::vector<std::string>& exec_args, | | 432 | + EnvironmentFlags::Flags flags, | | 433 | + ThreadId thread_id, | | 434 | + std::unique_ptr<InspectorParentHandle> inspector_parent_handle, | | 435 | + std::string_view thread_name) { | 417 | 436 | Isolate* isolate = isolate_data->isolate(); | 418 | 437 |
| 419 | 438 | Isolate::Scope isolate_scope(isolate); | @@ -434,7 +453,8 @@ Environment* CreateEnvironment( | 434 | 453 | exec_args, | 435 | 454 | env_snapshot_info, | 436 | 455 | flags, | 437 | | - thread_id); | | 456 | + thread_id, | | 457 | + thread_name); | 438 | 458 | CHECK_NOT_NULL(env); | 439 | 459 |
| 440 | 460 | if (use_snapshot) { | |
Collapse file Copy file name to clipboardExpand all lines: src/env-inl.h +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -703,6 +703,10 @@ inline uint64_t Environment::thread_id() const { | 703 | 703 | return thread_id_; | 704 | 704 | } | 705 | 705 |
| | 706 | +inline std::string_view Environment::thread_name() const { | | 707 | + return thread_name_; | | 708 | +} | | 709 | + | 706 | 710 | inline worker::Worker* Environment::worker_context() const { | 707 | 711 | return isolate_data()->worker_context(); | 708 | 712 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/env.cc +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -784,7 +784,8 @@ Environment::Environment(IsolateData* isolate_data, | 784 | 784 | const std::vector<std::string>& exec_args, | 785 | 785 | const EnvSerializeInfo* env_info, | 786 | 786 | EnvironmentFlags::Flags flags, | 787 | | - ThreadId thread_id) | | 787 | + ThreadId thread_id, | | 788 | + std::string_view thread_name) | 788 | 789 | : isolate_(isolate), | 789 | 790 | external_memory_accounter_(new ExternalMemoryAccounter()), | 790 | 791 | isolate_data_(isolate_data), | @@ -811,7 +812,8 @@ Environment::Environment(IsolateData* isolate_data, | 811 | 812 | flags_(flags), | 812 | 813 | thread_id_(thread_id.id == static_cast<uint64_t>(-1) | 813 | 814 | ? AllocateEnvironmentThreadId().id | 814 | | - : thread_id.id) { | | 815 | + : thread_id.id), | | 816 | + thread_name_(thread_name) { | 815 | 817 | if (!is_main_thread()) { | 816 | 818 | // If this is a Worker thread, we can always safely use the parent's | 817 | 819 | // Isolate's code cache because of the shared read-only heap. | |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -660,7 +660,8 @@ class Environment final : public MemoryRetainer { | 660 | 660 | const std::vector<std::string>& exec_args, | 661 | 661 | const EnvSerializeInfo* env_info, | 662 | 662 | EnvironmentFlags::Flags flags, | 663 | | - ThreadId thread_id); | | 663 | + ThreadId thread_id, | | 664 | + std::string_view thread_name = ""); | 664 | 665 | void InitializeMainContext(v8::Local<v8::Context> context, | 665 | 666 | const EnvSerializeInfo* env_info); | 666 | 667 | ~Environment() override; | @@ -807,6 +808,7 @@ class Environment final : public MemoryRetainer { | 807 | 808 | inline bool should_start_debug_signal_handler() const; | 808 | 809 | inline bool no_browser_globals() const; | 809 | 810 | inline uint64_t thread_id() const; | | 811 | + inline std::string_view thread_name() const; | 810 | 812 | inline worker::Worker* worker_context() const; | 811 | 813 | Environment* worker_parent_env() const; | 812 | 814 | inline void add_sub_worker_context(worker::Worker* context); | @@ -1176,6 +1178,7 @@ class Environment final : public MemoryRetainer { | 1176 | 1178 |
| 1177 | 1179 | uint64_t flags_; | 1178 | 1180 | uint64_t thread_id_; | | 1181 | + std::string thread_name_; | 1179 | 1182 | std::unordered_set<worker::Worker*> sub_worker_contexts_; | 1180 | 1183 |
| 1181 | 1184 | #if HAVE_INSPECTOR | |
Collapse file Copy file name to clipboardExpand all lines: src/env_properties.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 390 | 390 | V(table_string, "table") \ | 391 | 391 | V(target_string, "target") \ | 392 | 392 | V(thread_id_string, "threadId") \ | | 393 | + V(thread_name_string, "threadName") \ | 393 | 394 | V(ticketkeycallback_string, "onticketkeycallback") \ | 394 | 395 | V(timeout_string, "timeout") \ | 395 | 396 | V(time_to_first_byte_string, "timeToFirstByte") \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node.h +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -730,6 +730,16 @@ NODE_EXTERN Environment* CreateEnvironment( | 730 | 730 | ThreadId thread_id = {} /* allocates a thread id automatically */, | 731 | 731 | std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {}); | 732 | 732 |
| | 733 | +NODE_EXTERN Environment* CreateEnvironment( | | 734 | + IsolateData* isolate_data, | | 735 | + v8::Local<v8::Context> context, | | 736 | + const std::vector<std::string>& args, | | 737 | + const std::vector<std::string>& exec_args, | | 738 | + EnvironmentFlags::Flags flags, | | 739 | + ThreadId thread_id, | | 740 | + std::unique_ptr<InspectorParentHandle> inspector_parent_handle, | | 741 | + std::string_view thread_name); | | 742 | + | 733 | 743 | // Returns a handle that can be passed to `LoadEnvironment()`, making the | 734 | 744 | // child Environment accessible to the inspector as if it were a Node.js Worker. | 735 | 745 | // `child_thread_id` can be created using `AllocateEnvironmentThreadId()` | |
Collapse file Copy file name to clipboardExpand all lines: src/node_worker.cc +22-1Lines changed: 22 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,6 +33,7 @@ using v8::Local; | 33 | 33 | using v8::Locker; | 34 | 34 | using v8::Maybe; | 35 | 35 | using v8::Name; | | 36 | +using v8::NewStringType; | 36 | 37 | using v8::Null; | 37 | 38 | using v8::Number; | 38 | 39 | using v8::Object; | @@ -89,6 +90,15 @@ Worker::Worker(Environment* env, | 89 | 90 | Number::New(env->isolate(), static_cast<double>(thread_id_.id))) | 90 | 91 | .Check(); | 91 | 92 |
| | 93 | + object() | | 94 | + ->Set(env->context(), | | 95 | + env->thread_name_string(), | | 96 | + String::NewFromUtf8(env->isolate(), | | 97 | + name_.data(), | | 98 | + NewStringType::kNormal, | | 99 | + name_.size()) | | 100 | + .ToLocalChecked()) | | 101 | + .Check(); | 92 | 102 | // Without this check, to use the permission model with | 93 | 103 | // workers (--allow-worker) one would need to pass --allow-inspector as well | 94 | 104 | if (env->permission()->is_granted( | @@ -365,7 +375,8 @@ void Worker::Run() { | 365 | 375 | std::move(exec_argv_), | 366 | 376 | static_cast<EnvironmentFlags::Flags>(environment_flags_), | 367 | 377 | thread_id_, | 368 | | - std::move(inspector_parent_handle_))); | | 378 | + std::move(inspector_parent_handle_), | | 379 | + name_)); | 369 | 380 | if (is_stopped()) return; | 370 | 381 | CHECK_NOT_NULL(env_); | 371 | 382 | env_->set_env_vars(std::move(env_vars_)); | @@ -1239,6 +1250,16 @@ void CreateWorkerPerContextProperties(Local<Object> target, | 1239 | 1250 | Number::New(isolate, static_cast<double>(env->thread_id()))) | 1240 | 1251 | .Check(); | 1241 | 1252 |
| | 1253 | + target | | 1254 | + ->Set(env->context(), | | 1255 | + env->thread_name_string(), | | 1256 | + String::NewFromUtf8(isolate, | | 1257 | + env->thread_name().data(), | | 1258 | + NewStringType::kNormal, | | 1259 | + env->thread_name().size()) | | 1260 | + .ToLocalChecked()) | | 1261 | + .Check(); | | 1262 | + | 1242 | 1263 | target | 1243 | 1264 | ->Set(env->context(), | 1244 | 1265 | FIXED_ONE_BYTE_STRING(isolate, "isMainThread"), | |
|