AuroraRuntime/Source/Async/WorkItem.hpp

100 lines
3.1 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,
const 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> SetSchedTimeAbs(AuUInt32 ms) override;
AuSPtr<IWorkItem> SetSchedTimeNsAbs(AuUInt64 ns) override;
AuSPtr<IWorkItem> SetSchedSteadyTimeNsAbs(AuUInt64 ns) override;
AuSPtr<IWorkItem> SetSchedByLoopSource(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 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);
void DispatchExLocked(bool check);
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 failed {};
bool dispatchPending_ {};
AuUInt64 dispatchTimeNs_ {};
AuUInt64 delayTimeNs_ {};
void Fail();
bool Schedule();
void SendOff();
};
}