From c712fb7cd6222018cf615fd0071998bde6f16da9 Mon Sep 17 00:00:00 2001 From: Marcel Laverdet Date: Tue, 12 Nov 2019 14:26:08 -0800 Subject: src: add abstract `IsolatePlatformDelegate` Adds a new abstract class for module authors and embedders to register arbitrary isolates with `node::MultiIsolatePlatform`. PR-URL: https://github.com/nodejs/node/pull/30324 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis Reviewed-By: Joyee Cheung --- src/node_platform.cc | 66 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 19 deletions(-) (limited to 'src/node_platform.cc') diff --git a/src/node_platform.cc b/src/node_platform.cc index 6e3b4abfee..383f67ce0f 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -230,6 +230,11 @@ PerIsolatePlatformData::PerIsolatePlatformData( uv_unref(reinterpret_cast(flush_tasks_)); } +std::shared_ptr +PerIsolatePlatformData::GetForegroundTaskRunner() { + return shared_from_this(); +} + void PerIsolatePlatformData::FlushTasks(uv_async_t* handle) { auto platform_data = static_cast(handle->data); platform_data->FlushForegroundTasksInternal(); @@ -267,7 +272,7 @@ void PerIsolatePlatformData::PostNonNestableDelayedTask( } PerIsolatePlatformData::~PerIsolatePlatformData() { - Shutdown(); + CHECK(!flush_tasks_); } void PerIsolatePlatformData::AddShutdownCallback(void (*callback)(void*), @@ -325,18 +330,32 @@ NodePlatform::NodePlatform(int thread_pool_size, void NodePlatform::RegisterIsolate(Isolate* isolate, uv_loop_t* loop) { Mutex::ScopedLock lock(per_isolate_mutex_); - std::shared_ptr existing = per_isolate_[isolate]; - CHECK(!existing); - per_isolate_[isolate] = - std::make_shared(isolate, loop); + auto delegate = std::make_shared(isolate, loop); + IsolatePlatformDelegate* ptr = delegate.get(); + auto insertion = per_isolate_.emplace( + isolate, + std::make_pair(ptr, std::move(delegate))); + CHECK(insertion.second); +} + +void NodePlatform::RegisterIsolate(Isolate* isolate, + IsolatePlatformDelegate* delegate) { + Mutex::ScopedLock lock(per_isolate_mutex_); + auto insertion = per_isolate_.emplace( + isolate, + std::make_pair(delegate, std::shared_ptr{})); + CHECK(insertion.second); } void NodePlatform::UnregisterIsolate(Isolate* isolate) { Mutex::ScopedLock lock(per_isolate_mutex_); - std::shared_ptr existing = per_isolate_[isolate]; - CHECK(existing); - existing->Shutdown(); - per_isolate_.erase(isolate); + auto existing_it = per_isolate_.find(isolate); + CHECK_NE(existing_it, per_isolate_.end()); + auto& existing = existing_it->second; + if (existing.second) { + existing.second->Shutdown(); + } + per_isolate_.erase(existing_it); } void NodePlatform::AddIsolateFinishedCallback(Isolate* isolate, @@ -344,11 +363,11 @@ void NodePlatform::AddIsolateFinishedCallback(Isolate* isolate, Mutex::ScopedLock lock(per_isolate_mutex_); auto it = per_isolate_.find(isolate); if (it == per_isolate_.end()) { - CHECK(it->second); cb(data); return; } - it->second->AddShutdownCallback(cb, data); + CHECK(it->second.second); + it->second.second->AddShutdownCallback(cb, data); } void NodePlatform::Shutdown() { @@ -394,7 +413,7 @@ void PerIsolatePlatformData::RunForegroundTask(uv_timer_t* handle) { } void NodePlatform::DrainTasks(Isolate* isolate) { - std::shared_ptr per_isolate = ForIsolate(isolate); + std::shared_ptr per_isolate = ForNodeIsolate(isolate); do { // Worker tasks aren't associated with an Isolate. @@ -452,23 +471,32 @@ void NodePlatform::CallDelayedOnWorkerThread(std::unique_ptr task, } +IsolatePlatformDelegate* NodePlatform::ForIsolate(Isolate* isolate) { + Mutex::ScopedLock lock(per_isolate_mutex_); + auto data = per_isolate_[isolate]; + CHECK_NOT_NULL(data.first); + return data.first; +} + std::shared_ptr -NodePlatform::ForIsolate(Isolate* isolate) { +NodePlatform::ForNodeIsolate(Isolate* isolate) { Mutex::ScopedLock lock(per_isolate_mutex_); - std::shared_ptr data = per_isolate_[isolate]; - CHECK(data); - return data; + auto data = per_isolate_[isolate]; + CHECK(data.second); + return data.second; } bool NodePlatform::FlushForegroundTasks(Isolate* isolate) { - return ForIsolate(isolate)->FlushForegroundTasksInternal(); + return ForNodeIsolate(isolate)->FlushForegroundTasksInternal(); } -bool NodePlatform::IdleTasksEnabled(Isolate* isolate) { return false; } +bool NodePlatform::IdleTasksEnabled(Isolate* isolate) { + return ForIsolate(isolate)->IdleTasksEnabled(); +} std::shared_ptr NodePlatform::GetForegroundTaskRunner(Isolate* isolate) { - return ForIsolate(isolate); + return ForIsolate(isolate)->GetForegroundTaskRunner(); } double NodePlatform::MonotonicallyIncreasingTime() { -- cgit v1.2.3