2021-06-27 21:33:58 +00:00
|
|
|
/***
|
|
|
|
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
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
#include "ThreadPool.hpp"
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
namespace Aurora::Async
|
|
|
|
{
|
2023-10-24 14:33:30 +00:00
|
|
|
struct ThreadState;
|
|
|
|
|
2023-01-23 21:18:58 +00:00
|
|
|
struct WorkItem :
|
|
|
|
IWorkItem,
|
|
|
|
IAsyncRunnable,
|
|
|
|
AuEnableSharedFromThis<WorkItem>
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-01-23 21:18:58 +00:00
|
|
|
WorkItem(IThreadPoolInternal *owner,
|
|
|
|
const WorkerPId_t &worker,
|
2023-08-10 02:34:44 +00:00
|
|
|
const AuSPtr<IWorkItemHandler> &task);
|
2021-06-27 21:25:29 +00:00
|
|
|
~WorkItem();
|
|
|
|
|
2021-07-07 20:32:59 +00:00
|
|
|
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;
|
2021-07-15 16:16:23 +00:00
|
|
|
AuSPtr<IWorkItem> AddDelayTime(AuUInt32 ms) override;
|
|
|
|
AuSPtr<IWorkItem> AddDelayTimeNs(AuUInt64 ns) override;
|
2021-11-05 17:34:23 +00:00
|
|
|
AuSPtr<IWorkItem> SetSchedTimeAbs(AuUInt32 ms) override;
|
|
|
|
AuSPtr<IWorkItem> SetSchedTimeNsAbs(AuUInt64 ns) override;
|
2023-08-09 02:21:14 +00:00
|
|
|
AuSPtr<IWorkItem> SetSchedSteadyTimeNsAbs(AuUInt64 ns) override;
|
2023-12-01 09:22:51 +00:00
|
|
|
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
|
|
|
|
2021-07-07 20:32:59 +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
|
|
|
|
2021-09-29 10:47:54 +00:00
|
|
|
|
|
|
|
void *GetPrivateData() override;
|
|
|
|
|
2023-05-25 00:55:55 +00:00
|
|
|
EWorkPrio GetPrio() override;
|
|
|
|
void SetPrio(EWorkPrio prio) override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2023-10-24 14:33:30 +00:00
|
|
|
AuOptional<AuPair<AuUInt32, AuUInt32>> QueryFences();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool CheckAlive();
|
|
|
|
|
2021-06-30 09:28:52 +00:00
|
|
|
private:
|
2022-05-17 01:43:26 +00:00
|
|
|
void RunAsyncLocked();
|
2023-02-08 18:23:37 +00:00
|
|
|
void RunAsyncLocked2();
|
|
|
|
void RunAsyncLocked2(const IWorkItemHandler::ProcessInfo &info);
|
2022-05-16 23:41:27 +00:00
|
|
|
bool WaitForLocked(const AuList<AuSPtr<IWorkItem>> &workItem);
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
void DispatchEx(bool check);
|
2022-05-16 23:41:27 +00:00
|
|
|
void DispatchExLocked(bool check);
|
|
|
|
|
2023-08-10 02:34:44 +00:00
|
|
|
virtual void DispatchTask(IWorkItemHandler::ProcessInfo &info);
|
|
|
|
|
2023-10-24 14:33:30 +00:00
|
|
|
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_;
|
2023-05-25 00:55:55 +00:00
|
|
|
EWorkPrio prio_ = EWorkPrio::eNormalPrio;
|
2021-06-27 21:25:29 +00:00
|
|
|
AuList<AuSPtr<IWorkItem>> waitOn_;
|
|
|
|
AuList<AuSPtr<IWorkItem>> waiters_;
|
2023-05-25 00:55:55 +00:00
|
|
|
AuThreadPrimitives::CriticalSection lock;
|
|
|
|
AuThreadPrimitives::CriticalSection lock2;
|
|
|
|
AuThreadPrimitives::Event finishedEvent_;
|
2023-10-24 14:33:30 +00:00
|
|
|
AuUInt32 uShutdownCookie {};
|
|
|
|
AuOptionalEx<AuUInt32> optOtherCookie {};
|
2023-12-01 09:22:51 +00:00
|
|
|
AuSPtr<AuIO::IIOProcessorItem> pIOWatch;
|
|
|
|
AuSPtr<IO::Loop::ILoopSource> pIOWatchLS;
|
2023-10-24 14:33:30 +00:00
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
bool finished {};
|
|
|
|
bool failed {};
|
|
|
|
bool dispatchPending_ {};
|
2021-06-30 12:00:32 +00:00
|
|
|
AuUInt64 dispatchTimeNs_ {};
|
2021-07-15 16:16:23 +00:00
|
|
|
AuUInt64 delayTimeNs_ {};
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
void Fail();
|
2022-08-13 21:59:44 +00:00
|
|
|
bool Schedule();
|
2021-06-27 21:25:29 +00:00
|
|
|
void SendOff();
|
|
|
|
};
|
2023-08-10 02:34:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|