[*] Minor NT optimization: move branch

This commit is contained in:
Reece Wilson 2023-07-10 18:51:28 +01:00
parent bdec6ff8ba
commit c90a13ad95
2 changed files with 76 additions and 78 deletions

View File

@ -112,28 +112,6 @@ namespace Aurora::Threading::Primitives
InterlockedAndRelease((volatile LONG *)&uValueRef, ~0xFF);
#endif
if (gUseNativeWaitCondvar)
{
while (true)
{
auto uValue = uValueRef;
if (uValue < kFutexBitWait)
{
return;
}
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait, uValue) == uValue)
{
pWakeByAddressSingle((void *)&this->lock_.uWaitCount);
return;
}
SMPPause();
}
}
else
{
while (true)
{
auto uOld = uValueRef;
@ -149,6 +127,16 @@ namespace Aurora::Threading::Primitives
return;
}
if (gUseNativeWaitCondvar)
{
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait, uValue) == uValue)
{
pWakeByAddressSingle((void *)&uValueRef);
return;
}
}
else
{
if (uValue & kFutexBitWake)
{
if (AuAtomicCompareExchange(&uValueRef, uValue, uValue) == uValue)
@ -167,10 +155,10 @@ namespace Aurora::Threading::Primitives
pNtReleaseKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
return;
}
}
SMPPause();
}
}
#endif
}

View File

@ -70,12 +70,12 @@ namespace Aurora::Threading::Primitives
bool MutexImpl::LockMS(AuUInt64 uTimeout)
{
if (AuAtomicTestAndSet(&this->state_, 0) == 0)
if (this->TryLockNoSpin())
{
return true;
}
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
return this->LockNS(AuMSToNS<AuUInt64>(uTimeout));
}
bool MutexImpl::LockNS(AuUInt64 uTimeout)
@ -224,6 +224,8 @@ namespace Aurora::Threading::Primitives
void MutexImpl::Unlock()
{
#if defined(AURORA_FORCE_SRW_LOCKS)
if (gUseNativeWaitMutex)
{
auto &uValueRef = this->state_;
@ -250,9 +252,7 @@ namespace Aurora::Threading::Primitives
return;
}
else
{
#if defined(AURORA_FORCE_SRW_LOCKS)
::AcquireSRWLockExclusive(&this->atomicHolder_);
this->state_ = 0;
::ReleaseSRWLockExclusive(&this->atomicHolder_);
@ -283,6 +283,7 @@ namespace Aurora::Threading::Primitives
// ...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
@ -291,7 +292,6 @@ namespace Aurora::Threading::Primitives
{
auto uValue = uValueRef;
//
if (uValue < kFutexBitWait)
{
return;
@ -305,6 +305,16 @@ namespace Aurora::Threading::Primitives
return;
}
if (gUseNativeWaitMutex)
{
if (AuAtomicCompareExchange(&uValueRef, uValue - kFutexBitWait, uValue) == uValue)
{
pWakeByAddressSingle((void *)&this->state_);
return;
}
}
else
{
if (uValue & kFutexBitWake)
{
// StoreLoad paranoia
@ -324,13 +334,13 @@ namespace Aurora::Threading::Primitives
pNtReleaseKeyedEvent(gKeyedEventHandle, (void *)&uValueRef, 0, NULL);
return;
}
}
SMPPause();
}
#endif
}
}
AUKN_SYM IHyperWaitable *MutexNew()
{