@@ -16,10 +16,28 @@ using v8::Platform;
16
16
using v8::Task;
17
17
using v8::TracingController;
18
18
19
+ struct PlatformWorkerData {
20
+ TaskQueue<Task>* task_queue;
21
+ Mutex* platform_workers_mutex;
22
+ ConditionVariable* platform_workers_ready;
23
+ int * pending_platform_workers;
24
+ int id;
25
+ };
26
+
19
27
static void BackgroundRunner (void * data) {
28
+ std::unique_ptr<PlatformWorkerData>
29
+ worker_data (static_cast <PlatformWorkerData*>(data));
20
30
TRACE_EVENT_METADATA1 (" __metadata" , " thread_name" , " name" ,
21
31
" BackgroundTaskRunner" );
22
- TaskQueue<Task> *background_tasks = static_cast <TaskQueue<Task> *>(data);
32
+
33
+ // Notify the main thread that the platform worker is ready.
34
+ {
35
+ Mutex::ScopedLock lock (*worker_data->platform_workers_mutex );
36
+ (*worker_data->pending_platform_workers )--;
37
+ worker_data->platform_workers_ready ->Signal (lock);
38
+ }
39
+
40
+ TaskQueue<Task>* background_tasks = worker_data->task_queue ;
23
41
while (std::unique_ptr<Task> task = background_tasks->BlockingPop ()) {
24
42
task->Run ();
25
43
background_tasks->NotifyOfCompletion ();
@@ -144,15 +162,29 @@ class BackgroundTaskRunner::DelayedTaskScheduler {
144
162
};
145
163
146
164
BackgroundTaskRunner::BackgroundTaskRunner (int thread_pool_size) {
165
+ Mutex::ScopedLock lock (platform_workers_mutex_);
166
+ pending_platform_workers_ = thread_pool_size;
167
+
147
168
delayed_task_scheduler_.reset (
148
169
new DelayedTaskScheduler (&background_tasks_));
149
170
threads_.push_back (delayed_task_scheduler_->Start ());
171
+
150
172
for (int i = 0 ; i < thread_pool_size; i++) {
173
+ PlatformWorkerData* worker_data = new PlatformWorkerData{
174
+ &background_tasks_, &platform_workers_mutex_,
175
+ &platform_workers_ready_, &pending_platform_workers_, i
176
+ };
151
177
std::unique_ptr<uv_thread_t > t { new uv_thread_t () };
152
- if (uv_thread_create (t.get (), BackgroundRunner, &background_tasks_ ) != 0 )
178
+ if (uv_thread_create (t.get (), BackgroundRunner, worker_data ) != 0 )
153
179
break ;
154
180
threads_.push_back (std::move (t));
155
181
}
182
+
183
+ // Wait for platform workers to initialize before continuing with the
184
+ // bootstrap.
185
+ while (pending_platform_workers_ > 0 ) {
186
+ platform_workers_ready_.Wait (lock);
187
+ }
156
188
}
157
189
158
190
void BackgroundTaskRunner::PostTask (std::unique_ptr<Task> task) {
0 commit comments