5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_IMPL_CO_SPAWN_HPP
12 #define BOOST_ASIO_IMPL_CO_SPAWN_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/awaitable.hpp>
20 #include <boost/asio/dispatch.hpp>
21 #include <boost/asio/post.hpp>
22 #include <boost/asio/use_awaitable.hpp>
24 #include <boost/asio/detail/push_options.hpp>
30 template <typename T, typename Executor, typename F, typename Handler>
31 awaitable<void, Executor> co_spawn_entry_point(
32 awaitable<T, Executor>*, Executor ex, F f, Handler handler)
34 auto spawn_work = make_work_guard(ex);
35 auto handler_work = make_work_guard(handler, ex);
37 (void) co_await (post)(spawn_work.get_executor(),
38 use_awaitable_t<Executor>{});
47 (dispatch)(handler_work.get_executor(),
48 [handler = std::move(handler), t = std::move(t)]() mutable
50 handler(std::exception_ptr(), std::move(t));
58 (dispatch)(handler_work.get_executor(),
59 [handler = std::move(handler), e = std::current_exception()]() mutable
66 template <typename Executor, typename F, typename Handler>
67 awaitable<void, Executor> co_spawn_entry_point(
68 awaitable<void, Executor>*, Executor ex, F f, Handler handler)
70 auto spawn_work = make_work_guard(ex);
71 auto handler_work = make_work_guard(handler, ex);
73 (void) co_await (post)(spawn_work.get_executor(),
74 use_awaitable_t<Executor>{});
76 std::exception_ptr e = nullptr;
83 e = std::current_exception();
86 (dispatch)(handler_work.get_executor(),
87 [handler = std::move(handler), e]() mutable
93 template <typename Executor>
94 class initiate_co_spawn
97 typedef Executor executor_type;
99 template <typename OtherExecutor>
100 explicit initiate_co_spawn(const OtherExecutor& ex)
105 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
110 template <typename Handler, typename F>
111 void operator()(Handler&& handler, F&& f) const
113 typedef typename result_of<F()>::type awaitable_type;
115 auto a = (co_spawn_entry_point)(static_cast<awaitable_type*>(nullptr),
116 ex_, std::forward<F>(f), std::forward<Handler>(handler));
117 awaitable_handler<executor_type, void>(std::move(a), ex_).launch();
124 } // namespace detail
126 template <typename Executor, typename F,
127 BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature<
128 typename result_of<F()>::type>::type) CompletionToken>
129 inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken,
130 typename detail::awaitable_signature<typename result_of<F()>::type>::type)
131 co_spawn(const Executor& ex, F&& f, CompletionToken&& token,
133 is_executor<Executor>::value
136 return async_initiate<CompletionToken,
137 typename detail::awaitable_signature<typename result_of<F()>::type>>(
138 detail::initiate_co_spawn<
139 typename result_of<F()>::type::executor_type>(ex),
140 token, std::forward<F>(f));
143 template <typename ExecutionContext, typename F,
144 BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature<
145 typename result_of<F()>::type>::type) CompletionToken>
146 inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken,
147 typename detail::awaitable_signature<typename result_of<F()>::type>::type)
148 co_spawn(ExecutionContext& ctx, F&& f, CompletionToken&& token,
150 is_convertible<ExecutionContext&, execution_context&>::value
153 return (co_spawn)(ctx.get_executor(), std::forward<F>(f),
154 std::forward<CompletionToken>(token));
160 #include <boost/asio/detail/pop_options.hpp>
162 #endif // BOOST_ASIO_IMPL_CO_SPAWN_HPP