2021-10-02 10:28:49 +00:00
|
|
|
/***
|
2022-04-05 01:19:37 +00:00
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
2021-10-02 10:28:49 +00:00
|
|
|
|
2022-04-05 01:19:37 +00:00
|
|
|
File: WaitSingle.Unix.cpp
|
|
|
|
Date: 2022-4-4
|
2021-10-02 10:28:49 +00:00
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-04-05 01:19:37 +00:00
|
|
|
#include "WaitSingle.hpp"
|
2022-04-12 21:26:15 +00:00
|
|
|
#include <Source/Time/Time.hpp>
|
2021-10-02 10:28:49 +00:00
|
|
|
|
2022-04-13 11:00:35 +00:00
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
|
|
|
#include <Source/IO/UNIX/IOSubmit.Linux.hpp>
|
|
|
|
#endif
|
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
namespace Aurora::IO::Loop
|
2021-10-02 10:28:49 +00:00
|
|
|
{
|
2022-04-12 21:26:15 +00:00
|
|
|
bool WaitSingleGeneric::WaitForAtleastOne(AuUInt32 timeout, const AuList<AuUInt> &handles, const AuList<AuUInt> &handlesWrite, AuUInt &one, AuUInt &two)
|
2022-04-05 01:19:37 +00:00
|
|
|
{
|
|
|
|
fd_set readSet, writeSet;
|
|
|
|
AuUInt maxHandle {};
|
|
|
|
struct timeval tv {};
|
|
|
|
|
2022-04-13 11:00:35 +00:00
|
|
|
// TODO: IO SUBMIT HOOK
|
|
|
|
|
2022-04-05 01:19:37 +00:00
|
|
|
FD_ZERO(&readSet);
|
|
|
|
FD_ZERO(&writeSet);
|
|
|
|
|
2022-04-12 21:26:15 +00:00
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
AuTime::ms2tv(&tv, timeout);
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:19:37 +00:00
|
|
|
for (const auto i : handles)
|
|
|
|
{
|
2022-04-10 15:40:49 +00:00
|
|
|
if (i == -1) continue;
|
2022-04-05 01:19:37 +00:00
|
|
|
FD_SET(i, &readSet);
|
|
|
|
maxHandle = AuMax(maxHandle, i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto i : handlesWrite)
|
|
|
|
{
|
2022-04-10 15:40:49 +00:00
|
|
|
if (i == -1) continue;
|
2022-04-05 01:19:37 +00:00
|
|
|
FD_SET(i, &writeSet);
|
|
|
|
maxHandle = AuMax(maxHandle, i + 1);
|
|
|
|
}
|
|
|
|
|
2022-04-12 21:26:15 +00:00
|
|
|
auto active = select(maxHandle,
|
|
|
|
handles.size() ? &readSet : nullptr,
|
|
|
|
handlesWrite.size() ? &writeSet : nullptr,
|
|
|
|
nullptr,
|
|
|
|
timeout == AuUInt32(-1) ? nullptr : &tv);
|
2022-05-13 11:51:00 +00:00
|
|
|
|
2022-04-05 01:19:37 +00:00
|
|
|
if (active == -1)
|
|
|
|
{
|
|
|
|
// todo push error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-13 11:51:00 +00:00
|
|
|
if (active == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:19:37 +00:00
|
|
|
for (const auto i : handles)
|
|
|
|
{
|
|
|
|
if (!FD_ISSET(i, &readSet))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
one = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto i : handlesWrite)
|
|
|
|
{
|
|
|
|
if (!FD_ISSET(i, &writeSet))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
two = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-12 21:26:15 +00:00
|
|
|
bool WaitSingleGeneric::WaitForOne(AuUInt32 timeout, AuUInt read, AuUInt write)
|
2022-04-05 01:19:37 +00:00
|
|
|
{
|
|
|
|
fd_set readSet, writeSet;
|
|
|
|
struct timeval tv {};
|
2022-04-10 15:40:49 +00:00
|
|
|
int maxFd {};
|
2022-04-05 01:19:37 +00:00
|
|
|
|
2022-04-13 11:00:35 +00:00
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
|
|
|
{
|
|
|
|
bool bWriteTriggered, bReadTriggered;
|
|
|
|
|
|
|
|
if (IO::UNIX::LinuxOverlappedWaitForOne(timeout, read, write, bReadTriggered, bWriteTriggered))
|
|
|
|
{
|
|
|
|
if (!bWriteTriggered && !bReadTriggered)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-04-05 01:19:37 +00:00
|
|
|
FD_ZERO(&readSet);
|
|
|
|
FD_ZERO(&writeSet);
|
2022-04-12 21:26:15 +00:00
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
AuTime::ms2tv(&tv, timeout);
|
|
|
|
}
|
|
|
|
|
2022-04-10 15:40:49 +00:00
|
|
|
if (read != -1)
|
|
|
|
{
|
|
|
|
maxFd = read + 1;
|
|
|
|
FD_SET(read, &readSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write != -1)
|
|
|
|
{
|
|
|
|
FD_SET(write, &writeSet);
|
|
|
|
maxFd = AuMax(maxFd, int(write) + 1);
|
|
|
|
}
|
2022-04-05 01:19:37 +00:00
|
|
|
|
2022-04-12 21:26:15 +00:00
|
|
|
auto active = select(maxFd,
|
|
|
|
read != -1 ? &readSet : nullptr,
|
|
|
|
write != -1 ? &writeSet : nullptr,
|
|
|
|
nullptr,
|
|
|
|
timeout == AuUInt32(-1) ? nullptr : &tv);
|
2022-05-13 11:51:00 +00:00
|
|
|
|
|
|
|
if (active == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:19:37 +00:00
|
|
|
if (active == -1)
|
|
|
|
{
|
|
|
|
// todo push error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return active == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitSingleGeneric::OnPresleep()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitSingleGeneric::OnFinishSleep()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ELoopSource WaitSingleGeneric::GetType()
|
|
|
|
{
|
|
|
|
return ELoopSource::eSourceInternalReserved1;
|
|
|
|
}
|
2021-10-02 10:28:49 +00:00
|
|
|
}
|