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

84 lines
1.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;
do
{
ret = WaitForSingleObjectEx(reinterpret_cast<HANDLE>(handles.at(0)), timeout, true);
}
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;
}
}
auto idx = WaitForMultipleObjectsEx(ntHandles.size(), ntHandles.data(), false, timeout, true);
if (idx < WAIT_OBJECT_0)
{
return false;
}
one = handles[idx];
return true;
}
}
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;
}
}