Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 59a15b1

Browse files
Gabriel Charettecodebytere
authored andcommitted
backport: 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: nodejs/node#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> (cherry-picked from 0f3c2c6)
1 parent 86c6016 commit 59a15b1

File tree

5 files changed

+57
-59
lines changed

5 files changed

+57
-59
lines changed

src/node.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ static struct {
314314
}
315315

316316
void DrainVMTasks(Isolate* isolate) {
317-
platform_->DrainBackgroundTasks(isolate);
317+
platform_->DrainTasks(isolate);
318318
}
319319

320320
void CancelVMTasks(Isolate* isolate) {

src/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
242242
// posted during flushing of the queue are postponed until the next
243243
// flushing.
244244
virtual bool FlushForegroundTasks(v8::Isolate* isolate) = 0;
245-
virtual void DrainBackgroundTasks(v8::Isolate* isolate) = 0;
245+
virtual void DrainTasks(v8::Isolate* isolate) = 0;
246246
virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0;
247247

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

src/node_platform.cc

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@ using v8::Platform;
1717
using v8::Task;
1818
using v8::TracingController;
1919

20-
static void BackgroundRunner(void* data) {
20+
namespace {
21+
22+
static void WorkerThreadMain(void* data) {
2123
TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
2224
"BackgroundTaskRunner");
23-
TaskQueue<Task> *background_tasks = static_cast<TaskQueue<Task> *>(data);
24-
while (std::unique_ptr<Task> task = background_tasks->BlockingPop()) {
25+
TaskQueue<Task>* pending_worker_tasks = static_cast<TaskQueue<Task>*>(data);
26+
while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop()) {
2527
task->Run();
26-
background_tasks->NotifyOfCompletion();
28+
pending_worker_tasks->NotifyOfCompletion();
2729
}
2830
}
2931

30-
class BackgroundTaskRunner::DelayedTaskScheduler {
32+
} // namespace
33+
34+
class WorkerThreadsTaskRunner::DelayedTaskScheduler {
3135
public:
3236
explicit DelayedTaskScheduler(TaskQueue<Task>* tasks)
3337
: pending_worker_tasks_(tasks) {}
@@ -144,44 +148,42 @@ class BackgroundTaskRunner::DelayedTaskScheduler {
144148
std::unordered_set<uv_timer_t*> timers_;
145149
};
146150

147-
BackgroundTaskRunner::BackgroundTaskRunner(int thread_pool_size) {
151+
WorkerThreadsTaskRunner::WorkerThreadsTaskRunner(int thread_pool_size) {
148152
delayed_task_scheduler_.reset(
149-
new DelayedTaskScheduler(&background_tasks_));
153+
new DelayedTaskScheduler(&pending_worker_tasks_));
150154
threads_.push_back(delayed_task_scheduler_->Start());
151155
for (int i = 0; i < thread_pool_size; i++) {
152156
std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
153-
if (uv_thread_create(t.get(), BackgroundRunner, &background_tasks_) != 0)
157+
if (uv_thread_create(t.get(), WorkerThreadMain,
158+
&pending_worker_tasks_) != 0) {
154159
break;
160+
}
155161
threads_.push_back(std::move(t));
156162
}
157163
}
158164

159-
void BackgroundTaskRunner::PostTask(std::unique_ptr<Task> task) {
160-
background_tasks_.Push(std::move(task));
161-
}
162-
163-
void BackgroundTaskRunner::PostIdleTask(std::unique_ptr<v8::IdleTask> task) {
164-
UNREACHABLE();
165+
void WorkerThreadsTaskRunner::PostTask(std::unique_ptr<Task> task) {
166+
pending_worker_tasks_.Push(std::move(task));
165167
}
166168

167-
void BackgroundTaskRunner::PostDelayedTask(std::unique_ptr<v8::Task> task,
169+
void WorkerThreadsTaskRunner::PostDelayedTask(std::unique_ptr<v8::Task> task,
168170
double delay_in_seconds) {
169171
delayed_task_scheduler_->PostDelayedTask(std::move(task), delay_in_seconds);
170172
}
171173

172-
void BackgroundTaskRunner::BlockingDrain() {
173-
background_tasks_.BlockingDrain();
174+
void WorkerThreadsTaskRunner::BlockingDrain() {
175+
pending_worker_tasks_.BlockingDrain();
174176
}
175177

176-
void BackgroundTaskRunner::Shutdown() {
177-
background_tasks_.Stop();
178+
void WorkerThreadsTaskRunner::Shutdown() {
179+
pending_worker_tasks_.Stop();
178180
delayed_task_scheduler_->Stop();
179181
for (size_t i = 0; i < threads_.size(); i++) {
180182
CHECK_EQ(0, uv_thread_join(threads_[i].get()));
181183
}
182184
}
183185

184-
size_t BackgroundTaskRunner::NumberOfAvailableBackgroundThreads() const {
186+
int WorkerThreadsTaskRunner::NumberOfWorkerThreads() const {
185187
return threads_.size();
186188
}
187189

@@ -254,8 +256,8 @@ NodePlatform::NodePlatform(int thread_pool_size,
254256
TracingController* controller = new TracingController();
255257
tracing_controller_.reset(controller);
256258
}
257-
background_task_runner_ =
258-
std::make_shared<BackgroundTaskRunner>(thread_pool_size);
259+
worker_thread_task_runner_ =
260+
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
259261
}
260262

261263
void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
@@ -283,16 +285,16 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
283285
}
284286

285287
void NodePlatform::Shutdown() {
286-
background_task_runner_->Shutdown();
288+
worker_thread_task_runner_->Shutdown();
287289

288290
{
289291
Mutex::ScopedLock lock(per_isolate_mutex_);
290292
per_isolate_.clear();
291293
}
292294
}
293295

294-
size_t NodePlatform::NumberOfAvailableBackgroundThreads() {
295-
return background_task_runner_->NumberOfAvailableBackgroundThreads();
296+
int NodePlatform::NumberOfWorkerThreads() {
297+
return worker_thread_task_runner_->NumberOfWorkerThreads();
296298
}
297299

298300
void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
@@ -324,15 +326,12 @@ void PerIsolatePlatformData::CancelPendingDelayedTasks() {
324326
scheduled_delayed_tasks_.clear();
325327
}
326328

327-
void NodePlatform::DrainBackgroundTasks(Isolate* isolate) {
329+
void NodePlatform::DrainTasks(Isolate* isolate) {
328330
std::shared_ptr<PerIsolatePlatformData> per_isolate = ForIsolate(isolate);
329331

330332
do {
331-
// Right now, there is no way to drain only background tasks associated
332-
// with a specific isolate, so this sometimes does more work than
333-
// necessary. In the long run, that functionality is probably going to
334-
// be available anyway, though.
335-
background_task_runner_->BlockingDrain();
333+
// Worker tasks aren't associated with an Isolate.
334+
worker_thread_task_runner_->BlockingDrain();
336335
} while (per_isolate->FlushForegroundTasksInternal());
337336
}
338337

@@ -372,11 +371,17 @@ bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
372371
return did_work;
373372
}
374373

375-
void NodePlatform::CallOnBackgroundThread(Task* task,
376-
ExpectedRuntime expected_runtime) {
377-
background_task_runner_->PostTask(std::unique_ptr<Task>(task));
374+
void NodePlatform::CallOnWorkerThread(std::unique_ptr<v8::Task> task) {
375+
worker_thread_task_runner_->PostTask(std::move(task));
378376
}
379377

378+
void NodePlatform::CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
379+
double delay_in_seconds) {
380+
worker_thread_task_runner_->PostDelayedTask(std::move(task),
381+
delay_in_seconds);
382+
}
383+
384+
380385
std::shared_ptr<PerIsolatePlatformData>
381386
NodePlatform::ForIsolate(Isolate* isolate) {
382387
Mutex::ScopedLock lock(per_isolate_mutex_);
@@ -406,11 +411,6 @@ void NodePlatform::CancelPendingDelayedTasks(v8::Isolate* isolate) {
406411

407412
bool NodePlatform::IdleTasksEnabled(Isolate* isolate) { return false; }
408413

409-
std::shared_ptr<v8::TaskRunner>
410-
NodePlatform::GetBackgroundTaskRunner(Isolate* isolate) {
411-
return background_task_runner_;
412-
}
413-
414414
std::shared_ptr<v8::TaskRunner>
415415
NodePlatform::GetForegroundTaskRunner(Isolate* isolate) {
416416
return ForIsolate(isolate);

src/node_platform.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,22 @@ class PerIsolatePlatformData :
9595
std::vector<DelayedTaskPointer> scheduled_delayed_tasks_;
9696
};
9797

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

103-
void PostTask(std::unique_ptr<v8::Task> task) override;
104-
void PostIdleTask(std::unique_ptr<v8::IdleTask> task) override;
103+
void PostTask(std::unique_ptr<v8::Task> task);
105104
void PostDelayedTask(std::unique_ptr<v8::Task> task,
106-
double delay_in_seconds) override;
107-
bool IdleTasksEnabled() override { return false; };
105+
double delay_in_seconds);
108106

109107
void BlockingDrain();
110108
void Shutdown();
111109

112-
size_t NumberOfAvailableBackgroundThreads() const;
110+
int NumberOfWorkerThreads() const;
111+
113112
private:
114-
TaskQueue<v8::Task> background_tasks_;
113+
TaskQueue<v8::Task> pending_worker_tasks_;
115114

116115
class DelayedTaskScheduler;
117116
std::unique_ptr<DelayedTaskScheduler> delayed_task_scheduler_;
@@ -124,14 +123,15 @@ class NodePlatform : public MultiIsolatePlatform {
124123
NodePlatform(int thread_pool_size, v8::TracingController* tracing_controller);
125124
virtual ~NodePlatform() {}
126125

127-
void DrainBackgroundTasks(v8::Isolate* isolate) override;
126+
void DrainTasks(v8::Isolate* isolate) override;
128127
void CancelPendingDelayedTasks(v8::Isolate* isolate) override;
129128
void Shutdown();
130129

131130
// v8::Platform implementation.
132-
size_t NumberOfAvailableBackgroundThreads() override;
133-
void CallOnBackgroundThread(v8::Task* task,
134-
ExpectedRuntime expected_runtime) override;
131+
int NumberOfWorkerThreads() override;
132+
void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override;
133+
void CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
134+
double delay_in_seconds) override;
135135
void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override;
136136
void CallDelayedOnForegroundThread(v8::Isolate* isolate, v8::Task* task,
137137
double delay_in_seconds) override;
@@ -144,8 +144,6 @@ class NodePlatform : public MultiIsolatePlatform {
144144
void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
145145
void UnregisterIsolate(IsolateData* isolate_data) override;
146146

147-
std::shared_ptr<v8::TaskRunner> GetBackgroundTaskRunner(
148-
v8::Isolate* isolate) override;
149147
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
150148
v8::Isolate* isolate) override;
151149

@@ -157,7 +155,7 @@ class NodePlatform : public MultiIsolatePlatform {
157155
std::shared_ptr<PerIsolatePlatformData>> per_isolate_;
158156

159157
std::unique_ptr<v8::TracingController> tracing_controller_;
160-
std::shared_ptr<BackgroundTaskRunner> background_task_runner_;
158+
std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
161159
};
162160

163161
} // namespace node

src/node_worker.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void Worker::Run() {
168168
uv_run(&loop_, UV_RUN_DEFAULT);
169169
if (is_stopped()) break;
170170

171-
platform->DrainBackgroundTasks(isolate_);
171+
platform->DrainTasks(isolate_);
172172

173173
more = uv_loop_alive(&loop_);
174174
if (more && !is_stopped())
@@ -227,7 +227,7 @@ void Worker::Run() {
227227
// This call needs to be made while the `Environment` is still alive
228228
// because we assume that it is available for async tracking in the
229229
// NodePlatform implementation.
230-
platform->DrainBackgroundTasks(isolate_);
230+
platform->DrainTasks(isolate_);
231231
}
232232

233233
env_.reset();

0 commit comments

Comments
 (0)