From d389f9dda3af9acdd72f453a43e6eae8b233002c Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Thu, 15 Jun 2023 20:52:28 +0100 Subject: [PATCH] [*] Re-optimize the primitives for Windows 8+ on top of a Windows XP+ core --- .../Primitives/AuConditionMutex.NT.cpp | 32 ++++- .../Primitives/AuConditionVariable.NT.cpp | 126 ++++++++++++++---- Source/Threading/Primitives/AuMutex.NT.cpp | 67 ++-------- .../Threading/Primitives/AuSemaphore.NT.cpp | 60 ++------- 4 files changed, 150 insertions(+), 135 deletions(-) diff --git a/Source/Threading/Primitives/AuConditionMutex.NT.cpp b/Source/Threading/Primitives/AuConditionMutex.NT.cpp index b496417e..c66bc274 100644 --- a/Source/Threading/Primitives/AuConditionMutex.NT.cpp +++ b/Source/Threading/Primitives/AuConditionMutex.NT.cpp @@ -9,6 +9,7 @@ #include "AuConditionMutex.Generic.hpp" #include "SMTYield.hpp" #include "AuProcAddresses.NT.hpp" +#include "../AuWakeInternal.hpp" #if !defined(_AURUNTIME_GENERICCM) @@ -17,14 +18,18 @@ namespace Aurora::Threading::Primitives Win32ConditionMutex::Win32ConditionMutex() { #if !defined(AURORA_FORCE_SRW_LOCKS) + if (!pWaitOnAddress && !pNtCreateKeyedEvent) + { + InitProcAddresses(); + } + if (gKeyedEventHandle == INVALID_HANDLE_VALUE) { - if (!pNtCreateKeyedEvent) + if (!gUseNativeWaitCondvar) { - InitNTAddresses(); + SysAssert(pNtCreateKeyedEvent); + pNtCreateKeyedEvent(&gKeyedEventHandle, -1, NULL, 0); } - - pNtCreateKeyedEvent(&gKeyedEventHandle, -1, NULL, 0); } #else ::InitializeSRWLock(&this->lock_); @@ -60,7 +65,15 @@ namespace Aurora::Threading::Primitives if (AuAtomicCompareExchange(&uValueRef, uValue + kFutexBitWait, uValue) == uValue) { - pNtWaitForKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL); + if (gUseNativeWaitCondvar) + { + auto uCurrentValue = uValue + kFutexBitWait; // TODO: 1 bit? + InternalLTSWaitOnAddressHighRes((void *)&uValueRef, &uCurrentValue, sizeof(uCurrentValue), 0); + } + else + { + pNtWaitForKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL); + } AuAtomicSub(&uValueRef, kFutexBitWake); } @@ -114,7 +127,14 @@ namespace Aurora::Threading::Primitives if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait + kFutexBitWake, uValue) == uValue) { - pNtReleaseKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL); + if (gUseNativeWaitCondvar) + { + pWakeByAddressSingle((void *)&uValueRef); + } + else + { + pNtReleaseKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL); + } return; } diff --git a/Source/Threading/Primitives/AuConditionVariable.NT.cpp b/Source/Threading/Primitives/AuConditionVariable.NT.cpp index b15dead5..b231fdc9 100644 --- a/Source/Threading/Primitives/AuConditionVariable.NT.cpp +++ b/Source/Threading/Primitives/AuConditionVariable.NT.cpp @@ -9,6 +9,7 @@ #include "AuConditionVariable.Generic.hpp" #include