Skip to content

Commit b7350e8

Browse files
committed
src: move worker_context from Environment to IsolateData
Workers are fully in control of their Isolates, and this helps avoid a problem with later changes to `CreateEnvironment()` because now we can run the boostrapping code inside the latter. Backport-PR-URL: #35241 PR-URL: #30467 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent 9a5cec3 commit b7350e8

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

src/env-inl.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
6969
return async_wrap_providers_[index].Get(isolate_);
7070
}
7171

72+
inline void IsolateData::set_worker_context(worker::Worker* context) {
73+
CHECK_NULL(worker_context_); // Should be set only once.
74+
worker_context_ = context;
75+
}
76+
77+
inline worker::Worker* IsolateData::worker_context() const {
78+
return worker_context_;
79+
}
80+
7281
inline AsyncHooks::AsyncHooks()
7382
: async_ids_stack_(env()->isolate(), 16 * 2),
7483
fields_(env()->isolate(), kFieldsCount),
@@ -861,12 +870,7 @@ inline uint64_t Environment::thread_id() const {
861870
}
862871

863872
inline worker::Worker* Environment::worker_context() const {
864-
return worker_context_;
865-
}
866-
867-
inline void Environment::set_worker_context(worker::Worker* context) {
868-
CHECK_NULL(worker_context_); // Should be set only once.
869-
worker_context_ = context;
873+
return isolate_data()->worker_context();
870874
}
871875

872876
inline void Environment::add_sub_worker_context(worker::Worker* context) {

src/env.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ void Environment::Exit(int exit_code) {
973973
DisposePlatform();
974974
exit(exit_code);
975975
} else {
976-
worker_context_->Exit(exit_code);
976+
worker_context()->Exit(exit_code);
977977
}
978978
}
979979

@@ -987,8 +987,8 @@ void Environment::stop_sub_worker_contexts() {
987987
}
988988

989989
Environment* Environment::worker_parent_env() const {
990-
if (worker_context_ == nullptr) return nullptr;
991-
return worker_context_->env();
990+
if (worker_context() == nullptr) return nullptr;
991+
return worker_context()->env();
992992
}
993993

994994
void Environment::BuildEmbedderGraph(Isolate* isolate,

src/env.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ class IsolateData : public MemoryRetainer {
504504
inline v8::ArrayBuffer::Allocator* allocator() const;
505505
inline NodeArrayBufferAllocator* node_allocator() const;
506506

507+
inline worker::Worker* worker_context() const;
508+
inline void set_worker_context(worker::Worker* context);
509+
507510
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
508511
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
509512
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
@@ -552,6 +555,7 @@ class IsolateData : public MemoryRetainer {
552555
const bool uses_node_allocator_;
553556
MultiIsolatePlatform* platform_;
554557
std::shared_ptr<PerIsolateOptions> options_;
558+
worker::Worker* worker_context_ = nullptr;
555559
};
556560

557561
struct ContextInfo {
@@ -1074,7 +1078,6 @@ class Environment : public MemoryRetainer {
10741078
inline uint64_t thread_id() const;
10751079
inline worker::Worker* worker_context() const;
10761080
Environment* worker_parent_env() const;
1077-
inline void set_worker_context(worker::Worker* context);
10781081
inline void add_sub_worker_context(worker::Worker* context);
10791082
inline void remove_sub_worker_context(worker::Worker* context);
10801083
void stop_sub_worker_contexts();
@@ -1390,8 +1393,6 @@ class Environment : public MemoryRetainer {
13901393
std::vector<std::unique_ptr<fs::FileHandleReadWrap>>
13911394
file_handle_read_wrap_freelist_;
13921395

1393-
worker::Worker* worker_context_ = nullptr;
1394-
13951396
std::list<node_module> extra_linked_bindings_;
13961397
Mutex extra_linked_bindings_mutex_;
13971398

src/node_worker.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class WorkerThreadData {
185185
CHECK(isolate_data_);
186186
if (w_->per_isolate_opts_)
187187
isolate_data_->set_options(std::move(w_->per_isolate_opts_));
188+
isolate_data_->set_worker_context(w_);
188189
}
189190

190191
Mutex::ScopedLock lock(w_->mutex_);
@@ -327,7 +328,6 @@ void Worker::Run() {
327328
CHECK_NOT_NULL(env_);
328329
env_->set_env_vars(std::move(env_vars_));
329330
env_->set_abort_on_uncaught_exception(false);
330-
env_->set_worker_context(this);
331331

332332
env_->InitializeLibuv(start_profiler_idle_notifier_);
333333
}

0 commit comments

Comments
 (0)