123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224 |
- //
- // experimental/impl/coro.hpp
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2021-2023 Klemens D. Morgenstern
- // (klemens dot morgenstern at gmx dot net)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef BOOST_ASIO_EXPERIMENTAL_IMPL_CORO_HPP
- #define BOOST_ASIO_EXPERIMENTAL_IMPL_CORO_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <boost/asio/detail/config.hpp>
- #include <boost/asio/append.hpp>
- #include <boost/asio/associated_cancellation_slot.hpp>
- #include <boost/asio/bind_allocator.hpp>
- #include <boost/asio/deferred.hpp>
- #include <boost/asio/experimental/detail/coro_completion_handler.hpp>
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- namespace experimental {
- template <typename Yield, typename Return,
- typename Executor, typename Allocator>
- struct coro;
- namespace detail {
- struct coro_cancellation_source
- {
- cancellation_slot slot;
- cancellation_state state;
- bool throw_if_cancelled_ = true;
- void reset_cancellation_state()
- {
- state = cancellation_state(slot);
- }
- template <typename Filter>
- void reset_cancellation_state(Filter&& filter)
- {
- state = cancellation_state(slot, static_cast<Filter&&>(filter));
- }
- template <typename InFilter, typename OutFilter>
- void reset_cancellation_state(InFilter&& in_filter,
- OutFilter&& out_filter)
- {
- state = cancellation_state(slot,
- static_cast<InFilter&&>(in_filter),
- static_cast<OutFilter&&>(out_filter));
- }
- bool throw_if_cancelled() const
- {
- return throw_if_cancelled_;
- }
- void throw_if_cancelled(bool value)
- {
- throw_if_cancelled_ = value;
- }
- };
- template <typename Signature, typename Return,
- typename Executor, typename Allocator>
- struct coro_promise;
- template <typename T>
- struct is_noexcept : std::false_type
- {
- };
- template <typename Return, typename... Args>
- struct is_noexcept<Return(Args...)> : std::false_type
- {
- };
- template <typename Return, typename... Args>
- struct is_noexcept<Return(Args...) noexcept> : std::true_type
- {
- };
- template <typename T>
- constexpr bool is_noexcept_v = is_noexcept<T>::value;
- template <typename T>
- struct coro_error;
- template <>
- struct coro_error<boost::system::error_code>
- {
- static boost::system::error_code invalid()
- {
- return boost::asio::error::fault;
- }
- static boost::system::error_code cancelled()
- {
- return boost::asio::error::operation_aborted;
- }
- static boost::system::error_code interrupted()
- {
- return boost::asio::error::interrupted;
- }
- static boost::system::error_code done()
- {
- return boost::asio::error::broken_pipe;
- }
- };
- template <>
- struct coro_error<std::exception_ptr>
- {
- static std::exception_ptr invalid()
- {
- return std::make_exception_ptr(
- boost::system::system_error(
- coro_error<boost::system::error_code>::invalid()));
- }
- static std::exception_ptr cancelled()
- {
- return std::make_exception_ptr(
- boost::system::system_error(
- coro_error<boost::system::error_code>::cancelled()));
- }
- static std::exception_ptr interrupted()
- {
- return std::make_exception_ptr(
- boost::system::system_error(
- coro_error<boost::system::error_code>::interrupted()));
- }
- static std::exception_ptr done()
- {
- return std::make_exception_ptr(
- boost::system::system_error(
- coro_error<boost::system::error_code>::done()));
- }
- };
- template <typename T, typename Coroutine >
- struct coro_with_arg
- {
- using coro_t = Coroutine;
- T value;
- coro_t& coro;
- struct awaitable_t
- {
- T value;
- coro_t& coro;
- constexpr static bool await_ready() { return false; }
- template <typename Y, typename R, typename E, typename A>
- auto await_suspend(coroutine_handle<coro_promise<Y, R, E, A>> h)
- -> coroutine_handle<>
- {
- auto& hp = h.promise();
- if constexpr (!coro_promise<Y, R, E, A>::is_noexcept)
- {
- if ((hp.cancel->state.cancelled() != cancellation_type::none)
- && hp.cancel->throw_if_cancelled_)
- {
- boost::asio::detail::throw_error(
- boost::asio::error::operation_aborted, "coro-cancelled");
- }
- }
- if (hp.get_executor() == coro.get_executor())
- {
- coro.coro_->awaited_from = h;
- coro.coro_->reset_error();
- coro.coro_->input_ = std::move(value);
- coro.coro_->cancel = hp.cancel;
- return coro.coro_->get_handle();
- }
- else
- {
- coro.coro_->awaited_from =
- dispatch_coroutine(
- boost::asio::prefer(hp.get_executor(),
- execution::outstanding_work.tracked),
- [h]() mutable { h.resume(); }).handle;
- coro.coro_->reset_error();
- coro.coro_->input_ = std::move(value);
- struct cancel_handler
- {
- using src = std::pair<cancellation_signal,
- detail::coro_cancellation_source>;
- std::shared_ptr<src> st = std::make_shared<src>();
- cancel_handler(E e, coro_t& coro) : e(e), coro_(coro.coro_)
- {
- st->second.state =
- cancellation_state(st->second.slot = st->first.slot());
- }
- E e;
- typename coro_t::promise_type* coro_;
- void operator()(cancellation_type ct)
- {
- boost::asio::dispatch(e, [ct, st = st]() mutable
- {
- auto & [sig, state] = *st;
- sig.emit(ct);
- });
- }
- };
- if (hp.cancel->state.slot().is_connected())
- {
- hp.cancel->state.slot().template emplace<cancel_handler>(
- coro.get_executor(), coro);
- }
- auto hh = detail::coroutine_handle<
- typename coro_t::promise_type>::from_promise(*coro.coro_);
- return dispatch_coroutine(
- coro.coro_->get_executor(), [hh]() mutable { hh.resume(); }).handle;
- }
- }
- auto await_resume() -> typename coro_t::result_type
- {
- coro.coro_->cancel = nullptr;
- coro.coro_->rethrow_if();
- return std::move(coro.coro_->result_);
- }
- };
- template <typename CompletionToken>
- auto async_resume(CompletionToken&& token) &&
- {
- return coro.async_resume(std::move(value),
- std::forward<CompletionToken>(token));
- }
- auto operator co_await() &&
- {
- return awaitable_t{std::move(value), coro};
- }
- };
- template <bool IsNoexcept>
- struct coro_promise_error;
- template <>
- struct coro_promise_error<false>
- {
- std::exception_ptr error_;
- void reset_error()
- {
- error_ = std::exception_ptr{};
- }
- void unhandled_exception()
- {
- error_ = std::current_exception();
- }
- void rethrow_if()
- {
- if (error_)
- std::rethrow_exception(error_);
- }
- };
- #if defined(__GNUC__)
- # pragma GCC diagnostic push
- # if defined(__clang__)
- # pragma GCC diagnostic ignored "-Wexceptions"
- # else
- # pragma GCC diagnostic ignored "-Wterminate"
- # endif
- #elif defined(_MSC_VER)
- # pragma warning(push)
- # pragma warning (disable:4297)
- #endif
- template <>
- struct coro_promise_error<true>
- {
- void reset_error()
- {
- }
- void unhandled_exception() noexcept
- {
- throw;
- }
- void rethrow_if()
- {
- }
- };
- #if defined(__GNUC__)
- # pragma GCC diagnostic pop
- #elif defined(_MSC_VER)
- # pragma warning(pop)
- #endif
- template <typename T = void>
- struct yield_input
- {
- T& value;
- coroutine_handle<> awaited_from{noop_coroutine()};
- bool await_ready() const noexcept
- {
- return false;
- }
- template <typename U>
- coroutine_handle<> await_suspend(coroutine_handle<U>) noexcept
- {
- return std::exchange(awaited_from, noop_coroutine());
- }
- T await_resume() const noexcept
- {
- return std::move(value);
- }
- };
- template <>
- struct yield_input<void>
- {
- coroutine_handle<> awaited_from{noop_coroutine()};
- bool await_ready() const noexcept
- {
- return false;
- }
- auto await_suspend(coroutine_handle<>) noexcept
- {
- return std::exchange(awaited_from, noop_coroutine());
- }
- constexpr void await_resume() const noexcept
- {
- }
- };
- struct coro_awaited_from
- {
- coroutine_handle<> awaited_from{noop_coroutine()};
- auto final_suspend() noexcept
- {
- struct suspendor
- {
- coroutine_handle<> awaited_from;
- constexpr static bool await_ready() noexcept
- {
- return false;
- }
- auto await_suspend(coroutine_handle<>) noexcept
- {
- return std::exchange(awaited_from, noop_coroutine());
- }
- constexpr static void await_resume() noexcept
- {
- }
- };
- return suspendor{std::exchange(awaited_from, noop_coroutine())};
- }
- ~coro_awaited_from()
- {
- awaited_from.resume();
- }//must be on the right executor
- };
- template <typename Yield, typename Input, typename Return>
- struct coro_promise_exchange : coro_awaited_from
- {
- using result_type = coro_result_t<Yield, Return>;
- result_type result_;
- Input input_;
- auto yield_value(Yield&& y)
- {
- result_ = std::move(y);
- return yield_input<Input>{std::move(input_),
- std::exchange(awaited_from, noop_coroutine())};
- }
- auto yield_value(const Yield& y)
- {
- result_ = y;
- return yield_input<Input>{std::move(input_),
- std::exchange(awaited_from, noop_coroutine())};
- }
- void return_value(const Return& r)
- {
- result_ = r;
- }
- void return_value(Return&& r)
- {
- result_ = std::move(r);
- }
- };
- template <typename YieldReturn>
- struct coro_promise_exchange<YieldReturn, void, YieldReturn> : coro_awaited_from
- {
- using result_type = coro_result_t<YieldReturn, YieldReturn>;
- result_type result_;
- auto yield_value(const YieldReturn& y)
- {
- result_ = y;
- return yield_input<void>{std::exchange(awaited_from, noop_coroutine())};
- }
- auto yield_value(YieldReturn&& y)
- {
- result_ = std::move(y);
- return yield_input<void>{std::exchange(awaited_from, noop_coroutine())};
- }
- void return_value(const YieldReturn& r)
- {
- result_ = r;
- }
- void return_value(YieldReturn&& r)
- {
- result_ = std::move(r);
- }
- };
- template <typename Yield, typename Return>
- struct coro_promise_exchange<Yield, void, Return> : coro_awaited_from
- {
- using result_type = coro_result_t<Yield, Return>;
- result_type result_;
- auto yield_value(const Yield& y)
- {
- result_.template emplace<0>(y);
- return yield_input<void>{std::exchange(awaited_from, noop_coroutine())};
- }
- auto yield_value(Yield&& y)
- {
- result_.template emplace<0>(std::move(y));
- return yield_input<void>{std::exchange(awaited_from, noop_coroutine())};
- }
- void return_value(const Return& r)
- {
- result_.template emplace<1>(r);
- }
- void return_value(Return&& r)
- {
- result_.template emplace<1>(std::move(r));
- }
- };
- template <typename Yield, typename Input>
- struct coro_promise_exchange<Yield, Input, void> : coro_awaited_from
- {
- using result_type = coro_result_t<Yield, void>;
- result_type result_;
- Input input_;
- auto yield_value(Yield&& y)
- {
- result_ = std::move(y);
- return yield_input<Input>{input_,
- std::exchange(awaited_from, noop_coroutine())};
- }
- auto yield_value(const Yield& y)
- {
- result_ = y;
- return yield_input<Input>{input_,
- std::exchange(awaited_from, noop_coroutine())};
- }
- void return_void()
- {
- result_.reset();
- }
- };
- template <typename Return>
- struct coro_promise_exchange<void, void, Return> : coro_awaited_from
- {
- using result_type = coro_result_t<void, Return>;
- result_type result_;
- void yield_value();
- void return_value(const Return& r)
- {
- result_ = r;
- }
- void return_value(Return&& r)
- {
- result_ = std::move(r);
- }
- };
- template <>
- struct coro_promise_exchange<void, void, void> : coro_awaited_from
- {
- void return_void() {}
- void yield_value();
- };
- template <typename Yield>
- struct coro_promise_exchange<Yield, void, void> : coro_awaited_from
- {
- using result_type = coro_result_t<Yield, void>;
- result_type result_;
- auto yield_value(const Yield& y)
- {
- result_ = y;
- return yield_input<void>{std::exchange(awaited_from, noop_coroutine())};
- }
- auto yield_value(Yield&& y)
- {
- result_ = std::move(y);
- return yield_input<void>{std::exchange(awaited_from, noop_coroutine())};
- }
- void return_void()
- {
- result_.reset();
- }
- };
- template <typename Yield, typename Return,
- typename Executor, typename Allocator>
- struct coro_promise final :
- coro_promise_allocator<Allocator>,
- coro_promise_error<coro_traits<Yield, Return, Executor>::is_noexcept>,
- coro_promise_exchange<
- typename coro_traits<Yield, Return, Executor>::yield_type,
- typename coro_traits<Yield, Return, Executor>::input_type,
- typename coro_traits<Yield, Return, Executor>::return_type>
- {
- using coro_type = coro<Yield, Return, Executor, Allocator>;
- auto handle()
- {
- return coroutine_handle<coro_promise>::from_promise(this);
- }
- using executor_type = Executor;
- executor_type executor_;
- std::optional<coro_cancellation_source> cancel_source;
- coro_cancellation_source * cancel;
- using cancellation_slot_type = boost::asio::cancellation_slot;
- cancellation_slot_type get_cancellation_slot() const noexcept
- {
- return cancel ? cancel->state.slot() : cancellation_slot_type{};
- }
- using allocator_type =
- typename std::allocator_traits<associated_allocator_t<Executor>>::
- template rebind_alloc<std::byte>;
- using traits = coro_traits<Yield, Return, Executor>;
- using input_type = typename traits::input_type;
- using yield_type = typename traits::yield_type;
- using return_type = typename traits::return_type;
- using error_type = typename traits::error_type;
- using result_type = typename traits::result_type;
- constexpr static bool is_noexcept = traits::is_noexcept;
- auto get_executor() const -> Executor
- {
- return executor_;
- }
- auto get_handle()
- {
- return coroutine_handle<coro_promise>::from_promise(*this);
- }
- template <typename... Args>
- coro_promise(Executor executor, Args&&... args) noexcept
- : coro_promise_allocator<Allocator>(
- executor, std::forward<Args>(args)...),
- executor_(std::move(executor))
- {
- }
- template <typename First, typename... Args>
- coro_promise(First&& f, Executor executor, Args&&... args) noexcept
- : coro_promise_allocator<Allocator>(
- f, executor, std::forward<Args>(args)...),
- executor_(std::move(executor))
- {
- }
- template <typename First, detail::execution_context Context, typename... Args>
- coro_promise(First&& f, Context&& ctx, Args&&... args) noexcept
- : coro_promise_allocator<Allocator>(
- f, ctx, std::forward<Args>(args)...),
- executor_(ctx.get_executor())
- {
- }
- template <detail::execution_context Context, typename... Args>
- coro_promise(Context&& ctx, Args&&... args) noexcept
- : coro_promise_allocator<Allocator>(
- ctx, std::forward<Args>(args)...),
- executor_(ctx.get_executor())
- {
- }
- auto get_return_object()
- {
- return coro<Yield, Return, Executor, Allocator>{this};
- }
- auto initial_suspend() noexcept
- {
- return suspend_always{};
- }
- using coro_promise_exchange<
- typename coro_traits<Yield, Return, Executor>::yield_type,
- typename coro_traits<Yield, Return, Executor>::input_type,
- typename coro_traits<Yield, Return, Executor>::return_type>::yield_value;
- auto await_transform(this_coro::executor_t) const
- {
- struct exec_helper
- {
- const executor_type& value;
- constexpr static bool await_ready() noexcept
- {
- return true;
- }
- constexpr static void await_suspend(coroutine_handle<>) noexcept
- {
- }
- executor_type await_resume() const noexcept
- {
- return value;
- }
- };
- return exec_helper{executor_};
- }
- auto await_transform(this_coro::cancellation_state_t) const
- {
- struct exec_helper
- {
- const boost::asio::cancellation_state& value;
- constexpr static bool await_ready() noexcept
- {
- return true;
- }
- constexpr static void await_suspend(coroutine_handle<>) noexcept
- {
- }
- boost::asio::cancellation_state await_resume() const noexcept
- {
- return value;
- }
- };
- assert(cancel);
- return exec_helper{cancel->state};
- }
- // This await transformation resets the associated cancellation state.
- auto await_transform(this_coro::reset_cancellation_state_0_t) noexcept
- {
- struct result
- {
- detail::coro_cancellation_source * src_;
- bool await_ready() const noexcept
- {
- return true;
- }
- void await_suspend(coroutine_handle<void>) noexcept
- {
- }
- auto await_resume() const
- {
- return src_->reset_cancellation_state();
- }
- };
- return result{cancel};
- }
- // This await transformation resets the associated cancellation state.
- template <typename Filter>
- auto await_transform(
- this_coro::reset_cancellation_state_1_t<Filter> reset) noexcept
- {
- struct result
- {
- detail::coro_cancellation_source* src_;
- Filter filter_;
- bool await_ready() const noexcept
- {
- return true;
- }
- void await_suspend(coroutine_handle<void>) noexcept
- {
- }
- auto await_resume()
- {
- return src_->reset_cancellation_state(
- static_cast<Filter&&>(filter_));
- }
- };
- return result{cancel, static_cast<Filter&&>(reset.filter)};
- }
- // This await transformation resets the associated cancellation state.
- template <typename InFilter, typename OutFilter>
- auto await_transform(
- this_coro::reset_cancellation_state_2_t<InFilter, OutFilter> reset)
- noexcept
- {
- struct result
- {
- detail::coro_cancellation_source* src_;
- InFilter in_filter_;
- OutFilter out_filter_;
- bool await_ready() const noexcept
- {
- return true;
- }
- void await_suspend(coroutine_handle<void>) noexcept
- {
- }
- auto await_resume()
- {
- return src_->reset_cancellation_state(
- static_cast<InFilter&&>(in_filter_),
- static_cast<OutFilter&&>(out_filter_));
- }
- };
- return result{cancel,
- static_cast<InFilter&&>(reset.in_filter),
- static_cast<OutFilter&&>(reset.out_filter)};
- }
- // This await transformation determines whether cancellation is propagated as
- // an exception.
- auto await_transform(this_coro::throw_if_cancelled_0_t) noexcept
- requires (!is_noexcept)
- {
- struct result
- {
- detail::coro_cancellation_source* src_;
- bool await_ready() const noexcept
- {
- return true;
- }
- void await_suspend(coroutine_handle<void>) noexcept
- {
- }
- auto await_resume()
- {
- return src_->throw_if_cancelled();
- }
- };
- return result{cancel};
- }
- // This await transformation sets whether cancellation is propagated as an
- // exception.
- auto await_transform(
- this_coro::throw_if_cancelled_1_t throw_if_cancelled) noexcept
- requires (!is_noexcept)
- {
- struct result
- {
- detail::coro_cancellation_source* src_;
- bool value_;
- bool await_ready() const noexcept
- {
- return true;
- }
- void await_suspend(coroutine_handle<void>) noexcept
- {
- }
- auto await_resume()
- {
- src_->throw_if_cancelled(value_);
- }
- };
- return result{cancel, throw_if_cancelled.value};
- }
- template <typename Yield_, typename Return_,
- typename Executor_, typename Allocator_>
- auto await_transform(coro<Yield_, Return_, Executor_, Allocator_>& kr)
- -> decltype(auto)
- {
- return kr;
- }
- template <typename Yield_, typename Return_,
- typename Executor_, typename Allocator_>
- auto await_transform(coro<Yield_, Return_, Executor_, Allocator_>&& kr)
- {
- return std::move(kr);
- }
- template <typename T_, typename Coroutine >
- auto await_transform(coro_with_arg<T_, Coroutine>&& kr) -> decltype(auto)
- {
- return std::move(kr);
- }
- template <typename T_>
- requires requires(T_ t) {{ t.async_wait(deferred) }; }
- auto await_transform(T_& t) -> decltype(auto)
- {
- return await_transform(t.async_wait(deferred));
- }
- template <typename Op>
- auto await_transform(Op&& op,
- constraint_t<is_async_operation<Op>::value> = 0)
- {
- if ((cancel->state.cancelled() != cancellation_type::none)
- && cancel->throw_if_cancelled_)
- {
- boost::asio::detail::throw_error(
- boost::asio::error::operation_aborted, "coro-cancelled");
- }
- using signature = completion_signature_of_t<Op>;
- using result_type = detail::coro_completion_handler_type_t<signature>;
- using handler_type =
- typename detail::coro_completion_handler_type<signature>::template
- completion_handler<coro_promise>;
- struct aw_t
- {
- Op op;
- std::optional<result_type> result;
- constexpr static bool await_ready()
- {
- return false;
- }
- void await_suspend(coroutine_handle<coro_promise> h)
- {
- std::move(op)(handler_type{h, result});
- }
- auto await_resume()
- {
- if constexpr (is_noexcept)
- {
- if constexpr (std::tuple_size_v<result_type> == 0u)
- return;
- else if constexpr (std::tuple_size_v<result_type> == 1u)
- return std::get<0>(std::move(result).value());
- else
- return std::move(result).value();
- }
- else
- return detail::coro_interpret_result(std::move(result).value());
- }
- };
- return aw_t{std::move(op), {}};
- }
- };
- } // namespace detail
- template <typename Yield, typename Return,
- typename Executor, typename Allocator>
- struct coro<Yield, Return, Executor, Allocator>::awaitable_t
- {
- coro& coro_;
- constexpr static bool await_ready() { return false; }
- template <typename Y, typename R, typename E, typename A>
- auto await_suspend(
- detail::coroutine_handle<detail::coro_promise<Y, R, E, A>> h)
- -> detail::coroutine_handle<>
- {
- auto& hp = h.promise();
- if constexpr (!detail::coro_promise<Y, R, E, A>::is_noexcept)
- {
- if ((hp.cancel->state.cancelled() != cancellation_type::none)
- && hp.cancel->throw_if_cancelled_)
- {
- boost::asio::detail::throw_error(
- boost::asio::error::operation_aborted, "coro-cancelled");
- }
- }
- if (hp.get_executor() == coro_.get_executor())
- {
- coro_.coro_->awaited_from = h;
- coro_.coro_->cancel = hp.cancel;
- coro_.coro_->reset_error();
- return coro_.coro_->get_handle();
- }
- else
- {
- coro_.coro_->awaited_from = detail::dispatch_coroutine(
- boost::asio::prefer(hp.get_executor(),
- execution::outstanding_work.tracked),
- [h]() mutable
- {
- h.resume();
- }).handle;
- coro_.coro_->reset_error();
- struct cancel_handler
- {
- std::shared_ptr<std::pair<cancellation_signal,
- detail::coro_cancellation_source>> st = std::make_shared<
- std::pair<cancellation_signal, detail::coro_cancellation_source>>();
- cancel_handler(E e, coro& coro) : e(e), coro_(coro.coro_)
- {
- st->second.state = cancellation_state(
- st->second.slot = st->first.slot());
- }
- E e;
- typename coro::promise_type* coro_;
- void operator()(cancellation_type ct)
- {
- boost::asio::dispatch(e,
- [ct, st = st]() mutable
- {
- auto & [sig, state] = *st;
- sig.emit(ct);
- });
- }
- };
- if (hp.cancel->state.slot().is_connected())
- {
- hp.cancel->state.slot().template emplace<cancel_handler>(
- coro_.get_executor(), coro_);
- }
- auto hh = detail::coroutine_handle<
- detail::coro_promise<Yield, Return, Executor, Allocator>>::from_promise(
- *coro_.coro_);
- return detail::dispatch_coroutine(
- coro_.coro_->get_executor(),
- [hh]() mutable { hh.resume(); }).handle;
- }
- }
- auto await_resume() -> result_type
- {
- coro_.coro_->cancel = nullptr;
- coro_.coro_->rethrow_if();
- if constexpr (!std::is_void_v<result_type>)
- return std::move(coro_.coro_->result_);
- }
- };
- template <typename Yield, typename Return,
- typename Executor, typename Allocator>
- struct coro<Yield, Return, Executor, Allocator>::initiate_async_resume
- {
- typedef Executor executor_type;
- typedef Allocator allocator_type;
- typedef boost::asio::cancellation_slot cancellation_slot_type;
- explicit initiate_async_resume(coro* self)
- : coro_(self->coro_)
- {
- }
- executor_type get_executor() const noexcept
- {
- return coro_->get_executor();
- }
- allocator_type get_allocator() const noexcept
- {
- return coro_->get_allocator();
- }
- template <typename E, typename WaitHandler>
- auto handle(E exec, WaitHandler&& handler,
- std::true_type /* error is noexcept */,
- std::true_type /* result is void */) //noexcept
- {
- return [this, the_coro = coro_,
- h = std::forward<WaitHandler>(handler),
- exec = std::move(exec)]() mutable
- {
- assert(the_coro);
- auto ch = detail::coroutine_handle<promise_type>::from_promise(*the_coro);
- assert(ch && !ch.done());
- the_coro->awaited_from = post_coroutine(std::move(exec), std::move(h));
- the_coro->reset_error();
- ch.resume();
- };
- }
- template <typename E, typename WaitHandler>
- requires (!std::is_void_v<result_type>)
- auto handle(E exec, WaitHandler&& handler,
- std::true_type /* error is noexcept */,
- std::false_type /* result is void */) //noexcept
- {
- return [the_coro = coro_,
- h = std::forward<WaitHandler>(handler),
- exec = std::move(exec)]() mutable
- {
- assert(the_coro);
- auto ch = detail::coroutine_handle<promise_type>::from_promise(*the_coro);
- assert(ch && !ch.done());
- the_coro->awaited_from = detail::post_coroutine(
- exec, std::move(h), the_coro->result_).handle;
- the_coro->reset_error();
- ch.resume();
- };
- }
- template <typename E, typename WaitHandler>
- auto handle(E exec, WaitHandler&& handler,
- std::false_type /* error is noexcept */,
- std::true_type /* result is void */)
- {
- return [the_coro = coro_,
- h = std::forward<WaitHandler>(handler),
- exec = std::move(exec)]() mutable
- {
- if (!the_coro)
- return boost::asio::post(exec,
- boost::asio::append(std::move(h),
- detail::coro_error<error_type>::invalid()));
- auto ch = detail::coroutine_handle<promise_type>::from_promise(*the_coro);
- if (!ch)
- return boost::asio::post(exec,
- boost::asio::append(std::move(h),
- detail::coro_error<error_type>::invalid()));
- else if (ch.done())
- return boost::asio::post(exec,
- boost::asio::append(std::move(h),
- detail::coro_error<error_type>::done()));
- else
- {
- the_coro->awaited_from = detail::post_coroutine(
- exec, std::move(h), the_coro->error_).handle;
- the_coro->reset_error();
- ch.resume();
- }
- };
- }
- template <typename E, typename WaitHandler>
- auto handle(E exec, WaitHandler&& handler,
- std::false_type /* error is noexcept */,
- std::false_type /* result is void */)
- {
- return [the_coro = coro_,
- h = std::forward<WaitHandler>(handler),
- exec = std::move(exec)]() mutable
- {
- if (!the_coro)
- return boost::asio::post(exec,
- boost::asio::append(std::move(h),
- detail::coro_error<error_type>::invalid(), result_type{}));
- auto ch =
- detail::coroutine_handle<promise_type>::from_promise(*the_coro);
- if (!ch)
- return boost::asio::post(exec,
- boost::asio::append(std::move(h),
- detail::coro_error<error_type>::invalid(), result_type{}));
- else if (ch.done())
- return boost::asio::post(exec,
- boost::asio::append(std::move(h),
- detail::coro_error<error_type>::done(), result_type{}));
- else
- {
- the_coro->awaited_from = detail::post_coroutine(
- exec, std::move(h), the_coro->error_, the_coro->result_).handle;
- the_coro->reset_error();
- ch.resume();
- }
- };
- }
- template <typename WaitHandler>
- void operator()(WaitHandler&& handler)
- {
- const auto exec = boost::asio::prefer(
- get_associated_executor(handler, get_executor()),
- execution::outstanding_work.tracked);
- coro_->cancel = &coro_->cancel_source.emplace();
- coro_->cancel->state = cancellation_state(
- coro_->cancel->slot = get_associated_cancellation_slot(handler));
- boost::asio::dispatch(get_executor(),
- handle(exec, std::forward<WaitHandler>(handler),
- std::integral_constant<bool, is_noexcept>{},
- std::is_void<result_type>{}));
- }
- template <typename WaitHandler, typename Input>
- void operator()(WaitHandler&& handler, Input&& input)
- {
- const auto exec = boost::asio::prefer(
- get_associated_executor(handler, get_executor()),
- execution::outstanding_work.tracked);
- coro_->cancel = &coro_->cancel_source.emplace();
- coro_->cancel->state = cancellation_state(
- coro_->cancel->slot = get_associated_cancellation_slot(handler));
- boost::asio::dispatch(get_executor(),
- [h = handle(exec, std::forward<WaitHandler>(handler),
- std::integral_constant<bool, is_noexcept>{},
- std::is_void<result_type>{}),
- in = std::forward<Input>(input), the_coro = coro_]() mutable
- {
- the_coro->input_ = std::move(in);
- std::move(h)();
- });
- }
- private:
- typename coro::promise_type* coro_;
- };
- } // namespace experimental
- } // namespace asio
- } // namespace boost
- #include <boost/asio/detail/pop_options.hpp>
- #endif // BOOST_ASIO_EXPERIMENTAL_IMPL_CORO_HPP
|