Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: set up child Isolate inside Worker thread #26011

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,14 @@ void Environment::TrackingTraceStateObserver::UpdateTraceCategoryState() {

static std::atomic<uint64_t> next_thread_id{0};

uint64_t Environment::AllocateThreadId() {
return next_thread_id++;
}

Environment::Environment(IsolateData* isolate_data,
Local<Context> context,
Flags flags)
Flags flags,
uint64_t thread_id)
: isolate_(context->GetIsolate()),
isolate_data_(isolate_data),
immediate_info_(context->GetIsolate()),
Expand All @@ -181,7 +186,8 @@ Environment::Environment(IsolateData* isolate_data,
trace_category_state_(isolate_, kTraceCategoryCount),
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
flags_(flags),
thread_id_(next_thread_id++),
thread_id_(thread_id == static_cast<uint64_t>(-1) ?
addaleax marked this conversation as resolved.
Show resolved Hide resolved
AllocateThreadId() : thread_id),
fs_stats_field_array_(isolate_, kFsStatsBufferLength),
fs_stats_field_bigint_array_(isolate_, kFsStatsBufferLength),
context_(context->GetIsolate(), context) {
Expand Down
5 changes: 4 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ class Environment {

Environment(IsolateData* isolate_data,
v8::Local<v8::Context> context,
Flags flags = Flags());
Flags flags = Flags(),
uint64_t thread_id = static_cast<uint64_t>(-1));
~Environment();

void Start(bool start_profiler_idle_notifier);
Expand Down Expand Up @@ -769,6 +770,8 @@ class Environment {
inline bool has_run_bootstrapping_code() const;
inline void set_has_run_bootstrapping_code(bool has_run_bootstrapping_code);

static uint64_t AllocateThreadId();

inline bool is_main_thread() const;
inline bool owns_process_state() const;
inline bool owns_inspector() const;
Expand Down
7 changes: 4 additions & 3 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ Worker::Worker(Environment* env,
Local<Object> wrap,
const std::string& url,
std::shared_ptr<PerIsolateOptions> per_isolate_opts)
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), url_(url) {
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), url_(url),
thread_id_(Environment::AllocateThreadId()) {
Debug(this, "Creating new worker instance at %p", static_cast<void*>(this));

// Set up everything that needs to be set up in the parent environment.
Expand Down Expand Up @@ -114,11 +115,11 @@ Worker::Worker(Environment* env,
Context::Scope context_scope(context);

// TODO(addaleax): Use CreateEnvironment(), or generally another public API.
env_.reset(new Environment(isolate_data_.get(), context));
env_.reset(new Environment(
isolate_data_.get(), context, Flags::kNoFlags, thread_id_));
CHECK_NOT_NULL(env_);
env_->set_abort_on_uncaught_exception(false);
env_->set_worker_context(this);
thread_id_ = env_->thread_id();

env_->Start(env->profiler_idle_notifier_started());
env_->ProcessCliArgs(std::vector<std::string>{},
Expand Down