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

183 lines
4.7 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.
2022-11-17 07:46:07 +00:00
File: AuConditionMutex.Win32.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 "AuConditionMutex.Generic.hpp"
#include "SMTYield.hpp"
#include "AuProcAddresses.NT.hpp"
#include "../AuWakeInternal.hpp"
2021-06-27 21:25:29 +00:00
#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
2021-06-27 21:25:29 +00:00
}
Win32ConditionMutex::~Win32ConditionMutex()
{
2021-06-27 21:25:29 +00:00
}
bool Win32ConditionMutex::TryLock()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
return ::TryAcquireSRWLockExclusive(&this->lock_);
#else
return DoTryIf([=]()
{
return this->TryLockNoSpin();
});
#endif
}
bool Win32ConditionMutex::TryLockNoSpin()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
return ::TryAcquireSRWLockExclusive(&this->lock_);
#else
return AuAtomicTestAndSet(&this->lock_.uWaitCount, 0) == 0;
#endif
}
2021-06-27 21:25:29 +00:00
void Win32ConditionMutex::Lock()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
::AcquireSRWLockExclusive(&this->lock_);
#else
if (this->TryLock())
{
return;
}
while (!this->TryLockNoSpin())
{
auto &uValueRef = this->lock_.uWaitCount;
auto uValue = uValueRef | 1;
2023-07-10 11:31:06 +00:00
2023-04-01 08:53:00 +00:00
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
2021-06-27 21:25:29 +00:00
}
void Win32ConditionMutex::Unlock()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
::ReleaseSRWLockExclusive(&this->lock_);
#else
2023-05-08 14:21:15 +00:00
// Mirrors: ./AuMutex.NT.cpp
// keep this codeblock in parity
// defer to the comments in that source file
2023-07-10 11:31:06 +00:00
auto &uValueRef = this->lock_.uWaitCount;
2023-07-10 11:31:06 +00:00
#if defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_X64)
*(AuUInt8 *)&uValueRef = 0;
#else
InterlockedAndRelease((volatile LONG *)&uValueRef, ~0xFF);
#endif
2023-07-10 17:51:28 +00:00
while (true)
2023-07-10 11:31:06 +00:00
{
2023-07-10 17:51:28 +00:00
auto uOld = uValueRef;
auto uValue = uOld;
if (uValue & 1)
{
2023-07-10 17:51:28 +00:00
return;
}
2023-07-10 17:51:28 +00:00
if (uValue < kFutexBitWait)
{
return;
}
2023-07-10 17:51:28 +00:00
if (gUseNativeWaitCondvar)
{
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait, uValue) == uValue)
{
2023-07-10 17:51:28 +00:00
pWakeByAddressSingle((void *)&uValueRef);
return;
}
}
2023-07-10 17:51:28 +00:00
else
{
2023-07-10 11:31:06 +00:00
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;
}
}
2023-07-10 17:51:28 +00:00
SMPPause();
}
#endif
2021-06-27 21:25:29 +00:00
}
AuUInt Win32ConditionMutex::GetOSHandle()
{
2022-11-17 07:46:07 +00:00
return reinterpret_cast<AuUInt>(&this->lock_);
2021-06-27 21:25:29 +00:00
}
AUKN_SYM IConditionMutex *ConditionMutexNew()
{
return _new Win32ConditionMutex();
}
2022-11-17 07:46:07 +00:00
AUKN_SYM void ConditionMutexRelease(IConditionMutex *pMutex)
2021-06-27 21:25:29 +00:00
{
2022-11-17 07:46:07 +00:00
AuSafeDelete<Win32ConditionMutex *>(pMutex);
2021-06-27 21:25:29 +00:00
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, ConditionMutex, Win32ConditionMutex)
2021-06-27 21:25:29 +00:00
}
#endif