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: https://github.com/nodejs/node/pull/35241
PR-URL: https://github.com/nodejs/node/pull/30467
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
This commit is contained in:
Anna Henningsen committed 2020-09-23 20:00:20 +02:00
1 parent 9a5cec3466
commit b7350e8c6e
4 files changed
+18 -13

No files matched your search

+10 -6
View File
@@ -69,6 +69,15 @@ inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
return async_wrap_providers_[index].Get(isolate_);
}
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),
@@ -861,12 +870,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) {
+3 -3
View File
@@ -973,7 +973,7 @@ void Environment::Exit(int exit_code) {
DisposePlatform();
exit(exit_code);
} else {
worker_context_->Exit(exit_code);
worker_context()->Exit(exit_code);
}
}
@@ -987,8 +987,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 Environment::BuildEmbedderGraph(Isolate* isolate,
+4 -3
View File
@@ -504,6 +504,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)
@@ -552,6 +555,7 @@ class IsolateData : public MemoryRetainer {
const bool uses_node_allocator_;
MultiIsolatePlatform* platform_;
std::shared_ptr<PerIsolateOptions> options_;
worker::Worker* worker_context_ = nullptr;
};
struct ContextInfo {
@@ -1074,7 +1078,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();
@@ -1390,8 +1393,6 @@ class Environment : public MemoryRetainer {
std::vector<std::unique_ptr<fs::FileHandleReadWrap>>
file_handle_read_wrap_freelist_;
worker::Worker* worker_context_ = nullptr;
std::list<node_module> extra_linked_bindings_;
Mutex extra_linked_bindings_mutex_;
+1 -1
View File
@@ -185,6 +185,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_);
@@ -327,7 +328,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_);
}