From 1f8d06cbf0eff644d3862fd6c72f25e9ea553db9 Mon Sep 17 00:00:00 2001 From: Reece Date: Mon, 5 Jul 2021 14:35:13 +0100 Subject: [PATCH] [*] Expose AsyncApp::Poll --- Include/Aurora/Async/Async.hpp | 44 +++++++++++++++++++++++++++++----- Source/Async/AsyncApp.hpp | 4 ++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Include/Aurora/Async/Async.hpp b/Include/Aurora/Async/Async.hpp index 07a2b7a9..bf5f7445 100644 --- a/Include/Aurora/Async/Async.hpp +++ b/Include/Aurora/Async/Async.hpp @@ -51,27 +51,27 @@ namespace Aurora::Async }; - template + template struct FJob { std::function onSuccess = 0; std::function onFailure = 0; }; - template + template struct CJob { void(* onSuccess)(const Info_t &, const Result_t &); // void(* onFailure)(const Info_t &, bool taskNeverDispatched); // called from caller thread if taskNeverDispatched }; - template + template struct FTask { std::function(const Info_t &)> onFrame = 0; }; - template + template struct CTask { std::optional(* onFrame)(const Info_t &); @@ -284,11 +284,41 @@ namespace Aurora::Async cleanup(); } }; + + template + static std::function TranslateAsyncFunctionToDispatcher(std::function func) + { + auto cur = GetAsyncApp()->GetCurrentThread(); + return [=](Args... in) -> void + { + auto work = std::make_shared([=]() -> void { + func(in...); + }); + auto workItem = NewWorkItem(cur, work); + if (!workItem) throw "can't dispatch async call; out of memory"; + workItem->Dispatch(); + }; + } template - static void TranslateAsyncFunctionToDispatcher() + static std::function, Args...)> TranslateAsyncReturnableFunctionToDispatcher(std::function func) { - + auto cur = GetAsyncApp()->GetCurrentThread(); + return [=](std::function callback, Args... in) -> void + { + auto work = std::make_shared>(); + work.task.onProcess = [](const AVoid &) -> std::optional + { + return func(in...); + }; + work.callback.onSuccess = [=](const AVoid &, const B &ret) + { + callback(ret); + }; + auto workItem = NewWorkItem(cur, work); + if (!workItem) throw "can't dispatch async call; out of memory"; + workItem->Dispatch(); + }; } class IAsyncApp @@ -322,5 +352,7 @@ namespace Aurora::Async // Debug virtual void AssertInThreadGroup(ThreadGroup_t thread) = 0; virtual void AssertWorker(WorkerId_t id) = 0; + + virtual bool Poll(bool block) = 0; }; } \ No newline at end of file diff --git a/Source/Async/AsyncApp.hpp b/Source/Async/AsyncApp.hpp index 2054f333..1b33460f 100644 --- a/Source/Async/AsyncApp.hpp +++ b/Source/Async/AsyncApp.hpp @@ -54,12 +54,12 @@ namespace Aurora::Async void Run(DispatchTarget_t target, AuSPtr runnable); void ShutdownOutOfTasks(); - + + bool Poll(bool block) override; private: // TODO: BarrierMultiple bool Barrier(WorkerId_t, AuUInt32 ms, bool requireSignal, bool drop); - bool Poll(bool a); Threading::Primitives::RWLockUnique_t rwlock_;