From 8b9b47ba59836f71d0a77e049831cda3f920ec0b Mon Sep 17 00:00:00 2001 From: SirLynix Date: Sat, 3 Feb 2024 14:41:10 +0100 Subject: [PATCH] Core/TaskScheduler: Fix WaitForTasks returning before all tasks are executed This could happen because a worker only counts as active when starting a job, but after acquiring one. So one possibility was that : 1. All workers except W1 and W2 are idle 2. W1 is about to notify as idle, as W2 got a task (but did not notify as active yet) 3. W1 notifies as idle, idle count == worker count -> wake main thread 4. W2 didn't run its task yet --- src/Nazara/Core/TaskScheduler.cpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/Nazara/Core/TaskScheduler.cpp b/src/Nazara/Core/TaskScheduler.cpp index 5c32068ab..ca1d3f6e1 100644 --- a/src/Nazara/Core/TaskScheduler.cpp +++ b/src/Nazara/Core/TaskScheduler.cpp @@ -130,7 +130,6 @@ namespace Nz std::shuffle(randomWorkerIndices.begin(), randomWorkerIndices.end(), gen); } - bool idle = false; do { // Get a task @@ -152,12 +151,6 @@ namespace Nz if (task) { - if (idle) - { - m_owner.NotifyWorkerActive(); - idle = false; - } - #ifdef NAZARA_WITH_TSAN // Workaround for TSan false-positive __tsan_acquire(task); @@ -173,14 +166,12 @@ namespace Nz else { // Wait for tasks if we don't have any right now - if (!idle) - { - m_owner.NotifyWorkerIdle(); - idle = true; - } + m_owner.NotifyWorkerIdle(); m_notifier.wait(false); m_notifier.clear(); + + m_owner.NotifyWorkerActive(); } } while (m_running.load(std::memory_order_relaxed));