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