@@ -15,50 +15,52 @@ using v8::Platform;
15
15
using v8::Task;
16
16
using v8::TracingController;
17
17
18
- static void BackgroundRunner (void * data) {
18
+ namespace {
19
+
20
+ static void WorkerThreadMain (void * data) {
19
21
TRACE_EVENT_METADATA1 (" __metadata" , " thread_name" , " name" ,
20
22
" BackgroundTaskRunner" );
21
- TaskQueue<Task> *background_tasks = static_cast <TaskQueue<Task> *>(data);
22
- while (std::unique_ptr<Task> task = background_tasks ->BlockingPop ()) {
23
+ TaskQueue<Task>* pending_worker_tasks = static_cast <TaskQueue<Task>*>(data);
24
+ while (std::unique_ptr<Task> task = pending_worker_tasks ->BlockingPop ()) {
23
25
task->Run ();
24
- background_tasks ->NotifyOfCompletion ();
26
+ pending_worker_tasks ->NotifyOfCompletion ();
25
27
}
26
28
}
27
29
28
- BackgroundTaskRunner::BackgroundTaskRunner (int thread_pool_size) {
30
+ } // namespace
31
+
32
+ WorkerThreadsTaskRunner::WorkerThreadsTaskRunner (int thread_pool_size) {
29
33
for (int i = 0 ; i < thread_pool_size; i++) {
30
34
std::unique_ptr<uv_thread_t > t { new uv_thread_t () };
31
- if (uv_thread_create (t.get (), BackgroundRunner, &background_tasks_) != 0 )
35
+ if (uv_thread_create (t.get (), WorkerThreadMain,
36
+ &pending_worker_tasks_) != 0 ) {
32
37
break ;
38
+ }
33
39
threads_.push_back (std::move (t));
34
40
}
35
41
}
36
42
37
- void BackgroundTaskRunner::PostTask (std::unique_ptr<Task> task) {
38
- background_tasks_.Push (std::move (task));
39
- }
40
-
41
- void BackgroundTaskRunner::PostIdleTask (std::unique_ptr<v8::IdleTask> task) {
42
- UNREACHABLE ();
43
+ void WorkerThreadsTaskRunner::PostTask (std::unique_ptr<Task> task) {
44
+ pending_worker_tasks_.Push (std::move (task));
43
45
}
44
46
45
- void BackgroundTaskRunner ::PostDelayedTask (std::unique_ptr<v8::Task> task,
46
- double delay_in_seconds) {
47
+ void WorkerThreadsTaskRunner ::PostDelayedTask (std::unique_ptr<v8::Task> task,
48
+ double delay_in_seconds) {
47
49
UNREACHABLE ();
48
50
}
49
51
50
- void BackgroundTaskRunner ::BlockingDrain () {
51
- background_tasks_ .BlockingDrain ();
52
+ void WorkerThreadsTaskRunner ::BlockingDrain () {
53
+ pending_worker_tasks_ .BlockingDrain ();
52
54
}
53
55
54
- void BackgroundTaskRunner ::Shutdown () {
55
- background_tasks_ .Stop ();
56
+ void WorkerThreadsTaskRunner ::Shutdown () {
57
+ pending_worker_tasks_ .Stop ();
56
58
for (size_t i = 0 ; i < threads_.size (); i++) {
57
59
CHECK_EQ (0 , uv_thread_join (threads_[i].get ()));
58
60
}
59
61
}
60
62
61
- size_t BackgroundTaskRunner::NumberOfAvailableBackgroundThreads () const {
63
+ int WorkerThreadsTaskRunner::NumberOfWorkerThreads () const {
62
64
return threads_.size ();
63
65
}
64
66
@@ -131,8 +133,8 @@ NodePlatform::NodePlatform(int thread_pool_size,
131
133
TracingController* controller = new TracingController ();
132
134
tracing_controller_.reset (controller);
133
135
}
134
- background_task_runner_ =
135
- std::make_shared<BackgroundTaskRunner >(thread_pool_size);
136
+ worker_thread_task_runner_ =
137
+ std::make_shared<WorkerThreadsTaskRunner >(thread_pool_size);
136
138
}
137
139
138
140
void NodePlatform::RegisterIsolate (IsolateData* isolate_data, uv_loop_t * loop) {
@@ -160,16 +162,16 @@ void NodePlatform::UnregisterIsolate(IsolateData* isolate_data) {
160
162
}
161
163
162
164
void NodePlatform::Shutdown () {
163
- background_task_runner_ ->Shutdown ();
165
+ worker_thread_task_runner_ ->Shutdown ();
164
166
165
167
{
166
168
Mutex::ScopedLock lock (per_isolate_mutex_);
167
169
per_isolate_.clear ();
168
170
}
169
171
}
170
172
171
- size_t NodePlatform::NumberOfAvailableBackgroundThreads () {
172
- return background_task_runner_-> NumberOfAvailableBackgroundThreads ();
173
+ int NodePlatform::NumberOfWorkerThreads () {
174
+ return worker_thread_task_runner_-> NumberOfWorkerThreads ();
173
175
}
174
176
175
177
void PerIsolatePlatformData::RunForegroundTask (std::unique_ptr<Task> task) {
@@ -201,15 +203,12 @@ void PerIsolatePlatformData::CancelPendingDelayedTasks() {
201
203
scheduled_delayed_tasks_.clear ();
202
204
}
203
205
204
- void NodePlatform::DrainBackgroundTasks (Isolate* isolate) {
206
+ void NodePlatform::DrainTasks (Isolate* isolate) {
205
207
std::shared_ptr<PerIsolatePlatformData> per_isolate = ForIsolate (isolate);
206
208
207
209
do {
208
- // Right now, there is no way to drain only background tasks associated
209
- // with a specific isolate, so this sometimes does more work than
210
- // necessary. In the long run, that functionality is probably going to
211
- // be available anyway, though.
212
- background_task_runner_->BlockingDrain ();
210
+ // Worker tasks aren't associated with an Isolate.
211
+ worker_thread_task_runner_->BlockingDrain ();
213
212
} while (per_isolate->FlushForegroundTasksInternal ());
214
213
}
215
214
@@ -249,11 +248,17 @@ bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
249
248
return did_work;
250
249
}
251
250
252
- void NodePlatform::CallOnBackgroundThread (Task* task,
253
- ExpectedRuntime expected_runtime) {
254
- background_task_runner_->PostTask (std::unique_ptr<Task>(task));
251
+ void NodePlatform::CallOnWorkerThread (std::unique_ptr<v8::Task> task) {
252
+ worker_thread_task_runner_->PostTask (std::move (task));
255
253
}
256
254
255
+ void NodePlatform::CallDelayedOnWorkerThread (std::unique_ptr<v8::Task> task,
256
+ double delay_in_seconds) {
257
+ worker_thread_task_runner_->PostDelayedTask (std::move (task),
258
+ delay_in_seconds);
259
+ }
260
+
261
+
257
262
std::shared_ptr<PerIsolatePlatformData>
258
263
NodePlatform::ForIsolate (Isolate* isolate) {
259
264
Mutex::ScopedLock lock (per_isolate_mutex_);
@@ -283,11 +288,6 @@ void NodePlatform::CancelPendingDelayedTasks(v8::Isolate* isolate) {
283
288
284
289
bool NodePlatform::IdleTasksEnabled (Isolate* isolate) { return false ; }
285
290
286
- std::shared_ptr<v8::TaskRunner>
287
- NodePlatform::GetBackgroundTaskRunner (Isolate* isolate) {
288
- return background_task_runner_;
289
- }
290
-
291
291
std::shared_ptr<v8::TaskRunner>
292
292
NodePlatform::GetForegroundTaskRunner (Isolate* isolate) {
293
293
return ForIsolate (isolate);
0 commit comments