AuroraRuntime/Include/Aurora/Threading/Waitables/BooleanWaitable.hpp

82 lines
1.7 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: BooleanWaitable.hpp
Date: 2021-6-10
Author: Reece
***/
#pragma once
#include "../WakeOnAddress.hpp"
namespace Aurora::Threading::Waitables
{
struct BooleanWaitable : IWaitable
{
inline BooleanWaitable(bool *pValue) : pValue_(pValue)
{ }
inline BooleanWaitable(bool &refValue) : pValue_(&refValue)
{ }
inline BooleanWaitable(std::atomic<bool> &value) : pValue_((bool *)&value)
{
}
inline bool TryLock() override
{
return this->pValue_ ? *this->pValue_ : true;
}
inline bool HasOSHandle(AuMach &mach) override
{
return false;
}
inline bool HasLockImplementation() override
{
return true;
}
inline void Unlock() override
{
if (!this->pValue_)
{
return;
}
WakeOnAddress(this->pValue_);
}
inline void Lock() override
{
if (!this->pValue_)
{
return;
}
AuUInt8 uRef { 1 };
WaitOnAddress(this->pValue_, &uRef, 1, 0);
}
inline bool LockMS(AuUInt64 qwTimeout) override
{
return LockNS(AuMSToNS<AuUInt64>(qwTimeout));
}
inline bool LockNS(AuUInt64 qwTimeout) override
{
AuUInt8 uRef { 1 };
if (!this->pValue_)
{
return false;
}
return WaitOnAddress(this->pValue_, &uRef, 1, qwTimeout);
}
private:
bool *pValue_ {};
};
}