@@ -17,17 +17,21 @@ using v8::Platform;
17
17
using v8::Task;
18
18
using v8::TracingController;
19
19
20
- static void BackgroundRunner (void * data) {
20
+ namespace {
21
+
22
+ static void WorkerThreadMain (void * data) {
21
23
TRACE_EVENT_METADATA1 (" __metadata" , " thread_name" , " name" ,
22
24
" 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 ()) {
25
27
task->Run ();
26
- background_tasks ->NotifyOfCompletion ();
28
+ pending_worker_tasks ->NotifyOfCompletion ();
27
29
}
28
30
}
29
31
30
- class BackgroundTaskRunner ::DelayedTaskScheduler {
32
+ } // namespace
33
+
34
+ class WorkerThreadsTaskRunner ::DelayedTaskScheduler {
31
35
public:
32
36
explicit DelayedTaskScheduler (TaskQueue<Task>* tasks)
33
37
: pending_worker_tasks_(tasks) {}
@@ -144,44 +148,42 @@ class BackgroundTaskRunner::DelayedTaskScheduler {
144
148
std::unordered_set<uv_timer_t *> timers_;
145
149
};
146
150
147
- BackgroundTaskRunner::BackgroundTaskRunner (int thread_pool_size) {
151
+ WorkerThreadsTaskRunner::WorkerThreadsTaskRunner (int thread_pool_size) {
148
152
delayed_task_scheduler_.reset (
149
- new DelayedTaskScheduler (&background_tasks_ ));
153
+ new DelayedTaskScheduler (&pending_worker_tasks_ ));
150
154
threads_.push_back (delayed_task_scheduler_->Start ());
151
155
for (int i = 0 ; i < thread_pool_size; i++) {
152
156
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 ) {
154
159
break ;
160
+ }
155
161
threads_.push_back (std::move (t));
156
162
}
157
163
}
158
164
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));
165
167
}
166
168
167
- void BackgroundTaskRunner ::PostDelayedTask (std::unique_ptr<v8::Task> task,
169
+ void WorkerThreadsTaskRunner ::PostDelayedTask (std::unique_ptr<v8::Task> task,
168
170
double delay_in_seconds) {
169
171
delayed_task_scheduler_->PostDelayedTask (std::move (task), delay_in_seconds);
170
172
}
171
173
172
- void BackgroundTaskRunner ::BlockingDrain () {
173
- background_tasks_ .BlockingDrain ();
174
+ void WorkerThreadsTaskRunner ::BlockingDrain () {
175
+ pending_worker_tasks_ .BlockingDrain ();
174
176
}
175
177
176
- void BackgroundTaskRunner ::Shutdown () {
177
- background_tasks_ .Stop ();
178
+ void WorkerThreadsTaskRunner ::Shutdown () {
179
+ pending_worker_tasks_ .Stop ();
178
180
delayed_task_scheduler_->Stop ();
179
181
for (size_t i = 0 ; i < threads_.size (); i++) {
180
182
CHECK_EQ (0 , uv_thread_join (threads_[i].get ()));
181
183
}
182
184
}
183
185
184
- size_t BackgroundTaskRunner::NumberOfAvailableBackgroundThreads () const {
186
+ int WorkerThreadsTaskRunner::NumberOfWorkerThreads () const {
185
187
return threads_.size ();
186
188
}
187
189
@@ -254,8 +256,8 @@ NodePlatform::NodePlatform(int thread_pool_size,
254
256
TracingController* controller = new TracingController ();
255
257
tracing_controller_.reset (controller);
256
258
}
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);
259
261
}
260
262
261
263
void NodePlatform::RegisterIsolate (IsolateData* isolate_data, uv_loop_t * loop) {
@@ -283,16 +285,16 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
283
285
}
284
286
285
287
void NodePlatform::Shutdown () {
286
- background_task_runner_ ->Shutdown ();
288
+ worker_thread_task_runner_ ->Shutdown ();
287
289
288
290
{
289
291
Mutex::ScopedLock lock (per_isolate_mutex_);
290
292
per_isolate_.clear ();
291
293
}
292
294
}
293
295
294
- size_t NodePlatform::NumberOfAvailableBackgroundThreads () {
295
- return background_task_runner_-> NumberOfAvailableBackgroundThreads ();
296
+ int NodePlatform::NumberOfWorkerThreads () {
297
+ return worker_thread_task_runner_-> NumberOfWorkerThreads ();
296
298
}
297
299
298
300
void PerIsolatePlatformData::RunForegroundTask (std::unique_ptr<Task> task) {
@@ -324,15 +326,12 @@ void PerIsolatePlatformData::CancelPendingDelayedTasks() {
324
326
scheduled_delayed_tasks_.clear ();
325
327
}
326
328
327
- void NodePlatform::DrainBackgroundTasks (Isolate* isolate) {
329
+ void NodePlatform::DrainTasks (Isolate* isolate) {
328
330
std::shared_ptr<PerIsolatePlatformData> per_isolate = ForIsolate (isolate);
329
331
330
332
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 ();
336
335
} while (per_isolate->FlushForegroundTasksInternal ());
337
336
}
338
337
@@ -372,11 +371,17 @@ bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
372
371
return did_work;
373
372
}
374
373
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));
378
376
}
379
377
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
+
380
385
std::shared_ptr<PerIsolatePlatformData>
381
386
NodePlatform::ForIsolate (Isolate* isolate) {
382
387
Mutex::ScopedLock lock (per_isolate_mutex_);
@@ -406,11 +411,6 @@ void NodePlatform::CancelPendingDelayedTasks(v8::Isolate* isolate) {
406
411
407
412
bool NodePlatform::IdleTasksEnabled (Isolate* isolate) { return false ; }
408
413
409
- std::shared_ptr<v8::TaskRunner>
410
- NodePlatform::GetBackgroundTaskRunner (Isolate* isolate) {
411
- return background_task_runner_;
412
- }
413
-
414
414
std::shared_ptr<v8::TaskRunner>
415
415
NodePlatform::GetForegroundTaskRunner (Isolate* isolate) {
416
416
return ForIsolate (isolate);
0 commit comments