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
|
|
|
|
{
|
2022-01-26 00:01:06 +00:00
|
|
|
class WorkItem : public IWorkItem, public IAsyncRunnable, public AuEnableSharedFromThis<WorkItem>
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-11-05 17:34:23 +00:00
|
|
|
WorkItem(IThreadPoolInternal *owner, const WorkerId_t &worker_, const AuSPtr<IWorkItemHandler> &task_, bool supportsBlocking);
|
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;
|
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;
|
|
|
|
AuOptional<void *> ToWorkResultT() override;
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
float GetPrio() override;
|
|
|
|
void SetPrio(float val) override;
|
|
|
|
|
2021-06-30 09:28:52 +00:00
|
|
|
private:
|
2022-05-17 01:43:26 +00:00
|
|
|
void RunAsyncLocked();
|
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);
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
AuSPtr<IWorkItemHandler> task_;
|
2021-09-30 09:19:04 +00:00
|
|
|
WorkerId_t worker_;
|
2021-11-05 17:34:23 +00:00
|
|
|
float prio_ = 0.5f;
|
2021-06-27 21:25:29 +00:00
|
|
|
AuList<AuSPtr<IWorkItem>> waitOn_;
|
|
|
|
AuList<AuSPtr<IWorkItem>> waiters_;
|
2021-10-24 10:19:47 +00:00
|
|
|
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_ {};
|
2021-07-15 16:16:23 +00:00
|
|
|
AuUInt64 delayTimeNs_ {};
|
2021-11-05 17:34:23 +00:00
|
|
|
IThreadPoolInternal *owner_ {};
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
void Fail();
|
|
|
|
void Schedule();
|
|
|
|
void SendOff();
|
|
|
|
};
|
|
|
|
}
|