Skip to content

Commit 714c686

Browse files
gahaastargos
authored andcommitted
src: initialize PerIsolateData eagerly
1 parent 35f7df6 commit 714c686

File tree

7 files changed

+25
-16
lines changed

7 files changed

+25
-16
lines changed

src/env.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ IsolateData::IsolateData(Isolate* isolate,
3939
zero_fill_field_(zero_fill_field),
4040
platform_(platform) {
4141
if (platform_ != nullptr)
42-
platform_->RegisterIsolate(this, event_loop);
42+
platform_->RegisterIsolate(isolate_, event_loop);
4343

4444
// Create string and private symbol properties as internalized one byte
4545
// strings after the platform is properly initialized.
@@ -90,7 +90,7 @@ IsolateData::IsolateData(Isolate* isolate,
9090

9191
IsolateData::~IsolateData() {
9292
if (platform_ != nullptr)
93-
platform_->UnregisterIsolate(this);
93+
platform_->UnregisterIsolate(isolate_);
9494
}
9595

9696

src/node.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3602,17 +3602,22 @@ bool AllowWasmCodeGenerationCallback(
36023602
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
36033603
}
36043604

3605-
Isolate* NewIsolate(ArrayBufferAllocator* allocator) {
3605+
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
36063606
Isolate::CreateParams params;
36073607
params.array_buffer_allocator = allocator;
36083608
#ifdef NODE_ENABLE_VTUNE_PROFILING
36093609
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
36103610
#endif
36113611

3612-
Isolate* isolate = Isolate::New(params);
3612+
Isolate* isolate = Isolate::Allocate();
36133613
if (isolate == nullptr)
36143614
return nullptr;
36153615

3616+
// Register the isolate on the platform before the isolate gets initialized,
3617+
// so that the isolate can access the platform during initialization.
3618+
v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
3619+
Isolate::Initialize(isolate, params);
3620+
36163621
isolate->AddMessageListener(OnMessage);
36173622
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
36183623
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
@@ -3627,7 +3632,7 @@ inline int Start(uv_loop_t* event_loop,
36273632
int exec_argc, const char* const* exec_argv) {
36283633
std::unique_ptr<ArrayBufferAllocator, decltype(&FreeArrayBufferAllocator)>
36293634
allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator);
3630-
Isolate* const isolate = NewIsolate(allocator.get());
3635+
Isolate* const isolate = NewIsolate(allocator.get(), event_loop);
36313636
if (isolate == nullptr)
36323637
return 12; // Signal internal error.
36333638

@@ -3663,6 +3668,7 @@ inline int Start(uv_loop_t* event_loop,
36633668
}
36643669

36653670
isolate->Dispose();
3671+
v8_platform.Platform()->UnregisterIsolate(isolate);
36663672

36673673
return exit_code;
36683674
}

src/node.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,14 @@ class MultiIsolatePlatform : public v8::Platform {
233233
virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0;
234234

235235
// These will be called by the `IsolateData` creation/destruction functions.
236-
virtual void RegisterIsolate(IsolateData* isolate_data,
236+
virtual void RegisterIsolate(v8::Isolate* isolate,
237237
struct uv_loop_s* loop) = 0;
238-
virtual void UnregisterIsolate(IsolateData* isolate_data) = 0;
238+
virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
239239
};
240240

241241
// Creates a new isolate with Node.js-specific settings.
242-
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator);
242+
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
243+
struct uv_loop_s* event_loop);
243244

244245
// Creates a new context with Node.js-specific tweaks.
245246
NODE_EXTERN v8::Local<v8::Context> NewContext(

src/node_platform.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ NodePlatform::NodePlatform(int thread_pool_size,
137137
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
138138
}
139139

140-
void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
141-
Isolate* isolate = isolate_data->isolate();
140+
void NodePlatform::RegisterIsolate(Isolate* isolate, uv_loop_t* loop) {
142141
Mutex::ScopedLock lock(per_isolate_mutex_);
143142
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
144143
if (existing) {
@@ -150,8 +149,7 @@ void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
150149
}
151150
}
152151

153-
void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
154-
Isolate* isolate = isolate_data->isolate();
152+
void NodePlatform::UnregisterIsolate(Isolate* isolate) {
155153
Mutex::ScopedLock lock(per_isolate_mutex_);
156154
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
157155
CHECK(existing);

src/node_platform.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class NodePlatform : public MultiIsolatePlatform {
135135
v8::TracingController* GetTracingController() override;
136136
bool FlushForegroundTasks(v8::Isolate* isolate) override;
137137

138-
void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
139-
void UnregisterIsolate(IsolateData* isolate_data) override;
138+
void RegisterIsolate(v8::Isolate* isolate, uv_loop_t* loop) override;
139+
void UnregisterIsolate(v8::Isolate* isolate) override;
140140

141141
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
142142
v8::Isolate* isolate) override;

src/node_worker.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ Worker::Worker(Environment* env, Local<Object> wrap)
6767

6868
array_buffer_allocator_.reset(CreateArrayBufferAllocator());
6969

70-
isolate_ = NewIsolate(array_buffer_allocator_.get());
71-
CHECK_NE(isolate_, nullptr);
7270
CHECK_EQ(uv_loop_init(&loop_), 0);
71+
isolate_ = NewIsolate(array_buffer_allocator_.get(), &loop_);
72+
CHECK_NE(isolate_, nullptr);
7373

7474
thread_exit_async_.reset(new uv_async_t);
7575
thread_exit_async_->data = this;
@@ -263,6 +263,7 @@ void Worker::DisposeIsolate() {
263263
platform->CancelPendingDelayedTasks(isolate_);
264264

265265
isolate_data_.reset();
266+
platform->UnregisterIsolate(isolate_);
266267

267268
isolate_->Dispose();
268269
isolate_ = nullptr;

test/cctest/node_test_fixture.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ class NodeTestFixture : public ::testing::Test {
9090
&node::FreeArrayBufferAllocator);
9191
isolate_ = NewIsolate(allocator.get());
9292
CHECK_NE(isolate_, nullptr);
93+
platform->RegisterIsolate(isolate_, &current_loop);
94+
v8::Isolate::Initialize(isolate_, params);
9395
}
9496

9597
virtual void TearDown() {
9698
isolate_->Dispose();
99+
platform->UnregisterIsolate(isolate_);
97100
isolate_ = nullptr;
98101
}
99102
};

0 commit comments

Comments
 (0)