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