Skip to content

Commit

Permalink
src: initialize PerIsolateData eagerly
Browse files Browse the repository at this point in the history
PR-URL: #21983
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Ujjwal Sharma <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
gahaas authored and targos committed Sep 7, 2018
1 parent 3771c9a commit d5e7294
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ IsolateData::IsolateData(Isolate* isolate,
zero_fill_field_(zero_fill_field),
platform_(platform) {
if (platform_ != nullptr)
platform_->RegisterIsolate(this, event_loop);
platform_->RegisterIsolate(isolate_, event_loop);

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

Expand Down Expand Up @@ -99,7 +99,7 @@ IsolateData::IsolateData(Isolate* isolate,

IsolateData::~IsolateData() {
if (platform_ != nullptr)
platform_->UnregisterIsolate(this);
platform_->UnregisterIsolate(isolate_);
}


Expand Down
12 changes: 9 additions & 3 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3105,17 +3105,22 @@ bool AllowWasmCodeGenerationCallback(
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
}

Isolate* NewIsolate(ArrayBufferAllocator* allocator) {
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
Isolate::CreateParams params;
params.array_buffer_allocator = allocator;
#ifdef NODE_ENABLE_VTUNE_PROFILING
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
#endif

Isolate* isolate = Isolate::New(params);
Isolate* isolate = Isolate::Allocate();
if (isolate == nullptr)
return nullptr;

// Register the isolate on the platform before the isolate gets initialized,
// so that the isolate can access the platform during initialization.
v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
Isolate::Initialize(isolate, params);

isolate->AddMessageListener(OnMessage);
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
Expand All @@ -3130,7 +3135,7 @@ inline int Start(uv_loop_t* event_loop,
const std::vector<std::string>& exec_args) {
std::unique_ptr<ArrayBufferAllocator, decltype(&FreeArrayBufferAllocator)>
allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator);
Isolate* const isolate = NewIsolate(allocator.get());
Isolate* const isolate = NewIsolate(allocator.get(), event_loop);
if (isolate == nullptr)
return 12; // Signal internal error.

Expand Down Expand Up @@ -3168,6 +3173,7 @@ inline int Start(uv_loop_t* event_loop,
}

isolate->Dispose();
v8_platform.Platform()->UnregisterIsolate(isolate);

return exit_code;
}
Expand Down
7 changes: 4 additions & 3 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,14 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0;

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

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

// Creates a new context with Node.js-specific tweaks.
NODE_EXTERN v8::Local<v8::Context> NewContext(
Expand Down
6 changes: 2 additions & 4 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ NodePlatform::NodePlatform(int thread_pool_size,
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
}

void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
Isolate* isolate = isolate_data->isolate();
void NodePlatform::RegisterIsolate(Isolate* isolate, uv_loop_t* loop) {
Mutex::ScopedLock lock(per_isolate_mutex_);
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
if (existing) {
Expand All @@ -272,8 +271,7 @@ void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
}
}

void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
Isolate* isolate = isolate_data->isolate();
void NodePlatform::UnregisterIsolate(Isolate* isolate) {
Mutex::ScopedLock lock(per_isolate_mutex_);
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
CHECK(existing);
Expand Down
4 changes: 2 additions & 2 deletions src/node_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ class NodePlatform : public MultiIsolatePlatform {
v8::TracingController* GetTracingController() override;
bool FlushForegroundTasks(v8::Isolate* isolate) override;

void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
void UnregisterIsolate(IsolateData* isolate_data) override;
void RegisterIsolate(v8::Isolate* isolate, uv_loop_t* loop) override;
void UnregisterIsolate(v8::Isolate* isolate) override;

std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
v8::Isolate* isolate) override;
Expand Down
5 changes: 3 additions & 2 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ Worker::Worker(Environment* env, Local<Object> wrap)

array_buffer_allocator_.reset(CreateArrayBufferAllocator());

isolate_ = NewIsolate(array_buffer_allocator_.get());
CHECK_NE(isolate_, nullptr);
CHECK_EQ(uv_loop_init(&loop_), 0);
isolate_ = NewIsolate(array_buffer_allocator_.get(), &loop_);
CHECK_NE(isolate_, nullptr);

thread_exit_async_.reset(new uv_async_t);
thread_exit_async_->data = this;
Expand Down Expand Up @@ -265,6 +265,7 @@ void Worker::DisposeIsolate() {
platform->CancelPendingDelayedTasks(isolate_);

isolate_data_.reset();
platform->UnregisterIsolate(isolate_);

isolate_->Dispose();
isolate_ = nullptr;
Expand Down
3 changes: 3 additions & 0 deletions test/cctest/node_test_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ class NodeTestFixture : public ::testing::Test {
&node::FreeArrayBufferAllocator);
isolate_ = NewIsolate(allocator.get());
CHECK_NE(isolate_, nullptr);
platform->RegisterIsolate(isolate_, &current_loop);
v8::Isolate::Initialize(isolate_, params);
}

virtual void TearDown() {
isolate_->Dispose();
platform->UnregisterIsolate(isolate_);
isolate_ = nullptr;
}
};
Expand Down

0 comments on commit d5e7294

Please sign in to comment.