2022-06-11 23:01:27 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
File: AuIOProcessor.cpp
|
2022-06-11 23:01:27 +00:00
|
|
|
Date: 2022-6-6
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include <Aurora/IO/IOExperimental.hpp>
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuIOProcessorItem.hpp"
|
|
|
|
#include "AuIOProcessorItems.hpp"
|
|
|
|
#include "AuIOProcessor.hpp"
|
|
|
|
#include "AuIOPipeProcessor.hpp"
|
2022-07-06 02:40:09 +00:00
|
|
|
#include "Loop/Loop.hpp"
|
|
|
|
#include "Loop/LoopQueue.hpp"
|
2023-08-09 02:21:14 +00:00
|
|
|
#include <Async/ThreadPool.hpp>
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
IOProcessor::IOProcessor(AuUInt threadId,
|
|
|
|
bool bTickOnly,
|
2023-09-18 18:04:09 +00:00
|
|
|
AuOptionalEx<AuAsync::WorkerPId_t> worker,
|
2022-11-17 20:56:41 +00:00
|
|
|
const AuSPtr<AuLoop::ILoopQueue> &pLoopQueue,
|
|
|
|
bool bIsNoQueue) :
|
2022-08-29 15:46:46 +00:00
|
|
|
mutliplexIOAndTimer(!bTickOnly),
|
|
|
|
pLoopQueue(pLoopQueue),
|
2022-06-21 04:49:36 +00:00
|
|
|
asyncWorker(worker),
|
|
|
|
threadId(threadId),
|
2022-11-17 20:56:41 +00:00
|
|
|
streamProcessors(this),
|
|
|
|
bIsNoQueue(bIsNoQueue)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
IOProcessor::~IOProcessor()
|
|
|
|
{
|
|
|
|
this->ReleaseAllWatches();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::Init()
|
|
|
|
{
|
2023-09-18 18:04:09 +00:00
|
|
|
auto pQueue = this->ToQueue();
|
|
|
|
|
|
|
|
if (!pQueue)
|
2022-06-14 01:14:51 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
if (!this->items.Init())
|
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-18 18:04:09 +00:00
|
|
|
auto pTimer = AuLoop::NewLSTimer(0);
|
|
|
|
if (!pTimer)
|
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->timers.Init(this, pTimer))
|
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pQueue->SourceAdd(this->items.cvEvent))
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:56:41 +00:00
|
|
|
if (!this->bIsNoQueue)
|
|
|
|
{
|
|
|
|
auto pAS = AuMakeShared<AuLoop::ILoopSourceSubscriberFunctional>([pThat = AuSharedFromThis()](const AuSPtr<AuLoop::ILoopSource> &pSource) -> bool
|
|
|
|
{
|
|
|
|
pThat->ManualTick();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2023-09-18 18:04:09 +00:00
|
|
|
if (!pQueue->AddCallback(this->items.cvEvent, pAS))
|
2022-11-17 20:56:41 +00:00
|
|
|
{
|
2023-09-18 18:04:09 +00:00
|
|
|
return false;
|
2022-11-17 20:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-18 18:04:09 +00:00
|
|
|
|
|
|
|
(void)pQueue->Commit();
|
2022-06-14 01:14:51 +00:00
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
if (asyncWorker)
|
|
|
|
{
|
2023-09-15 17:08:02 +00:00
|
|
|
AuAtomicAdd(&AuStaticCast<AuAsync::ThreadPool>(asyncWorker.value().pool)->uAtomicIOProcessors, 1u);
|
2023-08-09 02:21:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::QueueIOEvent(const AuSPtr<IIOProcessorItem> &ioEvent)
|
|
|
|
{
|
|
|
|
return this->items.AddFrameOrFallback(AuStaticCast<IOProcessorItem>(ioEvent));
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt32 IOProcessor::TryTick()
|
|
|
|
{
|
|
|
|
if (!CheckThread())
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric("Wrong Thread");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AU_LOCK_GUARD(this->items.mutex);
|
|
|
|
FrameStart();
|
|
|
|
FrameRunThreadIO();
|
|
|
|
FrameRunCheckLSes();
|
|
|
|
|
|
|
|
bool bHasAlertedItems = this->items.workSignaled.size() ||
|
|
|
|
this->timers.nbTicker.HasPassed();
|
|
|
|
|
|
|
|
if (!bHasAlertedItems)
|
|
|
|
{
|
|
|
|
ReportState(EIOProcessorEventStage::eFrameEndOfFrame);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FrameRunEpilogue();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::TickForRegister(const AuSPtr<IIOProcessorItem> &ioEvent)
|
|
|
|
{
|
|
|
|
return this->items.AddFrameOrFallback(AuStaticCast<IOProcessorItem>(ioEvent));
|
|
|
|
}
|
|
|
|
|
2022-07-06 02:40:09 +00:00
|
|
|
void IOProcessor::TickForHack(const AuSPtr<IIOProcessorItem> &ioEvent)
|
|
|
|
{
|
|
|
|
SysAssert(this->TickForRegister(ioEvent));
|
|
|
|
|
|
|
|
if (AuExchange(this->bScheduled_, true))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
AuStaticCast<Loop::LoopQueue>(this->pLoopQueue)->AddHook([that = AuSharedFromThis()]()
|
2022-07-06 02:40:09 +00:00
|
|
|
{
|
|
|
|
that->RunTick();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
AuUInt32 IOProcessor::TickFor(const AuSPtr<IIOProcessorItem> &ioEvent)
|
|
|
|
{
|
|
|
|
SysAssert(TickForRegister(ioEvent));
|
|
|
|
|
|
|
|
if (IsTickOnly())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:31:47 +00:00
|
|
|
if (this->bFrameStart &&
|
|
|
|
!this->bNoDeferredTick)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CheckThread())
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric("Wrong Thread");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AU_LOCK_GUARD(this->items.mutex);
|
|
|
|
FrameStart();
|
|
|
|
FrameRunThreadIO();
|
|
|
|
FrameRunCheckLSes();
|
|
|
|
return FrameRunEpilogue();
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt32 IOProcessor::RunTick()
|
|
|
|
{
|
|
|
|
if (!CheckThread())
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric("Wrong Thread");
|
2022-12-06 22:53:37 +00:00
|
|
|
AuIO::IOSleep(1);
|
2022-06-11 23:01:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-06 02:40:09 +00:00
|
|
|
this->bScheduled_ = false;
|
2022-06-11 23:01:27 +00:00
|
|
|
this->bFrameStart = false;
|
|
|
|
|
|
|
|
AU_LOCK_GUARD(this->items.mutex);
|
|
|
|
FrameStart();
|
|
|
|
FrameWaitForAny(0);
|
2022-06-14 01:14:51 +00:00
|
|
|
FramePumpWaitingBlocked();
|
2022-06-11 23:01:27 +00:00
|
|
|
FrameRunThreadIO();
|
|
|
|
FrameRunCheckLSes();
|
|
|
|
return FrameRunEpilogue();
|
|
|
|
}
|
|
|
|
|
2022-12-06 22:53:37 +00:00
|
|
|
AuUInt32 IOProcessor::RunTickEx(AuUInt32 dwTimeout)
|
|
|
|
{
|
|
|
|
if (!CheckThread())
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric("Wrong Thread");
|
|
|
|
AuIO::IOSleep(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bScheduled_ = false;
|
|
|
|
this->bFrameStart = false;
|
|
|
|
|
|
|
|
AU_LOCK_GUARD(this->items.mutex);
|
|
|
|
FrameStart();
|
|
|
|
FrameWaitForAny(dwTimeout);
|
|
|
|
FramePumpWaitingBlocked();
|
|
|
|
FrameRunThreadIO();
|
|
|
|
FrameRunCheckLSes();
|
|
|
|
return FrameRunEpilogue();
|
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
AuUInt32 IOProcessor::ManualTick()
|
|
|
|
{
|
|
|
|
if (!CheckThread())
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric("Wrong Thread");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bFrameStart = false;
|
|
|
|
|
|
|
|
AU_LOCK_GUARD(this->items.mutex);
|
|
|
|
FrameStart();
|
|
|
|
FrameRunThreadIO();
|
|
|
|
FrameRunCheckLSes();
|
|
|
|
return FrameRunEpilogue();
|
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
void IOProcessor::FrameRunIOWorkUnits()
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(this->items.mutex);
|
|
|
|
|
|
|
|
while (this->workUnits.size())
|
|
|
|
{
|
|
|
|
for (auto workItems : AuExchange(this->workUnits, {}))
|
|
|
|
{
|
|
|
|
workItems->OnRun();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
AuUInt IOProcessor::FrameRunEpilogue()
|
|
|
|
{
|
|
|
|
FrameRunAlerted();
|
|
|
|
FrameRunAlertedSniffers();
|
2022-06-21 04:49:36 +00:00
|
|
|
FrameRunIOWorkUnits();
|
2023-05-11 15:05:00 +00:00
|
|
|
IO::SendBatched();
|
2022-06-11 23:01:27 +00:00
|
|
|
FrameEndOfFrameEvents();
|
|
|
|
return FrameFinalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::FrameRunCheckLSes()
|
|
|
|
{
|
|
|
|
if (!this->IsTickOnly())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &item : this->items.allItems)
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (item->pItem->IsRunOnSelfIO() && item->pItem->IsRunOnSelfIOCheckedOnTimerTick())
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
auto ls = item->pItem->GetSelfIOSource();
|
2022-06-11 23:01:27 +00:00
|
|
|
if (ls && ls->IsSignaled())
|
|
|
|
{
|
|
|
|
// Should deadlock without critical sections
|
|
|
|
this->QueueIOEvent(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::FrameRunAlerted()
|
|
|
|
{
|
|
|
|
for (auto &a : this->items.onTickReceivers)
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (a->pListener)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
a->pListener->Tick_RunOnTick();
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
if (!AuExists(this->items.workSignaled, a) &&
|
|
|
|
!AuExists(this->items.onTickReceivers, a))
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
a->pListener->Tick_Any();
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SysAssert(AuTryInsert(this->items.finalizeQueue, a));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &a : this->items.workSignaled)
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (a->pListener)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
a->pListener->Tick_SelfIOEvent();
|
|
|
|
a->pListener->Tick_Any();
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SysAssert(AuTryInsert(this->items.finalizeQueue, a));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::FrameRunAlertedSniffers()
|
|
|
|
{
|
|
|
|
if (this->items.workSignaled.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &a : this->items.onOtherReceivers)
|
|
|
|
{
|
|
|
|
if (AuExists(this->items.workSignaled, a))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (a->pListener)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
a->pListener->Tick_OtherIOEvent();
|
|
|
|
a->pListener->Tick_Any();
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
|
|
|
|
|
|
|
SysAssert(a->FailWatch());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->items.finalizeQueue.push_back(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::FrameEndOfFrameEvents()
|
|
|
|
{
|
|
|
|
ReportState(EIOProcessorEventStage::eFrameWorkDone);
|
|
|
|
|
|
|
|
for (auto &pumped : this->items.finalizeQueue)
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (!pumped->pListener)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
pumped->pListener->Tick_FrameEpilogue();
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
|
|
|
pumped->FailWatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:57:29 +00:00
|
|
|
auto hasRemoved = this->items.crossThreadAbort.size();
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
for (auto itr = this->items.crossThreadAbort.begin(); itr != this->items.crossThreadAbort.end(); )
|
|
|
|
{
|
|
|
|
bool fatal = itr->second;
|
|
|
|
auto item = itr->first;
|
|
|
|
|
|
|
|
|
|
|
|
this->ClearProcessor(item, fatal);
|
|
|
|
|
|
|
|
itr = this->items.crossThreadAbort.erase(itr);
|
|
|
|
}
|
2022-06-28 14:57:29 +00:00
|
|
|
|
|
|
|
if (hasRemoved)
|
|
|
|
{
|
|
|
|
ToQueue()->Commit();
|
|
|
|
}
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IOProcessor::ClearProcessor(const AuSPtr<IOProcessorItem> &processor, bool fatal)
|
|
|
|
{
|
|
|
|
if (!AuTryRemove(this->items.allItems, processor))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (processor->pListener)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-08-28 19:02:06 +00:00
|
|
|
if (fatal)
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
processor->pListener->OnFailureCompletion();
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
processor->pListener->OnNominalCompletion();
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
auto item = processor->pItem;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
if (item->IsRunOnTick())
|
|
|
|
{
|
|
|
|
if (!AuTryRemove(this->items.onTickReceivers, processor))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->IsRunOnOtherTick())
|
|
|
|
{
|
|
|
|
if (!AuTryRemove(this->items.onOtherReceivers, processor))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->IsRunOnSelfIO())
|
|
|
|
{
|
|
|
|
if (!AuTryRemove(this->items.registeredIO, processor))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto src = item->GetSelfIOSource();
|
|
|
|
if (!src)
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->ToQueue()->SourceRemove(src))
|
|
|
|
{
|
|
|
|
SysPushErrorNested("IO Remove Error [!!!]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt IOProcessor::FrameFinalize()
|
|
|
|
{
|
|
|
|
auto C = this->items.finalizeQueue.size();
|
|
|
|
this->items.workSignaled.clear();
|
|
|
|
this->items.finalizeQueue.clear();
|
|
|
|
|
|
|
|
ReportState(EIOProcessorEventStage::eFrameEndOfFrame);
|
|
|
|
this->bFrameStart = false;
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::InternalIsTickReady()
|
|
|
|
{
|
|
|
|
// TODO: tickless io ad-hoc event-as-condvar?
|
|
|
|
return this->items.workSignaled.size() ||
|
2022-08-28 19:02:06 +00:00
|
|
|
this->items.crossThreadAbort.size() ||
|
|
|
|
this->workUnits.size();
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::FrameWaitForAny(AuUInt32 msMax)
|
|
|
|
{
|
|
|
|
if (InternalIsTickReady())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->IsTickOnly())
|
|
|
|
{
|
|
|
|
auto next = this->timers.nbTicker.nextTriggerTime;
|
2023-04-22 21:58:20 +00:00
|
|
|
auto now = AuTime::SteadyClockNS();
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
if (now >= next)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto delay = AuNSToMS<AuUInt>(next - now);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (delay == 1)
|
|
|
|
{
|
|
|
|
bool ret {};
|
|
|
|
while (IO::IOYield());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return IO::WaitFor(msMax ? AuMin<AuUInt32>(msMax, delay) : delay, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
return this->pLoopQueue->WaitAny(msMax);
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
bool IOProcessor::SubmitIOWorkItem(const AuSPtr<IIOProcessorWorkUnit> &work)
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(this->items.mutex); // < critical section / reentrant | can nest submission
|
2022-08-28 19:02:06 +00:00
|
|
|
this->items.cvEvent->Set();
|
2022-11-28 16:01:08 +00:00
|
|
|
return AuTryInsert(this->workUnits, work);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::WakeupThread()
|
|
|
|
{
|
|
|
|
this->items.cvEvent->Set();
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
bool IOProcessor::AddEventListener(const AuSPtr<IIOProcessorEventListener> &eventListener)
|
|
|
|
{
|
2023-07-27 15:31:47 +00:00
|
|
|
AU_LOCK_GUARD(this->listenersSpinLock->AsWritable());
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
return AuTryInsert(this->listeners, eventListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::RemoveEventListener(const AuSPtr<IIOProcessorEventListener> &eventListener)
|
|
|
|
{
|
2023-07-27 15:31:47 +00:00
|
|
|
AU_LOCK_GUARD(this->listenersSpinLock->AsWritable());
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
AuTryRemove(this->listeners, eventListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::DispatchFrame(ProcessInfo &info)
|
|
|
|
{
|
|
|
|
this->ManualTick();
|
|
|
|
|
|
|
|
auto ns = this->refreshRateNs;
|
|
|
|
if (ns)
|
|
|
|
{
|
|
|
|
info = AuAsync::ETickType::eSchedule;
|
|
|
|
info.reschedNs = ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 01:14:51 +00:00
|
|
|
void IOProcessor::FramePumpWaitingBlocked()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
auto blocked = this->items.GetBlockedSignals();
|
|
|
|
if (blocked.size())
|
|
|
|
{
|
|
|
|
this->items.workSignaled.insert(this->items.workSignaled.end(), blocked.begin(), blocked.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 01:14:51 +00:00
|
|
|
void IOProcessor::FrameStart()
|
|
|
|
{
|
|
|
|
ReportState(EIOProcessorEventStage::eFrameStartOfFrame);
|
|
|
|
this->bFrameStart = true;
|
|
|
|
|
|
|
|
FramePumpWaitingBlocked();
|
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
void IOProcessor::FrameRunThreadIO()
|
|
|
|
{
|
|
|
|
ReportState(EIOProcessorEventStage::eFrameYieldIO);
|
|
|
|
while (IO::IOYield());
|
|
|
|
ReportState(EIOProcessorEventStage::eFrameIODone);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 IOProcessor::SetRefreshRate(AuUInt64 ns)
|
|
|
|
{
|
|
|
|
if (!CheckThread())
|
|
|
|
{
|
|
|
|
AU_THROW_STRING("Wrong Thread");
|
|
|
|
}
|
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
bool bBinaryCanged = bool(ns) != bool(this->refreshRateNs);
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
auto old = AuExchange(this->refreshRateNs, ns);
|
|
|
|
|
|
|
|
UpdateTimers();
|
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
if (bBinaryCanged)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (ns)
|
|
|
|
{
|
|
|
|
AddTimer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RemoveTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::HasRefreshRate()
|
|
|
|
{
|
|
|
|
return bool(this->refreshRateNs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::MultiplexRefreshRateWithIOEvents()
|
|
|
|
{
|
|
|
|
return this->mutliplexIOAndTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 IOProcessor::GetOwnedThreadId()
|
|
|
|
{
|
|
|
|
return this->threadId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::UpdateTimers()
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
this->timers.pLsTicker->UpdateTickRateIfAnyNs(this->refreshRateNs);
|
2022-06-11 23:01:27 +00:00
|
|
|
this->timers.nbTicker.nsTimeStep = this->refreshRateNs;
|
|
|
|
|
|
|
|
if (!this->timers.nbTicker.nextTriggerTime)
|
|
|
|
{
|
2023-04-22 21:58:20 +00:00
|
|
|
this->timers.nbTicker.nextTriggerTime = AuTime::SteadyClockNS() + this->timers.nbTicker.nsTimeStep;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::AddTimer()
|
|
|
|
{
|
|
|
|
if (IsAsync())
|
|
|
|
{
|
|
|
|
StartAsyncTimerIfAny();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddTimerLS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::AddTimerLS()
|
|
|
|
{
|
2023-09-15 17:08:02 +00:00
|
|
|
if (auto optWorker = this->asyncWorker)
|
2023-08-09 02:21:14 +00:00
|
|
|
{
|
2023-09-15 17:08:02 +00:00
|
|
|
AuAtomicAdd(&AuStaticCast<AuAsync::ThreadPool>(optWorker.value().pool)->uAtomicIOProcessorsWorthlessSources, 1u);
|
2023-08-09 02:21:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
this->ToQueue()->SourceAdd(this->timers.pLsTicker);
|
|
|
|
this->ToQueue()->AddCallback(this->timers.pLsTicker, AuSPtr<AuLoop::ILoopSourceSubscriber>(AuSharedFromThis(), &this->timers));
|
2022-06-11 23:01:27 +00:00
|
|
|
this->ToQueue()->Commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::StartAsyncTimerIfAny()
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (!this->pWorkItem)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2023-09-15 17:08:02 +00:00
|
|
|
this->pWorkItem = this->asyncWorker.value().pool->NewWorkItem(this->asyncWorker.value(), AuSharedFromThis());
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
this->pWorkItem->SetSchedTimeNs(this->refreshRateNs);
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
this->pWorkItem->Dispatch();
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::RemoveTimer()
|
|
|
|
{
|
|
|
|
if (IsAsync())
|
|
|
|
{
|
|
|
|
CancelWorkItem();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RemoveLSTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::CancelWorkItem()
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (!this->pWorkItem)
|
2022-06-14 01:14:51 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
this->pWorkItem->Cancel();
|
|
|
|
this->pWorkItem.reset();
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::RemoveLSTimer()
|
|
|
|
{
|
2022-06-14 01:14:51 +00:00
|
|
|
auto queue = this->ToQueue();
|
|
|
|
if (!queue)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
queue->SourceRemove(this->timers.pLsTicker);
|
2023-08-09 02:21:14 +00:00
|
|
|
|
2023-09-15 17:08:02 +00:00
|
|
|
if (auto optWorker = this->asyncWorker)
|
2023-08-09 02:21:14 +00:00
|
|
|
{
|
2023-09-15 17:08:02 +00:00
|
|
|
AuAtomicSub(&AuStaticCast<AuAsync::ThreadPool>(optWorker.value().pool)->uAtomicIOProcessorsWorthlessSources, 1u);
|
2023-08-09 02:21:14 +00:00
|
|
|
}
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::IsAsync()
|
|
|
|
{
|
2023-09-15 17:08:02 +00:00
|
|
|
return bool(this->asyncWorker);
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::IsTickOnly()
|
|
|
|
{
|
|
|
|
return !this->mutliplexIOAndTimer &&
|
|
|
|
this->timers.nbTicker.nextTriggerTime;
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
bool IOProcessor::RequestRemovalForItemFromAnyThread(const AuSPtr<IIOProcessorItem> &pProcessor)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
AuSPtr<IIOProcessorItem> IOProcessor::StartIOWatchEx(const AuSPtr<IIOWaitableItem> &pItem,
|
|
|
|
const AuSPtr<IIOEventListener> &pListener,
|
|
|
|
bool bSingleshot)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
auto item = AuMakeShared<IOProcessorItem>();
|
2022-11-17 20:56:41 +00:00
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
item->pParent = this;
|
|
|
|
item->pListener = pListener;
|
|
|
|
item->pItem = pItem;
|
|
|
|
item->bSingleshot = bSingleshot;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
AU_LOCK_GUARD(this->items.mutex);
|
|
|
|
|
|
|
|
this->items.allItems.push_back(item);
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (pItem->IsRunOnTick())
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (!AuTryInsert(this->items.onTickReceivers, item))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (pItem->IsRunOnOtherTick())
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (!AuTryInsert(this->items.onOtherReceivers, item))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (pItem->IsRunOnSelfIO())
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
auto src = pItem->GetSelfIOSource();
|
2022-06-11 23:01:27 +00:00
|
|
|
if (!src)
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
auto timeout = pItem->IOTimeoutInMS();
|
2022-06-28 14:57:29 +00:00
|
|
|
if (timeout)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-06-28 14:57:29 +00:00
|
|
|
if (!this->ToQueue()->SourceAddWithTimeout(src, timeout))
|
|
|
|
{
|
|
|
|
SysPushErrorNested("IO Error");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!this->ToQueue()->SourceAdd(src))
|
|
|
|
{
|
|
|
|
SysPushErrorNested("IO Error");
|
|
|
|
return {};
|
|
|
|
}
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 14:57:29 +00:00
|
|
|
if (!this->ToQueue()->AddCallbackEx(src, item))
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNested("IO Error");
|
|
|
|
SysAssert(this->ToQueue()->SourceRemove(src));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->ToQueue()->Commit())
|
|
|
|
{
|
|
|
|
SysPushErrorIO("midframe recommit");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AuTryInsert(this->items.registeredIO, item))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
SysAssert(this->ToQueue()->SourceRemove(src));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
2022-06-28 14:57:29 +00:00
|
|
|
|
|
|
|
AuSPtr<IIOProcessorItem> IOProcessor::StartSimpleIOWatchEx(const AuSPtr<IIOWaitableItem>& object, const AuSPtr<IIOSimpleEventListener>& listener, bool singleshot)
|
2022-06-22 13:42:17 +00:00
|
|
|
{
|
2022-08-28 19:02:06 +00:00
|
|
|
if (!listener)
|
|
|
|
{
|
|
|
|
return this->StartIOWatchEx(object, {}, singleshot);
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
auto adapter = DesimplifyIOEventListenerAdapter(listener);
|
|
|
|
if (!adapter)
|
2022-06-22 13:42:17 +00:00
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
return this->StartIOWatchEx(object, adapter, singleshot);
|
2022-06-22 13:42:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 14:57:29 +00:00
|
|
|
AuSPtr<IIOProcessorItem> IOProcessor::StartSimpleLSWatchEx(const AuSPtr<Loop::ILoopSource>& source, const AuSPtr<IIOSimpleEventListener>& listener, bool singleshot, AuUInt32 msTimeout)
|
2022-06-22 13:42:17 +00:00
|
|
|
{
|
2022-06-28 14:57:29 +00:00
|
|
|
auto sourceAdapter = NewWaitableLoopSourceEx(source, msTimeout);
|
2022-06-22 13:42:17 +00:00
|
|
|
if (!sourceAdapter)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:57:29 +00:00
|
|
|
return this->StartSimpleIOWatchEx(sourceAdapter, listener, singleshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IIOProcessorItem> IOProcessor::StartIOWatch(const AuSPtr<IIOWaitableItem> &object, const AuSPtr<IIOEventListener> &listener)
|
|
|
|
{
|
|
|
|
return StartIOWatchEx(object, listener, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IIOProcessorItem> IOProcessor::StartSimpleIOWatch(const AuSPtr<IIOWaitableItem> &object, const AuSPtr<IIOSimpleEventListener> &listener)
|
|
|
|
{
|
|
|
|
return this->StartSimpleIOWatchEx(object, listener, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IIOProcessorItem> IOProcessor::StartSimpleLSWatch(const AuSPtr<Loop::ILoopSource> &source, const AuSPtr<IIOSimpleEventListener> &listener)
|
|
|
|
{
|
|
|
|
return this->StartSimpleLSWatchEx(source, listener, false, 0);
|
2022-06-22 13:42:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
AuSPtr<IIOPipeProcessor> IOProcessor::ToPipeProcessor()
|
|
|
|
{
|
|
|
|
if (!CheckThread())
|
|
|
|
{
|
|
|
|
AU_THROW_STRING("Wrong Thread");
|
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
return AuSPtr<IIOPipeProcessor>(AuSharedFromThis(), &this->streamProcessors);
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
AuSPtr<AuLoop::ILoopQueue> IOProcessor::ToQueue()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
return this->pLoopQueue;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 15:31:47 +00:00
|
|
|
bool IOProcessor::ConfigureNoDeferredTicks(bool bOption)
|
|
|
|
{
|
|
|
|
return AuExchange(this->bNoDeferredTick, bOption);
|
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
void IOProcessor::ReleaseAllWatches()
|
|
|
|
{
|
2023-09-15 17:08:02 +00:00
|
|
|
if (auto optWorker = this->asyncWorker)
|
2023-08-09 02:21:14 +00:00
|
|
|
{
|
2023-09-15 17:08:02 +00:00
|
|
|
AuAtomicSub(&AuStaticCast<AuAsync::ThreadPool>(optWorker.value().pool)->uAtomicIOProcessors, 1u);
|
2023-08-09 02:21:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 01:14:51 +00:00
|
|
|
RemoveTimer();
|
2023-08-09 02:21:14 +00:00
|
|
|
|
2022-06-14 01:14:51 +00:00
|
|
|
auto queue = ToQueue();
|
2023-08-09 02:21:14 +00:00
|
|
|
|
2022-06-14 01:14:51 +00:00
|
|
|
if (queue)
|
|
|
|
{
|
|
|
|
if (this->items.cvEvent)
|
|
|
|
{
|
|
|
|
queue->SourceRemove(this->items.cvEvent);
|
|
|
|
}
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2022-06-22 13:42:17 +00:00
|
|
|
for (auto &io : this->items.registeredIO)
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
queue->SourceRemove(io->pItem->GetSelfIOSource());
|
2022-06-22 13:42:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 01:14:51 +00:00
|
|
|
queue->Commit();
|
|
|
|
}
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::HasItems()
|
|
|
|
{
|
2022-08-28 19:02:06 +00:00
|
|
|
bool bVal = bool(this->items.allItems.size()) ||
|
|
|
|
bool(this->workUnits.size()) || this->InternalIsTickReady();
|
|
|
|
return bVal;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IOProcessor::ReportState(EIOProcessorEventStage stage)
|
|
|
|
{
|
2023-07-27 15:31:47 +00:00
|
|
|
AU_LOCK_GUARD(this->listenersSpinLock->AsReadable());
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
for (auto &listeners : this->listeners)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
listeners->OnIOProcessorStateEvent(stage);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOProcessor::CheckThread()
|
|
|
|
{
|
|
|
|
if (this->threadId == 0)
|
|
|
|
{
|
|
|
|
this->threadId = AuThreads::GetThreadId();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->threadId == AuThreads::GetThreadId();
|
|
|
|
}
|
2022-11-17 20:56:41 +00:00
|
|
|
|
|
|
|
static AuSPtr<IIOProcessor> NewIOProcessorEx(bool tickOnly, const AuSPtr<AuLoop::ILoopQueue> &queue, bool bIsNoQueue)
|
|
|
|
{
|
2023-09-18 18:04:09 +00:00
|
|
|
auto processor = AuMakeShared<IOProcessor>(0, tickOnly, AuOptionalEx<AuAsync::WorkerPId_t> {}, queue, bIsNoQueue);
|
2023-10-24 16:43:00 +00:00
|
|
|
SysCheckNotNullMemory(processor, {});
|
2022-11-17 20:56:41 +00:00
|
|
|
|
|
|
|
if (!processor->Init())
|
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return processor;
|
|
|
|
}
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
AUKN_SYM AuSPtr<IIOProcessor> NewIOProcessor(bool tickOnly, const AuSPtr<AuLoop::ILoopQueue> &queue)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2023-09-18 18:04:09 +00:00
|
|
|
auto processor = AuMakeShared<IOProcessor>(0, tickOnly, AuOptionalEx<AuAsync::WorkerPId_t> {}, queue, false);
|
2023-10-24 16:43:00 +00:00
|
|
|
SysCheckNotNullMemory(processor, {});
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
if (!processor->Init())
|
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return processor;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IIOProcessor> NewIOProcessorOnThread(bool tickOnly, Async::WorkerPId_t id)
|
|
|
|
{
|
|
|
|
if (!id.pool)
|
|
|
|
{
|
2023-09-18 18:04:09 +00:00
|
|
|
SysPushErrorArg();
|
2022-06-11 23:01:27 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto thread = id.pool->ResolveHandle(id);
|
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric("Worker PID failed to resolve to a thread object");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto queue = id.pool->ToKernelWorkQueue(id);
|
|
|
|
if (!queue)
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric("Worker PID has no kernel work queue");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:56:41 +00:00
|
|
|
auto processor = AuMakeShared<IOProcessor>(AuUInt(thread.get()), tickOnly, id, queue, false);
|
2023-10-24 16:43:00 +00:00
|
|
|
SysCheckNotNullMemory(processor, {});
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
if (!processor->Init())
|
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return processor;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IIOProcessor> NewIOProcessorNoQueue(bool tickOnly)
|
|
|
|
{
|
|
|
|
auto loop = AuLoop::NewLoopQueue();
|
2023-10-24 16:43:00 +00:00
|
|
|
SysCheckNotNull(loop, {});
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2022-11-17 20:56:41 +00:00
|
|
|
return NewIOProcessorEx(tickOnly, loop, true);
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
}
|