Skip to content

Commit d5e7294

Browse files
gahaastargos
authored andcommitted
src: initialize PerIsolateData eagerly
PR-URL: #21983 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 3771c9a commit d5e7294

File tree

7 files changed

+25
-16
lines changed

7 files changed

+25
-16
lines changed

src/env.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ IsolateData::IsolateData(Isolate* isolate,
4646
zero_fill_field_(zero_fill_field),
4747
platform_(platform) {
4848
if (platform_ != nullptr)
49-
platform_->RegisterIsolate(this, event_loop);
49+
platform_->RegisterIsolate(isolate_, event_loop);
5050

5151
options_.reset(new PerIsolateOptions(*per_process_opts->per_isolate));
5252

@@ -99,7 +99,7 @@ IsolateData::IsolateData(Isolate* isolate,
9999

100100
IsolateData::~IsolateData() {
101101
if (platform_ != nullptr)
102-
platform_->UnregisterIsolate(this);
102+
platform_->UnregisterIsolate(isolate_);
103103
}
104104

105105

src/node.cc

+9-3
Original file line numberDiff line numberDiff line change
@@ -3105,17 +3105,22 @@ bool AllowWasmCodeGenerationCallback(
31053105
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
31063106
}
31073107

3108-
Isolate* NewIsolate(ArrayBufferAllocator* allocator) {
3108+
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
31093109
Isolate::CreateParams params;
31103110
params.array_buffer_allocator = allocator;
31113111
#ifdef NODE_ENABLE_VTUNE_PROFILING
31123112
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
31133113
#endif
31143114

3115-
Isolate* isolate = Isolate::New(params);
3115+
Isolate* isolate = Isolate::Allocate();
31163116
if (isolate == nullptr)
31173117
return nullptr;
31183118

3119+
// Register the isolate on the platform before the isolate gets initialized,
3120+
// so that the isolate can access the platform during initialization.
3121+
v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
3122+
Isolate::Initialize(isolate, params);
3123+
31193124
isolate->AddMessageListener(OnMessage);
31203125
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
31213126
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
@@ -3130,7 +3135,7 @@ inline int Start(uv_loop_t* event_loop,
31303135
const std::vector<std::string>& exec_args) {
31313136
std::unique_ptr<ArrayBufferAllocator, decltype(&FreeArrayBufferAllocator)>
31323137
allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator);
3133-
Isolate* const isolate = NewIsolate(allocator.get());
3138+
Isolate* const isolate = NewIsolate(allocator.get(), event_loop);
31343139
if (isolate == nullptr)
31353140
return 12; // Signal internal error.
31363141

@@ -3168,6 +3173,7 @@ inline int Start(uv_loop_t* event_loop,
31683173
}
31693174

31703175
isolate->Dispose();
3176+
v8_platform.Platform()->UnregisterIsolate(isolate);
31713177

31723178
return exit_code;
31733179
}

src/node.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,14 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
246246
virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0;
247247

248248
// These will be called by the `IsolateData` creation/destruction functions.
249-
virtual void RegisterIsolate(IsolateData* isolate_data,
249+
virtual void RegisterIsolate(v8::Isolate* isolate,
250250
struct uv_loop_s* loop) = 0;
251-
virtual void UnregisterIsolate(IsolateData* isolate_data) = 0;
251+
virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
252252
};
253253

254254
// Creates a new isolate with Node.js-specific settings.
255-
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator);
255+
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
256+
struct uv_loop_s* event_loop);
256257

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

src/node_platform.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ NodePlatform::NodePlatform(int thread_pool_size,
259259
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
260260
}
261261

262-
void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
263-
Isolate* isolate = isolate_data->isolate();
262+
void NodePlatform::RegisterIsolate(Isolate* isolate, uv_loop_t* loop) {
264263
Mutex::ScopedLock lock(per_isolate_mutex_);
265264
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
266265
if (existing) {
@@ -272,8 +271,7 @@ void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
272271
}
273272
}
274273

275-
void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
276-
Isolate* isolate = isolate_data->isolate();
274+
void NodePlatform::UnregisterIsolate(Isolate* isolate) {
277275
Mutex::ScopedLock lock(per_isolate_mutex_);
278276
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
279277
CHECK(existing);

src/node_platform.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ class NodePlatform : public MultiIsolatePlatform {
141141
v8::TracingController* GetTracingController() override;
142142
bool FlushForegroundTasks(v8::Isolate* isolate) override;
143143

144-
void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
145-
void UnregisterIsolate(IsolateData* isolate_data) override;
144+
void RegisterIsolate(v8::Isolate* isolate, uv_loop_t* loop) override;
145+
void UnregisterIsolate(v8::Isolate* isolate) override;
146146

147147
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
148148
v8::Isolate* isolate) override;

src/node_worker.cc

+3-2
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;
@@ -265,6 +265,7 @@ void Worker::DisposeIsolate() {
265265
platform->CancelPendingDelayedTasks(isolate_);
266266

267267
isolate_data_.reset();
268+
platform->UnregisterIsolate(isolate_);
268269

269270
isolate_->Dispose();
270271
isolate_ = nullptr;

test/cctest/node_test_fixture.h

+3
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)