AuroraRuntime/Source/Async/WorkItem.hpp

50 lines
1.4 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
namespace Aurora::Async
{
class WorkItem : public IWorkItem, public IAsyncRunnable, public std::enable_shared_from_this<WorkItem>
{
public:
WorkItem(const DispatchTarget_t &worker_, const AuSPtr<IWorkItemHandler> &task_, bool supportsBlocking);
~WorkItem();
void WaitFor(const AuSPtr<IWorkItem> &workItem) override;
void WaitFor(const AuList<AuSPtr<IWorkItem>> &workItem) override;
void SetSchedTime(AuUInt32 ms) override;
void Dispatch() override;
bool BlockUntilComplete() override;
bool HasFinished() override;
bool HasFailed() override;
void RunAsync() override;
void CancelAsync() override;
private:
void DispatchEx(bool check);
AuSPtr<IWorkItemHandler> task_;
DispatchTarget_t worker_;
AuList<AuSPtr<IWorkItem>> waitOn_;
AuList<AuSPtr<IWorkItem>> waiters_;
Threading::Primitives::SpinLock lock;
Threading::Primitives::EventUnique_t finishedEvent_;
bool finished {};
bool failed {};
bool dispatchPending_ {};
AuUInt32 dispatchTime_ {};
void Fail();
void Schedule();
void SendOff();
};
}