| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -715,6 +715,17 @@ An integer identifier for the current thread. On the corresponding worker object | |||
| 715 | 715 | (if there is any), it is available as [`worker.threadId`][]. | |
| 716 | 716 | This value is unique for each [`Worker`][] instance inside a single process. | |
| 717 | 717 | ||
| 718 | + ## `worker.threadName` | ||
| 719 | + | ||
| 720 | + <!-- YAML | ||
| 721 | + added: REPLACEME | ||
| 722 | + --> | ||
| 723 | + | ||
| 724 | + * {string|null} | ||
| 725 | + | ||
| 726 | + A string identifier for the current thread or null if the thread is not running. | ||
| 727 | + On the corresponding worker object (if there is any), it is available as [`worker.threadName`][]. | ||
| 728 | + | ||
| 718 | 729 | ## `worker.workerData` | |
| 719 | 730 | ||
| 720 | 731 | <!-- YAML | |
@@ -1849,6 +1860,17 @@ An integer identifier for the referenced thread. Inside the worker thread, | |||
| 1849 | 1860 | it is available as [`require('node:worker_threads').threadId`][]. | |
| 1850 | 1861 | This value is unique for each `Worker` instance inside a single process. | |
| 1851 | 1862 | ||
| 1863 | + ### `worker.threadName` | ||
| 1864 | + | ||
| 1865 | + <!-- YAML | ||
| 1866 | + added: REPLACEME | ||
| 1867 | + --> | ||
| 1868 | + | ||
| 1869 | + * {string|null} | ||
| 1870 | + | ||
| 1871 | + A string identifier for the referenced thread or null if the thread is not running. | ||
| 1872 | + Inside the worker thread, it is available as [`require('node:worker_threads').threadName`][]. | ||
| 1873 | + | ||
| 1852 | 1874 | ### `worker.unref()` | |
| 1853 | 1875 | ||
| 1854 | 1876 | <!-- YAML | |
@@ -1969,6 +1991,7 @@ thread spawned will spawn another until the application crashes. | |||
| 1969 | 1991 | [`require('node:worker_threads').parentPort.postMessage()`]: #workerpostmessagevalue-transferlist | |
| 1970 | 1992 | [`require('node:worker_threads').parentPort`]: #workerparentport | |
| 1971 | 1993 | [`require('node:worker_threads').threadId`]: #workerthreadid | |
| 1994 | + [`require('node:worker_threads').threadName`]: #workerthreadname | ||
| 1972 | 1995 | [`require('node:worker_threads').workerData`]: #workerworkerdata | |
| 1973 | 1996 | [`trace_events`]: tracing.md | |
| 1974 | 1997 | [`v8.getHeapSnapshot()`]: v8.md#v8getheapsnapshotoptions | |
@@ -1979,6 +2002,7 @@ thread spawned will spawn another until the application crashes. | |||
| 1979 | 2002 | [`worker.postMessage()`]: #workerpostmessagevalue-transferlist | |
| 1980 | 2003 | [`worker.terminate()`]: #workerterminate | |
| 1981 | 2004 | [`worker.threadId`]: #workerthreadid_1 | |
| 2005 | + [`worker.threadName`]: #workerthreadname_1 | ||
| 1982 | 2006 | [async-resource-worker-pool]: async_context.md#using-asyncresource-for-a-worker-thread-pool | |
| 1983 | 2007 | [browser `MessagePort`]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort | |
| 1984 | 2008 | [child processes]: child_process.md | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,6 +71,7 @@ const { | |||
| 71 | 71 | isInternalThread, | |
| 72 | 72 | resourceLimits: resourceLimitsRaw, | |
| 73 | 73 | threadId, | |
| 74 | + threadName, | ||
| 74 | 75 | Worker: WorkerImpl, | |
| 75 | 76 | kMaxYoungGenerationSizeMb, | |
| 76 | 77 | kMaxOldGenerationSizeMb, | |
@@ -432,6 +433,12 @@ class Worker extends EventEmitter { | |||
| 432 | 433 | return this[kHandle].threadId; | |
| 433 | 434 | } | |
| 434 | 435 | ||
| 436 | + get threadName() { | ||
| 437 | + if (this[kHandle] === null) return null; | ||
| 438 | + | ||
| 439 | + return this[kHandle].threadName; | ||
| 440 | + } | ||
| 441 | + | ||
| 435 | 442 | get stdin() { | |
| 436 | 443 | return this[kParentSideStdio].stdin; | |
| 437 | 444 | } | |
@@ -596,6 +603,7 @@ module.exports = { | |||
| 596 | 603 | getEnvironmentData, | |
| 597 | 604 | assignEnvironmentData, | |
| 598 | 605 | threadId, | |
| 606 | + threadName, | ||
| 599 | 607 | InternalWorker, | |
| 600 | 608 | Worker, | |
| 601 | 609 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ const { | |||
| 8 | 8 | setEnvironmentData, | |
| 9 | 9 | getEnvironmentData, | |
| 10 | 10 | threadId, | |
| 11 | + threadName, | ||
| 11 | 12 | Worker, | |
| 12 | 13 | } = require('internal/worker'); | |
| 13 | 14 | ||
@@ -42,6 +43,7 @@ module.exports = { | |||
| 42 | 43 | resourceLimits, | |
| 43 | 44 | postMessageToThread, | |
| 44 | 45 | threadId, | |
| 46 | + threadName, | ||
| 45 | 47 | SHARE_ENV, | |
| 46 | 48 | Worker, | |
| 47 | 49 | parentPort: null, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -405,6 +405,25 @@ Environment* CreateEnvironment( | |||
| 405 | 405 | EnvironmentFlags::Flags flags, | |
| 406 | 406 | ThreadId thread_id, | |
| 407 | 407 | std::unique_ptr<InspectorParentHandle> inspector_parent_handle) { | |
| 408 | + return CreateEnvironment(isolate_data, | ||
| 409 | + context, | ||
| 410 | + args, | ||
| 411 | + exec_args, | ||
| 412 | + flags, | ||
| 413 | + thread_id, | ||
| 414 | + std::move(inspector_parent_handle), | ||
| 415 | + {}); | ||
| 416 | + } | ||
| 417 | + | ||
| 418 | + Environment* CreateEnvironment( | ||
| 419 | + IsolateData* isolate_data, | ||
| 420 | + Local<Context> context, | ||
| 421 | + const std::vector<std::string>& args, | ||
| 422 | + const std::vector<std::string>& exec_args, | ||
| 423 | + EnvironmentFlags::Flags flags, | ||
| 424 | + ThreadId thread_id, | ||
| 425 | + std::unique_ptr<InspectorParentHandle> inspector_parent_handle, | ||
| 426 | + std::string_view thread_name) { | ||
| 408 | 427 | Isolate* isolate = isolate_data->isolate(); | |
| 409 | 428 | ||
| 410 | 429 | Isolate::Scope isolate_scope(isolate); | |
@@ -425,7 +444,8 @@ Environment* CreateEnvironment( | |||
| 425 | 444 | exec_args, | |
| 426 | 445 | env_snapshot_info, | |
| 427 | 446 | flags, | |
| 428 | - thread_id); | ||
| 447 | + thread_id, | ||
| 448 | + thread_name); | ||
| 429 | 449 | CHECK_NOT_NULL(env); | |
| 430 | 450 | ||
| 431 | 451 | if (use_snapshot) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -729,6 +729,10 @@ inline uint64_t Environment::thread_id() const { | |||
| 729 | 729 | return thread_id_; | |
| 730 | 730 | } | |
| 731 | 731 | ||
| 732 | + inline std::string_view Environment::thread_name() const { | ||
| 733 | + return thread_name_; | ||
| 734 | + } | ||
| 735 | + | ||
| 732 | 736 | inline worker::Worker* Environment::worker_context() const { | |
| 733 | 737 | return isolate_data()->worker_context(); | |
| 734 | 738 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -803,7 +803,8 @@ Environment::Environment(IsolateData* isolate_data, | |||
| 803 | 803 | const std::vector<std::string>& exec_args, | |
| 804 | 804 | const EnvSerializeInfo* env_info, | |
| 805 | 805 | EnvironmentFlags::Flags flags, | |
| 806 | - ThreadId thread_id) | ||
| 806 | + ThreadId thread_id, | ||
| 807 | + std::string_view thread_name) | ||
| 807 | 808 | : isolate_(isolate), | |
| 808 | 809 | isolate_data_(isolate_data), | |
| 809 | 810 | async_hooks_(isolate, MAYBE_FIELD_PTR(env_info, async_hooks)), | |
@@ -829,7 +830,8 @@ Environment::Environment(IsolateData* isolate_data, | |||
| 829 | 830 | flags_(flags), | |
| 830 | 831 | thread_id_(thread_id.id == static_cast<uint64_t>(-1) | |
| 831 | 832 | ? AllocateEnvironmentThreadId().id | |
| 832 | - : thread_id.id) { | ||
| 833 | + : thread_id.id), | ||
| 834 | + thread_name_(thread_name) { | ||
| 833 | 835 | constexpr bool is_shared_ro_heap = | |
| 834 | 836 | #ifdef NODE_V8_SHARED_RO_HEAP | |
| 835 | 837 | true; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -681,7 +681,8 @@ class Environment final : public MemoryRetainer { | |||
| 681 | 681 | const std::vector<std::string>& exec_args, | |
| 682 | 682 | const EnvSerializeInfo* env_info, | |
| 683 | 683 | EnvironmentFlags::Flags flags, | |
| 684 | - ThreadId thread_id); | ||
| 684 | + ThreadId thread_id, | ||
| 685 | + std::string_view thread_name = ""); | ||
| 685 | 686 | void InitializeMainContext(v8::Local<v8::Context> context, | |
| 686 | 687 | const EnvSerializeInfo* env_info); | |
| 687 | 688 | ~Environment() override; | |
@@ -826,6 +827,7 @@ class Environment final : public MemoryRetainer { | |||
| 826 | 827 | inline bool should_start_debug_signal_handler() const; | |
| 827 | 828 | inline bool no_browser_globals() const; | |
| 828 | 829 | inline uint64_t thread_id() const; | |
| 830 | + inline std::string_view thread_name() const; | ||
| 829 | 831 | inline worker::Worker* worker_context() const; | |
| 830 | 832 | Environment* worker_parent_env() const; | |
| 831 | 833 | inline void add_sub_worker_context(worker::Worker* context); | |
@@ -1201,6 +1203,7 @@ class Environment final : public MemoryRetainer { | |||
| 1201 | 1203 | ||
| 1202 | 1204 | uint64_t flags_; | |
| 1203 | 1205 | uint64_t thread_id_; | |
| 1206 | + std::string thread_name_; | ||
| 1204 | 1207 | std::unordered_set<worker::Worker*> sub_worker_contexts_; | |
| 1205 | 1208 | ||
| 1206 | 1209 | #if HAVE_INSPECTOR | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -377,6 +377,7 @@ | |||
| 377 | 377 | V(table_string, "table") \ | |
| 378 | 378 | V(target_string, "target") \ | |
| 379 | 379 | V(thread_id_string, "threadId") \ | |
| 380 | + V(thread_name_string, "threadName") \ | ||
| 380 | 381 | V(ticketkeycallback_string, "onticketkeycallback") \ | |
| 381 | 382 | V(timeout_string, "timeout") \ | |
| 382 | 383 | V(time_to_first_byte_string, "timeToFirstByte") \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -716,6 +716,16 @@ NODE_EXTERN Environment* CreateEnvironment( | |||
| 716 | 716 | ThreadId thread_id = {} /* allocates a thread id automatically */, | |
| 717 | 717 | std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {}); | |
| 718 | 718 | ||
| 719 | + NODE_EXTERN Environment* CreateEnvironment( | ||
| 720 | + IsolateData* isolate_data, | ||
| 721 | + v8::Local<v8::Context> context, | ||
| 722 | + const std::vector<std::string>& args, | ||
| 723 | + const std::vector<std::string>& exec_args, | ||
| 724 | + EnvironmentFlags::Flags flags, | ||
| 725 | + ThreadId thread_id, | ||
| 726 | + std::unique_ptr<InspectorParentHandle> inspector_parent_handle, | ||
| 727 | + std::string_view thread_name); | ||
| 728 | + | ||
| 719 | 729 | // Returns a handle that can be passed to `LoadEnvironment()`, making the | |
| 720 | 730 | // child Environment accessible to the inspector as if it were a Node.js Worker. | |
| 721 | 731 | // `child_thread_id` can be created using `AllocateEnvironmentThreadId()` | |
| 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( | |
@@ -371,7 +381,8 @@ void Worker::Run() { | |||
| 371 | 381 | std::move(exec_argv_), | |
| 372 | 382 | static_cast<EnvironmentFlags::Flags>(environment_flags_), | |
| 373 | 383 | thread_id_, | |
| 374 | - std::move(inspector_parent_handle_))); | ||
| 384 | + std::move(inspector_parent_handle_), | ||
| 385 | + name_)); | ||
| 375 | 386 | if (is_stopped()) return; | |
| 376 | 387 | CHECK_NOT_NULL(env_); | |
| 377 | 388 | env_->set_env_vars(std::move(env_vars_)); | |
@@ -1240,6 +1251,16 @@ void CreateWorkerPerContextProperties(Local<Object> target, | |||
| 1240 | 1251 | Number::New(isolate, static_cast<double>(env->thread_id()))) | |
| 1241 | 1252 | .Check(); | |
| 1242 | 1253 | ||
| 1254 | + target | ||
| 1255 | + ->Set(env->context(), | ||
| 1256 | + env->thread_name_string(), | ||
| 1257 | + String::NewFromUtf8(isolate, | ||
| 1258 | + env->thread_name().data(), | ||
| 1259 | + NewStringType::kNormal, | ||
| 1260 | + env->thread_name().size()) | ||
| 1261 | + .ToLocalChecked()) | ||
| 1262 | + .Check(); | ||
| 1263 | + | ||
| 1243 | 1264 | target | |
| 1244 | 1265 | ->Set(env->context(), | |
| 1245 | 1266 | FIXED_ONE_BYTE_STRING(isolate, "isMainThread"), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments