AuroraRuntime/Source/IO/Loop/WaitSingle.NT.cpp

151 lines
3.9 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: WaitSingle.NT.cpp
Date: 2021-10-1
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "WaitSingle.hpp"
namespace Aurora::IO::Loop
{
bool WaitSingleGeneric::WaitForAtleastOne(AuUInt32 timeout, const AuList<AuUInt> &handles, AuUInt &one)
{
if (handles.empty())
{
return {};
}
if (handles.size() == 1)
{
one = 0;
DWORD ret { WAIT_IO_COMPLETION };
AuUInt64 uEndTimeSteady = timeout ?
AuTime::SteadyClockNS() + AuMSToNS<AuUInt64>(timeout) :
0;
do
{
AuUInt32 uRemMS { INFINITE };
if (timeout)
{
auto uNow = AuTime::SteadyClockNS();
if (uNow < uEndTimeSteady)
{
uRemMS = AuNSToMS<AuUInt64>(uEndTimeSteady - uNow);
}
else
{
uRemMS = 0;
}
}
ret = WaitForSingleObjectEx(reinterpret_cast<HANDLE>(handles.at(0)), uRemMS, true);
if (timeout &&
ret != WAIT_IO_COMPLETION &&
ret != WAIT_OBJECT_0)
{
auto uNow = AuTime::SteadyClockNS();
if (uNow >= uEndTimeSteady)
{
break;
}
}
}
while (ret == WAIT_IO_COMPLETION);
return ret == WAIT_OBJECT_0;
}
else
{
AuList<HANDLE> ntHandles;
ntHandles.reserve(handles.size());
for (const auto &handle : handles)
{
if (!AuTryInsert(ntHandles, reinterpret_cast<HANDLE>(handle)))
{
return false;
}
}
DWORD ret { WAIT_IO_COMPLETION };
AuUInt64 uEndTimeSteady = timeout ?
AuTime::SteadyClockNS() + AuMSToNS<AuUInt64>(timeout) : 0;
do
{
AuUInt32 uRemMS { INFINITE };
if (timeout)
{
auto uNow = AuTime::SteadyClockNS();
if (uNow < uEndTimeSteady)
{
uRemMS = AuNSToMS<AuUInt64>(uEndTimeSteady - uNow);
}
else
{
uRemMS = 0;
}
}
ret = WaitForMultipleObjectsEx(ntHandles.size(), ntHandles.data(), false, uRemMS, true);
if (ret < WAIT_OBJECT_0)
{
continue;
}
if (ret < ntHandles.size())
{
one = handles[ret];
return true;
}
if (timeout)
{
auto uNow = AuTime::SteadyClockNS();
if (uNow >= uEndTimeSteady)
{
break;
}
}
}
while (ret == WAIT_IO_COMPLETION);
return false;
}
}
bool WaitSingleGeneric::WaitForOne(AuUInt32 timeout, AuUInt handle)
{
DWORD ret;
do
{
ret = WaitForSingleObjectEx(reinterpret_cast<HANDLE>(handle), timeout, true);
}
while (ret == WAIT_IO_COMPLETION);
return ret == WAIT_OBJECT_0;
}
void WaitSingleGeneric::OnPresleep()
{
}
void WaitSingleGeneric::OnFinishSleep()
{
}
ELoopSource WaitSingleGeneric::GetType()
{
return ELoopSource::eSourceInternalReserved1;
}
}