Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor launch #173

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions async_simple/uthread/Async.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,19 @@
namespace async_simple {
namespace uthread {

struct Launch {
struct Prompt {};
struct Schedule {};
struct Current {};
enum class Launch {
Prompt,
Schedule,
Current,
};

template <class T, class F,
typename std::enable_if<std::is_same<T, Launch::Prompt>::value,
T>::type* = nullptr>
inline Uthread async(F&& f, Executor* ex) {
template <Launch policy, class F>
requires(policy == Launch::Prompt) inline Uthread async(F&& f, Executor* ex) {
return Uthread(Attribute{ex}, std::forward<F>(f));
}

template <class T, class F,
typename std::enable_if<std::is_same<T, Launch::Schedule>::value,
T>::type* = nullptr>
inline void async(F&& f, Executor* ex) {
template <Launch policy, class F>
requires(policy == Launch::Schedule) inline void async(F&& f, Executor* ex) {
if (!ex)
AS_UNLIKELY { return; }
ex->schedule([f = std::move(f), ex]() {
Expand All @@ -67,10 +63,9 @@ inline void async(F&& f, Executor* ex) {
}

// schedule async task, set a callback
template <class T, class F, class C,
typename std::enable_if<std::is_same<T, Launch::Schedule>::value,
T>::type* = nullptr>
inline void async(F&& f, C&& c, Executor* ex) {
template <Launch policy, class F, class C>
requires(policy == Launch::Schedule) inline void async(F&& f, C&& c,
Executor* ex) {
if (!ex)
AS_UNLIKELY { return; }
ex->schedule([f = std::move(f), c = std::move(c), ex]() {
Expand All @@ -79,10 +74,8 @@ inline void async(F&& f, C&& c, Executor* ex) {
});
}

template <class T, class F,
typename std::enable_if<std::is_same<T, Launch::Current>::value,
T>::type* = nullptr>
inline void async(F&& f, Executor* ex) {
template <Launch policy, class F>
requires(policy == Launch::Current) inline void async(F&& f, Executor* ex) {
Uthread uth(Attribute{ex}, std::forward<F>(f));
uth.detach();
}
Expand Down
8 changes: 4 additions & 4 deletions async_simple/uthread/Collect.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ 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 <class Policy, class Iterator>
template <Launch Policy, class Iterator>
std::vector<typename std::enable_if<
!std::is_void<std::invoke_result_t<
typename std::iterator_traits<Iterator>::value_type>>::value,
std::invoke_result_t<typename std::iterator_traits<Iterator>::value_type>>::
type>
collectAll(Iterator first, Iterator last, Executor* ex) {
assert(std::distance(first, last) >= 0);
static_assert(!std::is_same<Launch::Prompt, Policy>::value,
static_assert(Policy != Launch::Prompt,
"collectAll not support Prompt launch policy");

using ResultType = std::invoke_result_t<
Expand Down Expand Up @@ -93,14 +93,14 @@ collectAll(Iterator first, Iterator last, Executor* ex) {
});
}

template <class Policy, class Iterator>
template <Launch Policy, class Iterator>
typename std::enable_if<
std::is_void<std::invoke_result_t<
typename std::iterator_traits<Iterator>::value_type>>::value,
void>::type
collectAll(Iterator first, Iterator last, Executor* ex) {
assert(std::distance(first, last) >= 0);
static_assert(!std::is_same<Launch::Prompt, Policy>::value,
static_assert(Launch::Prompt != Policy,
"collectN not support Prompt launch policy");

struct Context {
Expand Down