AuroraRuntime/Source/Async/WorkItem.hpp

109 lines
3.3 KiB
C++
Raw Normal View History

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: WorkItem.hpp
Date: 2021-6-26
Author: Reece
***/
2021-06-27 21:25:29 +00:00
#pragma once
#include "ThreadPool.hpp"
2021-06-27 21:25:29 +00:00
namespace Aurora::Async
{
struct ThreadState;
struct WorkItem :
IWorkItem,
IAsyncRunnable,
AuEnableSharedFromThis<WorkItem>
2021-06-27 21:25:29 +00:00
{
WorkItem(IThreadPoolInternal *owner,
const WorkerPId_t &worker,
const AuSPtr<IWorkItemHandler> &task);
2021-06-27 21:25:29 +00:00
~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;
2021-07-12 14:37:05 +00:00
AuSPtr<IWorkItem> Then(const AuSPtr<IWorkItem> &next) override;
2021-06-27 21:25:29 +00:00
AuSPtr<IWorkItem> Dispatch() override;
2021-06-27 21:25:29 +00:00
bool BlockUntilComplete() override;
bool HasFinished() override;
bool HasFailed() override;
2021-09-29 08:01:42 +00:00
void Cancel() override;
2021-06-30 09:28:52 +00:00
void RunAsync() override;
void CancelAsync() override;
2021-06-27 21:25:29 +00:00
void *GetPrivateData() override;
EWorkPrio GetPrio() override;
void SetPrio(EWorkPrio prio) override;
AuOptional<AuPair<AuUInt32, AuUInt32>> QueryFences();
protected:
bool CheckAlive();
2021-06-30 09:28:52 +00:00
private:
void RunAsyncLocked();
void RunAsyncLocked2();
void RunAsyncLocked2(const IWorkItemHandler::ProcessInfo &info);
bool WaitForLocked(const AuList<AuSPtr<IWorkItem>> &workItem);
2021-06-27 21:25:29 +00:00
void DispatchEx(bool check);
void DispatchExLocked(bool check);
virtual void DispatchTask(IWorkItemHandler::ProcessInfo &info);
IThreadPoolInternal *owner_ {};
AuSPtr<ThreadState> GetState();
2021-06-27 21:25:29 +00:00
AuSPtr<IWorkItemHandler> task_;
2023-09-22 05:05:39 +00:00
AuOptionalEx<WorkerPId_t> worker_;
EWorkPrio prio_ = EWorkPrio::eNormalPrio;
2021-06-27 21:25:29 +00:00
AuList<AuSPtr<IWorkItem>> waitOn_;
AuList<AuSPtr<IWorkItem>> waiters_;
AuThreadPrimitives::CriticalSection lock;
AuThreadPrimitives::CriticalSection lock2;
AuThreadPrimitives::Event finishedEvent_;
AuUInt32 uShutdownCookie {};
AuOptionalEx<AuUInt32> optOtherCookie {};
AuSPtr<AuIO::IIOProcessorItem> pIOWatch;
AuSPtr<IO::Loop::ILoopSource> pIOWatchLS;
2021-06-27 21:25:29 +00:00
bool finished {};
bool failed {};
bool dispatchPending_ {};
2021-06-30 12:00:32 +00:00
AuUInt64 dispatchTimeNs_ {};
AuUInt64 delayTimeNs_ {};
2021-06-27 21:25:29 +00:00
void Fail();
bool Schedule();
2021-06-27 21:25:29 +00:00
void SendOff();
};
struct FuncWorker : WorkItem
{
FuncWorker(IThreadPoolInternal *owner,
const WorkerPId_t &worker,
AuVoidFunc &&func);
void DispatchTask(IWorkItemHandler::ProcessInfo &info) override;
private:
AuVoidFunc func;
};
2021-06-27 21:25:29 +00:00
}