From ce1001bfa5128bc431eda38f758aea85c40c8150 Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Sun, 17 Jul 2022 21:40:35 +0800 Subject: [PATCH] Not finished Signed-off-by: WenTao Ou --- include/libcopp/coroutine/callable_promise.h | 11 ++++------- include/libcopp/coroutine/std_coroutine_common.h | 13 +++++++++---- include/libcotask/task_promise.h | 4 ++++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/libcopp/coroutine/callable_promise.h b/include/libcopp/coroutine/callable_promise.h index 70774b2..0325640 100644 --- a/include/libcopp/coroutine/callable_promise.h +++ b/include/libcopp/coroutine/callable_promise.h @@ -48,6 +48,7 @@ class LIBCOPP_COPP_API_HEAD_ONLY callable_promise_base : public pr callable_promise_base() = default; void return_void() noexcept { + set_flag(promise_flag::kHasReturned, true); if (get_status() < promise_status::kDone) { set_status(promise_status::kDone); } @@ -83,25 +84,21 @@ class LIBCOPP_COPP_API_HEAD_ONLY callable_promise_base : public p template callable_promise_base(TARGS&&... args) : data_(callable_promise_value_constructor::value>:: - construct(std::forward(args)...)), - has_return_(false) {} + construct(std::forward(args)...)) {} void return_value(value_type value) { + set_flag(promise_flag::kHasReturned, true); if (get_status() < promise_status::kDone) { set_status(promise_status::kDone); } data_ = std::move(value); - has_return_ = true; } UTIL_FORCEINLINE value_type& data() noexcept { return data_; } UTIL_FORCEINLINE const value_type& data() const noexcept { return data_; } - UTIL_FORCEINLINE bool has_return() const noexcept { return has_return_; } - protected: value_type data_; - bool has_return_; }; # if defined(LIBCOPP_MACRO_ENABLE_CONCEPTS) && LIBCOPP_MACRO_ENABLE_CONCEPTS @@ -242,7 +239,7 @@ class LIBCOPP_COPP_API_HEAD_ONLY callable_awaitable : public ca auto& callee_promise = get_callee().promise(); callee_promise.resume_waiting(get_callee(), true); - if (!callee_promise.has_return()) { + if (!callee_promise.check_flag(promise_flag::kHasReturned)) { return promise_error_transform()(callee_promise.get_status()); } diff --git a/include/libcopp/coroutine/std_coroutine_common.h b/include/libcopp/coroutine/std_coroutine_common.h index 556ff5e..2f9a882 100644 --- a/include/libcopp/coroutine/std_coroutine_common.h +++ b/include/libcopp/coroutine/std_coroutine_common.h @@ -10,7 +10,6 @@ #include // NOLINT(build/include_order) // clang-format on #include -#include #include #include #include @@ -53,6 +52,7 @@ enum class LIBCOPP_COPP_API_HEAD_ONLY promise_flag : uint8_t { kDestroying = 0, kFinalSuspend = 1, kInternalWaitting = 2, + kHasReturned = 3, kMax, }; @@ -238,14 +238,19 @@ class promise_base_type { return false; } - return flags_.test(static_cast(flag)); + return 0 != (flags_ & (1 << static_cast(flag))); } LIBCOPP_COPP_API_HEAD_ONLY inline void set_flag(promise_flag flag, bool value) noexcept { if (flag >= promise_flag::kMax) { return; } - flags_.set(static_cast(flag), value); + uint32_t flag_value = 1 << static_cast(flag); + if (value) { + flags_ |= flag_value; + } else { + flags_ &= ~flag_value; + } } LIBCOPP_COPP_API bool is_waiting() const noexcept; @@ -328,7 +333,7 @@ class promise_base_type { private: // promise_flags - std::bitset(promise_flag::kMax)> flags_; + uint32_t flags_; // promise_status promise_status status_; diff --git a/include/libcotask/task_promise.h b/include/libcotask/task_promise.h index 2e7715e..deeb852 100644 --- a/include/libcotask/task_promise.h +++ b/include/libcotask/task_promise.h @@ -433,6 +433,8 @@ class LIBCOPP_COPP_API_HEAD_ONLY task_promise_base task_promise_base(TARGS&&... args) : context_(std::make_shared(std::forward(args)...)) {} void return_void() noexcept { + set_flag(LIBCOPP_COPP_NAMESPACE_ID::promise_flag::kHasReturned, true); + if (get_status() < task_status_type::kDone) { set_status(task_status_type::kDone); } @@ -473,6 +475,8 @@ class LIBCOPP_COPP_API_HEAD_ONLY task_promise_base task_promise_base(TARGS&&... args) : context_(std::make_shared(std::forward(args)...)) {} void return_value(value_type value) { + set_flag(LIBCOPP_COPP_NAMESPACE_ID::promise_flag::kHasReturned, true); + if (get_status() < task_status_type::kDone) { set_status(task_status_type::kDone); }