@@ -16,48 +16,50 @@ using v8::Platform;
1616using v8::Task;
1717using v8::TracingController;
1818
19- static void BackgroundRunner (void * data) {
20- TaskQueue<Task>* background_tasks = static_cast <TaskQueue<Task>*>(data);
21- while (std::unique_ptr<Task> task = background_tasks->BlockingPop ()) {
19+ namespace {
20+
21+ static void WorkerThreadMain (void * data) {
22+ TaskQueue<Task>* pending_worker_tasks = static_cast <TaskQueue<Task>*>(data);
23+ while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop ()) {
2224 task->Run ();
23- background_tasks ->NotifyOfCompletion ();
25+ pending_worker_tasks ->NotifyOfCompletion ();
2426 }
2527}
2628
27- BackgroundTaskRunner::BackgroundTaskRunner (int thread_pool_size) {
29+ } // namespace
30+
31+ WorkerThreadsTaskRunner::WorkerThreadsTaskRunner (int thread_pool_size) {
2832 for (int i = 0 ; i < thread_pool_size; i++) {
2933 std::unique_ptr<uv_thread_t > t { new uv_thread_t () };
30- if (uv_thread_create (t.get (), BackgroundRunner, &background_tasks_) != 0 )
34+ if (uv_thread_create (t.get (), WorkerThreadMain,
35+ &pending_worker_tasks_) != 0 ) {
3136 break ;
37+ }
3238 threads_.push_back (std::move (t));
3339 }
3440}
3541
36- void BackgroundTaskRunner::PostTask (std::unique_ptr<Task> task) {
37- background_tasks_.Push (std::move (task));
38- }
39-
40- void BackgroundTaskRunner::PostIdleTask (std::unique_ptr<v8::IdleTask> task) {
41- UNREACHABLE ();
42+ void WorkerThreadsTaskRunner::PostTask (std::unique_ptr<Task> task) {
43+ pending_worker_tasks_.Push (std::move (task));
4244}
4345
44- void BackgroundTaskRunner ::PostDelayedTask (std::unique_ptr<v8::Task> task,
45- double delay_in_seconds) {
46+ void WorkerThreadsTaskRunner ::PostDelayedTask (std::unique_ptr<v8::Task> task,
47+ double delay_in_seconds) {
4648 UNREACHABLE ();
4749}
4850
49- void BackgroundTaskRunner ::BlockingDrain () {
50- background_tasks_ .BlockingDrain ();
51+ void WorkerThreadsTaskRunner ::BlockingDrain () {
52+ pending_worker_tasks_ .BlockingDrain ();
5153}
5254
53- void BackgroundTaskRunner ::Shutdown () {
54- background_tasks_ .Stop ();
55+ void WorkerThreadsTaskRunner ::Shutdown () {
56+ pending_worker_tasks_ .Stop ();
5557 for (size_t i = 0 ; i < threads_.size (); i++) {
5658 CHECK_EQ (0 , uv_thread_join (threads_[i].get ()));
5759 }
5860}
5961
60- size_t BackgroundTaskRunner::NumberOfAvailableBackgroundThreads () const {
62+ int WorkerThreadsTaskRunner::NumberOfWorkerThreads () const {
6163 return threads_.size ();
6264}
6365
@@ -120,8 +122,8 @@ NodePlatform::NodePlatform(int thread_pool_size,
120122 TracingController* controller = new TracingController ();
121123 tracing_controller_.reset (controller);
122124 }
123- background_task_runner_ =
124- std::make_shared<BackgroundTaskRunner >(thread_pool_size);
125+ worker_thread_task_runner_ =
126+ std::make_shared<WorkerThreadsTaskRunner >(thread_pool_size);
125127}
126128
127129void NodePlatform::RegisterIsolate (IsolateData* isolate_data, uv_loop_t * loop) {
@@ -147,16 +149,16 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
147149}
148150
149151void NodePlatform::Shutdown () {
150- background_task_runner_ ->Shutdown ();
152+ worker_thread_task_runner_ ->Shutdown ();
151153
152154 {
153155 Mutex::ScopedLock lock (per_isolate_mutex_);
154156 per_isolate_.clear ();
155157 }
156158}
157159
158- size_t NodePlatform::NumberOfAvailableBackgroundThreads () {
159- return background_task_runner_-> NumberOfAvailableBackgroundThreads ();
160+ int NodePlatform::NumberOfWorkerThreads () {
161+ return worker_thread_task_runner_-> NumberOfWorkerThreads ();
160162}
161163
162164void PerIsolatePlatformData::RunForegroundTask (std::unique_ptr<Task> task) {
@@ -188,15 +190,12 @@ void PerIsolatePlatformData::CancelPendingDelayedTasks() {
188190 scheduled_delayed_tasks_.clear ();
189191}
190192
191- void NodePlatform::DrainBackgroundTasks (Isolate* isolate) {
193+ void NodePlatform::DrainTasks (Isolate* isolate) {
192194 std::shared_ptr<PerIsolatePlatformData> per_isolate = ForIsolate (isolate);
193195
194196 do {
195- // Right now, there is no way to drain only background tasks associated
196- // with a specific isolate, so this sometimes does more work than
197- // necessary. In the long run, that functionality is probably going to
198- // be available anyway, though.
199- background_task_runner_->BlockingDrain ();
197+ // Worker tasks aren't associated with an Isolate.
198+ worker_thread_task_runner_->BlockingDrain ();
200199 } while (per_isolate->FlushForegroundTasksInternal ());
201200}
202201
@@ -230,11 +229,17 @@ bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
230229 return did_work;
231230}
232231
233- void NodePlatform::CallOnBackgroundThread (Task* task,
234- ExpectedRuntime expected_runtime) {
235- background_task_runner_->PostTask (std::unique_ptr<Task>(task));
232+ void NodePlatform::CallOnWorkerThread (std::unique_ptr<v8::Task> task) {
233+ worker_thread_task_runner_->PostTask (std::move (task));
236234}
237235
236+ void NodePlatform::CallDelayedOnWorkerThread (std::unique_ptr<v8::Task> task,
237+ double delay_in_seconds) {
238+ worker_thread_task_runner_->PostDelayedTask (std::move (task),
239+ delay_in_seconds);
240+ }
241+
242+
238243std::shared_ptr<PerIsolatePlatformData>
239244NodePlatform::ForIsolate (Isolate* isolate) {
240245 Mutex::ScopedLock lock (per_isolate_mutex_);
@@ -264,11 +269,6 @@ void NodePlatform::CancelPendingDelayedTasks(v8::Isolate* isolate) {
264269
265270bool NodePlatform::IdleTasksEnabled (Isolate* isolate) { return false ; }
266271
267- std::shared_ptr<v8::TaskRunner>
268- NodePlatform::GetBackgroundTaskRunner (Isolate* isolate) {
269- return background_task_runner_;
270- }
271-
272272std::shared_ptr<v8::TaskRunner>
273273NodePlatform::GetForegroundTaskRunner (Isolate* isolate) {
274274 return ForIsolate (isolate);
0 commit comments