AuroraRuntime/Source/Async/WorkItem.hpp
2024-02-28 14:47:00 +00:00

108 lines
3.5 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: WorkItem.hpp
Date: 2021-6-26
Author: Reece
***/
#pragma once
#include "ThreadPool.hpp"
#include "IAsyncRunnable.hpp"
namespace Aurora::Async
{
struct ThreadState;
struct WorkItem :
IWorkItem,
IAsyncRunnable,
AuEnableSharedFromThis<WorkItem>
{
WorkItem(IThreadPoolInternal *owner,
AuOptional<WorkerPId_t> worker,
const AuSPtr<IWorkItemHandler> &task);
~WorkItem();
AuSPtr<IWorkItem> WaitFor(const AuSPtr<IWorkItem> &workItem) override;
AuSPtr<IWorkItem> WaitFor(const AuList<AuSPtr<IWorkItem>> &workItem) override;
AuSPtr<IWorkItem> SetSchedTime(AuUInt32 ms) override;
AuSPtr<IWorkItem> SetSchedTimeNs(AuUInt64 ns) override;
AuSPtr<IWorkItem> AddDelayTime(AuUInt32 ms) override;
AuSPtr<IWorkItem> AddDelayTimeNs(AuUInt64 ns) override;
AuSPtr<IWorkItem> AddDelayTimeRepeating(AuUInt32 ms) override;
AuSPtr<IWorkItem> AddDelayTimeNsRepeating(AuUInt64 ns) override;
AuSPtr<IWorkItem> SetSchedTimeAbs(AuUInt32 ms) override;
AuSPtr<IWorkItem> SetSchedTimeNsAbs(AuUInt64 ns) override;
AuSPtr<IWorkItem> SetSchedSteadyTimeNsAbs(AuUInt64 ns) override;
AuSPtr<IWorkItem> SetSchedByLoopSourceOnce(const AuSPtr<IO::Loop::ILoopSource> &pLoopSource) override;
AuSPtr<IWorkItem> SetSchedByLoopSourceRepeating(const AuSPtr<IO::Loop::ILoopSource> &pLoopSource) override;
AuSPtr<IWorkItem> Then(const AuSPtr<IWorkItem> &next) override;
AuSPtr<IWorkItem> Dispatch() override;
bool BlockUntilComplete() override;
bool HasFinished() override;
bool HasFinishedRepeatable();
bool HasFailed() override;
void Cancel() override;
void RunAsync() override;
void CancelAsync() override;
void *GetPrivateData() override;
EWorkPriority GetPrio() override;
void SetPrio(EWorkPriority prio) override;
AuOptional<AuPair<AuUInt32, AuUInt32>> QueryFences() override;
protected:
bool CheckAlive();
virtual void DispatchTask(IWorkItemHandler::ProcessInfo &info);
virtual void Cleanup();
private:
void RunAsyncLocked();
void RunAsyncLocked2();
void RunAsyncLocked2(const IWorkItemHandler::ProcessInfo &info);
bool WaitForLocked(const AuList<AuSPtr<IWorkItem>> &workItem);
void DispatchEx(bool check, bool bIsIOTick = false);
void DispatchExLocked(bool check, bool bIsIOTick = false);
IThreadPoolInternal *owner_ {};
AuSPtr<ThreadState> GetState();
AuSPtr<IWorkItemHandler> task_;
AuOptionalEx<WorkerPId_t> worker_;
EWorkPriority prio_ = EWorkPriority::ePriorityNormal;
AuList<AuSPtr<IWorkItem>> waitOn_;
AuList<AuSPtr<IWorkItem>> waiters_;
AuCriticalSection lock;
AuCriticalSection lock2;
AuEvent finishedEvent_;
AuUInt32 uShutdownCookie {};
AuOptionalEx<AuUInt32> optOtherCookie {};
AuSPtr<AuIO::IIOProcessorItem> pIOWatch;
AuSPtr<AuIO::Loop::ILoopSource> pIOWatchLS;
bool finished {};
bool finished2 {};
bool failed {};
bool bIoRepeating {};
bool bRepeatingTimer {};
bool dispatchPending_ {};
AuUInt32 ioTickCount {};
AuUInt64 dispatchTimeNs_ {};
AuUInt64 delayTimeNs_ {};
void Fail();
bool Schedule();
void SendOff();
};
}