From 1360e5627d7f056e1ac45d3c50f2fd7afdcc9bf8 Mon Sep 17 00:00:00 2001 From: Reece Date: Wed, 29 Sep 2021 09:02:27 +0100 Subject: [PATCH] [*] Update async header --- Include/Aurora/Async/Async.hpp | 54 ++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/Include/Aurora/Async/Async.hpp b/Include/Aurora/Async/Async.hpp index b086dc7b..4452d0ae 100644 --- a/Include/Aurora/Async/Async.hpp +++ b/Include/Aurora/Async/Async.hpp @@ -70,6 +70,8 @@ namespace Aurora::Async std::function(const Info_t &)> onFrame = 0; }; + using FVoidTask = FTask; + template struct CTask { @@ -93,6 +95,7 @@ namespace Aurora::Async virtual bool BlockUntilComplete() = 0; virtual bool HasFinished() = 0; virtual bool HasFailed() = 0; + virtual void Cancel() = 0; }; AUKN_SYM AuSPtr NewWorkItem(const DispatchTarget_t &worker, const AuSPtr &task, bool supportsBlocking = false); @@ -154,6 +157,11 @@ namespace Aurora::Async caller = GetAsyncApp()->GetCurrentThread(); } + BasicWorkCallback(Task_t &&task) : task(std::move(task)) + { + caller = GetAsyncApp()->GetCurrentThread(); + } + Task_t task; Job_t callback; @@ -309,28 +317,38 @@ namespace Aurora::Async } }; - template - static std::function TranslateAsyncFunctionToDispatcher(std::function func) + #define ASYNC_ERROR(exp) if constexpr (std::is_same_v) { SysPushErrorGen(exp); return {}; } else { throw std::exception(exp); } + #define ASYNC_FINISH if constexpr (std::is_same_v) { return true; } + + template || std::is_void::value)> + static std::function TranslateAsyncFunctionToDispatcherWithThread(WorkerId_t id, std::function func) { - auto cur = GetAsyncApp()->GetCurrentThread(); - return [=](Args... in) -> void + return [=](Args... in) -> T { auto work = AuMakeShared([=]() -> void { func(in...); }); - auto workItem = NewWorkItem(cur, work); - if (!workItem) throw "can't dispatch async call; out of memory"; + 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; }; } - - template - static std::function, Args...)> TranslateAsyncReturnableFunctionToDispatcher(std::function func) + + template || std::is_void::value)> + static std::function TranslateAsyncFunctionToDispatcher(std::function func) { - auto cur = GetAsyncApp()->GetCurrentThread(); - return [=](std::function callback, Args... in) -> void + return TranslateAsyncFunctionToDispatcherWithThread(GetAsyncApp()->GetCurrentThread(), func); + } + + template || std::is_void::value)> + static std::function, Args...)> TranslateAsyncReturnableFunctionToDispatcherWithThread(WorkerId_t id, std::function(Args...)> func) + { + return [=](std::function callback, Args... in) -> T { auto work = AuMakeShared>(); + if (!work) ASYNC_ERROR("can't dispatch async call; out of memory"); work.task.onProcess = [](const AVoid &) -> AuOptional { return func(in...); @@ -339,11 +357,21 @@ namespace Aurora::Async { callback(ret); }; - auto workItem = NewWorkItem(cur, work); - if (!workItem) throw "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; }; } + + template || std::is_void::value)> + static std::function, Args...)> TranslateAsyncReturnableFunctionToDispatcher(std::function(Args...)> func) + { + return TranslateAsyncReturnableFunctionToDispatcherWithThread(GetAsyncApp()->GetCurrentThread(), func); + } + + #undef ASYNC_ERROR + #undef ASYNC_FINISH #endif