AuroraRuntime/Source/Threading/Primitives/AuMutex.NT.cpp

289 lines
7.5 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuMutex.NT.cpp
2021-06-27 21:25:29 +00:00
Date: 2021-6-12
Author: Reece
***/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
2022-11-17 07:46:07 +00:00
#include "AuMutex.Generic.hpp"
#include "SMPYield.hpp"
2021-06-27 21:25:29 +00:00
#if !defined(_AURUNTIME_GENERICMUTEX)
2022-11-17 07:46:07 +00:00
#include "AuMutex.NT.hpp"
#include "AuConditionMutex.NT.hpp"
#include <Time/Time.hpp>
#if !defined(NTSTATUS_TIMEOUT)
#define NTSTATUS_TIMEOUT 0x102
#endif
2021-06-27 21:25:29 +00:00
namespace Aurora::Threading::Primitives
{
Mutex::Mutex()
{
if (!pWaitOnAddress)
{
#if 0
::InitializeSRWLock(&this->atomicHolder_);
::InitializeConditionVariable(&this->wakeup_);
#endif
}
this->state_ = 0;
2021-06-27 21:25:29 +00:00
}
Mutex::~Mutex()
{
}
bool Mutex::HasOSHandle(AuMach &mach)
{
return false;
}
bool Mutex::TryLock()
{
return DoTryIf([=]()
{
return ::_interlockedbittestandset((volatile LONG *)&this->state_, 0) == 0;
});
2021-06-27 21:25:29 +00:00
}
bool Mutex::HasLockImplementation()
{
return true;
}
void Mutex::Lock()
{
auto status = Lock(0);
SysAssert(status, "Couldn't lock Mutex object");
}
bool Mutex::Lock(AuUInt64 uTimeout)
{
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
}
bool Mutex::LockNS(AuUInt64 uTimeout)
2021-06-27 21:25:29 +00:00
{
bool returnValue = false;
if (TryLock())
{
return true;
}
2021-06-27 21:25:29 +00:00
AuUInt64 uStartTime = Time::SteadyClockNS();
AuUInt64 uEndTime = uStartTime + uTimeout;
2021-06-27 21:25:29 +00:00
if (pWaitOnAddress)
2021-06-27 21:25:29 +00:00
{
auto state = this->state_;
while (::_interlockedbittestandset((volatile LONG *)&this->state_, 0) != 0)
2021-06-27 21:25:29 +00:00
{
AuUInt32 uTimeoutMS = INFINITE;
if (uTimeout != 0)
2021-06-27 21:25:29 +00:00
{
uStartTime = Time::SteadyClockNS();
if (uStartTime >= uEndTime)
{
return false;
}
uTimeoutMS = AuNSToMS<AuInt64>(uEndTime - uStartTime);
}
if (!uTimeoutMS)
{
SMPPause();
AuThreading::ContextYield();
}
else
{
(void)pWaitOnAddress(&this->state_, &state, sizeof(this->state_), uTimeoutMS);
2021-06-27 21:25:29 +00:00
}
2022-02-18 11:52:52 +00:00
state = this->state_;
2021-06-27 21:25:29 +00:00
}
return true;
}
else
{
#if 0
::AcquireSRWLockShared(&this->atomicHolder_);
BOOL status = false;
while (!this->TryLock())
2021-06-27 21:25:29 +00:00
{
AuUInt32 uTimeoutMS = INFINITE;
if (uTimeout != 0)
{
uStartTime = Time::SteadyClockNS();
if (uStartTime >= uEndTime)
{
goto exitWin32;
}
uTimeoutMS = AuNSToMS<AuInt64>(uEndTime - uStartTime);
}
if (!uTimeoutMS)
{
::ReleaseSRWLockShared(&this->atomicHolder_);
SMPPause();
AuThreading::ContextYield();
::AcquireSRWLockShared(&this->atomicHolder_);
}
else
{
(void)SleepConditionVariableSRW(&this->wakeup_, &this->atomicHolder_, uTimeoutMS, CONDITION_VARIABLE_LOCKMODE_SHARED);
}
2021-06-27 21:25:29 +00:00
}
returnValue = true;
2021-06-27 21:25:29 +00:00
exitWin32:
::ReleaseSRWLockShared(&this->atomicHolder_);
#else
if (!uTimeout)
{
while (!TryLock())
{
auto &uValueRef = this->state_;
auto uValue = uValueRef | 1;
if (AuAtomicCompareExchange(&uValueRef, uValue + FAST_M_WAIT, uValue) == uValue)
{
pNtWaitForKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
AuAtomicSub(&uValueRef, FAST_M_WAKE);
}
}
return true;
}
else
{
auto &uValueRef = this->state_;
returnValue = true;
auto uEndTimeSteady = AuTime::SteadyClockNS() + uTimeout;
auto uEndTimeWall = AuTime::CurrentClockNS() + uTimeout;
while (!TryLock())
{
auto uValue = uValueRef | 1;
if (AuTime::SteadyClockNS() >= uEndTimeSteady)
{
returnValue = TryLock();
break;
}
if (AuAtomicCompareExchange(&uValueRef, uValue + FAST_M_WAIT, uValue) == uValue)
{
auto uTargetTimeNt = AuTime::ConvertTimestampNs(uEndTimeWall);
LARGE_INTEGER word;
word.QuadPart = uTargetTimeNt;
auto uStatus = pNtWaitForKeyedEvent(gKeyedEventHandle, (void *)&this->state_, 0, &word);
if (uStatus == NTSTATUS_TIMEOUT)
{
auto uWWaiters = this->state_ & ~FAST_M_WAKE;
if (uWWaiters >= FAST_M_WAIT && _InterlockedCompareExchange(&this->state_, uWWaiters - FAST_M_WAIT, uWWaiters) == uWWaiters)
{
continue;
}
}
else
{
AuAtomicSub(&uValueRef, FAST_M_WAKE);
SysAssertDbg(uStatus == 0);
}
}
}
}
#endif
return returnValue;
}
2021-06-27 21:25:29 +00:00
}
void Mutex::Unlock()
{
if (!pWaitOnAddress)
{
#if 0
::AcquireSRWLockExclusive(&this->atomicHolder_);
this->state_ = 0;
::ReleaseSRWLockExclusive(&this->atomicHolder_);
::WakeAllConditionVariable(&this->wakeup_);
#else
auto &uValueRef = this->state_;
auto uValue = uValueRef;
while (true)
{
auto uNow = uValueRef;
auto uNext = uNow & ~0xFF;
if (AuAtomicCompareExchange(&uValueRef, uNext, uNow) == uNow)
{
break;
}
}
while (true)
{
if (uValue < FAST_M_WAIT)
{
return;
}
if (uValue & 1)
{
return;
}
if (uValue & FAST_M_WAKE)
{
return;
}
if (AuAtomicCompareExchange(&uValueRef, uValue - FAST_M_WAIT + FAST_M_WAKE, uValue) == uValue)
{
pNtReleaseKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
return;
}
SMPPause();
}
#endif
}
else
{
this->state_ = 0;
pWakeByAddressSingle((void *)&this->state_);
}
2021-06-27 21:25:29 +00:00
}
AUKN_SYM IWaitable *MutexNew()
{
return _new Mutex();
}
2022-11-17 07:46:07 +00:00
AUKN_SYM void MutexRelease(IWaitable *pMutex)
2021-06-27 21:25:29 +00:00
{
2022-11-17 07:46:07 +00:00
AuSafeDelete<Mutex *>(pMutex);
2021-06-27 21:25:29 +00:00
}
}
#endif