Skip to content

Commit 1764aae

Browse files
committed
worker: pre-allocate thread id
Allocate a thread id before actually creating the Environment instance. PR-URL: #26011 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 7ab34ae commit 1764aae

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/env.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,14 @@ void Environment::TrackingTraceStateObserver::UpdateTraceCategoryState() {
169169

170170
static std::atomic<uint64_t> next_thread_id{0};
171171

172+
uint64_t Environment::AllocateThreadId() {
173+
return next_thread_id++;
174+
}
175+
172176
Environment::Environment(IsolateData* isolate_data,
173177
Local<Context> context,
174-
Flags flags)
178+
Flags flags,
179+
uint64_t thread_id)
175180
: isolate_(context->GetIsolate()),
176181
isolate_data_(isolate_data),
177182
immediate_info_(context->GetIsolate()),
@@ -181,7 +186,7 @@ Environment::Environment(IsolateData* isolate_data,
181186
trace_category_state_(isolate_, kTraceCategoryCount),
182187
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
183188
flags_(flags),
184-
thread_id_(next_thread_id++),
189+
thread_id_(thread_id == kNoThreadId ? AllocateThreadId() : thread_id),
185190
fs_stats_field_array_(isolate_, kFsStatsBufferLength),
186191
fs_stats_field_bigint_array_(isolate_, kFsStatsBufferLength),
187192
context_(context->GetIsolate(), context) {

src/env.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,8 @@ class Environment {
616616

617617
Environment(IsolateData* isolate_data,
618618
v8::Local<v8::Context> context,
619-
Flags flags = Flags());
619+
Flags flags = Flags(),
620+
uint64_t thread_id = kNoThreadId);
620621
~Environment();
621622

622623
void Start(bool start_profiler_idle_notifier);
@@ -767,6 +768,9 @@ class Environment {
767768
inline bool has_run_bootstrapping_code() const;
768769
inline void set_has_run_bootstrapping_code(bool has_run_bootstrapping_code);
769770

771+
static uint64_t AllocateThreadId();
772+
static constexpr uint64_t kNoThreadId = -1;
773+
770774
inline bool is_main_thread() const;
771775
inline bool owns_process_state() const;
772776
inline bool owns_inspector() const;

src/node_worker.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ Worker::Worker(Environment* env,
7171
Local<Object> wrap,
7272
const std::string& url,
7373
std::shared_ptr<PerIsolateOptions> per_isolate_opts)
74-
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), url_(url) {
74+
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), url_(url),
75+
thread_id_(Environment::AllocateThreadId()) {
7576
Debug(this, "Creating new worker instance at %p", static_cast<void*>(this));
7677

7778
// Set up everything that needs to be set up in the parent environment.
@@ -114,11 +115,11 @@ Worker::Worker(Environment* env,
114115
Context::Scope context_scope(context);
115116

116117
// TODO(addaleax): Use CreateEnvironment(), or generally another public API.
117-
env_.reset(new Environment(isolate_data_.get(), context));
118+
env_.reset(new Environment(
119+
isolate_data_.get(), context, Flags::kNoFlags, thread_id_));
118120
CHECK_NOT_NULL(env_);
119121
env_->set_abort_on_uncaught_exception(false);
120122
env_->set_worker_context(this);
121-
thread_id_ = env_->thread_id();
122123

123124
env_->Start(env->profiler_idle_notifier_started());
124125
env_->ProcessCliArgs(std::vector<std::string>{},

0 commit comments

Comments
 (0)