AuroraRuntime/Source/Async/WorkItem.hpp
Jamie Reece Wilson 0f12603390 [+] AuAsync::NewWorkFunction
[+] AuAsync::IThreadPool::NewWorkFunction
[+] AU_DEBUG_MEMCRUNCH
2023-08-10 03:36:19 +01:00

96 lines
2.9 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"
namespace Aurora::Async
{
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> 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;
AuOptional<void *> ToWorkResultT() override;
EWorkPrio GetPrio() override;
void SetPrio(EWorkPrio prio) override;
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);
virtual void DispatchTask(IWorkItemHandler::ProcessInfo &info);
AuSPtr<IWorkItemHandler> task_;
WorkerPId_t worker_;
EWorkPrio prio_ = EWorkPrio::eNormalPrio;
AuList<AuSPtr<IWorkItem>> waitOn_;
AuList<AuSPtr<IWorkItem>> waiters_;
AuThreadPrimitives::CriticalSection lock;
AuThreadPrimitives::CriticalSection lock2;
AuThreadPrimitives::Event finishedEvent_;
bool finished {};
bool failed {};
bool dispatchPending_ {};
AuUInt64 dispatchTimeNs_ {};
AuUInt64 delayTimeNs_ {};
IThreadPoolInternal *owner_ {};
void Fail();
bool Schedule();
void SendOff();
};
struct FuncWorker : WorkItem
{
FuncWorker(IThreadPoolInternal *owner,
const WorkerPId_t &worker,
AuVoidFunc &&func);
void DispatchTask(IWorkItemHandler::ProcessInfo &info) override;
private:
AuVoidFunc func;
};
}