[+] bool AuFutexSemaphore::AcquireUntilExactlyEqualAbsNS(AuUInt32 uValue, AuOptional<AuUInt64> qwTimeoutAbs)

[+] bool AuFutexSemaphore::AcquireUntilAtleastAbsNS(AuUInt32 uValue, AuOptional<AuUInt64> qwTimeoutAbs)
This commit is contained in:
Reece Wilson 2024-03-10 09:28:50 +00:00
parent cc4d97d6e7
commit 6913daae7d

View File

@ -156,7 +156,7 @@ namespace Aurora::Threading::Waitables
return true;
}
inline bool LockUntilEqualAbsNS(AuUInt32 uValue,
inline bool LockUntilExactlyEqualAbsNS(AuUInt32 uValue,
AuOptional<AuUInt64> qwTimeoutAbs)
{
const AuUInt32 kRef { uValue };
@ -178,6 +178,76 @@ namespace Aurora::Threading::Waitables
return true;
}
inline bool AcquireUntilExactlyEqualAbsNS(AuUInt32 uValue,
AuOptional<AuUInt64> qwTimeoutAbs)
{
const AuUInt32 kRef { uValue };
AuUInt32 uState {};
bool bStatus {};
while (!TryWaitOnAddressUntilEqual((const void *)&this->uAtomicState, &kRef, sizeof(kRef)))
{
AuAtomicAdd(&this->uAtomicSleeping, 1u);
bStatus = WaitOnAddressUntilEqualSteady((const void *)&this->uAtomicState, &kRef, sizeof(kRef), qwTimeoutAbs.ValueOr(0), true);
AuAtomicSub(&this->uAtomicSleeping, 1u);
if (!bStatus)
{
break;
}
}
if (!bStatus)
{
return false;
}
while ((uState = AuAtomicLoad(&this->uAtomicState) >= uValue))
{
if (AuAtomicCompareExchange(&this->uAtomicState, uState - uValue, uState) == uState)
{
return true;
}
}
return false;
}
inline bool AcquireUntilAtleastAbsNS(AuUInt32 uValue,
AuOptional<AuUInt64> qwTimeoutAbs)
{
AuUInt32 uState {};
bool bStatus {};
while ((uState = AuAtomicLoad(&this->uAtomicState) < uValue))
{
AuAtomicAdd(&this->uAtomicSleeping, 1u);
bStatus = WaitOnAddressSteady((const void *)&this->uAtomicState, &uState, sizeof(uState), qwTimeoutAbs.ValueOr(0), true);
AuAtomicSub(&this->uAtomicSleeping, 1u);
if (!bStatus)
{
break;
}
}
if (!bStatus)
{
return false;
}
do
{
if (AuAtomicCompareExchange(&this->uAtomicState, uState - uValue, uState) == uState)
{
return true;
}
}
while ((uState = AuAtomicLoad(&this->uAtomicState) >= uValue));
return false;
}
AuAUInt32 uAtomicState {};
AuAUInt32 uAtomicSleeping {};
};