[*] Adopt new ROXTL atomics

...AuAtomicLoad + AuAtomicClearU8Lock
This commit is contained in:
Reece Wilson 2023-08-23 20:20:12 +01:00 committed by J Reece Wilson
parent 293a8ddd66
commit 3898a41198
10 changed files with 59 additions and 93 deletions

View File

@ -100,7 +100,7 @@ namespace Aurora::Threading::Waitables
AuUInt32 uWaitCount {}; AuUInt32 uWaitCount {};
AuUInt32 uWaiters {}; AuUInt32 uWaiters {};
uWaiters = this->uAtomicSleeping; uWaiters = AuAtomicLoad(&this->uAtomicSleeping);
if (uWaiters > 0) if (uWaiters > 0)
{ {
AuAtomicAdd(&this->uAtomicState, 1u); AuAtomicAdd(&this->uAtomicState, 1u);
@ -124,7 +124,7 @@ namespace Aurora::Threading::Waitables
AuUInt32 uWaitCount {}; AuUInt32 uWaitCount {};
AuUInt32 uWaiters {}; AuUInt32 uWaiters {};
uWaiters = this->uAtomicSleeping; uWaiters = AuAtomicLoad(&this->uAtomicSleeping);
if (uWaiters > 0) if (uWaiters > 0)
{ {
AuAtomicAdd(&this->uAtomicState, uWaiters); AuAtomicAdd(&this->uAtomicState, uWaiters);
@ -149,7 +149,7 @@ namespace Aurora::Threading::Waitables
AuUInt32 uWaitCount {}; AuUInt32 uWaitCount {};
AuUInt32 uWaiters {}; AuUInt32 uWaiters {};
uWaiters = this->uAtomicSleeping; uWaiters = AuAtomicLoad(&this->uAtomicSleeping);
if (uWaiters > 0) if (uWaiters > 0)
{ {
auto uMin = AuMin(uWaiters, uThreads); auto uMin = AuMin(uWaiters, uThreads);
@ -176,7 +176,7 @@ namespace Aurora::Threading::Waitables
auline bool TryLock3() auline bool TryLock3()
{ {
auto old = this->uAtomicState; auto old = AuAtomicLoad(&this->uAtomicState);
return ((old != 0 && AuAtomicCompareExchange(&this->uAtomicState, old - 1, old) == old)); return ((old != 0 && AuAtomicCompareExchange(&this->uAtomicState, old - 1, old) == old));
} }

View File

@ -78,7 +78,7 @@ namespace Aurora::Threading::Waitables
{ {
AuAtomicAdd(&this->uAtomicState, AuUInt32(uThreads)); AuAtomicAdd(&this->uAtomicState, AuUInt32(uThreads));
if (auto uSleeping = this->uAtomicSleeping) if (auto uSleeping = AuAtomicLoad(&this->uAtomicSleeping))
{ {
WakeNOnAddress((const void *)&this->uAtomicState, uSleeping); WakeNOnAddress((const void *)&this->uAtomicState, uSleeping);
} }

View File

@ -68,13 +68,9 @@ namespace Aurora::Threading::Waitables
inline void Unlock() override inline void Unlock() override
{ {
#if defined(AURORA_COMPILER_MSVC) AuAtomicClearU8Lock(&this->uAtomicState);
this->uAtomicState = 0;
#else
__sync_lock_release(&this->uAtomicState);
#endif
if (auto uSleeping = this->uAtomicSleeping) if (auto uSleeping = AuAtomicLoad(&this->uAtomicSleeping))
{ {
WakeOnAddress((const void *)&this->uAtomicState); WakeOnAddress((const void *)&this->uAtomicState);
} }

View File

@ -117,7 +117,7 @@ namespace Aurora::Threading::Primitives
AuUInt32 uWaitCount {}; AuUInt32 uWaitCount {};
AuUInt32 uWaiters {}; AuUInt32 uWaiters {};
uWaiters = this->uWaiters_; uWaiters = AuAtomicLoad(&this->uWaiters_);
if (uWaiters > 0) if (uWaiters > 0)
{ {
this->s_.Unlock(); this->s_.Unlock();
@ -141,7 +141,7 @@ namespace Aurora::Threading::Primitives
AuUInt32 uWaitCount {}; AuUInt32 uWaitCount {};
AuUInt32 uWaiters {}; AuUInt32 uWaiters {};
uWaiters = this->uWaiters_; uWaiters = AuAtomicLoad(&this->uWaiters_);
if (uWaiters > 0) if (uWaiters > 0)
{ {
this->s_.Unlock(uWaiters); this->s_.Unlock(uWaiters);

View File

@ -14,9 +14,6 @@
namespace Aurora::Threading::Primitives namespace Aurora::Threading::Primitives
{ {
#define barrier() __asm__ __volatile__("sfence": : :"memory")
#define compilerReorderBarrier() __asm__ __volatile__("": : :"memory")
LinuxConditionMutex::LinuxConditionMutex() LinuxConditionMutex::LinuxConditionMutex()
{ {
@ -188,9 +185,8 @@ namespace Aurora::Threading::Primitives
void LinuxConditionMutex::Unlock() void LinuxConditionMutex::Unlock()
{ {
__sync_lock_release(&this->uState_); AuAtomicClearU8Lock(&this->uState_);
compilerReorderBarrier(); if (AuAtomicLoad(&this->uSleeping_))
if (this->uSleeping_)
{ {
futex_wake(&this->uState_, 1); futex_wake(&this->uState_, 1);
} }

View File

@ -118,15 +118,7 @@ namespace Aurora::Threading::Primitives
auto &uValueRef = this->lock_.uWaitCount; auto &uValueRef = this->lock_.uWaitCount;
#if defined(AURORA_COMPILER_MSVC) AuAtomicClearU8Lock(&uValueRef);
#if defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_X64)
*(AuUInt8 *)&uValueRef = 0;
#else
InterlockedAndRelease((volatile LONG *)&uValueRef, ~0xFF);
#endif
#else
__sync_lock_release((AuUInt8 *)&uValueRef);
#endif
while (true) while (true)
{ {

View File

@ -138,7 +138,7 @@ namespace Aurora::Threading::Primitives
AuUInt32 uWaitCount {}; AuUInt32 uWaitCount {};
AuUInt32 uWaiters {}; AuUInt32 uWaiters {};
uWaiters = this->uSleeping_; uWaiters = AuAtomicLoad(&this->uSleeping_);
if (uWaiters > 0) if (uWaiters > 0)
{ {
AuAtomicAdd(&this->uState_, 1u); AuAtomicAdd(&this->uState_, 1u);
@ -163,7 +163,7 @@ namespace Aurora::Threading::Primitives
AuUInt32 uWaitCount {}; AuUInt32 uWaitCount {};
AuUInt32 uWaiters {}; AuUInt32 uWaiters {};
uWaiters = this->uSleeping_; uWaiters = AuAtomicLoad(&this->uSleeping_);
if (uWaiters > 0) if (uWaiters > 0)
{ {
AuAtomicAdd(&this->uState_, uWaiters); AuAtomicAdd(&this->uState_, uWaiters);

View File

@ -17,9 +17,6 @@
namespace Aurora::Threading::Primitives namespace Aurora::Threading::Primitives
{ {
#define barrier() __asm__ __volatile__("sfence": : :"memory")
#define compilerReorderBarrier() __asm__ __volatile__("": : :"memory")
MutexImpl::MutexImpl() MutexImpl::MutexImpl()
{ {
@ -153,9 +150,9 @@ namespace Aurora::Threading::Primitives
void MutexImpl::Unlock() void MutexImpl::Unlock()
{ {
__sync_lock_release(&this->state_); AuAtomicClearU8Lock(&this->state_);
compilerReorderBarrier();
if (this->dwSleeping_) if (AuAtomicLoad(&this->dwSleeping_))
{ {
futex_wake(&this->state_, 1); futex_wake(&this->state_, 1);
} }

View File

@ -273,36 +273,38 @@ namespace Aurora::Threading::Primitives
auto &uValueRef = this->state_; auto &uValueRef = this->state_;
#if defined(AURORA_COMPILER_MSVC) //#if defined(AURORA_COMPILER_MSVC)
#if defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_X64) // #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 // // Intel 64 and IA - 32 Architectures Software Developer's Manual, Volume 3A: Section: 8.2.3.1
*(AuUInt8 *)&uValueRef = 0; // *(AuUInt8 *)&uValueRef = 0;
//
// From this point onwards, our thread could be subject to StoreLoad re-ordering // // From this point onwards, our thread could be subject to StoreLoad re-ordering
// ...but it should not matter. // // ...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. // // 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. // // 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 // // 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 // // 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 // // 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: // // I fail to see how:
// *byte = 0; | | // // *byte = 0; | |
// | interlocked atomicbitset | interlocked atomicbitset fail // // | interlocked atomicbitset | interlocked atomicbitset fail
// | [logic] | interlocked atomic set kFutexBitWait // // | [logic] | interlocked atomic set kFutexBitWait
// | *byte = 0; | yield // // | *byte = 0; | yield
// | auto uValue =[acquire]= uValueRef // // | 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) // // ...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 // // Also note: mfence is far too expensive and the _ReadWriteBarrier() intrinsics do absolutely nothing
_ReadWriteBarrier(); // _ReadWriteBarrier();
#else // #else
InterlockedAndRelease((volatile LONG *)&uValueRef, ~0xFF); // InterlockedAndRelease((volatile LONG *)&uValueRef, ~0xFF);
#endif // #endif
#else //#else
__sync_lock_release((AuUInt8 *)&uValueRef); // __atomic_store_explicit((AuUInt8 *)&uValueRef, 0, __ATOMIC_RELEASE) // __sync_lock_release((AuUInt8 *)&uValueRef); // __atomic_store_explicit((AuUInt8 *)&uValueRef, 0, __ATOMIC_RELEASE)
#endif //#endif
// merged with ROXTL
AuAtomicClearU8Lock(&uValueRef);
while (true) while (true)
{ {

View File

@ -18,12 +18,6 @@ namespace Aurora::Threading::Primitives
#define ViewParent ((T *)(((char *)this) - (bIsReadView ? RWLockImpl<true>::kOffsetOfRead : RWLockImpl<true>::kOffsetOfWrite))) #define ViewParent ((T *)(((char *)this) - (bIsReadView ? RWLockImpl<true>::kOffsetOfRead : RWLockImpl<true>::kOffsetOfWrite)))
#endif #endif
#if defined(AURORA_COMPILER_MSVC)
#define RWLOCK_REORDER_BARRIER() ::MemoryBarrier();
#else
#define RWLOCK_REORDER_BARRIER()
#endif
static const auto kRWThreadWriterHardContextSwitchBias = 15; static const auto kRWThreadWriterHardContextSwitchBias = 15;
template<bool bIsReadView, typename T> template<bool bIsReadView, typename T>
@ -358,7 +352,7 @@ namespace Aurora::Threading::Primitives
} }
else else
{ {
auto uOld = this->state_; auto uOld = AuAtomicLoad(&this->state_);
if (uOld < 0) if (uOld < 0)
{ {
if (this->reentrantWriteLockHandle_ == GetThreadCookie()) if (this->reentrantWriteLockHandle_ == GetThreadCookie())
@ -399,7 +393,7 @@ namespace Aurora::Threading::Primitives
while (true) while (true)
{ {
AuInt32 iCurState; AuInt32 iCurState;
while ((iCurState = this->state_) != 0) while ((iCurState = AuAtomicLoad(&this->state_)) != 0)
{ {
AuInt64 uSecondTimeout = 0; AuInt64 uSecondTimeout = 0;
@ -409,13 +403,12 @@ namespace Aurora::Threading::Primitives
auto pSemaphore = this->GetFutexConditionWriter(); auto pSemaphore = this->GetFutexConditionWriter();
AuInt32 iCurState; AuInt32 iCurState;
while ((iCurState = this->state_) != 0) while ((iCurState = AuAtomicLoad(&this->state_)) != 0)
{ {
bool bStatusTwo {}; bool bStatusTwo {};
AuAtomicAdd(&this->writersPending_, 1); AuAtomicAdd(&this->writersPending_, 1);
static const AuUInt32 kExpect { 0 }; static const AuUInt32 kExpect { 0 };
RWLOCK_REORDER_BARRIER(); if ((iCurState = AuAtomicLoad(&this->state_)) == 0)
if ((iCurState = this->state_) == 0)
{ {
bStatus = true; bStatus = true;
bStatusTwo = true; bStatusTwo = true;
@ -655,8 +648,7 @@ namespace Aurora::Threading::Primitives
} }
else else
{ {
/* atomic read */ bElevation = AuAtomicLoad(&this->writersPending_) > 0;
bElevation = this->writersPending_ > 0;
} }
if (bElevation) if (bElevation)
@ -682,21 +674,13 @@ namespace Aurora::Threading::Primitives
if (!gUseFutexRWLock) if (!gUseFutexRWLock)
{ {
AU_LOCK_GUARD(this->mutex_); AU_LOCK_GUARD(this->mutex_);
#if defined(AURORA_COMPILER_MSVC) AuAtomicStore(&this->state_, 0);
this->state_ = 0;
#else
__sync_lock_release(&this->state_);
#endif
bElevationPending = this->writersPending_ > 0; bElevationPending = this->writersPending_ > 0;
} }
else else
{ {
bElevationPending = this->writersPending_ > 0; AuAtomicStore(&this->state_, 0);
#if defined(AURORA_COMPILER_MSVC) bElevationPending = AuAtomicLoad(&this->writersPending_) > 0;
this->state_ = 0;
#else
__sync_lock_release(&this->state_);
#endif
} }
if (bElevationPending) if (bElevationPending)
@ -818,15 +802,14 @@ namespace Aurora::Threading::Primitives
auto pSemaphore = this->GetFutexConditionWriter(); auto pSemaphore = this->GetFutexConditionWriter();
AuInt32 iCurState; AuInt32 iCurState;
while ((iCurState = this->state_) != 1) while ((iCurState = AuAtomicLoad(&this->state_)) != 1)
{ {
bool bStatusTwo {}; bool bStatusTwo {};
bool bStatus {}; bool bStatus {};
AuAtomicAdd(&this->writersPending_, 1); AuAtomicAdd(&this->writersPending_, 1);
static const AuUInt32 kExpect { 0 }; static const AuUInt32 kExpect { 0 };
RWLOCK_REORDER_BARRIER(); if ((iCurState = AuAtomicLoad(&this->state_)) == 1)
if ((iCurState = this->state_) == 1)
{ {
bStatus = true; bStatus = true;
bStatusTwo = true; bStatusTwo = true;