From 86ea71c8721c4a827d33c5caf590eb7af63d22c4 Mon Sep 17 00:00:00 2001 From: francoisdoray <49376157+francoisdoray@users.noreply.github.com> Date: Wed, 17 Apr 2024 10:45:50 -0400 Subject: [PATCH] [v8-tasks] Add source location to v8::TaskRunner, step 3/4. (#178) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: François Doray --- src/node_platform.cc | 24 +++++++++++++++--------- src/node_platform.h | 44 ++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/node_platform.cc b/src/node_platform.cc index 00ca9757bc4d0c..632217b088ee52 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -245,11 +245,13 @@ void PerIsolatePlatformData::FlushTasks(uv_async_t* handle) { platform_data->FlushForegroundTasksInternal(); } -void PerIsolatePlatformData::PostIdleTask(std::unique_ptr task) { +void PerIsolatePlatformData::PostIdleTaskImpl( + std::unique_ptr task, const v8::SourceLocation& location) { UNREACHABLE(); } -void PerIsolatePlatformData::PostTask(std::unique_ptr task) { +void PerIsolatePlatformData::PostTaskImpl(std::unique_ptr task, + const v8::SourceLocation& location) { if (flush_tasks_ == nullptr) { // V8 may post tasks during Isolate disposal. In that case, the only // sensible path forward is to discard the task. @@ -259,8 +261,10 @@ void PerIsolatePlatformData::PostTask(std::unique_ptr task) { uv_async_send(flush_tasks_); } -void PerIsolatePlatformData::PostDelayedTask( - std::unique_ptr task, double delay_in_seconds) { +void PerIsolatePlatformData::PostDelayedTaskImpl( + std::unique_ptr task, + double delay_in_seconds, + const v8::SourceLocation& location) { if (flush_tasks_ == nullptr) { // V8 may post tasks during Isolate disposal. In that case, the only // sensible path forward is to discard the task. @@ -274,14 +278,16 @@ void PerIsolatePlatformData::PostDelayedTask( uv_async_send(flush_tasks_); } -void PerIsolatePlatformData::PostNonNestableTask(std::unique_ptr task) { - PostTask(std::move(task)); +void PerIsolatePlatformData::PostNonNestableTaskImpl( + std::unique_ptr task, const v8::SourceLocation& location) { + PostTaskImpl(std::move(task), location); } -void PerIsolatePlatformData::PostNonNestableDelayedTask( +void PerIsolatePlatformData::PostNonNestableDelayedTaskImpl( std::unique_ptr task, - double delay_in_seconds) { - PostDelayedTask(std::move(task), delay_in_seconds); + double delay_in_seconds, + const v8::SourceLocation& location) { + PostDelayedTaskImpl(std::move(task), delay_in_seconds, location); } PerIsolatePlatformData::~PerIsolatePlatformData() { diff --git a/src/node_platform.h b/src/node_platform.h index 77cb5e6e4f891c..4c90e7247e1792 100644 --- a/src/node_platform.h +++ b/src/node_platform.h @@ -3,10 +3,10 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#include #include #include #include -#include #include "libplatform/libplatform.h" #include "node.h" @@ -50,27 +50,20 @@ struct DelayedTask { }; // This acts as the foreground task runner for a given Isolate. -class PerIsolatePlatformData : - public IsolatePlatformDelegate, - public v8::TaskRunner, - public std::enable_shared_from_this { +class PerIsolatePlatformData + : public IsolatePlatformDelegate, + public v8::TaskRunner, + public std::enable_shared_from_this { public: PerIsolatePlatformData(v8::Isolate* isolate, uv_loop_t* loop); ~PerIsolatePlatformData() override; std::shared_ptr GetForegroundTaskRunner() override; - void PostTask(std::unique_ptr task) override; - void PostIdleTask(std::unique_ptr task) override; - void PostDelayedTask(std::unique_ptr task, - double delay_in_seconds) override; bool IdleTasksEnabled() override { return false; } // Non-nestable tasks are treated like regular tasks. bool NonNestableTasksEnabled() const override { return true; } bool NonNestableDelayedTasksEnabled() const override { return true; } - void PostNonNestableTask(std::unique_ptr task) override; - void PostNonNestableDelayedTask(std::unique_ptr task, - double delay_in_seconds) override; void AddShutdownCallback(void (*callback)(void*), void* data); void Shutdown(); @@ -83,6 +76,21 @@ class PerIsolatePlatformData : const uv_loop_t* event_loop() const { return loop_; } private: + // v8::TaskRunner implementation. + void PostTaskImpl(std::unique_ptr task, + const v8::SourceLocation& location) override; + void PostDelayedTaskImpl(std::unique_ptr task, + double delay_in_seconds, + const v8::SourceLocation& location) override; + void PostIdleTaskImpl(std::unique_ptr task, + const v8::SourceLocation& location) override; + void PostNonNestableTaskImpl(std::unique_ptr task, + const v8::SourceLocation& location) override; + void PostNonNestableDelayedTaskImpl( + std::unique_ptr task, + double delay_in_seconds, + const v8::SourceLocation& location) override; + void DeleteFromScheduledTasks(DelayedTask* task); void DecreaseHandleCount(); @@ -107,7 +115,7 @@ class PerIsolatePlatformData : TaskQueue foreground_delayed_tasks_; // Use a custom deleter because libuv needs to close the handle first. - typedef std::unique_ptr + typedef std::unique_ptr DelayedTaskPointer; std::vector scheduled_delayed_tasks_; }; @@ -118,8 +126,7 @@ class WorkerThreadsTaskRunner { explicit WorkerThreadsTaskRunner(int thread_pool_size); void PostTask(std::unique_ptr task); - void PostDelayedTask(std::unique_ptr task, - double delay_in_seconds); + void PostDelayedTask(std::unique_ptr task, double delay_in_seconds); void BlockingDrain(); void Shutdown(); @@ -171,7 +178,8 @@ class NodePlatform : public MultiIsolatePlatform { void UnregisterIsolate(v8::Isolate* isolate) override; void AddIsolateFinishedCallback(v8::Isolate* isolate, - void (*callback)(void*), void* data) override; + void (*callback)(void*), + void* data) override; std::shared_ptr GetForegroundTaskRunner( v8::Isolate* isolate) override; @@ -184,8 +192,8 @@ class NodePlatform : public MultiIsolatePlatform { std::shared_ptr ForNodeIsolate(v8::Isolate* isolate); Mutex per_isolate_mutex_; - using DelegatePair = std::pair< - IsolatePlatformDelegate*, std::shared_ptr>; + using DelegatePair = std::pair>; std::unordered_map per_isolate_; v8::TracingController* tracing_controller_;