AuroraRuntime/Source/Threading/Primitives/AuSemaphore.NT.cpp
Reece e82ec4a343 [+] IWaitable::LockNS(...)
[+] AuThreading.WakeAllOnAddress
[+] AuThreading.WakeOnAddress
[+] AuThreading.WakeNOnAddress
[+] AuThreading.TryWaitOnAddress
[+] AuThreading.WaitOnAddress
[*] Further optimize synch primitives
[+] AuThreadPrimitives::RWRenterableLock
2023-03-12 15:27:28 +00:00

187 lines
4.5 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuSemaphore.NT.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuSemaphore.Generic.hpp"
#include "AuSemaphore.NT.hpp"
#include "SMPYield.hpp"
#if !defined(_AURUNTIME_GENERIC_SEMAPHORE)
namespace Aurora::Threading::Primitives
{
Semaphore::Semaphore(long iIntialValue)
{
this->value_ = iIntialValue;
if (!pWaitOnAddress)
{
::InitializeSRWLock(&this->lock_);
::InitializeConditionVariable(&this->winCond_);
}
}
Semaphore::~Semaphore()
{
}
bool Semaphore::HasOSHandle(AuMach &mach)
{
return false;
}
bool Semaphore::HasLockImplementation()
{
return true;
}
bool Semaphore::TryLock()
{
return DoTryIf([=]()
{
auto old = this->value_;
return (old != 0 && AuAtomicCompareExchange(&this->value_, old - 1, old) == old);
});
}
bool Semaphore::Lock(AuUInt64 uTimeout)
{
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
}
bool Semaphore::LockNS(AuUInt64 uTimeout)
{
if (this->TryLock())
{
return true;
}
AuUInt64 uStart = AuTime::SteadyClockNS();
AuUInt64 uEnd = uStart + uTimeout;
if (pWaitOnAddress)
{
auto old = this->value_;
//!tryLock (with old in a scope we can access)
while (!((old != 0) &&
(AuAtomicCompareExchange(&this->value_, old - 1, old) == old)))
{
AuUInt32 dwTimeoutMs = INFINITE;
if (uTimeout != 0)
{
uStart = Time::SteadyClockNS();
if (uStart >= uEnd)
{
return false;
}
dwTimeoutMs = AuNSToMS<AuInt64>(uEnd - uStart);
}
old = 0;
if (dwTimeoutMs == 0)
{
SMPPause();
AuThreading::ContextYield();
continue;
}
else
{
(void)pWaitOnAddress(&this->value_, &old, sizeof(this->value_), dwTimeoutMs);
}
old = this->value_;
}
return true;
}
else
{
::AcquireSRWLockShared(&this->lock_);
while (!TryLock())
{
AuUInt32 dwTimeoutMs = INFINITE;
if (uTimeout != 0)
{
uStart = Time::SteadyClockNS();
if (uStart >= uEnd)
{
::ReleaseSRWLockShared(&this->lock_);
return false;
}
dwTimeoutMs = AuNSToMS<AuInt64>(uEnd - uStart);
}
if (!dwTimeoutMs)
{
::ReleaseSRWLockShared(&this->lock_);
SMPPause();
AuThreading::ContextYield();
::AcquireSRWLockShared(&this->lock_);
}
else
{
(void)SleepConditionVariableSRW(&this->winCond_, &this->lock_, dwTimeoutMs, CONDITION_VARIABLE_LOCKMODE_SHARED);
}
}
::ReleaseSRWLockShared(&this->lock_);
}
return true;
}
void Semaphore::Lock()
{
auto status = Lock(0);
SysAssert(status, "Couldn't lock semaphore");
}
void Semaphore::Unlock(long count)
{
if (!pWaitOnAddress)
{
::AcquireSRWLockExclusive(&this->lock_);
AuAtomicAdd<AuInt32>(&this->value_, count);
::WakeAllConditionVariable(&this->winCond_);
::ReleaseSRWLockExclusive(&this->lock_);
}
else
{
AuAtomicAdd<AuInt32>(&this->value_, count);
if (count == 1)
{
pWakeByAddressSingle(&this->value_);
}
else
{
pWakeByAddressAll(&this->value_);
}
}
}
void Semaphore::Unlock()
{
return Unlock(1);
}
AUKN_SYM ISemaphore *SemaphoreNew(int iInitialCount)
{
return _new Semaphore(iInitialCount);
}
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)
{
AuSafeDelete<Semaphore *>(pSemaphore);
}
}
#endif