2021-10-03 12:47:16 +00:00
|
|
|
/***
|
|
|
|
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"
|
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
namespace Aurora::IO::Loop
|
2021-10-03 12:47:16 +00:00
|
|
|
{
|
2022-04-12 21:26:15 +00:00
|
|
|
bool WaitSingleGeneric::WaitForAtleastOne(AuUInt32 timeout, const AuList<AuUInt> &handles, AuUInt &one)
|
2021-10-03 12:47:16 +00:00
|
|
|
{
|
|
|
|
if (handles.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handles.size() == 1)
|
|
|
|
{
|
2022-03-30 17:56:56 +00:00
|
|
|
one = 0;
|
2022-04-15 18:20:12 +00:00
|
|
|
|
2023-12-23 08:03:59 +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
|
|
|
|
{
|
2023-12-23 08:03:59 +00:00
|
|
|
AuUInt32 uRemMS { INFINITE };
|
|
|
|
|
2023-12-23 16:55:04 +00:00
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
auto uNow = AuTime::SteadyClockNS();
|
|
|
|
if (uNow < uEndTimeSteady)
|
|
|
|
{
|
|
|
|
uRemMS = AuNSToMS<AuUInt64>(uEndTimeSteady - uNow);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uRemMS = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 08:03:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 18:20:12 +00:00
|
|
|
}
|
|
|
|
while (ret == WAIT_IO_COMPLETION);
|
|
|
|
|
2023-12-23 08:03:59 +00:00
|
|
|
return ret == WAIT_OBJECT_0;
|
2021-10-03 12:47:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AuList<HANDLE> ntHandles;
|
|
|
|
ntHandles.reserve(handles.size());
|
2022-04-12 21:26:15 +00:00
|
|
|
|
2021-10-03 12:47:16 +00:00
|
|
|
for (const auto &handle : handles)
|
|
|
|
{
|
2022-04-12 21:26:15 +00:00
|
|
|
if (!AuTryInsert(ntHandles, reinterpret_cast<HANDLE>(handle)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-03 12:47:16 +00:00
|
|
|
}
|
2023-12-23 08:03:59 +00:00
|
|
|
|
|
|
|
DWORD ret { WAIT_IO_COMPLETION };
|
|
|
|
AuUInt64 uEndTimeSteady = timeout ?
|
|
|
|
AuTime::SteadyClockNS() + AuMSToNS<AuUInt64>(timeout) : 0;
|
|
|
|
|
|
|
|
do
|
2022-03-30 17:56:56 +00:00
|
|
|
{
|
2023-12-23 08:03:59 +00:00
|
|
|
AuUInt32 uRemMS { INFINITE };
|
|
|
|
|
2023-12-23 16:55:04 +00:00
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
auto uNow = AuTime::SteadyClockNS();
|
|
|
|
if (uNow < uEndTimeSteady)
|
|
|
|
{
|
|
|
|
uRemMS = AuNSToMS<AuUInt64>(uEndTimeSteady - uNow);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uRemMS = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 08:03:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-03-30 17:56:56 +00:00
|
|
|
}
|
2023-12-23 08:03:59 +00:00
|
|
|
while (ret == WAIT_IO_COMPLETION);
|
2022-03-30 17:56:56 +00:00
|
|
|
|
2023-12-23 08:03:59 +00:00
|
|
|
return false;
|
2021-10-03 12:47:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 21:26:15 +00:00
|
|
|
bool WaitSingleGeneric::WaitForOne(AuUInt32 timeout, AuUInt handle)
|
2022-03-30 17:56:56 +00:00
|
|
|
{
|
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;
|
2022-03-30 17:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:47:16 +00:00
|
|
|
void WaitSingleGeneric::OnPresleep()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitSingleGeneric::OnFinishSleep()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ELoopSource WaitSingleGeneric::GetType()
|
|
|
|
{
|
|
|
|
return ELoopSource::eSourceInternalReserved1;
|
|
|
|
}
|
|
|
|
}
|