100 lines
2.2 KiB
C++
100 lines
2.2 KiB
C++
/***
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: WaitSingle.Unix.cpp
|
|
Date: 2022-4-4
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "WaitSingle.hpp"
|
|
|
|
namespace Aurora::Loop
|
|
{
|
|
bool WaitSingleGeneric::WaitForAtleastOne(const AuList<AuUInt> &handles, const AuList<AuUInt> &handlesWrite, AuUInt &one, AuUInt &two)
|
|
{
|
|
fd_set readSet, writeSet;
|
|
AuUInt maxHandle {};
|
|
struct timeval tv {};
|
|
|
|
FD_ZERO(&readSet);
|
|
FD_ZERO(&writeSet);
|
|
|
|
for (const auto i : handles)
|
|
{
|
|
FD_SET(i, &readSet);
|
|
maxHandle = AuMax(maxHandle, i + 1);
|
|
}
|
|
|
|
for (const auto i : handlesWrite)
|
|
{
|
|
FD_SET(i, &writeSet);
|
|
maxHandle = AuMax(maxHandle, i + 1);
|
|
}
|
|
|
|
auto active = select(maxHandle, handles.size() ? &readSet : NULL, handlesWrite.size() ? &writeSet : NULL, NULL, &tv);
|
|
if (active == -1)
|
|
{
|
|
// todo push error
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
bool WaitSingleGeneric::WaitForOne(AuUInt read, AuUInt write)
|
|
{
|
|
fd_set readSet, writeSet;
|
|
struct timeval tv {};
|
|
|
|
FD_ZERO(&readSet);
|
|
FD_ZERO(&writeSet);
|
|
FD_SET(read, &readSet);
|
|
FD_SET(write, &writeSet);
|
|
|
|
auto active = select(AuMax(read, write) + 1, read != -1 ? &readSet : NULL, write != -1 ? &writeSet : NULL, NULL, &tv);
|
|
if (active == -1)
|
|
{
|
|
// todo push error
|
|
return false;
|
|
}
|
|
|
|
return active == 1;
|
|
}
|
|
|
|
void WaitSingleGeneric::OnPresleep()
|
|
{
|
|
|
|
}
|
|
|
|
void WaitSingleGeneric::OnFinishSleep()
|
|
{
|
|
|
|
}
|
|
|
|
ELoopSource WaitSingleGeneric::GetType()
|
|
{
|
|
return ELoopSource::eSourceInternalReserved1;
|
|
}
|
|
} |