2021-06-27 21:33:58 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: WorkItem.cpp
|
|
|
|
Date: 2021-6-26
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-30 09:28:52 +00:00
|
|
|
#include "Async.hpp"
|
2021-06-27 21:25:29 +00:00
|
|
|
#include "WorkItem.hpp"
|
2021-06-30 09:28:52 +00:00
|
|
|
#include "AsyncApp.hpp"
|
2021-06-30 12:00:32 +00:00
|
|
|
#include "Schedular.hpp"
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
namespace Aurora::Async
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
WorkItem::WorkItem(IThreadPoolInternal *owner, const WorkerId_t &worker, const AuSPtr<IWorkItemHandler> &task, bool supportsBlocking) : worker_(worker), task_(task), owner_(owner)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
if (supportsBlocking)
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->finishedEvent_ = AuThreadPrimitives::EventUnique(false, true, true);
|
|
|
|
SysAssert(this->finishedEvent_);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkItem::~WorkItem()
|
|
|
|
{
|
2021-06-30 09:28:52 +00:00
|
|
|
//Fail();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 20:32:59 +00:00
|
|
|
AuSPtr<IWorkItem> WorkItem::WaitFor(const AuSPtr<IWorkItem> &workItem)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-06-30 09:41:58 +00:00
|
|
|
bool status {};
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
{
|
2022-02-19 11:43:57 +00:00
|
|
|
auto dependency = AuReinterpretCast<WorkItem>(workItem);
|
2021-06-30 09:41:58 +00:00
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
AU_LOCK_GUARD(this->lock);
|
2021-09-06 10:58:08 +00:00
|
|
|
AU_LOCK_GUARD(dependency->lock);
|
2021-06-30 09:34:01 +00:00
|
|
|
|
|
|
|
if (dependency->HasFailed())
|
|
|
|
{
|
2021-06-30 09:41:58 +00:00
|
|
|
status = true;
|
2021-06-30 09:34:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
dependency->waiters_.push_back(shared_from_this());
|
2021-11-05 17:34:23 +00:00
|
|
|
this->waitOn_.push_back(workItem);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-06-30 09:41:58 +00:00
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
Fail();
|
|
|
|
}
|
2021-07-07 20:32:59 +00:00
|
|
|
|
|
|
|
return AU_SHARED_FROM_THIS;
|
2021-06-30 09:41:58 +00:00
|
|
|
}
|
2021-07-12 14:37:05 +00:00
|
|
|
|
2021-07-07 20:32:59 +00:00
|
|
|
AuSPtr<IWorkItem> WorkItem::WaitFor(const AuList<AuSPtr<IWorkItem>> &workItems)
|
2021-06-30 09:41:58 +00:00
|
|
|
{
|
|
|
|
bool status {};
|
|
|
|
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
AU_LOCK_GUARD(this->lock);
|
2021-06-30 09:41:58 +00:00
|
|
|
|
|
|
|
for (auto &workItem : workItems)
|
|
|
|
{
|
2022-02-19 11:50:14 +00:00
|
|
|
auto dependency = AuReinterpretCast<WorkItem>(workItem);
|
2021-09-06 10:58:08 +00:00
|
|
|
AU_LOCK_GUARD(dependency->lock);
|
2021-06-30 09:41:58 +00:00
|
|
|
|
|
|
|
if (dependency->HasFailed())
|
|
|
|
{
|
|
|
|
status = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
dependency->waiters_.push_back(shared_from_this());
|
2021-11-05 17:34:23 +00:00
|
|
|
this->waitOn_.push_back(workItem);
|
2021-06-30 09:41:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
Fail();
|
|
|
|
}
|
2021-07-07 20:32:59 +00:00
|
|
|
|
|
|
|
return AU_SHARED_FROM_THIS;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-07-12 14:37:05 +00:00
|
|
|
|
|
|
|
AuSPtr<IWorkItem> WorkItem::Then(const AuSPtr<IWorkItem> &next)
|
|
|
|
{
|
|
|
|
auto that = AU_SHARED_FROM_THIS;
|
|
|
|
next->WaitFor(that);
|
|
|
|
next->Dispatch();
|
|
|
|
return that;
|
|
|
|
}
|
2021-06-30 12:00:32 +00:00
|
|
|
|
2021-07-07 20:32:59 +00:00
|
|
|
AuSPtr<IWorkItem> WorkItem::SetSchedTimeNs(AuUInt64 ns)
|
2021-06-30 12:00:32 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->dispatchTimeNs_ = Time::CurrentClockNS() + ns;
|
|
|
|
return AU_SHARED_FROM_THIS;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IWorkItem> WorkItem::SetSchedTimeAbs(AuUInt32 ms)
|
|
|
|
{
|
|
|
|
this->dispatchTimeNs_ = AuUInt64(ms) * AuUInt64(1000000);
|
|
|
|
return AU_SHARED_FROM_THIS;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IWorkItem> WorkItem::SetSchedTimeNsAbs(AuUInt64 ns)
|
|
|
|
{
|
|
|
|
this->dispatchTimeNs_ = ns;
|
2021-07-07 20:32:59 +00:00
|
|
|
return AU_SHARED_FROM_THIS;
|
2021-06-30 12:00:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 20:32:59 +00:00
|
|
|
AuSPtr<IWorkItem> WorkItem::SetSchedTime(AuUInt32 ms)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->dispatchTimeNs_ = Time::CurrentClockNS() + (AuUInt64(ms) * AuUInt64(1000000));
|
2021-07-07 20:32:59 +00:00
|
|
|
return AU_SHARED_FROM_THIS;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-07-15 16:16:23 +00:00
|
|
|
|
|
|
|
AuSPtr<IWorkItem> WorkItem::AddDelayTime(AuUInt32 ms)
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->delayTimeNs_ += AuUInt64(ms) * AuUInt64(1000000);
|
2021-07-15 16:16:23 +00:00
|
|
|
return AU_SHARED_FROM_THIS;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IWorkItem> WorkItem::AddDelayTimeNs(AuUInt64 ns)
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->delayTimeNs_ += ns;
|
2021-07-15 16:16:23 +00:00
|
|
|
return AU_SHARED_FROM_THIS;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-07-07 20:32:59 +00:00
|
|
|
AuSPtr<IWorkItem> WorkItem::Dispatch()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
DispatchEx(false);
|
2021-07-07 20:32:59 +00:00
|
|
|
return AU_SHARED_FROM_THIS;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkItem::DispatchEx(bool check)
|
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
AU_LOCK_GUARD(lock);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
if (check)
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
if (this->dispatchPending_)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 09:34:01 +00:00
|
|
|
if (HasFailed())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
for (auto itr = waitOn_.begin(); itr != waitOn_.end(); )
|
|
|
|
{
|
|
|
|
auto &waitable = *itr;
|
|
|
|
|
|
|
|
if (!waitable->HasFinished())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
itr = waitOn_.erase(itr);
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
this->dispatchPending_ = true;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
if (Time::CurrentClockNS() < this->dispatchTimeNs_)
|
2021-07-15 16:16:23 +00:00
|
|
|
{
|
|
|
|
Schedule();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
if (auto delay = AuExchange(delayTimeNs_, {}))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->dispatchTimeNs_ = delay + Time::CurrentClockNS();
|
2021-06-27 21:25:29 +00:00
|
|
|
Schedule();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SendOff();
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
float WorkItem::GetPrio()
|
|
|
|
{
|
|
|
|
return prio_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkItem::SetPrio(float val)
|
|
|
|
{
|
|
|
|
prio_ = val;
|
|
|
|
}
|
|
|
|
|
2021-06-30 09:28:52 +00:00
|
|
|
void WorkItem::CancelAsync()
|
|
|
|
{
|
2022-01-19 02:49:44 +00:00
|
|
|
AU_TRY_LOCK_GUARD_NAMED(this->lock, asd);
|
2021-06-30 09:28:52 +00:00
|
|
|
Fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkItem::RunAsync()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
AU_LOCK_GUARD(this->lock);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
IWorkItemHandler::ProcessInfo info(true);
|
2021-11-05 17:34:23 +00:00
|
|
|
info.pool = this->owner_->ToThreadPool();
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
if (this->task_)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->task_->DispatchFrame(info);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
switch (info.type)
|
|
|
|
{
|
|
|
|
case IWorkItemHandler::EProcessNext::eFinished:
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IWorkItemHandler::EProcessNext::eInvalid:
|
|
|
|
{
|
|
|
|
SysPanic("Handle Invalid");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IWorkItemHandler::EProcessNext::eSchedule:
|
|
|
|
{
|
|
|
|
if (info.reschedMs)
|
|
|
|
{
|
|
|
|
SetSchedTime(info.reschedMs);
|
2021-11-08 00:11:01 +00:00
|
|
|
}
|
|
|
|
else if (info.reschedNs)
|
2021-06-30 12:00:32 +00:00
|
|
|
{
|
|
|
|
SetSchedTimeNs(info.reschedNs);
|
|
|
|
}
|
2021-11-08 00:11:01 +00:00
|
|
|
else if (info.reschedClockAbsMs)
|
|
|
|
{
|
|
|
|
SetSchedTimeAbs(info.reschedMs);
|
|
|
|
}
|
|
|
|
else if (info.reschedClockAbsNs)
|
|
|
|
{
|
|
|
|
SetSchedTimeNsAbs(info.reschedNs);
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
WaitFor(info.waitFor);
|
2021-06-30 09:34:01 +00:00
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-06-30 09:34:01 +00:00
|
|
|
[[fallthrough]];
|
2021-06-27 21:25:29 +00:00
|
|
|
case IWorkItemHandler::EProcessNext::eRerun:
|
|
|
|
{
|
|
|
|
DispatchEx(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case IWorkItemHandler::EProcessNext::eFailed:
|
|
|
|
{
|
|
|
|
Fail();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
this->finished = true;
|
|
|
|
if (this->finishedEvent_)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->finishedEvent_->Set();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
for (auto &waiter : this->waiters_)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2022-02-19 11:50:14 +00:00
|
|
|
AuReinterpretCast<WorkItem>(waiter)->DispatchEx(true);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkItem::Fail()
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
if (auto task_ = AuExchange(this->task_, {}))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
task_->Shutdown();
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
for (auto &waiter : this->waiters_)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-02-19 11:50:14 +00:00
|
|
|
AuReinterpretCast<WorkItem>(waiter)->Fail();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
this->waiters_.clear();
|
|
|
|
this->waitOn_.clear();
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
if (this->finishedEvent_)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->finishedEvent_->Set();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkItem::BlockUntilComplete()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
if (!this->finishedEvent_) return false;
|
|
|
|
return this->owner_->WaitFor(this->worker_, AuUnsafeRaiiToShared(this->finishedEvent_), 0);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkItem::HasFinished()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
return this->finished;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 08:01:42 +00:00
|
|
|
void WorkItem::Cancel()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
AU_LOCK_GUARD(this->lock);
|
2021-09-29 08:01:42 +00:00
|
|
|
Fail();
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
bool WorkItem::HasFailed()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
return this->failed;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkItem::Schedule()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
Async::Schedule(this->dispatchTimeNs_, this->owner_, this->worker_, this->shared_from_this());
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkItem::SendOff()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
if (!this->task_)
|
2021-10-06 13:48:28 +00:00
|
|
|
{
|
|
|
|
// If we aren't actually calling a task interface, we may as well just dispatch objects waiting on us from here
|
|
|
|
RunAsync();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
this->owner_->Run(this->worker_, this->shared_from_this());
|
2021-10-06 13:48:28 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
static auto GetWorkerInternal(const AuSPtr<IThreadPool> &pool)
|
|
|
|
{
|
2022-01-19 18:53:22 +00:00
|
|
|
return AuStaticPointerCast<ThreadPool>(pool).get();
|
2021-11-05 17:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static auto GetWorkerInternal()
|
|
|
|
{
|
|
|
|
return static_cast<AsyncApp *>(GetAsyncApp());
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-09-30 09:19:04 +00:00
|
|
|
AUKN_SYM AuSPtr<IWorkItem> NewWorkItem(const WorkerId_t &worker, const AuSPtr<IWorkItemHandler> &task, bool supportsBlocking)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-10-06 13:48:28 +00:00
|
|
|
if (!task)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
return AuMakeShared<WorkItem>(GetWorkerInternal(), worker, task, supportsBlocking);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IWorkItem> NewWorkItem(const WorkerPId_t &worker, const AuSPtr<IWorkItemHandler> &task, bool supportsBlocking)
|
|
|
|
{
|
|
|
|
if (!task)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!worker.pool)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return AuMakeShared<WorkItem>(GetWorkerInternal(), worker, task, supportsBlocking);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-10-06 13:48:28 +00:00
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IWorkItem> NewFence()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
return AuMakeShared<WorkItem>(GetWorkerInternal(), WorkerId_t{}, AuSPtr<IWorkItemHandler>{}, true);
|
2021-10-06 13:48:28 +00:00
|
|
|
}
|
2021-09-29 10:47:54 +00:00
|
|
|
|
|
|
|
void *WorkItem::GetPrivateData()
|
|
|
|
{
|
|
|
|
if (!this->task_)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->task_->GetPrivateData();
|
|
|
|
}
|
|
|
|
|
|
|
|
AuOptional<void *> WorkItem::ToWorkResultT()
|
|
|
|
{
|
|
|
|
if (!this->task_)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto priv = reinterpret_cast<Async::WorkPriv *>(this->task_->GetPrivateData());
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->magic == AuConvertMagicTag32("BWOT"))
|
|
|
|
{
|
|
|
|
|
|
|
|
return reinterpret_cast<Async::BasicWorkCtx *>(priv)->opt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|