From f6437ef66e39a3c7401251287091eb84dda5019f Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Fri, 22 Sep 2023 06:05:39 +0100 Subject: [PATCH] [-] Engine 0.0/1.0 async --- Include/Aurora/Async/Async.hpp | 9 - Include/Aurora/Async/AuFutures.hpp | 4 +- Include/Aurora/Async/IWorkItem.hpp | 1 - Include/Aurora/Async/Legacy/DO NOT REMOVE.txt | 5 - Include/Aurora/Async/Legacy/JobFrom.hpp | 184 -------------- Include/Aurora/Async/Legacy/Jobs.hpp | 33 --- Include/Aurora/Async/Legacy/OldTrash.hpp | 146 ----------- Include/Aurora/Async/Legacy/TaskFrom.hpp | 86 ------- Include/Aurora/Async/Legacy/Tasks.hpp | 47 ---- Include/Aurora/Async/Legacy/WorkBasic.hpp | 121 ---------- Include/Aurora/Async/Legacy/WorkPairImpl.hpp | 226 ------------------ Source/Async/Schedular.cpp | 2 +- Source/Async/ThreadPool.cpp | 7 +- Source/Async/WorkItem.cpp | 52 +--- Source/Async/WorkItem.hpp | 3 +- Source/Console/Commands/Commands.cpp | 2 +- 16 files changed, 18 insertions(+), 910 deletions(-) delete mode 100755 Include/Aurora/Async/Legacy/DO NOT REMOVE.txt delete mode 100644 Include/Aurora/Async/Legacy/JobFrom.hpp delete mode 100644 Include/Aurora/Async/Legacy/Jobs.hpp delete mode 100644 Include/Aurora/Async/Legacy/OldTrash.hpp delete mode 100644 Include/Aurora/Async/Legacy/TaskFrom.hpp delete mode 100644 Include/Aurora/Async/Legacy/Tasks.hpp delete mode 100644 Include/Aurora/Async/Legacy/WorkBasic.hpp delete mode 100644 Include/Aurora/Async/Legacy/WorkPairImpl.hpp diff --git a/Include/Aurora/Async/Async.hpp b/Include/Aurora/Async/Async.hpp index 48b60b37..cc28cb34 100644 --- a/Include/Aurora/Async/Async.hpp +++ b/Include/Aurora/Async/Async.hpp @@ -14,9 +14,6 @@ #include "IThreadPool.hpp" #include "IAsyncApp.hpp" -#include "Legacy/Jobs.hpp" -#include "Legacy/Tasks.hpp" - namespace Aurora::Async { AUKN_SYM IAsyncApp * GetAsyncApp(); @@ -59,10 +56,4 @@ namespace Aurora::Async #if !defined(_CPPSHARP) #include "IPCPromises.hpp" - - #include "Legacy/JobFrom.hpp" - #include "Legacy/TaskFrom.hpp" - #include "Legacy/WorkPairImpl.hpp" - #include "Legacy/WorkBasic.hpp" - #include "Legacy/OldTrash.hpp" #endif diff --git a/Include/Aurora/Async/AuFutures.hpp b/Include/Aurora/Async/AuFutures.hpp index fa3d7035..e25a0650 100644 --- a/Include/Aurora/Async/AuFutures.hpp +++ b/Include/Aurora/Async/AuFutures.hpp @@ -265,11 +265,11 @@ private: return; } - AuAsync::NewWorkItem(this->pid.value(), AuMakeSharedPanic([pThat = this->SharedFromThis()] + AuAsync::DispatchOn(this->pid.value(), [pThat = this->SharedFromThis()] { AU_LOCK_GUARD(pThat->mutex); pThat->SubmitComplete(); - }))->Dispatch(); + }); } } diff --git a/Include/Aurora/Async/IWorkItem.hpp b/Include/Aurora/Async/IWorkItem.hpp index 385d4810..1b10de9d 100644 --- a/Include/Aurora/Async/IWorkItem.hpp +++ b/Include/Aurora/Async/IWorkItem.hpp @@ -56,6 +56,5 @@ namespace Aurora::Async virtual void Cancel() = 0; virtual void *GetPrivateData() = 0; - virtual AuOptional ToWorkResultT() = 0; }; } \ No newline at end of file diff --git a/Include/Aurora/Async/Legacy/DO NOT REMOVE.txt b/Include/Aurora/Async/Legacy/DO NOT REMOVE.txt deleted file mode 100755 index 56e4eb97..00000000 --- a/Include/Aurora/Async/Legacy/DO NOT REMOVE.txt +++ /dev/null @@ -1,5 +0,0 @@ - > Deprecated APIs < - -* Wont remove -* Powering old projects -* Sometimes used as shorthands internally \ No newline at end of file diff --git a/Include/Aurora/Async/Legacy/JobFrom.hpp b/Include/Aurora/Async/Legacy/JobFrom.hpp deleted file mode 100644 index a48f44a7..00000000 --- a/Include/Aurora/Async/Legacy/JobFrom.hpp +++ /dev/null @@ -1,184 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: JobFrom.hpp - Date: 2021-11-1 - Author: Reece -***/ - #pragma once - -namespace Aurora::Async -{ - template - static inline FJob JobFromPairConsumer(const /*AuConsumer */ Callable_t&onSuccess) - { - FJob ret; - ret.onSuccess = [=](const Info_t &in, const Result_t &a) - { - onSuccess(in, a); - }; - return ret; - } - - template - static inline FJob JobFromResultConsumer(/*AuConsumer */ Callable_t onSuccess) - { - FJob ret; - ret.onSuccess = [=](const Info_t &in, const Result_t &a) - { - onSuccess(a); - }; - return ret; - } - - template - FJob, Out_t> JobFromTupleConsumer(/*AuConsumer */ Callable_t onSuccess) - { - FJob, Out_t> ret; - ret.onSuccess = [=](const AuTuple &in, const Out_t &a) - { - AuTupleApply(onSuccess, AuTupleCat(in, AuMakeTuple(a))); - }; - return ret; - } - - template - FJob, Out_t> JobFromTupleConsumerEx(/*AuConsumer */ Callable_t onSuccess, FailureCallable_t onFailure) - { - FJob, Out_t> ret; - ret.onSuccess = [=](const AuTuple &in, const Out_t &a) - { - AuTupleApply(onSuccess, AuTupleCat(in, AuMakeTuple(a))); - }; - - ret.onFailure = [=](const AuTuple &in) - { - AuTupleApply(onFailure, in); - }; - return ret; - } - - template - static inline FJob JobFromPairConsumerEx(const AuConsumer &onSuccess, const AuConsumer &onFailure) - { - FJob ret; - ret.onSuccess = [=](const Info_t &in, const Result_t &a) - { - onSuccess(in, a); - }; - ret.onFailure = [=](const Info_t &a) - { - onFailure(a); - }; - return ret; - } - - template - static inline FJob JobFromConsumer(const AuConsumer &onSuccess, const AuVoidFunc &onFailure) - { - FJob ret; - ret.onSuccess = [=](const Info_t &in, const Result_t &a) - { - onSuccess(a); - }; - ret.onFailure = [=](const Info_t &a) - { - onFailure(); - }; - return ret; - } - - template - static inline FJob JobFromDerivedJob(const FJob &reference) - { - FJob ret; - ret.onSuccess = [=](const Info_t &in, const Result_t &a) - { - if (reference.onSuccess) - { - reference.onSuccess(in, a); - } - }; - ret.onFailure = [=](const Info_t &a) - { - if (reference.onFailure) - { - reference.onFailure(a); - } - }; - return ret; - } - - template - static inline FJob, ReturnValue_t> JobFromTupleClazz(const AuConsumer &onSuccess) - { - FJob, ReturnValue_t> ret; - ret.onSuccess = [=](const AuTuple &in, const ReturnValue_t &out) - { - onSuccess(AuGet<0>(in), out); - }; - return ret; - } - - template - static inline FJob, ReturnValue_t> JobFromTupleClazzEx(const AuConsumer &onSuccess, const AuConsumer &onFailure) - { - FJob, ReturnValue_t> ret; - ret.onSuccess = [=](const AuTuple &in, const ReturnValue_t &out) - { - onSuccess(AuGet<0>(in), out); - }; - - ret.onFailure = [=](const AuTuple &in) - { - onFailure(AuGet<0>(in)); - }; - return ret; - } - -#if 0 - template - static inline FJob, Args...>, ReturnValue_t> JobFromTupleClazz(const AuConsumer &onSuccess) - { - FJob, Args...>, ReturnValue_t> ret; - ret.onSuccess = [=](const AuTuple, Args...> &in, const ReturnValue_t &out) - { - onSuccess(out); - }; - return ret; - } - - template - static inline FJob, Args...>, ReturnValue_t> JobFromTupleClazz(const AuConsumer &onSuccess) - { - FJob, Args...>, ReturnValue_t> ret; - ret.onSuccess = [=](const AuTuple, Args...> &in, const ReturnValue_t &out) - { - AuTupleApply(onSuccess, AuTupleCat(AuTuplePopFront(in), AuMakeTuple(out))); - }; - return ret; - } -#endif - - template - static inline FJob, ReturnValue_t> JobFromTupleResultConsumer(const AuConsumer &onSuccess) - { - FJob, ReturnValue_t> ret; - ret.onSuccess = [=](const AuTuple &in, const ReturnValue_t &out) - { - onSuccess(out); - }; - return ret; - } - - template - static inline FJob, ReturnValue_t> JobFromTuple(const AuConsumer &onSuccess) - { - FJob, ReturnValue_t> ret; - ret.onSuccess = [=](const AuTuple &in, const ReturnValue_t &out) - { - AuTupleApply(onSuccess, AuTupleCat(in, AuMakeTuple(out))); - }; - return ret; - } -} \ No newline at end of file diff --git a/Include/Aurora/Async/Legacy/Jobs.hpp b/Include/Aurora/Async/Legacy/Jobs.hpp deleted file mode 100644 index f782e1d3..00000000 --- a/Include/Aurora/Async/Legacy/Jobs.hpp +++ /dev/null @@ -1,33 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: Jobs.hpp - Date: 2021-11-1 - Author: Reece -***/ - #pragma once - -namespace Aurora::Async -{ - template - struct FJob - { - using InfoType_t = Info_t; - using ResultType_t = Result_t; - - AuFunction onSuccess; - AuFunction onFailure; - }; - - template - struct CJob - { - using InfoType_t = Info_t; - using ResultType_t = Result_t; - - void(* onSuccess)(const Info_t &, const Result_t &); - void(* onFailure)(const Info_t &); - }; - - using FVoidJob = FJob; -} \ No newline at end of file diff --git a/Include/Aurora/Async/Legacy/OldTrash.hpp b/Include/Aurora/Async/Legacy/OldTrash.hpp deleted file mode 100644 index 19734f73..00000000 --- a/Include/Aurora/Async/Legacy/OldTrash.hpp +++ /dev/null @@ -1,146 +0,0 @@ -#pragma once - -namespace Aurora::Async -{ -#pragma region EASE_OF_READING - struct BasicWorkStdFunc : IWorkItemHandler - { - AuFunction callback; - AuFunction shutdown; // error - - inline BasicWorkStdFunc(AuFunction &&callback, AuFunction &&shutdown) : callback(std::move(callback)), shutdown(std::move(shutdown)) - {} - - inline BasicWorkStdFunc(AuFunction &&callback) : callback(std::move(callback)) - {} - - inline BasicWorkStdFunc(const AuFunction &callback) : callback(callback) - {} - - inline BasicWorkStdFunc(const AuFunction &callback, const AuFunction &shutdown) : callback(callback), shutdown(shutdown) - {} - - private: - #if !defined(_CPPSHARP) - inline void DispatchFrame(ProcessInfo &info) override - { - try - { - callback(); - } - catch (...) - { - Debug::PrintError(); - } - } - - inline void OnFailure() override - { - try - { - if (shutdown) - { - shutdown(); - } - } - catch (...) - { - Debug::PrintError(); - } - } - #endif - }; - - - /// @hideinitializer - template, typename Cleanup_t = AuFunction> - struct WorkItemCallable : IWorkItemHandler - { - Frame_t frame; - Cleanup_t cleanup; - - private: - void DispatchFrame(ProcessInfo &info) override - { - if constexpr (AuIsBaseOfTemplate::value) - { - if (!frame) - { - info.type = ETickType::eFinished; - return; - } - } - frame(); - info.type = ETickType::eFinished; - } - - void OnFailure() override - { - if constexpr (AuIsBaseOfTemplate::value) - { - if (!cleanup) - { - return; - } - } - cleanup(); - } - }; - - - #define ASYNC_ERROR(exp) { if constexpr (AuIsSame_v) { SysPushErrorGen(exp); return {}; } else { throw AuString(exp); } } - #define ASYNC_FINISH { if constexpr (AuIsSame_v) { return true; } } - - template || AuIsVoid_v)> - static AuFunction TranslateAsyncFunctionToDispatcherWithThread(WorkerPId_t id, AuFunction func) - { - if (!func) return {}; - return [=](Args&&... in) -> T - { - auto work = AuMakeShared([=]() -> void { - func(in...); - }); - if (!work) ASYNC_ERROR("can't dispatch async call. out of memory"); - auto workItem = NewWorkItem(id, work); - if (!workItem) ASYNC_ERROR("can't dispatch async call. out of memory"); - workItem->Dispatch(); - ASYNC_FINISH; - }; - } - - /// Async app only - template || AuIsVoid_v)> - static AuFunction TranslateAsyncFunctionToDispatcher(AuFunction func) - { - return TranslateAsyncFunctionToDispatcherWithThread(GetAsyncApp()->GetCurrentThread(), func); - } - - /// Async app only - template || AuIsVoid_v)> - static AuFunction, Args...)> TranslateAsyncReturnableFunctionToDispatcherWithThread(WorkerPId_t id, AuFunction func) - { - return [=](AuFunction callback, Args... in) -> T - { - auto work = AuMakeShared>(); - if (!work) ASYNC_ERROR("can't dispatch async call; out of memory"); - work.task.onProcess = [=](const AVoid &) -> B - { - if (!func) return B{}; - return func(in...); - }; - work.callback.onSuccess = [=](const AVoid &, const B &ret) - { - callback(ret); - }; - auto workItem = NewWorkItem(id, work); - if (!workItem) ASYNC_ERROR("can't dispatch async call; out of memory"); - workItem->Dispatch(); - ASYNC_FINISH; - }; - } - - #undef ASYNC_ERROR - #undef ASYNC_FINISH - -#pragma endregion EASE_OF_READING -} \ No newline at end of file diff --git a/Include/Aurora/Async/Legacy/TaskFrom.hpp b/Include/Aurora/Async/Legacy/TaskFrom.hpp deleted file mode 100644 index 2a8dd6dc..00000000 --- a/Include/Aurora/Async/Legacy/TaskFrom.hpp +++ /dev/null @@ -1,86 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: TaskFrom.hpp - Date: 2021-11-1 - Author: Reece -***/ - #pragma once - -namespace Aurora::Async -{ - template - FTask TaskFromConsumerRefT(ClazzImpl func) - { - FTask ret; - ret.onFrame = [=](const Info_t &in) -> Out_t - { - if constexpr (AuIsSame_v) - { - func(in); - return {}; - } - else - { - return func(in); - } - }; - return ret; - } - - template - FTask, Out_t> TaskFromTupleCallable(Functor func) - { - FTask, Out_t> ret; - ret.onFrame = [=](const AuTuple &in) -> Out_t - { - return AuTupleApply(func, in); - }; - return ret; - } - - template - FTask, Out_t> TaskFromTupleCallableWithOwnerArg(AuFunction func, const Owner_t &ownerToPin) - { - FTask, Out_t> ret; - ret.onFrame = [ownerToPin, callable = func](const AuTuple &in) -> Out_t - { - return AuTupleApply(callable, AuTupleCat(AuMakeTuple(ownerToPin), in)); - }; - return ret; - } - - template - Task_t TaskFromTupleCallableWithBindOwner(Functor func) - { - Task_t ret; - ret.onFrame = [=](const auto &in) -> ReturnValue_t - { - return AuTupleApply(func, AuTuplePopFront(in)); - }; - return ret; - } - - template - Task_t TaskFromTupleCallableWithBindOwner2(Functor func) - { - Task_t ret; - ret.onFrame = [=](const auto &in) -> ReturnValue_t - { - return AuTupleApply(func, AuTuplePopFront(in)); - }; - return ret; - } - - template - FTask TaskFromVoidVoid(const AuVoidFunc &func) - { - FTask ret; - ret.onFrame = [callable = func](const Info_t &in) -> Out_t - { - callable(); - return {}; - }; - return ret; - } -} diff --git a/Include/Aurora/Async/Legacy/Tasks.hpp b/Include/Aurora/Async/Legacy/Tasks.hpp deleted file mode 100644 index a5a109cb..00000000 --- a/Include/Aurora/Async/Legacy/Tasks.hpp +++ /dev/null @@ -1,47 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: Tasks.hpp - Date: 2021-11-1 - Author: Reece -***/ - #pragma once - -namespace Aurora::Async -{ - template - struct ITask - { - virtual Result_t OnFrame(const Info_t &in) const = 0; - }; - - template - struct FTask final : ITask - { - using InfoType_t = Info_t; - using ResultType_t = Result_t; - - AuFunction onFrame; - - virtual inline Result_t OnFrame(const Info_t &in) const override final - { - return onFrame(in); - } - }; - - template - struct CTask final : ITask - { - using InfoType_t = Info_t; - using ResultType_t = Result_t; - - Result_t(* onFrame)(const Info_t &) = 0; - - virtual inline Result_t OnFrame(const Info_t &in) const override final - { - return onFrame(in); - } - }; - - using FVoidTask = FTask; -} \ No newline at end of file diff --git a/Include/Aurora/Async/Legacy/WorkBasic.hpp b/Include/Aurora/Async/Legacy/WorkBasic.hpp deleted file mode 100644 index e4aefa2c..00000000 --- a/Include/Aurora/Async/Legacy/WorkBasic.hpp +++ /dev/null @@ -1,121 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: WorkBasic.hpp - Date: 2021-11-1 - Author: Reece -***/ - #pragma once - -namespace Aurora::Async -{ - - /// --- THREAD POOL -- - - template, typename Job_t = FJob, class a = const Job_t &, class b = Task_t &, class WorkerPId_tt = WorkerPId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - static AuSPtr NewWork(const WorkerPId_tt &worker, a task, b job, bool enableWait = false) - { - return worker.pool->NewWorkItem(worker, AuMakeShared>(task, job), enableWait); - } - - template, typename Job_t = FJob, class a = const Job_t &, class b = Task_t &, class WorkerPId_tt = WorkerPId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - static AuSPtr DispatchWork(const WorkerPId_tt &worker, a task, b job, bool enableWait = false) - { - return NewWork(worker, task, job, enableWait)->Dispatch(); - } - - template, typename Job_t = FJob, class a, class b = Job_t, class c, class WorkerPId_tt = WorkerPId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - static AuSPtr NewWork(const WorkerPId_tt &worker, a task, b job, c info, bool enableWait = false) - { - return worker.pool->NewWorkItem(worker, AuMakeShared>(task, job, info), enableWait); - } - - template, typename Job_t = FJob, class a, class b = Job_t, class c, class WorkerPId_tt = WorkerPId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - static AuSPtr DispatchWork(const WorkerPId_tt &worker, a task, b job, c info, bool enableWait = false) - { - return NewWork(worker, task, job, info, enableWait)->Dispatch(); - } - - template, typename Job_t = FJob, typename ClazzImpl, class a = const Job_t &, class b = const Info_t &, class WorkerPId_tt = WorkerPId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - AuSPtr DispatchFunctional(const WorkerPId_tt &worker, ClazzImpl task, a job, b inputParameters, bool enableWait = false) - { - return NewWork(worker, TaskFromConsumerRefT(task), job, inputParameters, enableWait); - } - - template, typename Job_t = FJob, typename ClazzImpl, class a = const Job_t &, class b = const Info_t &, class WorkerPId_tt = WorkerPId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - AuSPtr DispatchFunctor(const WorkerPId_tt &worker, ClazzImpl task, a job, b inputParameters, bool enableWait = false) - { - return NewWork(worker, TaskFromConsumerRefT(task), job, inputParameters, enableWait); - } - - template, typename Job_t = FJob, typename ClazzImpl, class a = const Job_t &, class b = const Info_t &, class WorkerPId_tt = WorkerPId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - AuSPtr DispatchVoid(const WorkerPId_tt &worker, ClazzImpl task, a job, b inputParameters, bool enableWait = false) - { - return NewWork(worker, TaskFromVoidVoid(task), job, inputParameters, enableWait); - } - - - /// --- ASYNC APP -- - - template, typename Job_t = FJob, class a = const Job_t &, class b = Task_t &, class WorkerId_tt = WorkerId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - static AuSPtr NewWork(const WorkerId_tt &worker, a task, b job, bool enableWait = false) - { - return NewWorkItem(worker, AuMakeShared>(task, job), enableWait); - } - - template, typename Job_t = FJob, class a = const Job_t &, class b = Task_t &, class WorkerId_tt = WorkerId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - static AuSPtr DispatchWork(const WorkerId_tt &worker, a task, b job, bool enableWait = false) - { - return NewWork(worker, task, job, enableWait)->Dispatch(); - } - - template, typename Job_t = FJob, class a, class b = Job_t, class c, class WorkerId_tt = WorkerId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - static AuSPtr NewWork(const WorkerId_tt &worker, a task, b job, c info, bool enableWait = false) - { - return NewWorkItem(worker, AuMakeShared>(task, job, info), enableWait); - } - - template, typename Job_t = FJob, class a, class b = Job_t, class c, class WorkerId_tt = WorkerId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - static AuSPtr DispatchWork(const WorkerId_tt &worker, a task, b job, c info, bool enableWait = false) - { - return NewWork(worker, task, job, info, enableWait)->Dispatch(); - } - - template, typename Job_t = FJob, typename ClazzImpl, class a = const Job_t &, class b = const Info_t &, class WorkerId_tt = WorkerId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - AuSPtr DispatchFunctional(const WorkerId_tt &worker, ClazzImpl task, a job, b inputParameters, bool enableWait = false) - { - return NewWork(worker, TaskFromConsumerRefT(task), job, inputParameters, enableWait); - } - - template, typename Job_t = FJob, typename ClazzImpl, class a = const Job_t &, class b = const Info_t &, class WorkerId_tt = WorkerId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - AuSPtr DispatchFunctor(const WorkerId_tt &worker, ClazzImpl task, a job, b inputParameters, bool enableWait = false) - { - return NewWork(worker, TaskFromConsumerRefT(task), job, inputParameters, enableWait); - } - - template, typename Job_t = FJob, typename ClazzImpl, class a = const Job_t &, class b = const Info_t &, class WorkerId_tt = WorkerId_t, AU_TEMPLATE_ENABLE_WHEN(AuIsSame_v)> - AuSPtr DispatchVoid(const WorkerId_t &worker, ClazzImpl task, a job, b inputParameters, bool enableWait = false) - { - return NewWork(worker, TaskFromVoidVoid(task), job, inputParameters, enableWait); - } - - template - static AuSPtr DispathSmartWork(const WorkerId_t &worker, AuSPtr owner, FunctorTask_t task, FuckYou_t job, Args ... in) - { - return DispatchWork, Args...>, ReturnValue_t>(worker, - TaskFromTupleCallableWithBindOwner2, Args...>, ReturnValue_t>, ReturnValue_t, FunctorTask_t>(task), - Async::JobFromTupleClazz, Args...>(job), - AuMakeTuple, Args...>(AU_FWD(owner), AuForward(in)...), - false); - } - - template - static AuSPtr DispathSmartWorkEx(const WorkerId_t &worker, AuSPtr owner, FunctorTask_t task, FuckYou_t success, FuckYou2_t failure, Args ... in) - { - return DispatchWork, Args...>, ReturnValue_t>(worker, - TaskFromTupleCallableWithBindOwner2, Args...>, ReturnValue_t>, ReturnValue_t, FunctorTask_t>(task), - Async::JobFromTupleClazzEx, Args...>(success, failure), - AuMakeTuple, Args...>(AU_FWD(owner), AuForward(in)...), - false); - } -} \ No newline at end of file diff --git a/Include/Aurora/Async/Legacy/WorkPairImpl.hpp b/Include/Aurora/Async/Legacy/WorkPairImpl.hpp deleted file mode 100644 index 4a4e17d5..00000000 --- a/Include/Aurora/Async/Legacy/WorkPairImpl.hpp +++ /dev/null @@ -1,226 +0,0 @@ -/*** - Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: WorkPairImpl.hpp - Date: 2021-11-1 - Author: Reece -***/ - #pragma once - -namespace Aurora::Threading::Primitives -{ - class SpinLoop; -} - -namespace Aurora::Async -{ - struct BasicWorkStdFunc; - - /// @hideinitializer - struct BasicWorkCtx : WorkPriv - { - BasicWorkCtx() - { - magic = AuConvertMagicTag32("BWOT"); - opt = nullptr; - } - void *opt; - }; - - /// @hideinitializer - template, typename Job_t = FJob> - struct WorkPairImpl : IWorkItemHandler, AuEnableSharedFromThis - { - WorkPairImpl() : caller_(Async::GetCurrentWorkerPId()) - {} - - WorkPairImpl(Task_t &&task) : task(AuMove(task)), caller_(Async::GetCurrentWorkerPId()) - {} - - WorkPairImpl(Task_t &&task, Job_t &&callback) : task(AuMove(task)), callback(AuMove(callback)), caller_(Async::GetCurrentWorkerPId()) - {} - - WorkPairImpl(const Task_t &task) : task(task), caller_(Async::GetCurrentWorkerPId()) - {} - - WorkPairImpl(const Task_t &task, const Job_t &callback) : task(task), callback(callback), caller_(Async::GetCurrentWorkerPId()) - {} - - WorkPairImpl(const Task_t &task, const Job_t &callback, const Info_t &info) : task(task), callback(callback), input(info), caller_(Async::GetCurrentWorkerPId()) - {} - - WorkPairImpl(Task_t &&task, const Job_t &callback, const Info_t &info) : task(AuMove(task)), callback(callback), input(info), caller_(Async::GetCurrentWorkerPId()) - {} - - WorkPairImpl(Task_t &&task, Job_t &&callback, const Info_t &info) : task(AuMove(task)), callback(AuMove(callback)), input(info), caller_(Async::GetCurrentWorkerPId()) - {} - - WorkPairImpl(Task_t &&task, Job_t &&callback, Info_t &&info) : task(AuMove(task)), callback(AuMove(callback)), input(AuMove(info)), caller_(Async::GetCurrentWorkerPId()) - {} - - WorkPairImpl(const Task_t &task, const Job_t &callback, Info_t &&info) : task(task), callback(callback), input(info), caller_(Async::GetCurrentWorkerPId()) - {} - - Info_t input; - Task_t task; - Job_t callback; - - - WorkPairImpl &SetTask(const Task_t &task) - { - this->task = task; - return *this; - } - - WorkPairImpl &SetTask(const Job_t &callback) - { - this->callback = callback; - return *this; - } - - private: - - static constexpr bool IsCallbackPtr = AuIsPointer_v || AuIsBaseOfTemplate::value || AuIsBaseOfTemplate::value; - static constexpr bool IsTaskPtr = AuIsPointer_v || AuIsBaseOfTemplate::value || AuIsBaseOfTemplate::value; - - //WorkerId_t caller; - WorkerPId_t caller_; - Threading::Primitives::SpinLock lock_; - - BasicWorkCtx secretContext_; - Result_t resultValue_; - - virtual void *GetPrivateData() override { return &secretContext_; } - - void DispatchFrame(ProcessInfo &info) override - { - AU_LOCK_GUARD(this->lock_); - - try - { - if constexpr (IsTaskPtr) - { - if (task) - { - resultValue_ = task->OnFrame(input); - } - } - else - { - resultValue_ = task.OnFrame(input); - } - - task = {}; - } - catch (...) - { - Debug::PrintError(); - ShutdownNoLock(); - return; - } - - auto pin = AuSharedFromThis(); - AuFunction func = [pin]() - { - try - { - pin->secretContext_.opt = &pin->resultValue_; - if constexpr (IsCallbackPtr) - { - pin->callback->onSuccess(pin->input, pin->resultValue_); - } - else - { - if (pin->callback.onSuccess) - { - pin->callback.onSuccess(pin->input,pin->resultValue_); - } - } - } - catch (...) - { - Debug::PrintError(); - } - - pin->callback = {}; - pin->caller_ = {}; - }; - - try - { - if (caller_ == Async::GetCurrentWorkerPId()) - { - func(); - } - else - { - AuFunction err = [pin]() - { - pin->CallOnFailure(); - }; - - // TODO: this is somewhat evil. double alloc when we could reuse this - if (!caller_.pool->NewWorkItem(caller_, AuMakeShared(func, err))->Dispatch()) - { - pin->CallOnFailure(); - } - } - - caller_ = {}; - } - catch (...) - { - Debug::PrintError(); - ShutdownNoLock(); - } - } - - void OnFailure() override - { - AU_LOCK_GUARD(this->lock_); - ShutdownNoLock(); - } - - inline void ShutdownNoLock() - { - caller_ = {}; - try - { - CallOnFailure(); - } - catch (...) - { - Debug::PrintError(); - } - callback = {}; - task = {}; - } - - inline void CallOnFailure() - { - if constexpr (IsCallbackPtr) - { - if constexpr (AuIsBaseOfTemplateonFailure)>::value) - { - if (!callback->onFailure) - { - return; - } - } - - callback->onFailure(input); - } - else - { - if constexpr (AuIsBaseOfTemplate::value) - { - if (!callback.onFailure) - { - return; - } - } - callback.onFailure(input); - } - } - }; -} \ No newline at end of file diff --git a/Source/Async/Schedular.cpp b/Source/Async/Schedular.cpp index ad3f5899..fbfd0107 100644 --- a/Source/Async/Schedular.cpp +++ b/Source/Async/Schedular.cpp @@ -173,7 +173,7 @@ namespace Aurora::Async { if (!AuExchange(gLockedPump, true)) { - NewWorkItem(gMainThread.value(), AuMakeShared(PumpSysThread))->Dispatch(); + DispatchOn(gMainThread.value(), PumpSysThread); } SchedNextTime(AuMSToNS(gRuntimeConfig.async.dwLegacyMainThreadSystemTickMS) + AuTime::SteadyClockNS()); diff --git a/Source/Async/ThreadPool.cpp b/Source/Async/ThreadPool.cpp index fffaeff3..28198900 100644 --- a/Source/Async/ThreadPool.cpp +++ b/Source/Async/ThreadPool.cpp @@ -998,14 +998,11 @@ namespace Aurora::Async AuSPtr pFeature, bool bNonBlock) { - auto work = AuMakeSharedThrow(([=]() + auto pWorkItem = DispatchOn({ this->SharedFromThis(), id }, [=]() { GetThreadState()->features.push_back(pFeature); pFeature->Init(); - })); - - auto pWorkItem = this->NewWorkItem(id, work)->Dispatch(); - SysAssert(pWorkItem); + }); if (!bNonBlock) { diff --git a/Source/Async/WorkItem.cpp b/Source/Async/WorkItem.cpp index 72c3fc49..f2c7a527 100644 --- a/Source/Async/WorkItem.cpp +++ b/Source/Async/WorkItem.cpp @@ -38,6 +38,7 @@ namespace Aurora::Async WorkItem::~WorkItem() { + //Fail(); } @@ -268,7 +269,6 @@ namespace Aurora::Async { if (this->task_) { - try { this->task_->DispatchFrame(info); @@ -306,20 +306,7 @@ namespace Aurora::Async IWorkItemHandler::ProcessInfo info(true); info.pool = this->owner_->ToThreadPool(); - if (this->task_) - { - try - { - this->task_->DispatchFrame(info); - } - catch (...) - { - // TODO: runtime config for root level exception caught behaviour - SysPushErrorCatch(); - Fail(); - return; - } - } + DispatchTask(info); AU_LOCK_GUARD(this->lock); RunAsyncLocked2(info); @@ -417,7 +404,12 @@ namespace Aurora::Async bool WorkItem::BlockUntilComplete() { if (!this->finishedEvent_) return false; - return this->owner_->WaitFor(this->worker_, AuUnsafeRaiiToShared(this->finishedEvent_.AsPointer()), 0); + if (!this->worker_) + { + this->finishedEvent_->Wait(); + return true; + }; + return this->owner_->WaitFor(this->worker_.value(), AuUnsafeRaiiToShared(this->finishedEvent_.AsPointer()), 0); } bool WorkItem::HasFinished() @@ -438,19 +430,19 @@ namespace Aurora::Async bool WorkItem::Schedule() { - return Async::Schedule(this->dispatchTimeNs_, this->owner_, this->worker_, AuSharedFromThis()); + return Async::Schedule(this->dispatchTimeNs_, this->owner_, this->worker_.value(), AuSharedFromThis()); } void WorkItem::SendOff() { - if (!this->task_) + if (!this->worker_) { // If we aren't actually calling a task interface, we may as well just dispatch objects waiting on us from here RunAsyncLocked2(); } else { - this->owner_->Run(this->worker_, AuSharedFromThis()); + this->owner_->Run(this->worker_.value(), AuSharedFromThis()); } } @@ -553,26 +545,4 @@ namespace Aurora::Async return this->task_->GetPrivateData(); } - - AuOptional WorkItem::ToWorkResultT() - { - if (!this->task_) - { - return nullptr; - } - - auto priv = reinterpret_cast(this->task_->GetPrivateData()); - if (!priv) - { - return nullptr; - } - - if (priv->magic == AuConvertMagicTag32("BWOT")) - { - - return reinterpret_cast(priv)->opt; - } - - return {}; - } } \ No newline at end of file diff --git a/Source/Async/WorkItem.hpp b/Source/Async/WorkItem.hpp index b188af0a..ebf3bfab 100644 --- a/Source/Async/WorkItem.hpp +++ b/Source/Async/WorkItem.hpp @@ -45,7 +45,6 @@ namespace Aurora::Async void *GetPrivateData() override; - AuOptional ToWorkResultT() override; EWorkPrio GetPrio() override; void SetPrio(EWorkPrio prio) override; @@ -62,7 +61,7 @@ namespace Aurora::Async virtual void DispatchTask(IWorkItemHandler::ProcessInfo &info); AuSPtr task_; - WorkerPId_t worker_; + AuOptionalEx worker_; EWorkPrio prio_ = EWorkPrio::eNormalPrio; AuList> waitOn_; AuList> waiters_; diff --git a/Source/Console/Commands/Commands.cpp b/Source/Console/Commands/Commands.cpp index 0cee2f31..136d9405 100644 --- a/Source/Console/Commands/Commands.cpp +++ b/Source/Console/Commands/Commands.cpp @@ -175,7 +175,7 @@ namespace Aurora::Console::Commands } else { - NewWorkItem(gCommandDispatcher.value(), AuMakeShared(func))->Dispatch()->BlockUntilComplete(); + DispatchOn(gCommandDispatcher.value(), func)->BlockUntilComplete(); } }