Skip to content

Commit 64c0122

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. PR-URL: #30467 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent 288382a commit 64c0122

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
@@ -64,6 +64,15 @@ inline MultiIsolatePlatform* IsolateData::platform() const {
6464
return platform_;
6565
}
6666

67+
inline void IsolateData::set_worker_context(worker::Worker* context) {
68+
CHECK_NULL(worker_context_); // Should be set only once.
69+
worker_context_ = context;
70+
}
71+
72+
inline worker::Worker* IsolateData::worker_context() const {
73+
return worker_context_;
74+
}
75+
6776
inline AsyncHooks::AsyncHooks()
6877
: async_ids_stack_(env()->isolate(), 16 * 2),
6978
fields_(env()->isolate(), kFieldsCount),
@@ -904,12 +913,7 @@ inline uint64_t Environment::thread_id() const {
904913
}
905914

906915
inline worker::Worker* Environment::worker_context() const {
907-
return worker_context_;
908-
}
909-
910-
inline void Environment::set_worker_context(worker::Worker* context) {
911-
CHECK_NULL(worker_context_); // Should be set only once.
912-
worker_context_ = context;
916+
return isolate_data()->worker_context();
913917
}
914918

915919
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
@@ -970,7 +970,7 @@ void Environment::Exit(int exit_code) {
970970
DisposePlatform();
971971
exit(exit_code);
972972
} else {
973-
worker_context_->Exit(exit_code);
973+
worker_context()->Exit(exit_code);
974974
}
975975
}
976976

@@ -984,8 +984,8 @@ void Environment::stop_sub_worker_contexts() {
984984
}
985985

986986
Environment* Environment::worker_parent_env() const {
987-
if (worker_context_ == nullptr) return nullptr;
988-
return worker_context_->env();
987+
if (worker_context() == nullptr) return nullptr;
988+
return worker_context()->env();
989989
}
990990

991991
void MemoryTracker::TrackField(const char* edge_name,

src/env.h

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

499+
inline worker::Worker* worker_context() const;
500+
inline void set_worker_context(worker::Worker* context);
501+
499502
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
500503
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
501504
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
@@ -540,6 +543,7 @@ class IsolateData : public MemoryRetainer {
540543
const bool uses_node_allocator_;
541544
MultiIsolatePlatform* platform_;
542545
std::shared_ptr<PerIsolateOptions> options_;
546+
worker::Worker* worker_context_ = nullptr;
543547
};
544548

545549
struct ContextInfo {
@@ -1057,7 +1061,6 @@ class Environment : public MemoryRetainer {
10571061
inline uint64_t thread_id() const;
10581062
inline worker::Worker* worker_context() const;
10591063
Environment* worker_parent_env() const;
1060-
inline void set_worker_context(worker::Worker* context);
10611064
inline void add_sub_worker_context(worker::Worker* context);
10621065
inline void remove_sub_worker_context(worker::Worker* context);
10631066
void stop_sub_worker_contexts();
@@ -1378,8 +1381,6 @@ class Environment : public MemoryRetainer {
13781381
std::vector<std::unique_ptr<fs::FileHandleReadWrap>>
13791382
file_handle_read_wrap_freelist_;
13801383

1381-
worker::Worker* worker_context_ = nullptr;
1382-
13831384
std::list<node_module> extra_linked_bindings_;
13841385
Mutex extra_linked_bindings_mutex_;
13851386

src/node_worker.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class WorkerThreadData {
183183
CHECK(isolate_data_);
184184
if (w_->per_isolate_opts_)
185185
isolate_data_->set_options(std::move(w_->per_isolate_opts_));
186+
isolate_data_->set_worker_context(w_);
186187
}
187188

188189
Mutex::ScopedLock lock(w_->mutex_);
@@ -317,7 +318,6 @@ void Worker::Run() {
317318
CHECK_NOT_NULL(env_);
318319
env_->set_env_vars(std::move(env_vars_));
319320
env_->set_abort_on_uncaught_exception(false);
320-
env_->set_worker_context(this);
321321

322322
env_->InitializeLibuv(start_profiler_idle_notifier_);
323323
}

0 commit comments

Comments
 (0)