34 constexpr static bool IsVoid =
false;
38 template<
typename U = R>
41 std::lock_guard guard { self };
42 self.Ret_.emplace (std::forward<U> (val));
55 std::lock_guard guard { self };
62 template<
typename Promise>
65 using Handle_t = std::coroutine_handle<Promise>;
78 :
Subtask_ { std::exchange (other.Subtask_, {}) }
79 ,
OuterTask_ { std::exchange (other.OuterTask_, {}) }
92 std::lock_guard guard { promise };
102 const auto& promise =
Subtask_.promise ();
103 std::lock_guard guard { promise };
104 return CheckTaskFinishedUnlocked (promise);
109 auto& promise =
Subtask_.promise ();
110 std::lock_guard guard { promise };
111 if (CheckTaskFinishedUnlocked (promise))
115 promise.AddAwaiter (handle);
121 auto& promise =
Subtask_.promise ();
122 std::lock_guard guard { promise };
123 if (promise.Exception_)
126 std::rethrow_exception (promise.Exception_);
132 if constexpr (!Promise::IsVoid)
133 return Promise::ResumeValue (*promise.Ret_);
136 bool CheckTaskFinishedUnlocked (
const Promise& promise)
const
138 if (promise.Exception_)
141 if constexpr (Promise::IsVoid)
142 return promise.Done_;
144 return static_cast<bool> (promise.Ret_);
149 template<
typename R,
template<
typename>
typename... Extensions>
155 using Handle_t = std::coroutine_handle<promise_type>;
160 , Extensions<promise_type>...
165 auto GetAddress () {
return Handle_t::from_promise (*this).address (); }
169 return Task { Handle_t::from_promise (*
this) };
183 this->
Refs_.fetch_add (1);
188 if (this->
Refs_.fetch_sub (1) == 1)
189 Handle_t::from_promise (*this).destroy ();
195 template<
typename RR>
198 template<
template<
typename>
typename F>
201 explicit Task (
const std::coroutine_handle<promise_type>& handle)
205 handle.promise ().IncRef ();
211 Handle_.promise ().DecRef ();
215 : Handle_ { other.Handle_ }
218 Handle_.promise ().IncRef ();
224 *
this = std::move (task);
230 std::swap (Handle_, other.Handle_);
235 std::swap (Handle_, other.Handle_);
239 auto operator co_await ()
const noexcept
247 template<
typename R,
template<
typename>
typename... Extensions>
Task & operator=(const Task &other)
Task(Task &&other) noexcept
Task< F< T >, SharedTaskExtension... > ApplyResult_t
Task< RR, SharedTaskExtension... > ReplaceResult_t
Task(const std::coroutine_handle< promise_type > &handle)
std::suspend_never initial_suspend() const noexcept
auto final_suspend() noexcept
void unhandled_exception()
bool await_ready() const noexcept
decltype(auto) await_resume() const noexcept
bool await_suspend(std::coroutine_handle< Promise > handle) noexcept
typename Task< R, Extensions... >::promise_type Promise
std::exception_ptr Exception_
std::atomic< size_t > Refs_
static constexpr bool IsVoid
void return_void(this auto &&self) noexcept
static constexpr bool IsVoid
void return_value(this auto &&self, U &&val)
bool await_ready() const noexcept
TaskAwaiter(Handle_t subtask)
std::coroutine_handle< Promise > Handle_t
TaskAwaiter & operator=(const TaskAwaiter &)=delete
TaskAwaiter(TaskAwaiter &&other) noexcept
bool await_suspend(std::coroutine_handle<> handle)
TaskAwaiter(const TaskAwaiter &)=delete
std::coroutine_handle OuterTask_