From 76ac770674c4a4d5142c9f3bb1c278d3bfb9cb59 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Tue, 22 Aug 2023 09:44:54 +0100 Subject: [PATCH] [*] Update a handful of condvar cas's and account for laziness along the way --- .../Threading/Waitables/FutexCondWaitable.hpp | 19 +++++++++++-------- Source/Threading/Primitives/AuConditionEx.cpp | 6 ++++-- .../Primitives/AuConditionVariable.Linux.cpp | 6 ++++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Include/Aurora/Threading/Waitables/FutexCondWaitable.hpp b/Include/Aurora/Threading/Waitables/FutexCondWaitable.hpp index 46cb09bc..4c723f09 100644 --- a/Include/Aurora/Threading/Waitables/FutexCondWaitable.hpp +++ b/Include/Aurora/Threading/Waitables/FutexCondWaitable.hpp @@ -129,10 +129,11 @@ namespace Aurora::Threading::Waitables { AuAtomicAdd(&this->uAtomicState, uWaiters); WakeNOnAddress((const void *)&this->uAtomicState, uWaiters); - uWaitCount = 1; + uWaitCount = uWaiters; } - while (AuAtomicCompareExchange(&this->uAtomicSleeping, uWaiters - uWaitCount, uWaiters) != uWaiters) + while (uWaiters && + AuAtomicCompareExchange(&this->uAtomicSleeping, uWaiters - uWaitCount, uWaiters) != uWaiters) { uWaiters = this->uAtomicSleeping; @@ -151,18 +152,20 @@ namespace Aurora::Threading::Waitables uWaiters = this->uAtomicSleeping; if (uWaiters > 0) { - AuAtomicAdd(&this->uAtomicState, uThreads); - WakeNOnAddress((const void *)&this->uAtomicState, uThreads); - uWaitCount = 1; + auto uMin = AuMin(uWaiters, uThreads); + AuAtomicAdd(&this->uAtomicState, uMin); + WakeNOnAddress((const void *)&this->uAtomicState, uMin); + uWaitCount = uMin; } - while (AuAtomicCompareExchange(&this->uAtomicSleeping, uWaiters - uWaitCount, uWaiters) != uWaiters) + while (uWaiters && + AuAtomicCompareExchange(&this->uAtomicSleeping, uWaiters - uWaitCount, uWaiters) != uWaiters) { uWaiters = this->uAtomicSleeping; - if (uWaiters == 0) + if (uWaiters <= uWaitCount) { - return; + uWaitCount = uWaiters; } } } diff --git a/Source/Threading/Primitives/AuConditionEx.cpp b/Source/Threading/Primitives/AuConditionEx.cpp index e7b0d3b1..8812aa4b 100644 --- a/Source/Threading/Primitives/AuConditionEx.cpp +++ b/Source/Threading/Primitives/AuConditionEx.cpp @@ -124,7 +124,8 @@ namespace Aurora::Threading::Primitives uWaitCount = 1; } - while (AuAtomicCompareExchange(&this->uWaiters_, uWaiters - uWaitCount, uWaiters) != uWaiters) + while (uWaiters && + AuAtomicCompareExchange(&this->uWaiters_, uWaiters - uWaitCount, uWaiters) != uWaiters) { uWaiters = this->uWaiters_; @@ -147,7 +148,8 @@ namespace Aurora::Threading::Primitives uWaitCount = uWaiters; } - while (AuAtomicCompareExchange(&this->uWaiters_, uWaiters - uWaitCount, uWaiters) != uWaiters) + while (uWaiters && + AuAtomicCompareExchange(&this->uWaiters_, uWaiters - uWaitCount, uWaiters) != uWaiters) { uWaiters = this->uWaiters_; diff --git a/Source/Threading/Primitives/AuConditionVariable.Linux.cpp b/Source/Threading/Primitives/AuConditionVariable.Linux.cpp index 829fa438..c5ee7260 100644 --- a/Source/Threading/Primitives/AuConditionVariable.Linux.cpp +++ b/Source/Threading/Primitives/AuConditionVariable.Linux.cpp @@ -146,7 +146,8 @@ namespace Aurora::Threading::Primitives uWaitCount = 1; } - while (AuAtomicCompareExchange(&this->uSleeping_, uWaiters - uWaitCount, uWaiters) != uWaiters) + while (uWaiters && + AuAtomicCompareExchange(&this->uSleeping_, uWaiters - uWaitCount, uWaiters) != uWaiters) { uWaiters = this->uSleeping_; @@ -170,7 +171,8 @@ namespace Aurora::Threading::Primitives uWaitCount = uWaiters; } - while (AuAtomicCompareExchange(&this->uSleeping_, uWaiters - uWaitCount, uWaiters) != uWaiters) + while (uWaiters && + AuAtomicCompareExchange(&this->uSleeping_, uWaiters - uWaitCount, uWaiters) != uWaiters) { uWaiters = this->uSleeping_;