Skip to content

Commit df12c1d

Browse files
Gabriel Charettetargos
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
1 parent 2ea393e commit df12c1d

File tree

5 files changed

+56
-58
lines changed

5 files changed

+56
-58
lines changed

src/node.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ static struct {
312312
}
313313

314314
void DrainVMTasks(Isolate* isolate) {
315-
platform_->DrainBackgroundTasks(isolate);
315+
platform_->DrainTasks(isolate);
316316
}
317317

318318
void CancelVMTasks(Isolate* isolate) {

src/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class Environment;
225225
class MultiIsolatePlatform : public v8::Platform {
226226
public:
227227
virtual ~MultiIsolatePlatform() { }
228-
virtual void DrainBackgroundTasks(v8::Isolate* isolate) = 0;
228+
virtual void DrainTasks(v8::Isolate* isolate) = 0;
229229
virtual void CancelPendingDelayedTasks(v8::Isolate* isolate) = 0;
230230

231231
// 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,50 +16,52 @@ using v8::Platform;
1616
using v8::Task;
1717
using v8::TracingController;
1818

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

29-
BackgroundTaskRunner::BackgroundTaskRunner(int thread_pool_size) {
31+
} // namespace
32+
33+
WorkerThreadsTaskRunner::WorkerThreadsTaskRunner(int thread_pool_size) {
3034
for (int i = 0; i < thread_pool_size; i++) {
3135
std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
32-
if (uv_thread_create(t.get(), BackgroundRunner, &background_tasks_) != 0)
36+
if (uv_thread_create(t.get(), WorkerThreadMain,
37+
&pending_worker_tasks_) != 0) {
3338
break;
39+
}
3440
threads_.push_back(std::move(t));
3541
}
3642
}
3743

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

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

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

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

62-
size_t BackgroundTaskRunner::NumberOfAvailableBackgroundThreads() const {
64+
int WorkerThreadsTaskRunner::NumberOfWorkerThreads() const {
6365
return threads_.size();
6466
}
6567

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

139141
void NodePlatform::RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) {
@@ -161,16 +163,16 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
161163
}
162164

163165
void NodePlatform::Shutdown() {
164-
background_task_runner_->Shutdown();
166+
worker_thread_task_runner_->Shutdown();
165167

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

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

176178
void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
@@ -202,15 +204,12 @@ void PerIsolatePlatformData::CancelPendingDelayedTasks() {
202204
scheduled_delayed_tasks_.clear();
203205
}
204206

205-
void NodePlatform::DrainBackgroundTasks(Isolate* isolate) {
207+
void NodePlatform::DrainTasks(Isolate* isolate) {
206208
std::shared_ptr<PerIsolatePlatformData> per_isolate = ForIsolate(isolate);
207209

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

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

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

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

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

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

src/node_platform.h

Lines changed: 14 additions & 16 deletions
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;
@@ -142,8 +142,6 @@ class NodePlatform : public MultiIsolatePlatform {
142142
void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
143143
void UnregisterIsolate(IsolateData* isolate_data) override;
144144

145-
std::shared_ptr<v8::TaskRunner> GetBackgroundTaskRunner(
146-
v8::Isolate* isolate) override;
147145
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
148146
v8::Isolate* isolate) override;
149147

@@ -155,7 +153,7 @@ class NodePlatform : public MultiIsolatePlatform {
155153
std::shared_ptr<PerIsolatePlatformData>> per_isolate_;
156154

157155
std::unique_ptr<v8::TracingController> tracing_controller_;
158-
std::shared_ptr<BackgroundTaskRunner> background_task_runner_;
156+
std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
159157
};
160158

161159
} // namespace node

src/node_worker.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void Worker::Run() {
155155
uv_run(&loop_, UV_RUN_DEFAULT);
156156
if (is_stopped()) break;
157157

158-
platform->DrainBackgroundTasks(isolate_);
158+
platform->DrainTasks(isolate_);
159159

160160
more = uv_loop_alive(&loop_);
161161
if (more && !is_stopped())
@@ -211,7 +211,7 @@ void Worker::Run() {
211211
// This call needs to be made while the `Environment` is still alive
212212
// because we assume that it is available for async tracking in the
213213
// NodePlatform implementation.
214-
platform->DrainBackgroundTasks(isolate_);
214+
platform->DrainTasks(isolate_);
215215
}
216216

217217
env_.reset();

0 commit comments

Comments
 (0)