42 constexpr static bool IsVoid =
false;
46 template<
typename U = R>
49 std::lock_guard guard { self };
50 self.Ret_.emplace (std::forward<U> (val));
63 std::lock_guard guard { self };
70 template<
typename Promise>
73 using Handle_t = std::coroutine_handle<Promise>;
86 :
Subtask_ { std::exchange (other.Subtask_, {}) }
87 ,
OuterTask_ { std::exchange (other.OuterTask_, {}) }
100 std::lock_guard guard { promise };
110 const auto& promise =
Subtask_.promise ();
111 std::lock_guard guard { promise };
112 return CheckTaskFinishedUnlocked (promise);
117 auto& promise =
Subtask_.promise ();
118 std::lock_guard guard { promise };
119 if (CheckTaskFinishedUnlocked (promise))
123 promise.AddAwaiter (handle);
129 auto& promise =
Subtask_.promise ();
130 std::lock_guard guard { promise };
131 if (promise.Exception_)
134 std::rethrow_exception (promise.Exception_);
140 if constexpr (!Promise::IsVoid)
141 return Promise::ResumeValue (*promise.Ret_);
144 bool CheckTaskFinishedUnlocked (
const Promise& promise)
const
146 if (promise.Exception_)
149 if constexpr (Promise::IsVoid)
150 return promise.Done_;
152 return static_cast<bool> (promise.Ret_);
157 template<
typename R,
template<
typename>
typename... Extensions>
163 using Handle_t = std::coroutine_handle<promise_type>;
168 , Extensions<promise_type>...
173 auto GetAddress () {
return Handle_t::from_promise (*this).address (); }
177 return Task { Handle_t::from_promise (*
this) };
195 this->
Refs_.fetch_add (1);
200 if (this->
Refs_.fetch_sub (1) == 1)
201 Handle_t::from_promise (*this).destroy ();
207 template<
typename RR>
210 template<
template<
typename>
typename F>
213 explicit Task (
const std::coroutine_handle<promise_type>& handle)
217 handle.promise ().IncRef ();
223 Handle_.promise ().DecRef ();
227 : Handle_ { other.Handle_ }
230 Handle_.promise ().IncRef ();
236 *
this = std::move (task);
242 std::swap (Handle_, other.Handle_);
247 std::swap (Handle_, other.Handle_);
251 auto operator co_await ()
const noexcept
259 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_