From 64c01222d9b62942f356048026742a7c8d778cef Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 12 Nov 2019 18:57:10 +0000 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/30467 Reviewed-By: James M Snell Reviewed-By: Gireesh Punathil --- src/env-inl.h | 16 ++++++++++------ src/env.cc | 6 +++--- src/env.h | 7 ++++--- src/node_worker.cc | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/env-inl.h b/src/env-inl.h index 69b7316e405d41..3969969a970f62 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -64,6 +64,15 @@ inline MultiIsolatePlatform* IsolateData::platform() const { return platform_; } +inline void IsolateData::set_worker_context(worker::Worker* context) { + CHECK_NULL(worker_context_); // Should be set only once. + worker_context_ = context; +} + +inline worker::Worker* IsolateData::worker_context() const { + return worker_context_; +} + inline AsyncHooks::AsyncHooks() : async_ids_stack_(env()->isolate(), 16 * 2), fields_(env()->isolate(), kFieldsCount), @@ -904,12 +913,7 @@ inline uint64_t Environment::thread_id() const { } inline worker::Worker* Environment::worker_context() const { - return worker_context_; -} - -inline void Environment::set_worker_context(worker::Worker* context) { - CHECK_NULL(worker_context_); // Should be set only once. - worker_context_ = context; + return isolate_data()->worker_context(); } inline void Environment::add_sub_worker_context(worker::Worker* context) { diff --git a/src/env.cc b/src/env.cc index a3ee9158c49d49..a2b31edad0c42d 100644 --- a/src/env.cc +++ b/src/env.cc @@ -970,7 +970,7 @@ void Environment::Exit(int exit_code) { DisposePlatform(); exit(exit_code); } else { - worker_context_->Exit(exit_code); + worker_context()->Exit(exit_code); } } @@ -984,8 +984,8 @@ void Environment::stop_sub_worker_contexts() { } Environment* Environment::worker_parent_env() const { - if (worker_context_ == nullptr) return nullptr; - return worker_context_->env(); + if (worker_context() == nullptr) return nullptr; + return worker_context()->env(); } void MemoryTracker::TrackField(const char* edge_name, diff --git a/src/env.h b/src/env.h index 350f6f2f1a3682..0007142232a2d0 100644 --- a/src/env.h +++ b/src/env.h @@ -496,6 +496,9 @@ class IsolateData : public MemoryRetainer { inline v8::ArrayBuffer::Allocator* allocator() const; inline NodeArrayBufferAllocator* node_allocator() const; + inline worker::Worker* worker_context() const; + inline void set_worker_context(worker::Worker* context); + #define VP(PropertyName, StringValue) V(v8::Private, PropertyName) #define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName) #define VS(PropertyName, StringValue) V(v8::String, PropertyName) @@ -540,6 +543,7 @@ class IsolateData : public MemoryRetainer { const bool uses_node_allocator_; MultiIsolatePlatform* platform_; std::shared_ptr options_; + worker::Worker* worker_context_ = nullptr; }; struct ContextInfo { @@ -1057,7 +1061,6 @@ class Environment : public MemoryRetainer { inline uint64_t thread_id() const; inline worker::Worker* worker_context() const; Environment* worker_parent_env() const; - inline void set_worker_context(worker::Worker* context); inline void add_sub_worker_context(worker::Worker* context); inline void remove_sub_worker_context(worker::Worker* context); void stop_sub_worker_contexts(); @@ -1378,8 +1381,6 @@ class Environment : public MemoryRetainer { std::vector> file_handle_read_wrap_freelist_; - worker::Worker* worker_context_ = nullptr; - std::list extra_linked_bindings_; Mutex extra_linked_bindings_mutex_; diff --git a/src/node_worker.cc b/src/node_worker.cc index 1e1877e026c63c..e71866b9b1f493 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -183,6 +183,7 @@ class WorkerThreadData { CHECK(isolate_data_); if (w_->per_isolate_opts_) isolate_data_->set_options(std::move(w_->per_isolate_opts_)); + isolate_data_->set_worker_context(w_); } Mutex::ScopedLock lock(w_->mutex_); @@ -317,7 +318,6 @@ void Worker::Run() { CHECK_NOT_NULL(env_); env_->set_env_vars(std::move(env_vars_)); env_->set_abort_on_uncaught_exception(false); - env_->set_worker_context(this); env_->InitializeLibuv(start_profiler_idle_notifier_); }