AuroraRuntime/Include/Aurora/Threading/InitOnce.hpp

345 lines
7.2 KiB
C++
Raw Normal View History

2023-09-17 03:40:15 +00:00
/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: InitOnce.hpp
Date: 2023-09-17
Author: Reece
***/
#pragma once
namespace Aurora::Threading
{
struct InitOnceLocker;
}
namespace __audetail
{
struct InitOnceABI
{
inline bool IsUninitialized();
inline bool IsReady();
inline bool TrySet();
inline void Wait();
protected:
inline bool _TryAcquire();
2023-09-20 01:57:40 +00:00
inline bool _TryAcquireFailable();
inline void _Finish();
inline void _Wakeup();
2023-09-20 01:57:40 +00:00
inline void _FinishFailed();
inline bool _LockAbsNS(AuUInt64 qwAbsTimeoutInNs /* = 0, infinity*/);
protected:
AuAUInt32 uToken_ {};
AuAUInt32 uSleepers_ {};
friend struct Aurora::Threading::InitOnceLocker;
};
}
2023-09-17 03:40:15 +00:00
namespace Aurora::Threading
{
struct InitOnce final :
private IWaitable,
private __audetail::InitOnceABI
2023-09-17 03:40:15 +00:00
{
inline bool IsUninitialized()
{
return InitOnceABI::IsUninitialized();
2023-09-17 03:40:15 +00:00
}
inline bool IsReady()
{
return InitOnceABI::IsReady();
2023-09-17 03:40:15 +00:00
}
inline bool TrySet()
{
return InitOnceABI::TrySet();
2023-09-17 03:40:15 +00:00
}
template <typename Callable>
bool TryInit(const Callable &callback)
{
2023-09-20 01:57:40 +00:00
if (InitOnceABI::_TryAcquire())
2023-09-17 03:40:15 +00:00
{
callback();
2023-09-20 01:57:40 +00:00
InitOnceABI::_Finish();
2023-09-17 03:40:15 +00:00
return true;
}
else
{
return false;
}
}
template <typename Callable>
void Call(const Callable &callback)
{
if (this->TryInit(callback))
{
return;
}
if (this->IsReady())
{
return;
}
this->Wait();
2023-09-17 03:40:15 +00:00
}
2023-09-20 01:57:40 +00:00
template <typename Callable>
bool TryCall(const Callable &callback, bool bWait = true)
{
bool bRet {};
if (InitOnceABI::_TryAcquireFailable())
{
if (callback())
{
InitOnceABI::_Finish();
return true;
}
else
{
InitOnceABI::_FinishFailed();
return false;
}
}
if ((!(bRet = this->IsReady())) &&
(bWait))
{
this->Wait();
bRet = this->IsReady();
}
return bRet;
}
2023-09-17 03:40:15 +00:00
inline void Wait()
{
InitOnceABI::Wait();
2023-09-17 03:40:15 +00:00
}
inline IWaitable *ToBarrier()
{
return this;
}
protected:
inline bool HasOSHandle(AuMach &mach) override
2023-09-17 03:40:15 +00:00
{
return false;
}
inline bool HasLockImplementation() override
2023-09-17 03:40:15 +00:00
{
return true;
}
inline void Lock() override
2023-09-17 03:40:15 +00:00
{
this->LockAbsNS(0);
}
inline bool LockNS(AuUInt64 qwTimeoutInNs) override
2023-09-17 03:40:15 +00:00
{
if (this->IsReady())
{
return true;
}
return IWaitable::LockNS(qwTimeoutInNs);
}
inline bool LockAbsNS(AuUInt64 qwAbsTimeoutInNs /* = 0, infinity*/) override
2023-09-17 03:40:15 +00:00
{
return this->_LockAbsNS(qwAbsTimeoutInNs);
}
2023-09-17 03:40:15 +00:00
inline bool TryLock() override
{
return this->IsReady();
}
2023-09-17 03:40:15 +00:00
inline void Unlock() override
{
2023-09-17 03:40:15 +00:00
}
2023-09-17 03:40:15 +00:00
private:
friend struct InitOnceLocker;
};
struct InitOnceLocker
{
2023-09-20 01:57:40 +00:00
cstatic bool TryLock(InitOnce *pInitOnce, bool bFailable = false)
{
2023-09-20 01:57:40 +00:00
if (bFailable)
{
return pInitOnce->_TryAcquireFailable();
}
else
{
return pInitOnce->_TryAcquire();
}
2023-09-17 03:40:15 +00:00
}
2023-09-20 01:57:40 +00:00
cstatic void Finish(InitOnce *pInitOnce, bool bFailed = false)
2023-09-17 03:40:15 +00:00
{
2023-09-20 01:57:40 +00:00
if (bFailed)
{
pInitOnce->_FinishFailed();
}
else
{
pInitOnce->_Finish();
}
2023-09-17 03:40:15 +00:00
}
2023-09-20 01:57:40 +00:00
cstatic bool TryLock(__audetail::InitOnceABI *pInitOnce, bool bFailable = false)
2023-09-17 03:40:15 +00:00
{
2023-09-20 01:57:40 +00:00
if (bFailable)
{
return pInitOnce->_TryAcquireFailable();
}
else
{
return pInitOnce->_TryAcquire();
}
}
2023-09-17 03:40:15 +00:00
2023-09-20 01:57:40 +00:00
cstatic void Finish(__audetail::InitOnceABI *pInitOnce, bool bFailed = false)
{
2023-09-20 01:57:40 +00:00
if (bFailed)
{
pInitOnce->_FinishFailed();
}
else
{
pInitOnce->_Finish();
}
2023-09-17 03:40:15 +00:00
}
/**
* Example usage:
*
* static AuInitOnceSmall gSmall; // or static AuInitOnce gInitOnce;
* static_assert(sizeof(gSmall) == 8);
*
* if (AuThreading::InitOnceLocker::TryLock(&gSmall))
* {
* // My non-AuInitOnce::TryInit/Call() callback logic here
* AuThreading::InitOnceLocker::Finish(&gSmall);
* }
* else
* {
* gSmall.Wait();
* }
*/
2023-09-17 03:40:15 +00:00
};
}
namespace __audetail
{
bool InitOnceABI::IsUninitialized()
{
return (AuAtomicLoad(&this->uToken_) & 1) == 0;
}
bool InitOnceABI::IsReady()
{
return (AuAtomicLoad(&this->uToken_) & 2) != 0;
}
bool InitOnceABI::TrySet()
{
if (this->_TryAcquire())
{
this->_Finish();
return true;
}
else
{
return false;
}
}
void InitOnceABI::Wait()
{
this->_LockAbsNS(0);
}
bool InitOnceABI::_TryAcquire()
{
return AuAtomicTestAndSet(&this->uToken_, 0) == 0;
}
2023-09-20 01:57:40 +00:00
bool InitOnceABI::_TryAcquireFailable()
{
if (AuAtomicTestAndSet(&this->uToken_, 0) == 0)
{
AuAtomicUnset(&this->uToken_, 2);
return true;
}
else
{
return false;
}
}
void InitOnceABI::_Finish()
{
AuAtomicSet(&this->uToken_, 1);
this->_Wakeup();
}
void InitOnceABI::_Wakeup()
{
if (auto uSleepers = AuAtomicLoad(&this->uSleepers_))
{
Aurora::Threading::WakeNOnAddress((const void *)&this->uToken_, uSleepers);
}
}
2023-09-20 01:57:40 +00:00
void InitOnceABI::_FinishFailed()
{
AuAtomicUnset(&this->uToken_, 0);
AuAtomicSet(&this->uToken_, 2);
InitOnceABI::_Wakeup();
}
bool InitOnceABI::_LockAbsNS(AuUInt64 qwAbsTimeoutInNs /* = 0, infinity*/)
{
auto uCurrent = AuAtomicLoad(&this->uToken_);
2023-09-20 01:57:40 +00:00
while ((uCurrent & (2 | 4)) == 0)
{
AuAtomicAdd(&this->uSleepers_, 1u);
bool bRet = Aurora::Threading::WaitOnAddressSteady((const void *)&this->uToken_, &uCurrent, sizeof(uCurrent), qwAbsTimeoutInNs);
AuAtomicSub(&this->uSleepers_, 1u);
if (!bRet)
{
return this->IsReady();
}
uCurrent = AuAtomicLoad(&this->uToken_);
}
return true;
}
2023-09-17 03:40:15 +00:00
}