// // impl/co_spawn.hpp // ~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // 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 ASIO_IMPL_CO_SPAWN_HPP #define ASIO_IMPL_CO_SPAWN_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/associated_cancellation_slot.hpp" #include "asio/awaitable.hpp" #include "asio/dispatch.hpp" #include "asio/execution/outstanding_work.hpp" #include "asio/post.hpp" #include "asio/prefer.hpp" #include "asio/use_awaitable.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template class co_spawn_work_guard { public: typedef typename decay< typename prefer_result::type >::type executor_type; co_spawn_work_guard(const Executor& ex) : executor_(asio::prefer(ex, execution::outstanding_work.tracked)) { } executor_type get_executor() const ASIO_NOEXCEPT { return executor_; } private: executor_type executor_; }; #if !defined(ASIO_NO_TS_EXECUTORS) template struct co_spawn_work_guard::value >::type> : executor_work_guard { co_spawn_work_guard(const Executor& ex) : executor_work_guard(ex) { } }; #endif // !defined(ASIO_NO_TS_EXECUTORS) template inline co_spawn_work_guard make_co_spawn_work_guard(const Executor& ex) { return co_spawn_work_guard(ex); } template awaitable co_spawn_entry_point( awaitable*, Executor ex, F f, Handler handler) { auto spawn_work = make_co_spawn_work_guard(ex); auto handler_work = make_co_spawn_work_guard( asio::get_associated_executor(handler, ex)); (void) co_await (dispatch)( use_awaitable_t{__FILE__, __LINE__, "co_spawn_entry_point"}); (co_await awaitable_thread_has_context_switched{}) = false; std::exception_ptr e = nullptr; bool done = false; try { T t = co_await f(); done = true; bool switched = (co_await awaitable_thread_has_context_switched{}); if (!switched) { (void) co_await (post)( use_awaitable_t{__FILE__, __LINE__, "co_spawn_entry_point"}); } (dispatch)(handler_work.get_executor(), [handler = std::move(handler), t = std::move(t)]() mutable { std::move(handler)(std::exception_ptr(), std::move(t)); }); co_return; } catch (...) { if (done) throw; e = std::current_exception(); } bool switched = (co_await awaitable_thread_has_context_switched{}); if (!switched) { (void) co_await (post)( use_awaitable_t{__FILE__, __LINE__, "co_spawn_entry_point"}); } (dispatch)(handler_work.get_executor(), [handler = std::move(handler), e]() mutable { std::move(handler)(e, T()); }); } template awaitable co_spawn_entry_point( awaitable*, Executor ex, F f, Handler handler) { auto spawn_work = make_co_spawn_work_guard(ex); auto handler_work = make_co_spawn_work_guard( asio::get_associated_executor(handler, ex)); (void) co_await (dispatch)( use_awaitable_t{__FILE__, __LINE__, "co_spawn_entry_point"}); (co_await awaitable_thread_has_context_switched{}) = false; std::exception_ptr e = nullptr; try { co_await f(); } catch (...) { e = std::current_exception(); } bool switched = (co_await awaitable_thread_has_context_switched{}); if (!switched) { (void) co_await (post)( use_awaitable_t{__FILE__, __LINE__, "co_spawn_entry_point"}); } (dispatch)(handler_work.get_executor(), [handler = std::move(handler), e]() mutable { std::move(handler)(e); }); } template class awaitable_as_function { public: explicit awaitable_as_function(awaitable&& a) : awaitable_(std::move(a)) { } awaitable operator()() { return std::move(awaitable_); } private: awaitable awaitable_; }; template class co_spawn_cancellation_handler { public: co_spawn_cancellation_handler(const Handler&, const Executor& ex) : ex_(ex) { } cancellation_slot slot() { return signal_.slot(); } void operator()(cancellation_type_t type) { cancellation_signal* sig = &signal_; asio::dispatch(ex_, [sig, type]{ sig->emit(type); }); } private: cancellation_signal signal_; Executor ex_; }; template class co_spawn_cancellation_handler::asio_associated_executor_is_unspecialised, void >::value >::type> { public: co_spawn_cancellation_handler(const Handler&, const Executor&) { } cancellation_slot slot() { return signal_.slot(); } void operator()(cancellation_type_t type) { signal_.emit(type); } private: cancellation_signal signal_; }; template class initiate_co_spawn { public: typedef Executor executor_type; template explicit initiate_co_spawn(const OtherExecutor& ex) : ex_(ex) { } executor_type get_executor() const ASIO_NOEXCEPT { return ex_; } template void operator()(Handler&& handler, F&& f) const { typedef typename result_of::type awaitable_type; typedef typename decay::type handler_type; typedef co_spawn_cancellation_handler< handler_type, Executor> cancel_handler_type; auto slot = asio::get_associated_cancellation_slot(handler); cancel_handler_type* cancel_handler = slot.is_connected() ? &slot.template emplace(handler, ex_) : nullptr; cancellation_slot proxy_slot( cancel_handler ? cancel_handler->slot() : cancellation_slot()); cancellation_state cancel_state(proxy_slot); auto a = (co_spawn_entry_point)(static_cast(nullptr), ex_, std::forward(f), std::forward(handler)); awaitable_handler(std::move(a), ex_, proxy_slot, cancel_state).launch(); } private: Executor ex_; }; } // namespace detail template inline ASIO_INITFN_AUTO_RESULT_TYPE( CompletionToken, void(std::exception_ptr, T)) co_spawn(const Executor& ex, awaitable a, CompletionToken&& token, typename constraint< (is_executor::value || execution::is_executor::value) && is_convertible::value >::type) { return async_initiate( detail::initiate_co_spawn(AwaitableExecutor(ex)), token, detail::awaitable_as_function(std::move(a))); } template inline ASIO_INITFN_AUTO_RESULT_TYPE( CompletionToken, void(std::exception_ptr)) co_spawn(const Executor& ex, awaitable a, CompletionToken&& token, typename constraint< (is_executor::value || execution::is_executor::value) && is_convertible::value >::type) { return async_initiate( detail::initiate_co_spawn(AwaitableExecutor(ex)), token, detail::awaitable_as_function< void, AwaitableExecutor>(std::move(a))); } template inline ASIO_INITFN_AUTO_RESULT_TYPE( CompletionToken, void(std::exception_ptr, T)) co_spawn(ExecutionContext& ctx, awaitable a, CompletionToken&& token, typename constraint< is_convertible::value && is_convertible::value >::type) { return (co_spawn)(ctx.get_executor(), std::move(a), std::forward(token)); } template inline ASIO_INITFN_AUTO_RESULT_TYPE( CompletionToken, void(std::exception_ptr)) co_spawn(ExecutionContext& ctx, awaitable a, CompletionToken&& token, typename constraint< is_convertible::value && is_convertible::value >::type) { return (co_spawn)(ctx.get_executor(), std::move(a), std::forward(token)); } template ::type>::type) CompletionToken> inline ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, typename detail::awaitable_signature::type>::type) co_spawn(const Executor& ex, F&& f, CompletionToken&& token, typename constraint< is_executor::value || execution::is_executor::value >::type) { return async_initiate::type>::type>( detail::initiate_co_spawn< typename result_of::type::executor_type>(ex), token, std::forward(f)); } template ::type>::type) CompletionToken> inline ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, typename detail::awaitable_signature::type>::type) co_spawn(ExecutionContext& ctx, F&& f, CompletionToken&& token, typename constraint< is_convertible::value >::type) { return (co_spawn)(ctx.get_executor(), std::forward(f), std::forward(token)); } } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_IMPL_CO_SPAWN_HPP