worker: add name for worker · nodejs/node@eecd327 · GitHub
Skip to content

Commit eecd327

Browse files
theanarkhRafaelGSS
authored andcommitted
worker: add name for worker
PR-URL: #59213 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 8686b80 commit eecd327

11 files changed

Lines changed: 118 additions & 5 deletions

File tree

doc/api/worker_threads.md

Lines changed: 24 additions & 0 deletions

lib/internal/worker.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const {
7272
isInternalThread,
7373
resourceLimits: resourceLimitsRaw,
7474
threadId,
75+
threadName,
7576
Worker: WorkerImpl,
7677
kMaxYoungGenerationSizeMb,
7778
kMaxOldGenerationSizeMb,
@@ -433,6 +434,12 @@ class Worker extends EventEmitter {
433434
return this[kHandle].threadId;
434435
}
435436

437+
get threadName() {
438+
if (this[kHandle] === null) return null;
439+
440+
return this[kHandle].threadName;
441+
}
442+
436443
get stdin() {
437444
return this[kParentSideStdio].stdin;
438445
}
@@ -597,6 +604,7 @@ module.exports = {
597604
getEnvironmentData,
598605
assignEnvironmentData,
599606
threadId,
607+
threadName,
600608
InternalWorker,
601609
Worker,
602610
};

lib/worker_threads.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
setEnvironmentData,
99
getEnvironmentData,
1010
threadId,
11+
threadName,
1112
Worker,
1213
} = require('internal/worker');
1314

@@ -44,6 +45,7 @@ module.exports = {
4445
resourceLimits,
4546
postMessageToThread,
4647
threadId,
48+
threadName,
4749
SHARE_ENV,
4850
Worker,
4951
parentPort: null,

src/api/environment.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,25 @@ Environment* CreateEnvironment(
414414
EnvironmentFlags::Flags flags,
415415
ThreadId thread_id,
416416
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) {
417436
Isolate* isolate = isolate_data->isolate();
418437

419438
Isolate::Scope isolate_scope(isolate);
@@ -434,7 +453,8 @@ Environment* CreateEnvironment(
434453
exec_args,
435454
env_snapshot_info,
436455
flags,
437-
thread_id);
456+
thread_id,
457+
thread_name);
438458
CHECK_NOT_NULL(env);
439459

440460
if (use_snapshot) {

src/env-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,10 @@ inline uint64_t Environment::thread_id() const {
703703
return thread_id_;
704704
}
705705

706+
inline std::string_view Environment::thread_name() const {
707+
return thread_name_;
708+
}
709+
706710
inline worker::Worker* Environment::worker_context() const {
707711
return isolate_data()->worker_context();
708712
}

src/env.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ Environment::Environment(IsolateData* isolate_data,
784784
const std::vector<std::string>& exec_args,
785785
const EnvSerializeInfo* env_info,
786786
EnvironmentFlags::Flags flags,
787-
ThreadId thread_id)
787+
ThreadId thread_id,
788+
std::string_view thread_name)
788789
: isolate_(isolate),
789790
external_memory_accounter_(new ExternalMemoryAccounter()),
790791
isolate_data_(isolate_data),
@@ -811,7 +812,8 @@ Environment::Environment(IsolateData* isolate_data,
811812
flags_(flags),
812813
thread_id_(thread_id.id == static_cast<uint64_t>(-1)
813814
? AllocateEnvironmentThreadId().id
814-
: thread_id.id) {
815+
: thread_id.id),
816+
thread_name_(thread_name) {
815817
if (!is_main_thread()) {
816818
// If this is a Worker thread, we can always safely use the parent's
817819
// Isolate's code cache because of the shared read-only heap.

src/env.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,8 @@ class Environment final : public MemoryRetainer {
660660
const std::vector<std::string>& exec_args,
661661
const EnvSerializeInfo* env_info,
662662
EnvironmentFlags::Flags flags,
663-
ThreadId thread_id);
663+
ThreadId thread_id,
664+
std::string_view thread_name = "");
664665
void InitializeMainContext(v8::Local<v8::Context> context,
665666
const EnvSerializeInfo* env_info);
666667
~Environment() override;
@@ -807,6 +808,7 @@ class Environment final : public MemoryRetainer {
807808
inline bool should_start_debug_signal_handler() const;
808809
inline bool no_browser_globals() const;
809810
inline uint64_t thread_id() const;
811+
inline std::string_view thread_name() const;
810812
inline worker::Worker* worker_context() const;
811813
Environment* worker_parent_env() const;
812814
inline void add_sub_worker_context(worker::Worker* context);
@@ -1176,6 +1178,7 @@ class Environment final : public MemoryRetainer {
11761178

11771179
uint64_t flags_;
11781180
uint64_t thread_id_;
1181+
std::string thread_name_;
11791182
std::unordered_set<worker::Worker*> sub_worker_contexts_;
11801183

11811184
#if HAVE_INSPECTOR

src/env_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@
390390
V(table_string, "table") \
391391
V(target_string, "target") \
392392
V(thread_id_string, "threadId") \
393+
V(thread_name_string, "threadName") \
393394
V(ticketkeycallback_string, "onticketkeycallback") \
394395
V(timeout_string, "timeout") \
395396
V(time_to_first_byte_string, "timeToFirstByte") \

src/node.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,16 @@ NODE_EXTERN Environment* CreateEnvironment(
730730
ThreadId thread_id = {} /* allocates a thread id automatically */,
731731
std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {});
732732

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+
733743
// Returns a handle that can be passed to `LoadEnvironment()`, making the
734744
// child Environment accessible to the inspector as if it were a Node.js Worker.
735745
// `child_thread_id` can be created using `AllocateEnvironmentThreadId()`

src/node_worker.cc

Lines changed: 22 additions & 1 deletion

0 commit comments

Comments
 (0)