Skip to content

Commit 2230a51

Browse files
Gabriel Charettehashseed
authored andcommitted
Use modern v8::Platform worker threads APIs.
Precursor to removing deprecated APIs on the v8 side @ https://chromium-review.googlesource.com/c/v8/v8/+/1045310
1 parent a8a0667 commit 2230a51

File tree

4 files changed

+54
-56
lines changed

4 files changed

+54
-56
lines changed

src/node.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static struct {
301301
}
302302

303303
void DrainVMTasks(Isolate* isolate) {
304-
platform_->DrainBackgroundTasks(isolate);
304+
platform_->DrainTasks(isolate);
305305
}
306306

307307
void CancelVMTasks(Isolate* isolate) {

src/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class Environment;
220220
class MultiIsolatePlatform : public v8::Platform {
221221
public:
222222
virtual ~MultiIsolatePlatform() { }
223-
virtual void DrainBackgroundTasks(v8::Isolate* isolate) = 0;
223+
virtual void DrainTasks(v8::Isolate* isolate) = 0;
224224
virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0;
225225

226226
// These will be called by the `IsolateData` creation/destruction functions.

src/node_platform.cc

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,50 @@ using v8::Platform;
1616
using v8::Task;
1717
using v8::TracingController;
1818

19-
static void BackgroundRunner(void* data) {
20-
TaskQueue<Task>* background_tasks = static_cast<TaskQueue<Task>*>(data);
21-
while (std::unique_ptr<Task> task = background_tasks->BlockingPop()) {
19+
namespace {
20+
21+
static void WorkerThreadMain(void* data) {
22+
TaskQueue<Task>* pending_worker_tasks = static_cast<TaskQueue<Task>*>(data);
23+
while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop()) {
2224
task->Run();
23-
background_tasks->NotifyOfCompletion();
25+
pending_worker_tasks->NotifyOfCompletion();
2426
}
2527
}
2628

27-
BackgroundTaskRunner::BackgroundTaskRunner(int thread_pool_size) {
29+
} // namespace
30+
31+
WorkerThreadsTaskRunner::WorkerThreadsTaskRunner(int thread_pool_size) {
2832
for (int i = 0; i < thread_pool_size; i++) {
2933
std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
30-
if (uv_thread_create(t.get(), BackgroundRunner, &background_tasks_) != 0)
34+
if (uv_thread_create(t.get(), WorkerThreadMain,
35+
&pending_worker_tasks_) != 0) {
3136
break;
37+
}
3238
threads_.push_back(std::move(t));
3339
}
3440
}
3541

36-
void BackgroundTaskRunner::PostTask(std::unique_ptr<Task> task) {
37-
background_tasks_.Push(std::move(task));
38-
}
39-
40-
void BackgroundTaskRunner::PostIdleTask(std::unique_ptr<v8::IdleTask> task) {
41-
UNREACHABLE();
42+
void WorkerThreadsTaskRunner::PostTask(std::unique_ptr<Task> task) {
43+
pending_worker_tasks_.Push(std::move(task));
4244
}
4345

44-
void BackgroundTaskRunner::PostDelayedTask(std::unique_ptr<v8::Task> task,
45-
double delay_in_seconds) {
46+
void WorkerThreadsTaskRunner::PostDelayedTask(std::unique_ptr<v8::Task> task,
47+
double delay_in_seconds) {
4648
UNREACHABLE();
4749
}
4850

49-
void BackgroundTaskRunner::BlockingDrain() {
50-
background_tasks_.BlockingDrain();
51+
void WorkerThreadsTaskRunner::BlockingDrain() {
52+
pending_worker_tasks_.BlockingDrain();
5153
}
5254

53-
void BackgroundTaskRunner::Shutdown() {
54-
background_tasks_.Stop();
55+
void WorkerThreadsTaskRunner::Shutdown() {
56+
pending_worker_tasks_.Stop();
5557
for (size_t i = 0; i < threads_.size(); i++) {
5658
CHECK_EQ(0, uv_thread_join(threads_[i].get()));
5759
}
5860
}
5961

60-
size_t BackgroundTaskRunner::NumberOfAvailableBackgroundThreads() const {
62+
int WorkerThreadsTaskRunner::NumberOfWorkerThreads() const {
6163
return threads_.size();
6264
}
6365

@@ -120,8 +122,8 @@ NodePlatform::NodePlatform(int thread_pool_size,
120122
TracingController* controller = new TracingController();
121123
tracing_controller_.reset(controller);
122124
}
123-
background_task_runner_ =
124-
std::make_shared<BackgroundTaskRunner>(thread_pool_size);
125+
worker_thread_task_runner_ =
126+
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
125127
}
126128

127129
void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
@@ -147,16 +149,16 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
147149
}
148150

149151
void NodePlatform::Shutdown() {
150-
background_task_runner_->Shutdown();
152+
worker_thread_task_runner_->Shutdown();
151153

152154
{
153155
Mutex::ScopedLock lock(per_isolate_mutex_);
154156
per_isolate_.clear();
155157
}
156158
}
157159

158-
size_t NodePlatform::NumberOfAvailableBackgroundThreads() {
159-
return background_task_runner_->NumberOfAvailableBackgroundThreads();
160+
int NodePlatform::NumberOfWorkerThreads() {
161+
return worker_thread_task_runner_->NumberOfWorkerThreads();
160162
}
161163

162164
void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
@@ -188,15 +190,12 @@ void PerIsolatePlatformData::CancelPendingDelayedTasks() {
188190
scheduled_delayed_tasks_.clear();
189191
}
190192

191-
void NodePlatform::DrainBackgroundTasks(Isolate* isolate) {
193+
void NodePlatform::DrainTasks(Isolate* isolate) {
192194
std::shared_ptr<PerIsolatePlatformData> per_isolate = ForIsolate(isolate);
193195

194196
do {
195-
// Right now, there is no way to drain only background tasks associated
196-
// with a specific isolate, so this sometimes does more work than
197-
// necessary. In the long run, that functionality is probably going to
198-
// be available anyway, though.
199-
background_task_runner_->BlockingDrain();
197+
// Worker tasks aren't associated with an Isolate.
198+
worker_thread_task_runner_->BlockingDrain();
200199
} while (per_isolate->FlushForegroundTasksInternal());
201200
}
202201

@@ -230,11 +229,17 @@ bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
230229
return did_work;
231230
}
232231

233-
void NodePlatform::CallOnBackgroundThread(Task* task,
234-
ExpectedRuntime expected_runtime) {
235-
background_task_runner_->PostTask(std::unique_ptr<Task>(task));
232+
void NodePlatform::CallOnWorkerThread(std::unique_ptr<v8::Task> task) {
233+
worker_thread_task_runner_->PostTask(std::move(task));
236234
}
237235

236+
void NodePlatform::CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
237+
double delay_in_seconds) {
238+
worker_thread_task_runner_->PostDelayedTask(std::move(task),
239+
delay_in_seconds);
240+
}
241+
242+
238243
std::shared_ptr<PerIsolatePlatformData>
239244
NodePlatform::ForIsolate(Isolate* isolate) {
240245
Mutex::ScopedLock lock(per_isolate_mutex_);
@@ -264,11 +269,6 @@ void NodePlatform::CancelPendingDelayedTasks(v8::Isolate* isolate) {
264269

265270
bool NodePlatform::IdleTasksEnabled(Isolate* isolate) { return false; }
266271

267-
std::shared_ptr<v8::TaskRunner>
268-
NodePlatform::GetBackgroundTaskRunner(Isolate* isolate) {
269-
return background_task_runner_;
270-
}
271-
272272
std::shared_ptr<v8::TaskRunner>
273273
NodePlatform::GetForegroundTaskRunner(Isolate* isolate) {
274274
return ForIsolate(isolate);

src/node_platform.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,22 @@ class PerIsolatePlatformData :
8989
std::vector<DelayedTaskPointer> scheduled_delayed_tasks_;
9090
};
9191

92-
// This acts as the single background task runner for all Isolates.
93-
class BackgroundTaskRunner : public v8::TaskRunner {
92+
// This acts as the single worker thread task runner for all Isolates.
93+
class WorkerThreadsTaskRunner {
9494
public:
95-
explicit BackgroundTaskRunner(int thread_pool_size);
95+
explicit WorkerThreadsTaskRunner(int thread_pool_size);
9696

97-
void PostTask(std::unique_ptr<v8::Task> task) override;
98-
void PostIdleTask(std::unique_ptr<v8::IdleTask> task) override;
97+
void PostTask(std::unique_ptr<v8::Task> task);
9998
void PostDelayedTask(std::unique_ptr<v8::Task> task,
100-
double delay_in_seconds) override;
101-
bool IdleTasksEnabled() override { return false; };
99+
double delay_in_seconds);
102100

103101
void BlockingDrain();
104102
void Shutdown();
105103

106-
size_t NumberOfAvailableBackgroundThreads() const;
104+
int NumberOfWorkerThreads() const;
105+
107106
private:
108-
TaskQueue<v8::Task> background_tasks_;
107+
TaskQueue<v8::Task> pending_worker_tasks_;
109108
std::vector<std::unique_ptr<uv_thread_t>> threads_;
110109
};
111110

@@ -114,14 +113,15 @@ class NodePlatform : public MultiIsolatePlatform {
114113
NodePlatform(int thread_pool_size, v8::TracingController* tracing_controller);
115114
virtual ~NodePlatform() {}
116115

117-
void DrainBackgroundTasks(v8::Isolate* isolate) override;
116+
void DrainTasks(v8::Isolate* isolate) override;
118117
void CancelPendingDelayedTasks(v8::Isolate* isolate) override;
119118
void Shutdown();
120119

121120
// v8::Platform implementation.
122-
size_t NumberOfAvailableBackgroundThreads() override;
123-
void CallOnBackgroundThread(v8::Task* task,
124-
ExpectedRuntime expected_runtime) override;
121+
int NumberOfWorkerThreads() override;
122+
void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override;
123+
void CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
124+
double delay_in_seconds) override;
125125
void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override;
126126
void CallDelayedOnForegroundThread(v8::Isolate* isolate, v8::Task* task,
127127
double delay_in_seconds) override;
@@ -135,8 +135,6 @@ class NodePlatform : public MultiIsolatePlatform {
135135
void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
136136
void UnregisterIsolate(IsolateData* isolate_data) override;
137137

138-
std::shared_ptr<v8::TaskRunner> GetBackgroundTaskRunner(
139-
v8::Isolate* isolate) override;
140138
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
141139
v8::Isolate* isolate) override;
142140

@@ -148,7 +146,7 @@ class NodePlatform : public MultiIsolatePlatform {
148146
std::shared_ptr<PerIsolatePlatformData>> per_isolate_;
149147

150148
std::unique_ptr<v8::TracingController> tracing_controller_;
151-
std::shared_ptr<BackgroundTaskRunner> background_task_runner_;
149+
std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
152150
};
153151

154152
} // namespace node

0 commit comments

Comments
 (0)