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

374 lines
11 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.
File: AuMutex.NT.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 "AuMutex.Generic.hpp"
#include "SMTYield.hpp"
#include "../AuWakeInternal.hpp"
2021-06-27 21:25:29 +00:00
#if !defined(_AURUNTIME_GENERICMUTEX)
2022-11-17 07:46:07 +00:00
#include "AuMutex.NT.hpp"
#include "AuConditionMutex.NT.hpp"
#include <Time/Time.hpp>
#if !defined(NTSTATUS_TIMEOUT)
#define NTSTATUS_TIMEOUT 0x102
#endif
2021-06-27 21:25:29 +00:00
namespace Aurora::Threading::Primitives
{
MutexImpl::MutexImpl()
2021-06-27 21:25:29 +00:00
{
if (!pWaitOnAddress)
{
#if defined(AURORA_FORCE_SRW_LOCKS)
::InitializeSRWLock(&this->atomicHolder_);
::InitializeConditionVariable(&this->wakeup_);
#endif
}
this->state_ = 0;
2021-06-27 21:25:29 +00:00
}
MutexImpl::~MutexImpl()
2021-06-27 21:25:29 +00:00
{
}
bool MutexImpl::HasOSHandle(AuMach &mach)
2021-06-27 21:25:29 +00:00
{
return false;
}
bool MutexImpl::TryLockHeavy()
2021-06-27 21:25:29 +00:00
{
return DoTryIf([=]()
{
return this->TryLockNoSpin();
});
2021-06-27 21:25:29 +00:00
}
bool MutexImpl::TryLock()
{
if (gRuntimeConfig.threadingConfig.bPreferNtMutexSpinTryLock)
{
return TryLockHeavy();
}
else
{
return TryLockNoSpin();
}
}
bool MutexImpl::TryLockNoSpin()
{
return AuAtomicTestAndSet(&this->state_, 0) == 0;
}
bool MutexImpl::HasLockImplementation()
2021-06-27 21:25:29 +00:00
{
return true;
}
void MutexImpl::SlowLock()
2021-06-27 21:25:29 +00:00
{
auto status = LockNS(0);
2021-06-27 21:25:29 +00:00
SysAssert(status, "Couldn't lock Mutex object");
}
bool MutexImpl::LockMS(AuUInt64 uTimeout)
{
2023-07-10 17:51:28 +00:00
if (this->TryLockNoSpin())
{
return true;
}
2023-07-10 17:51:28 +00:00
return this->LockNS(AuMSToNS<AuUInt64>(uTimeout));
}
bool MutexImpl::LockNS(AuUInt64 uTimeout)
2021-06-27 21:25:29 +00:00
{
bool returnValue = false;
if (this->TryLockHeavy())
{
return true;
}
2021-06-27 21:25:29 +00:00
AuUInt64 uEndTime = uTimeout ? Time::SteadyClockNS() + uTimeout : 0;
int iYieldCounter {};
2021-06-27 21:25:29 +00:00
if (gUseNativeWaitMutex)
2021-06-27 21:25:29 +00:00
{
while (!this->TryLockNoSpin())
2021-06-27 21:25:29 +00:00
{
auto &uValueRef = this->state_;
auto uValue = uValueRef | 1;
auto uNextValue = uValue + kFutexBitWait;
if (AuAtomicCompareExchange(&uValueRef, uNextValue, uValue) == uValue)
2021-06-27 21:25:29 +00:00
{
if (!InternalLTSWaitOnAddressHighRes((void *)&uValueRef, &uNextValue, sizeof(uNextValue), uEndTime))
{
return false;
}
2021-06-27 21:25:29 +00:00
}
}
return true;
}
else
{
#if defined(AURORA_FORCE_SRW_LOCKS)
::AcquireSRWLockShared(&this->atomicHolder_);
BOOL status = false;
while (!this->TryLockNoSpin())
2021-06-27 21:25:29 +00:00
{
AuUInt32 uTimeoutMS = INFINITE;
if (uTimeout != 0)
{
auto uStartTime = Time::SteadyClockNS();
if (uStartTime >= uEndTime)
{
goto exitWin32;
}
uTimeoutMS = AuNSToMS<AuInt64>(uEndTime - uStartTime);
}
if (!uTimeoutMS)
{
::ReleaseSRWLockShared(&this->atomicHolder_);
SMPPause();
AuThreading::ContextYield();
::AcquireSRWLockShared(&this->atomicHolder_);
}
else
{
(void)SleepConditionVariableSRW(&this->wakeup_, &this->atomicHolder_, uTimeoutMS, CONDITION_VARIABLE_LOCKMODE_SHARED);
}
2021-06-27 21:25:29 +00:00
}
returnValue = true;
2021-06-27 21:25:29 +00:00
exitWin32:
::ReleaseSRWLockShared(&this->atomicHolder_);
#else
if (!uTimeout)
{
while (!this->TryLockNoSpin())
{
auto &uValueRef = this->state_;
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);
}
}
return true;
}
else
{
auto &uValueRef = this->state_;
returnValue = true;
auto uEndTimeSteady = AuTime::SteadyClockNS() + uTimeout;
auto uEndTimeWall = AuTime::CurrentClockNS() + uTimeout;
bool bFailed {};
while (bFailed || (!this->TryLockNoSpin()))
{
auto uValue = uValueRef | 1;
if (!bFailed &&
AuTime::SteadyClockNS() >= uEndTimeSteady)
{
returnValue = this->TryLock();
break;
}
2023-04-01 08:53:00 +00:00
if (bFailed || AuAtomicCompareExchange(&uValueRef, uValue + kFutexBitWait, uValue) == uValue)
{
auto uTargetTimeNt = AuTime::ConvertTimestampNs(uEndTimeWall);
LARGE_INTEGER word;
word.QuadPart = uTargetTimeNt;
auto uStatus = pNtWaitForKeyedEvent(gKeyedEventHandle, (void *)&this->state_, 0, &word);
if (uStatus == NTSTATUS_TIMEOUT)
{
2023-04-01 08:53:00 +00:00
auto uWWaiters = this->state_ & ~kFutexBitWake;
if (uWWaiters >= kFutexBitWait && AuAtomicCompareExchange(&this->state_, uWWaiters - kFutexBitWait, uWWaiters) == uWWaiters)
{
continue;
}
else
{
bFailed = true;
continue;
}
}
else
{
2023-04-01 08:53:00 +00:00
AuAtomicSub(&uValueRef, kFutexBitWake);
SysAssertDbg(uStatus == 0);
}
}
bFailed = false;
}
}
#endif
return returnValue;
}
2021-06-27 21:25:29 +00:00
}
void MutexImpl::Unlock()
2021-06-27 21:25:29 +00:00
{
2023-07-10 17:51:28 +00:00
#if defined(AURORA_FORCE_SRW_LOCKS)
if (gUseNativeWaitMutex)
{
auto &uValueRef = this->state_;
*(AuUInt8 *)&uValueRef = 0;
while (true)
{
auto uValue = uValueRef;
if (uValue < kFutexBitWait)
{
return;
}
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait, uValue) == uValue)
{
pWakeByAddressSingle((void *)&this->state_);
return;
}
SMPPause();
}
return;
}
2023-07-10 17:51:28 +00:00
::AcquireSRWLockExclusive(&this->atomicHolder_);
this->state_ = 0;
::ReleaseSRWLockExclusive(&this->atomicHolder_);
::WakeAllConditionVariable(&this->wakeup_);
#else
2023-07-10 17:51:28 +00:00
auto &uValueRef = this->state_;
2023-08-23 15:38:22 +00:00
#if defined(AURORA_COMPILER_MSVC)
#if defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_X64)
// Intel 64 and IA - 32 Architectures Software Developer's Manual, Volume 3A: Section: 8.2.3.1
*(AuUInt8 *)&uValueRef = 0;
// From this point onwards, our thread could be subject to StoreLoad re-ordering
// ...but it should not matter.
// Given the memory model of x86[64], we can only really expect to be out of order during an unfenced load operation, which in this class, can only be expected under this function before the CAS.
// No other place reads.
// Re-ordering race condition 1: one thread wins an atomic bit set, that we dont catch until the CAS, resulting in: a slow implicit fence under the cas, a mm_pause stall, a compare, and a return
// alt: uValueRef reads zero, resulting in a preemptive return while no threads need to be awoken
// Re-ordering race condition 2: we unlock, multiple threads enter ::Lock(), we somehow read `uValue = uValueRef` as zero, and then the first atomic bitsetandtest winner thread signals the keyed mutex
// I fail to see how:
// *byte = 0; | |
// | interlocked atomicbitset | interlocked atomicbitset fail
// | [logic] | interlocked atomic set kFutexBitWait
// | *byte = 0; | yield
// | auto uValue =[acquire]= uValueRef
// ...would result in the second thread missing the third threads atomic set kFutexBitWait (cst (?) on the account of 8.2.3.1, 8.2.3.8, etc)
// Also note: mfence is far too expensive and the _ReadWriteBarrier() intrinsics do absolutely nothing
_ReadWriteBarrier();
#else
InterlockedAndRelease((volatile LONG *)&uValueRef, ~0xFF);
#endif
2023-07-10 17:51:28 +00:00
#else
2023-08-23 15:38:22 +00:00
__sync_lock_release((AuUInt8 *)&uValueRef); // __atomic_store_explicit((AuUInt8 *)&uValueRef, 0, __ATOMIC_RELEASE)
2023-07-10 17:51:28 +00:00
#endif
while (true)
{
auto uValue = uValueRef;
2023-07-10 17:51:28 +00:00
if (uValue < kFutexBitWait)
{
2023-07-10 17:51:28 +00:00
return;
}
2023-07-10 17:51:28 +00:00
// StoreLoad race-conditions here cannot result in a return
// We should see StoreLoads of at least our *pByte = 0
// or we should at least see the CST of kFutexBitWait being applied
if (uValue & 1)
{
return;
}
2023-07-10 17:51:28 +00:00
if (gUseNativeWaitMutex)
{
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait, uValue) == uValue)
{
2023-07-10 17:51:28 +00:00
pWakeByAddressSingle((void *)&this->state_);
return;
}
2023-07-10 17:51:28 +00:00
}
else
{
2023-04-01 08:53:00 +00:00
if (uValue & kFutexBitWake)
{
// StoreLoad paranoia
if (AuAtomicCompareExchange(&uValueRef, uValue, uValue) == uValue)
{
return;
}
else
{
SMPPause();
continue;
}
}
2023-04-01 08:53:00 +00:00
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait + kFutexBitWake, uValue) == uValue)
{
pNtReleaseKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
return;
}
}
2023-07-10 17:51:28 +00:00
SMPPause();
}
2023-07-10 17:51:28 +00:00
#endif
2021-06-27 21:25:29 +00:00
}
AUKN_SYM IHyperWaitable *MutexNew()
2021-06-27 21:25:29 +00:00
{
return _new MutexImpl();
2021-06-27 21:25:29 +00:00
}
AUKN_SYM void MutexRelease(IHyperWaitable *pMutex)
2021-06-27 21:25:29 +00:00
{
AuSafeDelete<MutexImpl *>(pMutex);
2021-06-27 21:25:29 +00:00
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, Mutex, MutexImpl)
2021-06-27 21:25:29 +00:00
}
#endif