diff options
Diffstat (limited to 'src/node_platform.cc')
-rw-r--r-- | src/node_platform.cc | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/node_platform.cc b/src/node_platform.cc index d96db08692..b8f1344727 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -95,7 +95,7 @@ void PerIsolatePlatformData::PostDelayedTask( } PerIsolatePlatformData::~PerIsolatePlatformData() { - FlushForegroundTasksInternal(); + while (FlushForegroundTasksInternal()) {} CancelPendingDelayedTasks(); uv_close(reinterpret_cast<uv_handle_t*>(flush_tasks_), @@ -223,7 +223,13 @@ bool PerIsolatePlatformData::FlushForegroundTasksInternal() { }); }); } - while (std::unique_ptr<Task> task = foreground_tasks_.Pop()) { + // Move all foreground tasks into a separate queue and flush that queue. + // This way tasks that are posted while flushing the queue will be run on the + // next call of FlushForegroundTasksInternal. + std::queue<std::unique_ptr<Task>> tasks = foreground_tasks_.PopAll(); + while (!tasks.empty()) { + std::unique_ptr<Task> task = std::move(tasks.front()); + tasks.pop(); did_work = true; RunForegroundTask(std::move(task)); } @@ -254,8 +260,8 @@ void NodePlatform::CallDelayedOnForegroundThread(Isolate* isolate, std::unique_ptr<Task>(task), delay_in_seconds); } -void NodePlatform::FlushForegroundTasks(v8::Isolate* isolate) { - ForIsolate(isolate)->FlushForegroundTasksInternal(); +bool NodePlatform::FlushForegroundTasks(v8::Isolate* isolate) { + return ForIsolate(isolate)->FlushForegroundTasksInternal(); } void NodePlatform::CancelPendingDelayedTasks(v8::Isolate* isolate) { @@ -348,4 +354,12 @@ void TaskQueue<T>::Stop() { tasks_available_.Broadcast(scoped_lock); } +template <class T> +std::queue<std::unique_ptr<T>> TaskQueue<T>::PopAll() { + Mutex::ScopedLock scoped_lock(lock_); + std::queue<std::unique_ptr<T>> result; + result.swap(task_queue_); + return result; +} + } // namespace node |