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

82 lines
1.7 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
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"
2021-06-27 21:25:29 +00:00
namespace Aurora::Threading::Waitables
{
struct BooleanWaitable : IWaitable
2021-06-27 21:25:29 +00:00
{
inline BooleanWaitable(bool *pValue) : pValue_(pValue)
{ }
inline BooleanWaitable(bool &refValue) : pValue_(&refValue)
{ }
inline BooleanWaitable(std::atomic<bool> &value) : pValue_((bool *)&value)
2021-06-27 21:25:29 +00:00
{
}
inline bool TryLock() override
2021-06-27 21:25:29 +00:00
{
return this->pValue_ ? *this->pValue_ : true;
2021-06-27 21:25:29 +00:00
}
inline bool HasOSHandle(AuMach &mach) override
2021-06-27 21:25:29 +00:00
{
return false;
}
inline bool HasLockImplementation() override
2021-06-27 21:25:29 +00:00
{
return true;
2021-06-27 21:25:29 +00:00
}
inline void Unlock() override
2021-06-27 21:25:29 +00:00
{
if (!this->pValue_)
{
return;
}
WakeOnAddress(this->pValue_);
2021-06-27 21:25:29 +00:00
}
inline void Lock() override
2021-06-27 21:25:29 +00:00
{
if (!this->pValue_)
{
return;
}
AuUInt8 uRef { 1 };
WaitOnAddress(this->pValue_, &uRef, 1, 0);
2021-06-27 21:25:29 +00:00
}
inline bool LockMS(AuUInt64 qwTimeout) override
2021-06-27 21:25:29 +00:00
{
return LockNS(AuMSToNS<AuUInt64>(qwTimeout));
2021-06-27 21:25:29 +00:00
}
inline bool LockNS(AuUInt64 qwTimeout) override
{
AuUInt8 uRef { 1 };
if (!this->pValue_)
{
return false;
}
return WaitOnAddress(this->pValue_, &uRef, 1, qwTimeout);
}
2021-06-27 21:25:29 +00:00
private:
bool *pValue_ {};
2021-06-27 21:25:29 +00:00
};
}