Skip to content

Commit 0f3c2c6

Browse files
Gabriel Charettetargos
Gabriel Charette
authored andcommitted
src: 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 PR-URL: #21079 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Yang Guo <yangguo@chromium.org>
1 parent 8bcb128 commit 0f3c2c6

File tree

5 files changed

+56
-58
lines changed

5 files changed

+56
-58
lines changed

src/node.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ static struct {
404404
}
405405

406406
void DrainVMTasks(Isolate* isolate) {
407-
platform_->DrainBackgroundTasks(isolate);
407+
platform_->DrainTasks(isolate);
408408
}
409409

410410
void CancelVMTasks(Isolate* isolate) {

src/node.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class MultiIsolatePlatform : public v8::Platform {
229229
// posted during flushing of the queue are postponed until the next
230230
// flushing.
231231
virtual bool FlushForegroundTasks(v8::Isolate* isolate) = 0;
232-
virtual void DrainBackgroundTasks(v8::Isolate* isolate) = 0;
232+
virtual void DrainTasks(v8::Isolate* isolate) = 0;
233233
virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0;
234234

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

src/node_platform.cc

+38-38
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,52 @@ using v8::Platform;
1515
using v8::Task;
1616
using v8::TracingController;
1717

18-
static void BackgroundRunner(void* data) {
18+
namespace {
19+
20+
static void WorkerThreadMain(void* data) {
1921
TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
2022
"BackgroundTaskRunner");
21-
TaskQueue<Task> *background_tasks = static_cast<TaskQueue<Task> *>(data);
22-
while (std::unique_ptr<Task> task = background_tasks->BlockingPop()) {
23+
TaskQueue<Task>* pending_worker_tasks = static_cast<TaskQueue<Task>*>(data);
24+
while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop()) {
2325
task->Run();
24-
background_tasks->NotifyOfCompletion();
26+
pending_worker_tasks->NotifyOfCompletion();
2527
}
2628
}
2729

28-
BackgroundTaskRunner::BackgroundTaskRunner(int thread_pool_size) {
30+
} // namespace
31+
32+
WorkerThreadsTaskRunner::WorkerThreadsTaskRunner(int thread_pool_size) {
2933
for (int i = 0; i < thread_pool_size; i++) {
3034
std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
31-
if (uv_thread_create(t.get(), BackgroundRunner, &background_tasks_) != 0)
35+
if (uv_thread_create(t.get(), WorkerThreadMain,
36+
&pending_worker_tasks_) != 0) {
3237
break;
38+
}
3339
threads_.push_back(std::move(t));
3440
}
3541
}
3642

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

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

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

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

61-
size_t BackgroundTaskRunner::NumberOfAvailableBackgroundThreads() const {
63+
int WorkerThreadsTaskRunner::NumberOfWorkerThreads() const {
6264
return threads_.size();
6365
}
6466

@@ -131,8 +133,8 @@ NodePlatform::NodePlatform(int thread_pool_size,
131133
TracingController* controller = new TracingController();
132134
tracing_controller_.reset(controller);
133135
}
134-
background_task_runner_ =
135-
std::make_shared<BackgroundTaskRunner>(thread_pool_size);
136+
worker_thread_task_runner_ =
137+
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
136138
}
137139

138140
void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
@@ -160,16 +162,16 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
160162
}
161163

162164
void NodePlatform::Shutdown() {
163-
background_task_runner_->Shutdown();
165+
worker_thread_task_runner_->Shutdown();
164166

165167
{
166168
Mutex::ScopedLock lock(per_isolate_mutex_);
167169
per_isolate_.clear();
168170
}
169171
}
170172

171-
size_t NodePlatform::NumberOfAvailableBackgroundThreads() {
172-
return background_task_runner_->NumberOfAvailableBackgroundThreads();
173+
int NodePlatform::NumberOfWorkerThreads() {
174+
return worker_thread_task_runner_->NumberOfWorkerThreads();
173175
}
174176

175177
void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
@@ -201,15 +203,12 @@ void PerIsolatePlatformData::CancelPendingDelayedTasks() {
201203
scheduled_delayed_tasks_.clear();
202204
}
203205

204-
void NodePlatform::DrainBackgroundTasks(Isolate* isolate) {
206+
void NodePlatform::DrainTasks(Isolate* isolate) {
205207
std::shared_ptr<PerIsolatePlatformData> per_isolate = ForIsolate(isolate);
206208

207209
do {
208-
// Right now, there is no way to drain only background tasks associated
209-
// with a specific isolate, so this sometimes does more work than
210-
// necessary. In the long run, that functionality is probably going to
211-
// be available anyway, though.
212-
background_task_runner_->BlockingDrain();
210+
// Worker tasks aren't associated with an Isolate.
211+
worker_thread_task_runner_->BlockingDrain();
213212
} while (per_isolate->FlushForegroundTasksInternal());
214213
}
215214

@@ -249,11 +248,17 @@ bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
249248
return did_work;
250249
}
251250

252-
void NodePlatform::CallOnBackgroundThread(Task* task,
253-
ExpectedRuntime expected_runtime) {
254-
background_task_runner_->PostTask(std::unique_ptr<Task>(task));
251+
void NodePlatform::CallOnWorkerThread(std::unique_ptr<v8::Task> task) {
252+
worker_thread_task_runner_->PostTask(std::move(task));
255253
}
256254

255+
void NodePlatform::CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
256+
double delay_in_seconds) {
257+
worker_thread_task_runner_->PostDelayedTask(std::move(task),
258+
delay_in_seconds);
259+
}
260+
261+
257262
std::shared_ptr<PerIsolatePlatformData>
258263
NodePlatform::ForIsolate(Isolate* isolate) {
259264
Mutex::ScopedLock lock(per_isolate_mutex_);
@@ -283,11 +288,6 @@ void NodePlatform::CancelPendingDelayedTasks(v8::Isolate* isolate) {
283288

284289
bool NodePlatform::IdleTasksEnabled(Isolate* isolate) { return false; }
285290

286-
std::shared_ptr<v8::TaskRunner>
287-
NodePlatform::GetBackgroundTaskRunner(Isolate* isolate) {
288-
return background_task_runner_;
289-
}
290-
291291
std::shared_ptr<v8::TaskRunner>
292292
NodePlatform::GetForegroundTaskRunner(Isolate* isolate) {
293293
return ForIsolate(isolate);

src/node_platform.h

+14-16
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,22 @@ class PerIsolatePlatformData :
9393
std::vector<DelayedTaskPointer> scheduled_delayed_tasks_;
9494
};
9595

96-
// This acts as the single background task runner for all Isolates.
97-
class BackgroundTaskRunner : public v8::TaskRunner {
96+
// This acts as the single worker thread task runner for all Isolates.
97+
class WorkerThreadsTaskRunner {
9898
public:
99-
explicit BackgroundTaskRunner(int thread_pool_size);
99+
explicit WorkerThreadsTaskRunner(int thread_pool_size);
100100

101-
void PostTask(std::unique_ptr<v8::Task> task) override;
102-
void PostIdleTask(std::unique_ptr<v8::IdleTask> task) override;
101+
void PostTask(std::unique_ptr<v8::Task> task);
103102
void PostDelayedTask(std::unique_ptr<v8::Task> task,
104-
double delay_in_seconds) override;
105-
bool IdleTasksEnabled() override { return false; };
103+
double delay_in_seconds);
106104

107105
void BlockingDrain();
108106
void Shutdown();
109107

110-
size_t NumberOfAvailableBackgroundThreads() const;
108+
int NumberOfWorkerThreads() const;
109+
111110
private:
112-
TaskQueue<v8::Task> background_tasks_;
111+
TaskQueue<v8::Task> pending_worker_tasks_;
113112
std::vector<std::unique_ptr<uv_thread_t>> threads_;
114113
};
115114

@@ -118,14 +117,15 @@ class NodePlatform : public MultiIsolatePlatform {
118117
NodePlatform(int thread_pool_size, v8::TracingController* tracing_controller);
119118
virtual ~NodePlatform() {}
120119

121-
void DrainBackgroundTasks(v8::Isolate* isolate) override;
120+
void DrainTasks(v8::Isolate* isolate) override;
122121
void CancelPendingDelayedTasks(v8::Isolate* isolate) override;
123122
void Shutdown();
124123

125124
// v8::Platform implementation.
126-
size_t NumberOfAvailableBackgroundThreads() override;
127-
void CallOnBackgroundThread(v8::Task* task,
128-
ExpectedRuntime expected_runtime) override;
125+
int NumberOfWorkerThreads() override;
126+
void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override;
127+
void CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
128+
double delay_in_seconds) override;
129129
void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override;
130130
void CallDelayedOnForegroundThread(v8::Isolate* isolate, v8::Task* task,
131131
double delay_in_seconds) override;
@@ -138,8 +138,6 @@ class NodePlatform : public MultiIsolatePlatform {
138138
void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
139139
void UnregisterIsolate(IsolateData* isolate_data) override;
140140

141-
std::shared_ptr<v8::TaskRunner> GetBackgroundTaskRunner(
142-
v8::Isolate* isolate) override;
143141
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
144142
v8::Isolate* isolate) override;
145143

@@ -151,7 +149,7 @@ class NodePlatform : public MultiIsolatePlatform {
151149
std::shared_ptr<PerIsolatePlatformData>> per_isolate_;
152150

153151
std::unique_ptr<v8::TracingController> tracing_controller_;
154-
std::shared_ptr<BackgroundTaskRunner> background_task_runner_;
152+
std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
155153
};
156154

157155
} // namespace node

src/node_worker.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void Worker::Run() {
173173
uv_run(&loop_, UV_RUN_DEFAULT);
174174
if (is_stopped()) break;
175175

176-
platform->DrainBackgroundTasks(isolate_);
176+
platform->DrainTasks(isolate_);
177177

178178
more = uv_loop_alive(&loop_);
179179
if (more && !is_stopped())
@@ -232,7 +232,7 @@ void Worker::Run() {
232232
// This call needs to be made while the `Environment` is still alive
233233
// because we assume that it is available for async tracking in the
234234
// NodePlatform implementation.
235-
platform->DrainBackgroundTasks(isolate_);
235+
platform->DrainTasks(isolate_);
236236
}
237237

238238
env_.reset();

0 commit comments

Comments
 (0)