From e88718a48bab17f62ff9199c64cf4212550b3844 Mon Sep 17 00:00:00 2001 From: Reece Date: Wed, 15 Mar 2023 02:38:26 +0000 Subject: [PATCH] [*] Windows 7 optimization [?] --- Source/Threading/Primitives/AuRWLock.cpp | 9 +--- .../Threading/Primitives/AuSemaphore.NT.cpp | 48 +++++++++---------- .../Threading/Primitives/AuSemaphore.NT.hpp | 7 ++- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/Source/Threading/Primitives/AuRWLock.cpp b/Source/Threading/Primitives/AuRWLock.cpp index 6bc9d57f..96999d64 100644 --- a/Source/Threading/Primitives/AuRWLock.cpp +++ b/Source/Threading/Primitives/AuRWLock.cpp @@ -194,16 +194,9 @@ namespace Aurora::Threading::Primitives this->writersPending_--; return false; } - - uSecondTimeout = AuNSToMS(uSecondTimeout); - if (!uSecondTimeout) - { - this->writersPending_--; - return false; - } } - if (!this->condition_.WaitForSignal(uSecondTimeout)) + if (!this->condition_.WaitForSignalNS(uSecondTimeout)) { this->writersPending_--; return false; diff --git a/Source/Threading/Primitives/AuSemaphore.NT.cpp b/Source/Threading/Primitives/AuSemaphore.NT.cpp index cb041005..5a2d8f87 100644 --- a/Source/Threading/Primitives/AuSemaphore.NT.cpp +++ b/Source/Threading/Primitives/AuSemaphore.NT.cpp @@ -13,15 +13,10 @@ #if !defined(_AURUNTIME_GENERIC_SEMAPHORE) namespace Aurora::Threading::Primitives { - Semaphore::Semaphore(long iIntialValue) + Semaphore::Semaphore(long iIntialValue) : + var(AuUnsafeRaiiToShared(&this->mutex)) { this->value_ = iIntialValue; - - if (!pWaitOnAddress) - { - ::InitializeSRWLock(&this->lock_); - ::InitializeConditionVariable(&this->winCond_); - } } Semaphore::~Semaphore() @@ -103,38 +98,31 @@ namespace Aurora::Threading::Primitives } else { - ::AcquireSRWLockShared(&this->lock_); + this->mutex.Lock(); while (!TryLock()) { - AuUInt32 dwTimeoutMs = INFINITE; + AuUInt32 dwTimeoutNs = INFINITE; if (uTimeout != 0) { uStart = Time::SteadyClockNS(); if (uStart >= uEnd) { - ::ReleaseSRWLockShared(&this->lock_); + this->mutex.Unlock(); return false; } - dwTimeoutMs = AuNSToMS(uEnd - uStart); - } - - if (!dwTimeoutMs) - { - ::ReleaseSRWLockShared(&this->lock_); - SMPPause(); - AuThreading::ContextYield(); - ::AcquireSRWLockShared(&this->lock_); + dwTimeoutNs = uEnd - uStart; + var.WaitForSignalNS(dwTimeoutNs); } else { - (void)SleepConditionVariableSRW(&this->winCond_, &this->lock_, dwTimeoutMs, CONDITION_VARIABLE_LOCKMODE_SHARED); + var.WaitForSignalNS(0); } } - ::ReleaseSRWLockShared(&this->lock_); + this->mutex.Unlock(); } return true; @@ -150,10 +138,22 @@ namespace Aurora::Threading::Primitives { if (!pWaitOnAddress) { - ::AcquireSRWLockExclusive(&this->lock_); + this->mutex.Lock(); AuAtomicAdd(&this->value_, count); - ::WakeAllConditionVariable(&this->winCond_); - ::ReleaseSRWLockExclusive(&this->lock_); + + if (count) + { + this->var.Signal(); + } + else + { + for (AU_ITERATE_N(i, count)) + { + (void)i; + this->var.Signal(); + } + } + this->mutex.Unlock(); } else { diff --git a/Source/Threading/Primitives/AuSemaphore.NT.hpp b/Source/Threading/Primitives/AuSemaphore.NT.hpp index 8d2e2469..33c78eca 100644 --- a/Source/Threading/Primitives/AuSemaphore.NT.hpp +++ b/Source/Threading/Primitives/AuSemaphore.NT.hpp @@ -7,6 +7,9 @@ ***/ #pragma once +#include "AuConditionMutex.Generic.hpp" +#include "AuConditionVariable.NT.hpp" + namespace Aurora::Threading::Primitives { struct Semaphore : ISemaphore @@ -25,7 +28,7 @@ namespace Aurora::Threading::Primitives private: AuInt32 value_ {}; - CONDITION_VARIABLE winCond_; - SRWLOCK lock_; + ConditionMutexImpl mutex; + ConditionVariableImpl var; }; } \ No newline at end of file