Skip to content

Commit

Permalink
src: reduce platform worker barrier lifetime
Browse files Browse the repository at this point in the history
Minor cleanup in the lifetime for the platform worker initialization
synchronization barrier.

PR-URL: nodejs#23419
Backport-PR-URL: nodejs#28844
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
ofrobots authored and BethGriggs committed Sep 20, 2019
1 parent f862276 commit 859d475
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
21 changes: 12 additions & 9 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void BackgroundRunner(void* data) {
class BackgroundTaskRunner::DelayedTaskScheduler {
public:
explicit DelayedTaskScheduler(TaskQueue<Task>* tasks)
: pending_worker_tasks_(tasks) {}
: background_tasks_(tasks) {}

std::unique_ptr<uv_thread_t> Start() {
auto start_thread = [](void* data) {
Expand Down Expand Up @@ -139,7 +139,7 @@ class BackgroundTaskRunner::DelayedTaskScheduler {
static void RunTask(uv_timer_t* timer) {
DelayedTaskScheduler* scheduler =
ContainerOf(&DelayedTaskScheduler::loop_, timer->loop);
scheduler->pending_worker_tasks_->Push(scheduler->TakeTimerTask(timer));
scheduler->background_tasks_->Push(scheduler->TakeTimerTask(timer));
}

std::unique_ptr<Task> TakeTimerTask(uv_timer_t* timer) {
Expand All @@ -153,7 +153,7 @@ class BackgroundTaskRunner::DelayedTaskScheduler {
}

uv_sem_t ready_;
TaskQueue<v8::Task>* pending_worker_tasks_;
TaskQueue<v8::Task>* background_tasks_;

TaskQueue<v8::Task> tasks_;
uv_loop_t loop_;
Expand All @@ -162,17 +162,20 @@ class BackgroundTaskRunner::DelayedTaskScheduler {
};

BackgroundTaskRunner::BackgroundTaskRunner(int thread_pool_size) {
Mutex::ScopedLock lock(platform_workers_mutex_);
pending_platform_workers_ = thread_pool_size;
Mutex platform_workers_mutex;
ConditionVariable platform_workers_ready;

Mutex::ScopedLock lock(platform_workers_mutex);
int pending_platform_workers = thread_pool_size;

delayed_task_scheduler_.reset(
new DelayedTaskScheduler(&background_tasks_));
threads_.push_back(delayed_task_scheduler_->Start());

for (int i = 0; i < thread_pool_size; i++) {
PlatformWorkerData* worker_data = new PlatformWorkerData{
&background_tasks_, &platform_workers_mutex_,
&platform_workers_ready_, &pending_platform_workers_, i
&background_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(), BackgroundRunner, worker_data) != 0)
Expand All @@ -182,8 +185,8 @@ BackgroundTaskRunner::BackgroundTaskRunner(int thread_pool_size) {

// Wait for platform workers to initialize before continuing with the
// bootstrap.
while (pending_platform_workers_ > 0) {
platform_workers_ready_.Wait(lock);
while (pending_platform_workers > 0) {
platform_workers_ready.Wait(lock);
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/node_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ class BackgroundTaskRunner : public v8::TaskRunner {
std::unique_ptr<DelayedTaskScheduler> delayed_task_scheduler_;

std::vector<std::unique_ptr<uv_thread_t>> threads_;

Mutex platform_workers_mutex_;
ConditionVariable platform_workers_ready_;
int pending_platform_workers_;
};

class NodePlatform : public MultiIsolatePlatform {
Expand Down

0 comments on commit 859d475

Please sign in to comment.