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

135 lines
3.3 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"
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 (gKeyedEventHandle == INVALID_HANDLE_VALUE)
{
if (!pNtCreateKeyedEvent)
{
InitNTAddresses();
}
2023-03-15 00:48:37 +00:00
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 !AuAtomicTestAndSet(&this->lock_.uWaitCount, 0);
});
#endif
}
2021-06-27 21:25:29 +00:00
void Win32ConditionMutex::Lock()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
::AcquireSRWLockExclusive(&this->lock_);
#else
while (!TryLock())
{
auto &uValueRef = this->lock_.uWaitCount;
auto uValue = uValueRef | 1;
2023-04-01 08:53:00 +00:00
if (AuAtomicCompareExchange(&uValueRef, uValue + kFutexBitWait, uValue) == uValue)
{
pNtWaitForKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
2023-04-01 08:53:00 +00:00
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
auto &uValueRef = this->lock_.uWaitCount;
#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
2023-04-01 08:53:00 +00:00
AuAtomicAnd(&uValueRef, ~0xFFu);
#endif
while (true)
{
auto uOld = uValueRef;
auto uValue = uOld;
if (uValue & 1)
{
return;
}
2023-04-01 08:53:00 +00:00
if (uValue < kFutexBitWait)
{
return;
}
2023-04-01 08:53:00 +00:00
if (uValue & kFutexBitWake)
{
return;
}
2023-04-01 08:53:00 +00:00
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait + kFutexBitWake, uValue) == uValue)
{
pNtReleaseKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
return;
}
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