From 941e6a06b14f6d88d646de503f41a9414e0b984d Mon Sep 17 00:00:00 2001 From: 4kangjc <1158035520@qq.com> Date: Fri, 28 Oct 2022 21:00:06 +0800 Subject: [PATCH] refactor uthread::Launch --- async_simple/uthread/Async.h | 34 ++++++++++++++-------------------- async_simple/uthread/Collect.h | 8 ++++---- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/async_simple/uthread/Async.h b/async_simple/uthread/Async.h index 56e8fcc0b..97966cf26 100644 --- a/async_simple/uthread/Async.h +++ b/async_simple/uthread/Async.h @@ -41,23 +41,20 @@ namespace async_simple { namespace uthread { -struct Launch { - struct Prompt {}; - struct Schedule {}; - struct Current {}; +enum class Launch { + Prompt, + Schedule, + Current, }; -template ::value, - T>::type* = nullptr> -inline Uthread async(F&& f, Executor* ex) { +template +inline std::enable_if_t async(F&& f, + Executor* ex) { return Uthread(Attribute{ex}, std::forward(f)); } -template ::value, - T>::type* = nullptr> -inline void async(F&& f, Executor* ex) { +template +inline std::enable_if_t async(F&& f, Executor* ex) { if (!ex) AS_UNLIKELY { return; } ex->schedule([f = std::move(f), ex]() { @@ -67,10 +64,9 @@ inline void async(F&& f, Executor* ex) { } // schedule async task, set a callback -template ::value, - T>::type* = nullptr> -inline void async(F&& f, C&& c, Executor* ex) { +template +inline std::enable_if_t async(F&& f, C&& c, + Executor* ex) { if (!ex) AS_UNLIKELY { return; } ex->schedule([f = std::move(f), c = std::move(c), ex]() { @@ -79,10 +75,8 @@ inline void async(F&& f, C&& c, Executor* ex) { }); } -template ::value, - T>::type* = nullptr> -inline void async(F&& f, Executor* ex) { +template +inline std::enable_if_t async(F&& f, Executor* ex) { Uthread uth(Attribute{ex}, std::forward(f)); uth.detach(); } diff --git a/async_simple/uthread/Collect.h b/async_simple/uthread/Collect.h index 0edcfcf5a..e39e25132 100644 --- a/async_simple/uthread/Collect.h +++ b/async_simple/uthread/Collect.h @@ -50,7 +50,7 @@ namespace uthread { // TODO: Due to it is possible that the user of async_simple doesn't support // c++17, we didn't merge this two implementation by if constexpr. Merge them // once the codebases are ready to use c++17. -template +template std::vector::value_type>>::value, @@ -58,7 +58,7 @@ std::vector collectAll(Iterator first, Iterator last, Executor* ex) { assert(std::distance(first, last) >= 0); - static_assert(!std::is_same::value, + static_assert(Policy != Launch::Prompt, "collectAll not support Prompt launch policy"); using ResultType = std::invoke_result_t< @@ -93,14 +93,14 @@ collectAll(Iterator first, Iterator last, Executor* ex) { }); } -template +template typename std::enable_if< std::is_void::value_type>>::value, void>::type collectAll(Iterator first, Iterator last, Executor* ex) { assert(std::distance(first, last) >= 0); - static_assert(!std::is_same::value, + static_assert(Launch::Prompt != Policy, "collectN not support Prompt launch policy"); struct Context {