Skip to content

Commit

Permalink
[v8-tasks] Add source location to v8::TaskRunner, step 3/4. (nodejs#178)
Browse files Browse the repository at this point in the history
Co-authored-by: François Doray <fdoray@chromium.org>
  • Loading branch information
2 people authored and victorgomes committed Jun 3, 2024
1 parent adec83d commit acbfacf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
24 changes: 15 additions & 9 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ void PerIsolatePlatformData::FlushTasks(uv_async_t* handle) {
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();
}

void PerIsolatePlatformData::PostTask(std::unique_ptr<Task> task) {
void PerIsolatePlatformData::PostTaskImpl(std::unique_ptr<Task> 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.
Expand All @@ -259,8 +261,10 @@ void PerIsolatePlatformData::PostTask(std::unique_ptr<Task> task) {
uv_async_send(flush_tasks_);
}

void PerIsolatePlatformData::PostDelayedTask(
std::unique_ptr<Task> task, double delay_in_seconds) {
void PerIsolatePlatformData::PostDelayedTaskImpl(
std::unique_ptr<Task> 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.
Expand All @@ -274,14 +278,16 @@ void PerIsolatePlatformData::PostDelayedTask(
uv_async_send(flush_tasks_);
}

void PerIsolatePlatformData::PostNonNestableTask(std::unique_ptr<Task> task) {
PostTask(std::move(task));
void PerIsolatePlatformData::PostNonNestableTaskImpl(
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,
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() {
Expand Down
44 changes: 26 additions & 18 deletions src/node_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <functional>
#include <queue>
#include <unordered_map>
#include <vector>
#include <functional>

#include "libplatform/libplatform.h"
#include "node.h"
Expand Down Expand Up @@ -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<PerIsolatePlatformData> {
class PerIsolatePlatformData
: public IsolatePlatformDelegate,
public v8::TaskRunner,
public std::enable_shared_from_this<PerIsolatePlatformData> {
public:
PerIsolatePlatformData(v8::Isolate* isolate, uv_loop_t* loop);
~PerIsolatePlatformData() 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; }

// 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<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 Shutdown();
Expand All @@ -83,6 +76,21 @@ class PerIsolatePlatformData :
const uv_loop_t* event_loop() const { return loop_; }

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 DecreaseHandleCount();

Expand All @@ -107,7 +115,7 @@ class PerIsolatePlatformData :
TaskQueue<DelayedTask> foreground_delayed_tasks_;

// 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;
std::vector<DelayedTaskPointer> scheduled_delayed_tasks_;
};
Expand All @@ -118,8 +126,7 @@ class WorkerThreadsTaskRunner {
explicit WorkerThreadsTaskRunner(int thread_pool_size);

void PostTask(std::unique_ptr<v8::Task> task);
void PostDelayedTask(std::unique_ptr<v8::Task> task,
double delay_in_seconds);
void PostDelayedTask(std::unique_ptr<v8::Task> task, double delay_in_seconds);

void BlockingDrain();
void Shutdown();
Expand Down Expand Up @@ -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<v8::TaskRunner> GetForegroundTaskRunner(
v8::Isolate* isolate) override;
Expand All @@ -184,8 +192,8 @@ class NodePlatform : public MultiIsolatePlatform {
std::shared_ptr<PerIsolatePlatformData> ForNodeIsolate(v8::Isolate* isolate);

Mutex per_isolate_mutex_;
using DelegatePair = std::pair<
IsolatePlatformDelegate*, std::shared_ptr<PerIsolatePlatformData>>;
using DelegatePair = std::pair<IsolatePlatformDelegate*,
std::shared_ptr<PerIsolatePlatformData>>;
std::unordered_map<v8::Isolate*, DelegatePair> per_isolate_;

v8::TracingController* tracing_controller_;
Expand Down

0 comments on commit acbfacf

Please sign in to comment.