[*] Windows 7 optimization [?]

This commit is contained in:
Reece Wilson 2023-03-15 02:38:26 +00:00
parent 2fc4564f5d
commit e88718a48b
3 changed files with 30 additions and 34 deletions

View File

@ -194,16 +194,9 @@ namespace Aurora::Threading::Primitives
this->writersPending_--; this->writersPending_--;
return false; return false;
} }
uSecondTimeout = AuNSToMS<AuUInt64>(uSecondTimeout);
if (!uSecondTimeout)
{
this->writersPending_--;
return false;
}
} }
if (!this->condition_.WaitForSignal(uSecondTimeout)) if (!this->condition_.WaitForSignalNS(uSecondTimeout))
{ {
this->writersPending_--; this->writersPending_--;
return false; return false;

View File

@ -13,15 +13,10 @@
#if !defined(_AURUNTIME_GENERIC_SEMAPHORE) #if !defined(_AURUNTIME_GENERIC_SEMAPHORE)
namespace Aurora::Threading::Primitives namespace Aurora::Threading::Primitives
{ {
Semaphore::Semaphore(long iIntialValue) Semaphore::Semaphore(long iIntialValue) :
var(AuUnsafeRaiiToShared(&this->mutex))
{ {
this->value_ = iIntialValue; this->value_ = iIntialValue;
if (!pWaitOnAddress)
{
::InitializeSRWLock(&this->lock_);
::InitializeConditionVariable(&this->winCond_);
}
} }
Semaphore::~Semaphore() Semaphore::~Semaphore()
@ -103,38 +98,31 @@ namespace Aurora::Threading::Primitives
} }
else else
{ {
::AcquireSRWLockShared(&this->lock_); this->mutex.Lock();
while (!TryLock()) while (!TryLock())
{ {
AuUInt32 dwTimeoutMs = INFINITE; AuUInt32 dwTimeoutNs = INFINITE;
if (uTimeout != 0) if (uTimeout != 0)
{ {
uStart = Time::SteadyClockNS(); uStart = Time::SteadyClockNS();
if (uStart >= uEnd) if (uStart >= uEnd)
{ {
::ReleaseSRWLockShared(&this->lock_); this->mutex.Unlock();
return false; return false;
} }
dwTimeoutMs = AuNSToMS<AuInt64>(uEnd - uStart); dwTimeoutNs = uEnd - uStart;
} var.WaitForSignalNS(dwTimeoutNs);
if (!dwTimeoutMs)
{
::ReleaseSRWLockShared(&this->lock_);
SMPPause();
AuThreading::ContextYield();
::AcquireSRWLockShared(&this->lock_);
} }
else else
{ {
(void)SleepConditionVariableSRW(&this->winCond_, &this->lock_, dwTimeoutMs, CONDITION_VARIABLE_LOCKMODE_SHARED); var.WaitForSignalNS(0);
} }
} }
::ReleaseSRWLockShared(&this->lock_); this->mutex.Unlock();
} }
return true; return true;
@ -150,10 +138,22 @@ namespace Aurora::Threading::Primitives
{ {
if (!pWaitOnAddress) if (!pWaitOnAddress)
{ {
::AcquireSRWLockExclusive(&this->lock_); this->mutex.Lock();
AuAtomicAdd<AuInt32>(&this->value_, count); AuAtomicAdd<AuInt32>(&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 else
{ {

View File

@ -7,6 +7,9 @@
***/ ***/
#pragma once #pragma once
#include "AuConditionMutex.Generic.hpp"
#include "AuConditionVariable.NT.hpp"
namespace Aurora::Threading::Primitives namespace Aurora::Threading::Primitives
{ {
struct Semaphore : ISemaphore struct Semaphore : ISemaphore
@ -25,7 +28,7 @@ namespace Aurora::Threading::Primitives
private: private:
AuInt32 value_ {}; AuInt32 value_ {};
CONDITION_VARIABLE winCond_; ConditionMutexImpl mutex;
SRWLOCK lock_; ConditionVariableImpl var;
}; };
} }