src: add source location to v8::TaskRunner

Refs: https://chromium-review.googlesource.com/c/v8/v8/+/5300826
PR-URL: https://github.com/nodejs/node/pull/54077
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
This commit is contained in:
François Doray 2024-04-16 20:25:25 +00:00 committed by Michaël Zasso
parent b7bcf3e121
commit 525b3f22d1
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
2 changed files with 41 additions and 27 deletions

View File

@ -245,11 +245,13 @@ void PerIsolatePlatformData::FlushTasks(uv_async_t* handle) {
platform_data->FlushForegroundTasksInternal(); platform_data->FlushForegroundTasksInternal();
} }
void PerIsolatePlatformData::PostIdleTask(std::unique_ptr<v8::IdleTask> task) { void PerIsolatePlatformData::PostIdleTaskImpl(
std::unique_ptr<v8::IdleTask> task, const v8::SourceLocation& location) {
UNREACHABLE(); UNREACHABLE();
} }
void PerIsolatePlatformData::PostTask(std::unique_ptr<Task> task) { void PerIsolatePlatformData::PostTaskImpl(std::unique_ptr<Task> task,
const v8::SourceLocation& location) {
if (flush_tasks_ == nullptr) { if (flush_tasks_ == nullptr) {
// V8 may post tasks during Isolate disposal. In that case, the only // V8 may post tasks during Isolate disposal. In that case, the only
// sensible path forward is to discard the task. // sensible path forward is to discard the task.
@ -259,8 +261,10 @@ void PerIsolatePlatformData::PostTask(std::unique_ptr<Task> task) {
uv_async_send(flush_tasks_); uv_async_send(flush_tasks_);
} }
void PerIsolatePlatformData::PostDelayedTask( void PerIsolatePlatformData::PostDelayedTaskImpl(
std::unique_ptr<Task> task, double delay_in_seconds) { std::unique_ptr<Task> task,
double delay_in_seconds,
const v8::SourceLocation& location) {
if (flush_tasks_ == nullptr) { if (flush_tasks_ == nullptr) {
// V8 may post tasks during Isolate disposal. In that case, the only // V8 may post tasks during Isolate disposal. In that case, the only
// sensible path forward is to discard the task. // sensible path forward is to discard the task.
@ -274,14 +278,16 @@ void PerIsolatePlatformData::PostDelayedTask(
uv_async_send(flush_tasks_); uv_async_send(flush_tasks_);
} }
void PerIsolatePlatformData::PostNonNestableTask(std::unique_ptr<Task> task) { void PerIsolatePlatformData::PostNonNestableTaskImpl(
PostTask(std::move(task)); std::unique_ptr<Task> task, const v8::SourceLocation& location) {
PostTaskImpl(std::move(task), location);
} }
void PerIsolatePlatformData::PostNonNestableDelayedTask( void PerIsolatePlatformData::PostNonNestableDelayedTaskImpl(
std::unique_ptr<Task> task, std::unique_ptr<Task> task,
double delay_in_seconds) { double delay_in_seconds,
PostDelayedTask(std::move(task), delay_in_seconds); const v8::SourceLocation& location) {
PostDelayedTaskImpl(std::move(task), delay_in_seconds, location);
} }
PerIsolatePlatformData::~PerIsolatePlatformData() { PerIsolatePlatformData::~PerIsolatePlatformData() {

View File

@ -3,10 +3,10 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include <functional>
#include <queue> #include <queue>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <functional>
#include "libplatform/libplatform.h" #include "libplatform/libplatform.h"
#include "node.h" #include "node.h"
@ -50,27 +50,20 @@ struct DelayedTask {
}; };
// This acts as the foreground task runner for a given Isolate. // This acts as the foreground task runner for a given Isolate.
class PerIsolatePlatformData : class PerIsolatePlatformData
public IsolatePlatformDelegate, : public IsolatePlatformDelegate,
public v8::TaskRunner, public v8::TaskRunner,
public std::enable_shared_from_this<PerIsolatePlatformData> { public std::enable_shared_from_this<PerIsolatePlatformData> {
public: public:
PerIsolatePlatformData(v8::Isolate* isolate, uv_loop_t* loop); PerIsolatePlatformData(v8::Isolate* isolate, uv_loop_t* loop);
~PerIsolatePlatformData() override; ~PerIsolatePlatformData() override;
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner() override; std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner() override;
void PostTask(std::unique_ptr<v8::Task> task) override;
void PostIdleTask(std::unique_ptr<v8::IdleTask> task) override;
void PostDelayedTask(std::unique_ptr<v8::Task> task,
double delay_in_seconds) override;
bool IdleTasksEnabled() override { return false; } bool IdleTasksEnabled() override { return false; }
// Non-nestable tasks are treated like regular tasks. // Non-nestable tasks are treated like regular tasks.
bool NonNestableTasksEnabled() const override { return true; } bool NonNestableTasksEnabled() const override { return true; }
bool NonNestableDelayedTasksEnabled() const override { return true; } bool NonNestableDelayedTasksEnabled() const override { return true; }
void PostNonNestableTask(std::unique_ptr<v8::Task> task) override;
void PostNonNestableDelayedTask(std::unique_ptr<v8::Task> task,
double delay_in_seconds) override;
void AddShutdownCallback(void (*callback)(void*), void* data); void AddShutdownCallback(void (*callback)(void*), void* data);
void Shutdown(); void Shutdown();
@ -83,6 +76,21 @@ class PerIsolatePlatformData :
const uv_loop_t* event_loop() const { return loop_; } const uv_loop_t* event_loop() const { return loop_; }
private: private:
// v8::TaskRunner implementation.
void PostTaskImpl(std::unique_ptr<v8::Task> task,
const v8::SourceLocation& location) override;
void PostDelayedTaskImpl(std::unique_ptr<v8::Task> task,
double delay_in_seconds,
const v8::SourceLocation& location) override;
void PostIdleTaskImpl(std::unique_ptr<v8::IdleTask> task,
const v8::SourceLocation& location) override;
void PostNonNestableTaskImpl(std::unique_ptr<v8::Task> task,
const v8::SourceLocation& location) override;
void PostNonNestableDelayedTaskImpl(
std::unique_ptr<v8::Task> task,
double delay_in_seconds,
const v8::SourceLocation& location) override;
void DeleteFromScheduledTasks(DelayedTask* task); void DeleteFromScheduledTasks(DelayedTask* task);
void DecreaseHandleCount(); void DecreaseHandleCount();
@ -107,7 +115,7 @@ class PerIsolatePlatformData :
TaskQueue<DelayedTask> foreground_delayed_tasks_; TaskQueue<DelayedTask> foreground_delayed_tasks_;
// Use a custom deleter because libuv needs to close the handle first. // Use a custom deleter because libuv needs to close the handle first.
typedef std::unique_ptr<DelayedTask, void(*)(DelayedTask*)> typedef std::unique_ptr<DelayedTask, void (*)(DelayedTask*)>
DelayedTaskPointer; DelayedTaskPointer;
std::vector<DelayedTaskPointer> scheduled_delayed_tasks_; std::vector<DelayedTaskPointer> scheduled_delayed_tasks_;
}; };
@ -118,8 +126,7 @@ class WorkerThreadsTaskRunner {
explicit WorkerThreadsTaskRunner(int thread_pool_size); explicit WorkerThreadsTaskRunner(int thread_pool_size);
void PostTask(std::unique_ptr<v8::Task> task); void PostTask(std::unique_ptr<v8::Task> task);
void PostDelayedTask(std::unique_ptr<v8::Task> task, void PostDelayedTask(std::unique_ptr<v8::Task> task, double delay_in_seconds);
double delay_in_seconds);
void BlockingDrain(); void BlockingDrain();
void Shutdown(); void Shutdown();
@ -171,7 +178,8 @@ class NodePlatform : public MultiIsolatePlatform {
void UnregisterIsolate(v8::Isolate* isolate) override; void UnregisterIsolate(v8::Isolate* isolate) override;
void AddIsolateFinishedCallback(v8::Isolate* isolate, void AddIsolateFinishedCallback(v8::Isolate* isolate,
void (*callback)(void*), void* data) override; void (*callback)(void*),
void* data) override;
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner( std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
v8::Isolate* isolate) override; v8::Isolate* isolate) override;
@ -184,8 +192,8 @@ class NodePlatform : public MultiIsolatePlatform {
std::shared_ptr<PerIsolatePlatformData> ForNodeIsolate(v8::Isolate* isolate); std::shared_ptr<PerIsolatePlatformData> ForNodeIsolate(v8::Isolate* isolate);
Mutex per_isolate_mutex_; Mutex per_isolate_mutex_;
using DelegatePair = std::pair< using DelegatePair = std::pair<IsolatePlatformDelegate*,
IsolatePlatformDelegate*, std::shared_ptr<PerIsolatePlatformData>>; std::shared_ptr<PerIsolatePlatformData>>;
std::unordered_map<v8::Isolate*, DelegatePair> per_isolate_; std::unordered_map<v8::Isolate*, DelegatePair> per_isolate_;
v8::TracingController* tracing_controller_; v8::TracingController* tracing_controller_;