@@ -18,10 +18,29 @@ using v8::TracingController;
18
18
19
19
namespace {
20
20
21
+ struct PlatformWorkerData {
22
+ TaskQueue<Task>* task_queue;
23
+ Mutex* platform_workers_mutex;
24
+ ConditionVariable* platform_workers_ready;
25
+ int * pending_platform_workers;
26
+ int id;
27
+ };
28
+
21
29
static void PlatformWorkerThread (void * data) {
30
+ std::unique_ptr<PlatformWorkerData>
31
+ worker_data (static_cast <PlatformWorkerData*>(data));
32
+
33
+ TaskQueue<Task>* pending_worker_tasks = worker_data->task_queue ;
22
34
TRACE_EVENT_METADATA1 (" __metadata" , " thread_name" , " name" ,
23
35
" PlatformWorkerThread" );
24
- TaskQueue<Task>* pending_worker_tasks = static_cast <TaskQueue<Task>*>(data);
36
+
37
+ // Notify the main thread that the platform worker is ready.
38
+ {
39
+ Mutex::ScopedLock lock (*worker_data->platform_workers_mutex );
40
+ (*worker_data->pending_platform_workers )--;
41
+ worker_data->platform_workers_ready ->Signal (lock);
42
+ }
43
+
25
44
while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop ()) {
26
45
task->Run ();
27
46
pending_worker_tasks->NotifyOfCompletion ();
@@ -148,17 +167,31 @@ class WorkerThreadsTaskRunner::DelayedTaskScheduler {
148
167
};
149
168
150
169
WorkerThreadsTaskRunner::WorkerThreadsTaskRunner (int thread_pool_size) {
170
+ Mutex::ScopedLock lock (platform_workers_mutex_);
171
+ pending_platform_workers_ = thread_pool_size;
172
+
151
173
delayed_task_scheduler_.reset (
152
174
new DelayedTaskScheduler (&pending_worker_tasks_));
153
175
threads_.push_back (delayed_task_scheduler_->Start ());
176
+
154
177
for (int i = 0 ; i < thread_pool_size; i++) {
178
+ PlatformWorkerData* worker_data = new PlatformWorkerData{
179
+ &pending_worker_tasks_, &platform_workers_mutex_,
180
+ &platform_workers_ready_, &pending_platform_workers_, 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