Skip to content

Commit

Permalink
src: ready background workers before bootstrap
Browse files Browse the repository at this point in the history
Make sure background workers are ready before proceeding with the
bootstrap or post-bootstrap execution of any code that may trigger
`process.exit()`.

Fixes: #23065
  • Loading branch information
ofrobots committed Oct 3, 2018
1 parent 5d8373a commit 3afca3a
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,30 @@ using v8::TracingController;

namespace {

static Mutex platform_workers_mutex;
static ConditionVariable platform_workers_ready;
static int pending_platform_workers;

struct PlatformWorkerData {
TaskQueue<Task>* task_queue;
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(platform_workers_mutex);
pending_platform_workers--;
platform_workers_ready.Signal(lock);
}

while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop()) {
task->Run();
pending_worker_tasks->NotifyOfCompletion();
Expand Down Expand Up @@ -148,17 +168,30 @@ 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_, 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) {
Expand Down

0 comments on commit 3afca3a

Please sign in to comment.