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

178 lines
4.4 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2021-06-27 21:25:29 +00:00
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)
#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
if (ThrdCfg::gPreferNtCondMutexSpinTryLock)
{
2023-09-09 18:46:08 +00:00
return this->TryLockHeavy();
}
else
{
2023-09-09 18:46:08 +00:00
return this->TryLockNoSpin();
}
#endif
}
bool Win32ConditionMutex::TryLockHeavy()
{
return DoTryIf([=]()
{
2023-09-09 18:46:08 +00:00
return this->TryLockNoSpin();
});
}
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->TryLockHeavy())
{
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;
AuAtomicClearU8Lock(&uValueRef);
2023-08-23 15:38:22 +00:00
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)
{
InternalLTSWakeOne((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()
{
2023-08-21 16:34:24 +00:00
return _new ConditionMutexImpl();
2021-06-27 21:25:29 +00:00
}
2022-11-17 07:46:07 +00:00
AUKN_SYM void ConditionMutexRelease(IConditionMutex *pMutex)
2021-06-27 21:25:29 +00:00
{
2023-08-21 16:34:24 +00:00
AuSafeDelete<ConditionMutexImpl *>(pMutex);
2021-06-27 21:25:29 +00:00
}
2023-08-21 16:34:24 +00:00
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, ConditionMutex, ConditionMutexImpl)
2021-06-27 21:25:29 +00:00
}
#endif