Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix
  • Loading branch information
cspotcode committed Jan 19, 2022
commit c806cda37f0389c37206734666df8c53f557b661
36 changes: 32 additions & 4 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,30 @@ void NodePlatform::DrainTasks(Isolate* isolate) {
std::shared_ptr<PerIsolatePlatformData> per_isolate = ForNodeIsolate(isolate);
if (!per_isolate) return;

do {
// Worker tasks aren't associated with an Isolate.
worker_thread_task_runner_->BlockingDrain();
} while (per_isolate->FlushForegroundTasksInternal());
// Note: does not execute delayed tasks
while(true) {
bool has_background_tasks = per_isolate->HasPendingBackgroundTasks();
bool did_foreground_work = per_isolate->FlushForegroundTasksInternal();

// Guaranteed no tasks remain, because background had nothing and foreground cannot possibly have posted to background
if(!has_background_tasks && !did_foreground_work) break;

// Background work is in-progress, and we're sure we did not execute the resulting tasks on the foreground.
// Wait for them to arrive
if(has_background_tasks && !did_foreground_work) {
per_isolate->WaitForNonDelayedForegroundTasks();
}

// Only unaccounted possibility is that we did_foreground_work; loop again
}
}

bool PerIsolatePlatformData::HasPendingBackgroundTasks() {
return isolate_->HasPendingBackgroundTasks();
}

void PerIsolatePlatformData::WaitForNonDelayedForegroundTasks() {
foreground_tasks_->Wait();
}

bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
Expand Down Expand Up @@ -572,6 +592,14 @@ void TaskQueue<T>::Push(std::unique_ptr<T> task) {
tasks_available_.Signal(scoped_lock);
}

template <class T>
std::unique_ptr<T> TaskQueue<T>::Wait() {
Mutex::ScopedLock scoped_lock(lock_);
while (task_queue_.empty() && !stopped_) {
tasks_available_.Wait(scoped_lock);
}
}

template <class T>
std::unique_ptr<T> TaskQueue<T>::Pop() {
Mutex::ScopedLock scoped_lock(lock_);
Expand Down
3 changes: 3 additions & 0 deletions src/node_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TaskQueue {
std::unique_ptr<T> Pop();
std::unique_ptr<T> BlockingPop();
std::queue<std::unique_ptr<T>> PopAll();
void Wait();
void NotifyOfCompletion();
void BlockingDrain();
void Stop();
Expand Down Expand Up @@ -80,6 +81,8 @@ class PerIsolatePlatformData :
// flushing.
bool FlushForegroundTasksInternal();

bool HasPendingBackgroundTasks();

const uv_loop_t* event_loop() const { return loop_; }

private:
Expand Down