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

199 lines
5.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuConditionMutex.Win32.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuConditionMutex.Generic.hpp"
#include "SMTYield.hpp"
#include "AuProcAddresses.NT.hpp"
#include "../AuWakeInternal.hpp"
#if !defined(_AURUNTIME_GENERICCM)
namespace Aurora::Threading::Primitives
{
Win32ConditionMutex::Win32ConditionMutex()
{
#if !defined(AURORA_FORCE_SRW_LOCKS)
if (!pWaitOnAddress && !pNtCreateKeyedEvent)
{
InitProcAddresses();
}
if (gKeyedEventHandle == INVALID_HANDLE_VALUE)
{
if (!gUseNativeWaitCondvar)
{
SysAssert(pNtCreateKeyedEvent);
pNtCreateKeyedEvent(&gKeyedEventHandle, -1, NULL, 0);
}
}
#else
::InitializeSRWLock(&this->lock_);
#endif
}
Win32ConditionMutex::~Win32ConditionMutex()
{
}
bool Win32ConditionMutex::TryLock()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
return ::TryAcquireSRWLockExclusive(&this->lock_);
#else
if (gRuntimeConfig.threadingConfig.bPreferNtCondMutexSpinTryLock)
{
return TryLockHeavy();
}
else
{
return TryLockNoSpin();
}
#endif
}
bool Win32ConditionMutex::TryLockHeavy()
{
return DoTryIf([=]()
{
return TryLockNoSpin();
});
}
bool Win32ConditionMutex::TryLockNoSpin()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
return ::TryAcquireSRWLockExclusive(&this->lock_);
#else
return AuAtomicTestAndSet(&this->lock_.uWaitCount, 0) == 0;
#endif
}
void Win32ConditionMutex::Lock()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
::AcquireSRWLockExclusive(&this->lock_);
#else
if (this->TryLockHeavy())
{
return;
}
while (!this->TryLockNoSpin())
{
auto &uValueRef = this->lock_.uWaitCount;
auto uValue = uValueRef | 1;
if (AuAtomicCompareExchange(&uValueRef, uValue + kFutexBitWait, uValue) == uValue)
{
if (gUseNativeWaitCondvar)
{
auto uCurrentValue = uValue + kFutexBitWait;
InternalLTSWaitOnAddressHighRes((void *)&uValueRef, &uCurrentValue, sizeof(uCurrentValue), 0);
}
else
{
pNtWaitForKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
AuAtomicSub(&uValueRef, kFutexBitWake);
}
}
}
#endif
}
void Win32ConditionMutex::Unlock()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
::ReleaseSRWLockExclusive(&this->lock_);
#else
// Mirrors: ./AuMutex.NT.cpp
// keep this codeblock in parity
// defer to the comments in that source file
auto &uValueRef = this->lock_.uWaitCount;
#if defined(AURORA_COMPILER_MSVC)
#if defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_X64)
*(AuUInt8 *)&uValueRef = 0;
#else
InterlockedAndRelease((volatile LONG *)&uValueRef, ~0xFF);
#endif
#else
__sync_lock_release((AuUInt8 *)&uValueRef);
#endif
while (true)
{
auto uOld = uValueRef;
auto uValue = uOld;
if (uValue & 1)
{
return;
}
if (uValue < kFutexBitWait)
{
return;
}
if (gUseNativeWaitCondvar)
{
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait, uValue) == uValue)
{
pWakeByAddressSingle((void *)&uValueRef);
return;
}
}
else
{
if (uValue & kFutexBitWake)
{
if (AuAtomicCompareExchange(&uValueRef, uValue, uValue) == uValue)
{
return;
}
else
{
SMPPause();
continue;
}
}
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait + kFutexBitWake, uValue) == uValue)
{
pNtReleaseKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
return;
}
}
SMPPause();
}
#endif
}
AuUInt Win32ConditionMutex::GetOSHandle()
{
return reinterpret_cast<AuUInt>(&this->lock_);
}
AUKN_SYM IConditionMutex *ConditionMutexNew()
{
return _new ConditionMutexImpl();
}
AUKN_SYM void ConditionMutexRelease(IConditionMutex *pMutex)
{
AuSafeDelete<ConditionMutexImpl *>(pMutex);
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, ConditionMutex, ConditionMutexImpl)
}
#endif