Skip to content

Commit

Permalink
Not finished
Browse files Browse the repository at this point in the history
Signed-off-by: WenTao Ou <[email protected]>
  • Loading branch information
owent committed Jul 17, 2022
1 parent d90ba82 commit ce1001b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
11 changes: 4 additions & 7 deletions include/libcopp/coroutine/callable_promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class LIBCOPP_COPP_API_HEAD_ONLY callable_promise_base<TVALUE, true> : 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);
}
Expand Down Expand Up @@ -83,25 +84,21 @@ class LIBCOPP_COPP_API_HEAD_ONLY callable_promise_base<TVALUE, false> : public p
template <class... TARGS>
callable_promise_base(TARGS&&... args)
: data_(callable_promise_value_constructor<value_type, !std::is_constructible<value_type, TARGS...>::value>::
construct(std::forward<TARGS>(args)...)),
has_return_(false) {}
construct(std::forward<TARGS>(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
Expand Down Expand Up @@ -242,7 +239,7 @@ class LIBCOPP_COPP_API_HEAD_ONLY callable_awaitable<TPROMISE, false> : 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<value_type>()(callee_promise.get_status());
}

Expand Down
13 changes: 9 additions & 4 deletions include/libcopp/coroutine/std_coroutine_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <libcopp/utils/config/stl_include_prefix.h> // NOLINT(build/include_order)
// clang-format on
#include <assert.h>
#include <bitset>
#include <cstddef>
#include <memory>
#include <type_traits>
Expand Down Expand Up @@ -53,6 +52,7 @@ enum class LIBCOPP_COPP_API_HEAD_ONLY promise_flag : uint8_t {
kDestroying = 0,
kFinalSuspend = 1,
kInternalWaitting = 2,
kHasReturned = 3,
kMax,
};

Expand Down Expand Up @@ -238,14 +238,19 @@ class promise_base_type {
return false;
}

return flags_.test(static_cast<size_t>(flag));
return 0 != (flags_ & (1 << static_cast<uint8_t>(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<size_t>(flag), value);
uint32_t flag_value = 1 << static_cast<uint8_t>(flag);
if (value) {
flags_ |= flag_value;
} else {
flags_ &= ~flag_value;
}
}

LIBCOPP_COPP_API bool is_waiting() const noexcept;
Expand Down Expand Up @@ -328,7 +333,7 @@ class promise_base_type {

private:
// promise_flags
std::bitset<static_cast<size_t>(promise_flag::kMax)> flags_;
uint32_t flags_;

// promise_status
promise_status status_;
Expand Down
4 changes: 4 additions & 0 deletions include/libcotask/task_promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ class LIBCOPP_COPP_API_HEAD_ONLY task_promise_base<TVALUE, TPRIVATE_DATA, true>
task_promise_base(TARGS&&... args) : context_(std::make_shared<context_type>(std::forward<TARGS>(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);
}
Expand Down Expand Up @@ -473,6 +475,8 @@ class LIBCOPP_COPP_API_HEAD_ONLY task_promise_base<TVALUE, TPRIVATE_DATA, false>
task_promise_base(TARGS&&... args) : context_(std::make_shared<context_type>(std::forward<TARGS>(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);
}
Expand Down

0 comments on commit ce1001b

Please sign in to comment.