diff options
author | Ali Ijaz Sheikh <ofrobots@google.com> | 2018-09-27 01:58:45 +0300 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2018-10-06 07:31:09 +0300 |
commit | e273abc01be9f04511e44cacea20c4f4935b970f (patch) | |
tree | 7bfbb74d06900194461cf23c6608c9721544c152 /src/node_platform.cc | |
parent | 6e43dfb6e3b17d26d927da40e1df8e2c1d07dc95 (diff) |
src: ready background workers before bootstrap
Make sure background workers are ready before proceeding with the
bootstrap or post-bootstrap execution of any code that may trigger
`process.exit()`.
Fixes: https://github.com/nodejs/node/issues/23065
PR-URL: https://github.com/nodejs/node/pull/23233
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src/node_platform.cc')
-rw-r--r-- | src/node_platform.cc | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/src/node_platform.cc b/src/node_platform.cc index 1c237159f2d..b583ce85423 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -18,10 +18,29 @@ using v8::TracingController; namespace { +struct PlatformWorkerData { + TaskQueue<Task>* task_queue; + Mutex* platform_workers_mutex; + ConditionVariable* platform_workers_ready; + int* pending_platform_workers; + int id; +}; + static void PlatformWorkerThread(void* data) { + std::unique_ptr<PlatformWorkerData> + worker_data(static_cast<PlatformWorkerData*>(data)); + + TaskQueue<Task>* pending_worker_tasks = worker_data->task_queue; TRACE_EVENT_METADATA1("__metadata", "thread_name", "name", "PlatformWorkerThread"); - TaskQueue<Task>* pending_worker_tasks = static_cast<TaskQueue<Task>*>(data); + + // Notify the main thread that the platform worker is ready. + { + Mutex::ScopedLock lock(*worker_data->platform_workers_mutex); + (*worker_data->pending_platform_workers)--; + worker_data->platform_workers_ready->Signal(lock); + } + while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop()) { task->Run(); pending_worker_tasks->NotifyOfCompletion(); @@ -148,17 +167,31 @@ class WorkerThreadsTaskRunner::DelayedTaskScheduler { }; WorkerThreadsTaskRunner::WorkerThreadsTaskRunner(int thread_pool_size) { + Mutex::ScopedLock lock(platform_workers_mutex_); + pending_platform_workers_ = thread_pool_size; + delayed_task_scheduler_.reset( new DelayedTaskScheduler(&pending_worker_tasks_)); threads_.push_back(delayed_task_scheduler_->Start()); + for (int i = 0; i < thread_pool_size; i++) { + PlatformWorkerData* worker_data = new PlatformWorkerData{ + &pending_worker_tasks_, &platform_workers_mutex_, + &platform_workers_ready_, &pending_platform_workers_, i + }; std::unique_ptr<uv_thread_t> t { new uv_thread_t() }; if (uv_thread_create(t.get(), PlatformWorkerThread, - &pending_worker_tasks_) != 0) { + worker_data) != 0) { break; } threads_.push_back(std::move(t)); } + + // Wait for platform workers to initialize before continuing with the + // bootstrap. + while (pending_platform_workers_ > 0) { + platform_workers_ready_.Wait(lock); + } } void WorkerThreadsTaskRunner::PostTask(std::unique_ptr<Task> task) { |