@@ -16,50 +16,52 @@ using v8::Platform;
1616using v8::Task;
1717using 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
139141void NodePlatform::RegisterIsolate (IsolateData* isolate_data, uv_loop_t * loop) {
@@ -161,16 +163,16 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
161163}
162164
163165void 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
176178void 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+
258263std::shared_ptr<PerIsolatePlatformData>
259264NodePlatform::ForIsolate (Isolate* isolate) {
260265 Mutex::ScopedLock lock (per_isolate_mutex_);
@@ -284,11 +289,6 @@ void NodePlatform::CancelPendingDelayedTasks(v8::Isolate* isolate) {
284289
285290bool 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-
292292std::shared_ptr<v8::TaskRunner>
293293NodePlatform::GetForegroundTaskRunner (Isolate* isolate) {
294294 return ForIsolate (isolate);
0 commit comments