@@ -17,17 +17,21 @@ using v8::Platform;
1717using v8::Task;
1818using 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
261263void NodePlatform::RegisterIsolate (IsolateData* isolate_data, uv_loop_t * loop) {
@@ -283,16 +285,16 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
283285}
284286
285287void 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
298300void 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+
380385std::shared_ptr<PerIsolatePlatformData>
381386NodePlatform::ForIsolate (Isolate* isolate) {
382387 Mutex::ScopedLock lock (per_isolate_mutex_);
@@ -406,11 +411,6 @@ void NodePlatform::CancelPendingDelayedTasks(v8::Isolate* isolate) {
406411
407412bool 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-
414414std::shared_ptr<v8::TaskRunner>
415415NodePlatform::GetForegroundTaskRunner (Isolate* isolate) {
416416 return ForIsolate (isolate);
0 commit comments