[+] Au[Futex]Countdown

This commit is contained in:
Reece Wilson 2024-06-30 05:34:14 +01:00
parent 0555e8cc32
commit f2e3ceef99
3 changed files with 197 additions and 1 deletions

View File

@ -103,8 +103,10 @@ using AuFutexSemaphore = AuThreading::Waitables::FutexSemaphoreWaitable;
using AuFutexCond = AuThreading::Waitables::FutexCondWaitable;
using AuFutexCondition = AuThreading::Waitables::FutexCondWaitable;
using AuFutexBarrier = AuThreading::Waitables::FutexBarrier;
using AuFutexCountdown = AuThreading::Waitables::FutexCountdown;
using AuBarrier = AuThreading::Waitables::FutexBarrier;
using AuCountdown = AuThreading::Waitables::FutexCountdown;
using AuInitOnce = AuThreading::InitOnce;
using AuInitOnceSmall = __audetail::InitOnceABI;

View File

@ -0,0 +1,193 @@
/***
Copyright (C) 2024 Jamie Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FutexCountdown.hpp
Date: 2024-06-30
Author: Reece
***/
#pragma once
#include "../WakeOnAddress.hpp"
#include "../SpinTime.hpp"
#include "_.inl"
namespace Aurora::Threading::Waitables
{
struct FutexCountdown final : IWaitable
{
inline constexpr FutexCountdown()
{}
inline constexpr FutexCountdown(AuUInt32 uUsers) :
uAtomicState(uUsers)
{ }
inline constexpr FutexCountdown(const FutexCountdown &that) :
uAtomicState(that.uAtomicState)
{ }
inline constexpr FutexCountdown(FutexCountdown &&that) :
uAtomicState(that.uAtomicState)
{ }
AURT_WAITABLE_COPY_OPERATORS(FutexCountdown)
inline auline void AddUser()
{
AuAtomicAdd(&this->uAtomicState, 1u);
}
inline auline void AddUsers(AuUInt32 uCount)
{
AuAtomicAdd(&this->uAtomicState, uCount);
}
inline auline void DecrementUser()
{
if (!AuAtomicSub(&this->uAtomicState, 1u))
{
if (AuAtomicLoad(&this->uAtomicSleeping))
{
WakeAllOnAddress((const void *)&this->uAtomicState);
}
}
}
inline auline void DecrementUsers(AuUInt32 uCount)
{
if (!AuAtomicSub(&this->uAtomicState, uCount))
{
if (AuAtomicLoad(&this->uAtomicSleeping))
{
WakeAllOnAddress((const void *)&this->uAtomicState);
}
}
}
inline auline bool TryLockNoSpin()
{
return AuAtomicLoad(&this->uAtomicState) == 0;
}
inline bool TryLock() override
{
static const AuUInt32 kRef { 0 };
if (this->TryLockNoSpin())
{
return true;
}
return TryWaitOnAddressSpecial(EWaitMethod::eEqual,
(const void *)&this->uAtomicState,
&kRef,
sizeof(kRef));
}
inline bool HasOSHandle(AuMach &mach) override
{
return false;
}
inline bool HasLockImplementation() override
{
return true;
}
inline void Unlock() override
{
}
inline void Lock() override
{
static const AuUInt32 kRef { 0 };
if (this->TryLock())
{
return;
}
while (!this->TryLockNoSpin())
{
AuAtomicAdd(&this->uAtomicSleeping, 1u);
WaitOnAddressSpecial(EWaitMethod::eEqual, (const void *)&this->uAtomicState, &kRef, sizeof(kRef), 0, true);
AuAtomicSub(&this->uAtomicSleeping, 1u);
}
}
inline bool LockMS(AuUInt64 qwTimeout) override
{
return LockNS(AuMSToNS<AuUInt64>(qwTimeout));
}
inline bool LockAbsMS(AuUInt64 qwTimeout) override
{
return LockAbsNS(AuMSToNS<AuUInt64>(qwTimeout));
}
inline bool LockNS(AuUInt64 qwTimeout) override
{
static const AuUInt32 kRef { 0 };
if (this->TryLockNoSpin())
{
return true;
}
auto qwEndTime = qwTimeout ?
Time::SteadyClockNS() + qwTimeout :
0;
if (this->TryLock())
{
return true;
}
while (!this->TryLockNoSpin())
{
bool bStatus {};
AuAtomicAdd(&this->uAtomicSleeping, 1u);
bStatus = WaitOnAddressSpecialSteady(EWaitMethod::eEqual, (const void *)&this->uAtomicState, &kRef, sizeof(kRef), qwEndTime, true);
AuAtomicSub(&this->uAtomicSleeping, 1u);
if (!bStatus)
{
return this->TryLock();
}
}
return true;
}
inline bool LockAbsNS(AuUInt64 qwTimeoutAbs) override
{
static const AuUInt32 kRef { 0 };
if (this->TryLockNoSpin())
{
return true;
}
while (!this->TryLockNoSpin())
{
bool bStatus {};
AuAtomicAdd(&this->uAtomicSleeping, 1u);
bStatus = WaitOnAddressSpecialSteady(EWaitMethod::eEqual, (const void *)&this->uAtomicState, &kRef, sizeof(kRef), qwTimeoutAbs, true);
AuAtomicSub(&this->uAtomicSleeping, 1u);
if (!bStatus)
{
return this->TryLock();
}
}
return true;
}
AuAUInt32 uAtomicState {};
AuAUInt32 uAtomicSleeping {};
};
}

View File

@ -13,4 +13,5 @@
#include "FutexWaitable.hpp"
#include "FutexSemaphoreWaitable.hpp"
#include "FutexCondWaitable.hpp"
#include "FutexBarrier.hpp"
#include "FutexBarrier.hpp"
#include "FutexCountdown.hpp"