Skip to content

Commit

Permalink
src: move worker_context from Environment to IsolateData
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax committed Mar 21, 2020
1 parent 288382a commit 64c0122
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
16 changes: 10 additions & 6 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -540,6 +543,7 @@ class IsolateData : public MemoryRetainer {
const bool uses_node_allocator_;
MultiIsolatePlatform* platform_;
std::shared_ptr<PerIsolateOptions> options_;
worker::Worker* worker_context_ = nullptr;
};

struct ContextInfo {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -1378,8 +1381,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_;

Expand Down
2 changes: 1 addition & 1 deletion src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down Expand Up @@ -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_);
}
Expand Down

0 comments on commit 64c0122

Please sign in to comment.