305 lines
8.3 KiB
C++
305 lines
8.3 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: AuMutex.NT.cpp
|
|
Date: 2021-6-12
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "AuMutex.Generic.hpp"
|
|
#include "SMTYield.hpp"
|
|
|
|
#if !defined(_AURUNTIME_GENERICMUTEX)
|
|
#include "AuMutex.NT.hpp"
|
|
#include "AuConditionMutex.NT.hpp"
|
|
#include <Time/Time.hpp>
|
|
|
|
#if !defined(NTSTATUS_TIMEOUT)
|
|
#define NTSTATUS_TIMEOUT 0x102
|
|
#endif
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
{
|
|
MutexImpl::MutexImpl()
|
|
{
|
|
if (!pWaitOnAddress)
|
|
{
|
|
#if defined(AURORA_FORCE_SRW_LOCKS)
|
|
::InitializeSRWLock(&this->atomicHolder_);
|
|
::InitializeConditionVariable(&this->wakeup_);
|
|
#endif
|
|
}
|
|
this->state_ = 0;
|
|
}
|
|
|
|
MutexImpl::~MutexImpl()
|
|
{
|
|
|
|
}
|
|
|
|
bool MutexImpl::HasOSHandle(AuMach &mach)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool MutexImpl::TryLock()
|
|
{
|
|
return DoTryIf([=]()
|
|
{
|
|
return AuAtomicTestAndSet(&this->state_, 0) == 0;
|
|
});
|
|
}
|
|
|
|
bool MutexImpl::HasLockImplementation()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
void MutexImpl::SlowLock()
|
|
{
|
|
auto status = LockNS(0);
|
|
SysAssert(status, "Couldn't lock Mutex object");
|
|
}
|
|
|
|
bool MutexImpl::LockMS(AuUInt64 uTimeout)
|
|
{
|
|
if (AuAtomicTestAndSet(&this->state_, 0) == 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
|
|
}
|
|
|
|
bool MutexImpl::LockNS(AuUInt64 uTimeout)
|
|
{
|
|
bool returnValue = false;
|
|
|
|
if (TryLock())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
AuUInt64 uStartTime = Time::SteadyClockNS();
|
|
AuUInt64 uEndTime = uStartTime + uTimeout;
|
|
|
|
if (pWaitOnAddress &&
|
|
!gRuntimeConfig.threadingConfig.bPreferNt51XpMutexesOver81)
|
|
{
|
|
auto state = this->state_;
|
|
while (::_interlockedbittestandset((volatile LONG *)&this->state_, 0) != 0)
|
|
{
|
|
AuUInt32 uTimeoutMS = INFINITE;
|
|
|
|
if (uTimeout != 0)
|
|
{
|
|
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);
|
|
}
|
|
|
|
state = this->state_;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
#if defined(AURORA_FORCE_SRW_LOCKS)
|
|
::AcquireSRWLockShared(&this->atomicHolder_);
|
|
|
|
BOOL status = false;
|
|
while (!this->TryLock())
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
returnValue = true;
|
|
|
|
exitWin32:
|
|
::ReleaseSRWLockShared(&this->atomicHolder_);
|
|
#else
|
|
|
|
if (!uTimeout)
|
|
{
|
|
while (!TryLock())
|
|
{
|
|
auto &uValueRef = this->state_;
|
|
auto uValue = uValueRef | 1;
|
|
|
|
if (AuAtomicCompareExchange(&uValueRef, uValue + kFutexBitWait, uValue) == uValue)
|
|
{
|
|
pNtWaitForKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
|
|
AuAtomicSub(&uValueRef, kFutexBitWake);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
auto &uValueRef = this->state_;
|
|
returnValue = true;
|
|
|
|
auto uEndTimeSteady = AuTime::SteadyClockNS() + uTimeout;
|
|
auto uEndTimeWall = AuTime::CurrentClockNS() + uTimeout;
|
|
bool bFailed {};
|
|
|
|
while (bFailed || (!TryLock()))
|
|
{
|
|
auto uValue = uValueRef | 1;
|
|
|
|
if (!bFailed &&
|
|
AuTime::SteadyClockNS() >= uEndTimeSteady)
|
|
{
|
|
returnValue = TryLock();
|
|
break;
|
|
}
|
|
|
|
if (bFailed || AuAtomicCompareExchange(&uValueRef, uValue + kFutexBitWait, 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_ & ~kFutexBitWake;
|
|
if (uWWaiters >= kFutexBitWait && AuAtomicCompareExchange(&this->state_, uWWaiters - kFutexBitWait, uWWaiters) == uWWaiters)
|
|
{
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
bFailed = true;
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
AuAtomicSub(&uValueRef, kFutexBitWake);
|
|
SysAssertDbg(uStatus == 0);
|
|
}
|
|
}
|
|
|
|
bFailed = false;
|
|
}
|
|
}
|
|
|
|
#endif
|
|
return returnValue;
|
|
}
|
|
}
|
|
|
|
void MutexImpl::Unlock()
|
|
{
|
|
if (!pWaitOnAddress ||
|
|
gRuntimeConfig.threadingConfig.bPreferNt51XpMutexesOver81)
|
|
{
|
|
#if defined(AURORA_FORCE_SRW_LOCKS)
|
|
::AcquireSRWLockExclusive(&this->atomicHolder_);
|
|
this->state_ = 0;
|
|
::ReleaseSRWLockExclusive(&this->atomicHolder_);
|
|
::WakeAllConditionVariable(&this->wakeup_);
|
|
#else
|
|
|
|
auto &uValueRef = this->state_;
|
|
|
|
#if defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_X64)
|
|
// Intel 64 and IA - 32 Architectures Software Developer's Manual, Volume 3A
|
|
// Section: 9.1.1
|
|
*(AuUInt8 *)&uValueRef = 0;
|
|
#else
|
|
AuAtomicAnd(&uValueRef, ~0xFFu);
|
|
#endif
|
|
|
|
while (true)
|
|
{
|
|
auto uValue = uValueRef;
|
|
|
|
if (uValue < kFutexBitWait)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (uValue & 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (uValue & kFutexBitWake)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait + kFutexBitWake, uValue) == uValue)
|
|
{
|
|
pNtReleaseKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
|
|
return;
|
|
}
|
|
|
|
SMPPause();
|
|
}
|
|
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
this->state_ = 0;
|
|
pWakeByAddressSingle((void *)&this->state_);
|
|
}
|
|
}
|
|
|
|
AUKN_SYM IHyperWaitable *MutexNew()
|
|
{
|
|
return _new MutexImpl();
|
|
}
|
|
|
|
AUKN_SYM void MutexRelease(IHyperWaitable *pMutex)
|
|
{
|
|
AuSafeDelete<MutexImpl *>(pMutex);
|
|
}
|
|
|
|
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, Mutex, MutexImpl)
|
|
}
|
|
|
|
#endif |