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-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,
|
|
|
|
const AuSPtr<IWorkItemHandler> &task,
|
|
|
|
bool bSupportsBlocking);
|
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();
|
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);
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
AuSPtr<IWorkItemHandler> task_;
|
2023-01-23 21:18:58 +00:00
|
|
|
WorkerPId_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;
|
2023-02-08 18:23:37 +00:00
|
|
|
AuThreadPrimitives::SpinLock lock2;
|
2021-10-24 10:19:47 +00:00
|
|
|
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();
|
2022-08-13 21:59:44 +00:00
|
|
|
bool Schedule();
|
2021-06-27 21:25:29 +00:00
|
|
|
void SendOff();
|
|
|
|
};
|
|
|
|
}
|