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

137 lines
3.4 KiB
C++
Raw Normal View History

/***
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;
2022-04-15 18:20:12 +00:00
DWORD ret { WAIT_IO_COMPLETION };
AuUInt64 uEndTimeSteady = timeout ?
AuTime::SteadyClockNS() + AuMSToNS<AuUInt64>(timeout) :
0;
2022-04-15 18:20:12 +00:00
do
{
AuUInt32 uRemMS { INFINITE };
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;
}
uRemMS = AuNSToMS<AuUInt64>(uEndTimeSteady);
if (!uRemMS)
{
break;
}
}
2022-04-15 18:20:12 +00:00
}
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 };
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;
}
uRemMS = AuNSToMS<AuUInt64>(uEndTimeSteady);
if (!uRemMS)
{
break;
}
}
}
while (ret == WAIT_IO_COMPLETION);
return false;
}
}
bool WaitSingleGeneric::WaitForOne(AuUInt32 timeout, AuUInt handle)
{
2022-04-15 18:20:12 +00:00
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;
}
}