AuroraRuntime/Source/IO/WaitMultiple.Linux.cpp

147 lines
3.6 KiB
C++
Raw Normal View History

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: WaitMultiple.Linux.cpp
Date: 2022-5-13
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "IO.hpp"
#include "WaitMultiple.Linux.hpp"
2022-06-12 13:53:35 +00:00
#include <Source/IO/Loop/Loop.hpp>
#include <Source/IO/Loop/LSHandle.hpp>
#include <Source/IO/Loop/LSEvent.hpp>
#include "UNIX/IOSubmit.Linux.hpp"
2022-06-12 13:53:35 +00:00
#include "FS/Async.Linux.hpp"
namespace Aurora::IO
{
AUKN_SYM AuUInt32 WaitMultiple(const AuList<AuSPtr<IAsyncTransaction>> &files, AuUInt32 timeout)
{
AuCtorCode_t code;
AuUInt32 count {};
AuUInt64 endTime = AuTime::CurrentInternalClockMS() + AuUInt64(timeout);
auto waitQueue = AuTryConstruct<AuList<AuSPtr<IAsyncTransaction>>>(code, files);
if (!code)
{
return 0;
}
while (true)
{
if (waitQueue.empty())
{
break;
}
AuUInt32 timeoutMS {};
if (timeout)
{
AuInt64 delta = (AuInt64)endTime - AuTime::CurrentInternalClockMS();
if (delta <= 0)
{
break;
}
timeoutMS = delta;
}
if (!UNIX::LinuxOverlappedPoll(timeoutMS))
{
continue;
}
for (auto itr = waitQueue.begin();
itr != waitQueue.end();)
{
if (AuStaticPointerCast<FS::LinuxAsyncFileTransaction>(*itr)->Complete())
{
count ++;
itr = waitQueue.erase(itr);
}
else
{
itr++;
}
}
if (count)
{
break;
}
}
return count;
}
AUKN_SYM AuList<AuSPtr<IAsyncTransaction>> WaitMultiple2(const AuList<AuSPtr<IAsyncTransaction>> &transactions, AuUInt32 requestedTimeoutMs)
{
AuCtorCode_t code;
AuList<AuSPtr<IAsyncTransaction>> retTransactions;
AuUInt64 endTime = AuTime::CurrentInternalClockMS() + AuUInt64(requestedTimeoutMs);
auto waitQueue = AuTryConstruct<AuList<AuSPtr<IAsyncTransaction>>>(code, transactions);
if (!code)
{
2022-06-12 13:53:35 +00:00
SysPushErrorMem();
return {};
}
while (true)
{
if (waitQueue.empty())
{
break;
}
AuUInt32 timeoutMS {};
if (requestedTimeoutMs)
{
AuInt64 delta = (AuInt64)endTime - AuTime::CurrentInternalClockMS();
if (delta <= 0)
{
break;
}
timeoutMS = delta;
}
if (!UNIX::LinuxOverlappedPoll(timeoutMS))
{
continue;
}
for (auto itr = waitQueue.begin();
itr != waitQueue.end();)
{
if (AuStaticPointerCast<FS::LinuxAsyncFileTransaction>(*itr)->Complete())
{
AuTryInsert(retTransactions, *itr);
itr = waitQueue.erase(itr);
}
else
{
itr++;
}
}
if (retTransactions.size())
{
break;
}
}
return retTransactions;
}
AUKN_SYM void SendBatched()
{
#if defined(AURORA_IS_LINUX_DERIVED)
UNIX::SendIOBuffers();
#endif
}
}