2021-06-27 21:25:29 +00:00
|
|
|
/***
|
2024-01-29 14:09:59 +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"
|
2023-03-21 03:18:09 +00:00
|
|
|
#include "SMTYield.hpp"
|
2023-03-15 00:35:29 +00:00
|
|
|
#include "AuProcAddresses.NT.hpp"
|
2023-06-15 19:52:28 +00:00
|
|
|
#include "../AuWakeInternal.hpp"
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
#if !defined(_AURUNTIME_GENERICCM)
|
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
|
|
|
Win32ConditionMutex::Win32ConditionMutex()
|
|
|
|
{
|
2023-03-16 18:25:23 +00:00
|
|
|
#if !defined(AURORA_FORCE_SRW_LOCKS)
|
|
|
|
#else
|
|
|
|
::InitializeSRWLock(&this->lock_);
|
|
|
|
#endif
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Win32ConditionMutex::~Win32ConditionMutex()
|
|
|
|
{
|
2023-03-21 03:18:09 +00:00
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 13:26:17 +00:00
|
|
|
bool Win32ConditionMutex::TryLock()
|
|
|
|
{
|
2023-03-16 18:25:23 +00:00
|
|
|
#if defined(AURORA_FORCE_SRW_LOCKS)
|
|
|
|
return ::TryAcquireSRWLockExclusive(&this->lock_);
|
|
|
|
#else
|
2023-09-09 16:37:14 +00:00
|
|
|
if (ThrdCfg::gPreferNtCondMutexSpinTryLock)
|
2023-08-19 17:14:28 +00:00
|
|
|
{
|
2023-09-09 18:46:08 +00:00
|
|
|
return this->TryLockHeavy();
|
2023-08-19 17:14:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-09 18:46:08 +00:00
|
|
|
return this->TryLockNoSpin();
|
2023-08-19 17:14:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Win32ConditionMutex::TryLockHeavy()
|
|
|
|
{
|
2023-03-15 00:35:29 +00:00
|
|
|
return DoTryIf([=]()
|
|
|
|
{
|
2023-09-09 18:46:08 +00:00
|
|
|
return this->TryLockNoSpin();
|
2023-03-15 00:35:29 +00:00
|
|
|
});
|
2023-01-30 13:26:17 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 12:12:17 +00:00
|
|
|
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()
|
|
|
|
{
|
2023-03-16 18:25:23 +00:00
|
|
|
#if defined(AURORA_FORCE_SRW_LOCKS)
|
|
|
|
::AcquireSRWLockExclusive(&this->lock_);
|
|
|
|
#else
|
2023-07-10 12:12:17 +00:00
|
|
|
|
2023-08-19 17:14:28 +00:00
|
|
|
if (this->TryLockHeavy())
|
2023-07-10 12:12:17 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!this->TryLockNoSpin())
|
2023-03-15 00:35:29 +00:00
|
|
|
{
|
|
|
|
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)
|
2023-03-15 00:35:29 +00:00
|
|
|
{
|
2023-06-15 19:52:28 +00:00
|
|
|
if (gUseNativeWaitCondvar)
|
|
|
|
{
|
2023-07-09 22:33:12 +00:00
|
|
|
auto uCurrentValue = uValue + kFutexBitWait;
|
2023-06-15 19:52:28 +00:00
|
|
|
InternalLTSWaitOnAddressHighRes((void *)&uValueRef, &uCurrentValue, sizeof(uCurrentValue), 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pNtWaitForKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
|
2023-07-09 22:33:12 +00:00
|
|
|
AuAtomicSub(&uValueRef, kFutexBitWake);
|
2023-06-15 19:52:28 +00:00
|
|
|
}
|
2023-03-15 00:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-16 18:25:23 +00:00
|
|
|
#endif
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Win32ConditionMutex::Unlock()
|
|
|
|
{
|
2023-03-16 18:25:23 +00:00
|
|
|
#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-09 22:33:12 +00:00
|
|
|
|
2023-07-10 11:31:06 +00:00
|
|
|
auto &uValueRef = this->lock_.uWaitCount;
|
2023-07-09 22:33:12 +00:00
|
|
|
|
2023-08-23 19:20:12 +00:00
|
|
|
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-09 22:33:12 +00:00
|
|
|
{
|
2023-07-10 17:51:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-07-09 22:33:12 +00:00
|
|
|
|
2023-07-10 17:51:28 +00:00
|
|
|
if (uValue < kFutexBitWait)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-07-09 22:33:12 +00:00
|
|
|
|
2023-07-10 17:51:28 +00:00
|
|
|
if (gUseNativeWaitCondvar)
|
|
|
|
{
|
2023-07-09 22:33:12 +00:00
|
|
|
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait, uValue) == uValue)
|
|
|
|
{
|
2023-09-09 11:29:43 +00:00
|
|
|
InternalLTSWakeOne((void *)&uValueRef);
|
2023-07-09 22:33:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-07-10 17:51:28 +00:00
|
|
|
else
|
2023-03-15 00:35:29 +00:00
|
|
|
{
|
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-03-15 00:35:29 +00:00
|
|
|
}
|
2023-07-10 17:51:28 +00:00
|
|
|
|
|
|
|
SMPPause();
|
2023-03-15 00:35:29 +00:00
|
|
|
}
|
2023-03-16 18:25:23 +00:00
|
|
|
#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-03-21 03:18:09 +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
|