AuroraRuntime/Source/Async/WorkItem.hpp

75 lines
2.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
{
class WorkItem : public IWorkItem, public IAsyncRunnable, public AuEnableSharedFromThis<WorkItem>
2021-06-27 21:25:29 +00:00
{
public:
WorkItem(IThreadPoolInternal *owner, const WorkerId_t &worker_, const AuSPtr<IWorkItemHandler> &task_, bool supportsBlocking);
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;
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;
AuOptional<void *> ToWorkResultT() override;
float GetPrio() override;
void SetPrio(float val) override;
2021-06-30 09:28:52 +00:00
private:
void RunAsyncLocked();
bool WaitForLocked(const AuList<AuSPtr<IWorkItem>> &workItem);
2021-06-27 21:25:29 +00:00
void DispatchEx(bool check);
void DispatchExLocked(bool check);
2021-06-27 21:25:29 +00:00
AuSPtr<IWorkItemHandler> task_;
WorkerId_t worker_;
float prio_ = 0.5f;
2021-06-27 21:25:29 +00:00
AuList<AuSPtr<IWorkItem>> waitOn_;
AuList<AuSPtr<IWorkItem>> waiters_;
AuThreadPrimitives::SpinLock lock;
AuThreadPrimitives::EventUnique_t finishedEvent_;
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_ {};
IThreadPoolInternal *owner_ {};
2021-06-27 21:25:29 +00:00
void Fail();
void Schedule();
void SendOff();
};
}