AuroraRuntime/Source/IO/Loop/WaitSingleBase.cpp

121 lines
3.4 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: WaitSingle.Generic.cpp
Date: 2021-10-2
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "WaitSingleBase.hpp"
namespace Aurora::IO::Loop
{
bool WaitSingleBase::IsSignaled()
{
bool val {};
AuUInt one {AuNumericLimits<AuUInt>::max()};
AuUInt two {AuNumericLimits<AuUInt>::max()};
this->OnPresleep();
if (this->Singular())
{
#if defined(AURORA_IS_POSIX_DERIVED)
auto handle = this->GetHandle();
auto handleRw = this->GetWriteHandle();
val = this->WaitForOne(0, handle, handleRw);
#else
auto handle = this->GetHandle();
val = this->WaitForOne(0, handle);
#endif
if (val)
{
val = this->OnTrigger(handle);
}
}
else
{
#if defined(AURORA_IS_POSIX_DERIVED)
auto handles = this->GetHandles();
auto handlesRw = this->GetWriteHandles();
val = this->WaitForAtleastOne(0, handles, handlesRw, one, two);
if (one == -1) one = two;
#else
auto handles = this->GetHandles();
val = this->WaitForAtleastOne(0, handles, one);
#endif
if (val)
{
val = this->OnTrigger(one);
}
}
this->OnFinishSleep();
return val;
}
bool WaitSingleBase::WaitOn(AuUInt32 timeout)
{
bool val {};
AuUInt one {AuNumericLimits<AuUInt>::max()};
AuUInt two {AuNumericLimits<AuUInt>::max()};
this->OnPresleep();
auto uEndTime = timeout ?
AuTime::SteadyClockNS() + AuMSToNS<AuUInt64>(timeout) :
0;
do
{
if (this->Singular())
{
#if defined(AURORA_IS_POSIX_DERIVED)
auto handle = this->GetHandle();
auto handleRw = this->GetWriteHandle();
val = this->WaitForOne(timeout ? timeout : AuUInt32(-1), handle, handleRw);
#else
auto handle = this->GetHandle();
val = this->WaitForOne(timeout ? timeout : AuUInt32(-1), handle);
#endif
if (val)
{
val = this->OnTrigger(handle);
}
}
else
{
#if defined(AURORA_IS_POSIX_DERIVED)
auto handles = this->GetHandles();
auto handlesRw = this->GetWriteHandles();
val = this->WaitForAtleastOne(timeout ? timeout : AuUInt32(-1), handles, handlesRw, one, two);
if (one == -1) one = two;
#else
auto handles = this->GetHandles();
val = this->WaitForAtleastOne(timeout ? timeout : AuUInt32(-1), handles, one);
#endif
if (val)
{
val = this->OnTrigger(one);
}
}
if (timeout)
{
auto uStartTime = Time::SteadyClockNS();
if (uStartTime >= uEndTime)
{
return false;
}
timeout = AuNSToMS<AuInt64>(uEndTime - uStartTime);
}
}
while (timeout && !val);
this->OnFinishSleep();
return val;
}
}