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_SPAWN_HPP
12 #define BOOST_ASIO_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/coroutine/all.hpp>
20 #include <boost/asio/bind_executor.hpp>
21 #include <boost/asio/detail/memory.hpp>
22 #include <boost/asio/detail/type_traits.hpp>
23 #include <boost/asio/detail/wrapped_handler.hpp>
24 #include <boost/asio/executor.hpp>
25 #include <boost/asio/io_context.hpp>
26 #include <boost/asio/is_executor.hpp>
27 #include <boost/asio/strand.hpp>
29 #include <boost/asio/detail/push_options.hpp>
34 /// Context object the represents the currently executing coroutine.
36 * The basic_yield_context class is used to represent the currently executing
37 * stackful coroutine. A basic_yield_context may be passed as a handler to an
38 * asynchronous operation. For example:
40 * @code template <typename Handler>
41 * void my_coroutine(basic_yield_context<Handler> yield)
44 * std::size_t n = my_socket.async_read_some(buffer, yield);
48 * The initiating function (async_read_some in the above example) suspends the
49 * current coroutine. The coroutine is resumed when the asynchronous operation
50 * completes, and the result of the operation is returned.
52 template <typename Handler>
53 class basic_yield_context
56 /// The coroutine callee type, used by the implementation.
58 * When using Boost.Coroutine v1, this type is:
59 * @code typename coroutine<void()> @endcode
60 * When using Boost.Coroutine v2 (unidirectional coroutines), this type is:
61 * @code push_coroutine<void> @endcode
63 #if defined(GENERATING_DOCUMENTATION)
64 typedef implementation_defined callee_type;
65 #elif defined(BOOST_COROUTINES_UNIDIRECT) || defined(BOOST_COROUTINES_V2)
66 typedef boost::coroutines::push_coroutine<void> callee_type;
68 typedef boost::coroutines::coroutine<void()> callee_type;
71 /// The coroutine caller type, used by the implementation.
73 * When using Boost.Coroutine v1, this type is:
74 * @code typename coroutine<void()>::caller_type @endcode
75 * When using Boost.Coroutine v2 (unidirectional coroutines), this type is:
76 * @code pull_coroutine<void> @endcode
78 #if defined(GENERATING_DOCUMENTATION)
79 typedef implementation_defined caller_type;
80 #elif defined(BOOST_COROUTINES_UNIDIRECT) || defined(BOOST_COROUTINES_V2)
81 typedef boost::coroutines::pull_coroutine<void> caller_type;
83 typedef boost::coroutines::coroutine<void()>::caller_type caller_type;
86 /// Construct a yield context to represent the specified coroutine.
88 * Most applications do not need to use this constructor. Instead, the
89 * spawn() function passes a yield context as an argument to the coroutine
93 const detail::weak_ptr<callee_type>& coro,
94 caller_type& ca, Handler& handler)
102 /// Construct a yield context from another yield context type.
104 * Requires that OtherHandler be convertible to Handler.
106 template <typename OtherHandler>
107 basic_yield_context(const basic_yield_context<OtherHandler>& other)
108 : coro_(other.coro_),
110 handler_(other.handler_),
115 /// Return a yield context that sets the specified error_code.
117 * By default, when a yield context is used with an asynchronous operation, a
118 * non-success error_code is converted to system_error and thrown. This
119 * operator may be used to specify an error_code object that should instead be
120 * set with the asynchronous operation's result. For example:
122 * @code template <typename Handler>
123 * void my_coroutine(basic_yield_context<Handler> yield)
126 * std::size_t n = my_socket.async_read_some(buffer, yield[ec]);
129 * // An error occurred.
134 basic_yield_context operator[](boost::system::error_code& ec) const
136 basic_yield_context tmp(*this);
141 #if defined(GENERATING_DOCUMENTATION)
143 #endif // defined(GENERATING_DOCUMENTATION)
144 detail::weak_ptr<callee_type> coro_;
147 boost::system::error_code* ec_;
150 #if defined(GENERATING_DOCUMENTATION)
151 /// Context object that represents the currently executing coroutine.
152 typedef basic_yield_context<unspecified> yield_context;
153 #else // defined(GENERATING_DOCUMENTATION)
154 typedef basic_yield_context<
155 executor_binder<void(*)(), executor> > yield_context;
156 #endif // defined(GENERATING_DOCUMENTATION)
159 * @defgroup spawn boost::asio::spawn
161 * @brief Start a new stackful coroutine.
163 * The spawn() function is a high-level wrapper over the Boost.Coroutine
164 * library. This function enables programs to implement asynchronous logic in a
165 * synchronous manner, as illustrated by the following example:
167 * @code boost::asio::spawn(my_strand, do_echo);
171 * void do_echo(boost::asio::yield_context yield)
178 * std::size_t length =
179 * my_socket.async_read_some(
180 * boost::asio::buffer(data), yield);
182 * boost::asio::async_write(my_socket,
183 * boost::asio::buffer(data, length), yield);
186 * catch (std::exception& e)
194 /// Start a new stackful coroutine, calling the specified handler when it
197 * This function is used to launch a new coroutine.
199 * @param function The coroutine function. The function must have the signature:
200 * @code void function(basic_yield_context<Handler> yield); @endcode
202 * @param attributes Boost.Coroutine attributes used to customise the coroutine.
204 template <typename Function>
205 void spawn(BOOST_ASIO_MOVE_ARG(Function) function,
206 const boost::coroutines::attributes& attributes
207 = boost::coroutines::attributes());
209 /// Start a new stackful coroutine, calling the specified handler when it
212 * This function is used to launch a new coroutine.
214 * @param handler A handler to be called when the coroutine exits. More
215 * importantly, the handler provides an execution context (via the the handler
216 * invocation hook) for the coroutine. The handler must have the signature:
217 * @code void handler(); @endcode
219 * @param function The coroutine function. The function must have the signature:
220 * @code void function(basic_yield_context<Handler> yield); @endcode
222 * @param attributes Boost.Coroutine attributes used to customise the coroutine.
224 template <typename Handler, typename Function>
225 void spawn(BOOST_ASIO_MOVE_ARG(Handler) handler,
226 BOOST_ASIO_MOVE_ARG(Function) function,
227 const boost::coroutines::attributes& attributes
228 = boost::coroutines::attributes(),
229 typename enable_if<!is_executor<typename decay<Handler>::type>::value &&
230 !is_convertible<Handler&, execution_context&>::value>::type* = 0);
232 /// Start a new stackful coroutine, inheriting the execution context of another.
234 * This function is used to launch a new coroutine.
236 * @param ctx Identifies the current coroutine as a parent of the new
237 * coroutine. This specifies that the new coroutine should inherit the
238 * execution context of the parent. For example, if the parent coroutine is
239 * executing in a particular strand, then the new coroutine will execute in the
242 * @param function The coroutine function. The function must have the signature:
243 * @code void function(basic_yield_context<Handler> yield); @endcode
245 * @param attributes Boost.Coroutine attributes used to customise the coroutine.
247 template <typename Handler, typename Function>
248 void spawn(basic_yield_context<Handler> ctx,
249 BOOST_ASIO_MOVE_ARG(Function) function,
250 const boost::coroutines::attributes& attributes
251 = boost::coroutines::attributes());
253 /// Start a new stackful coroutine that executes on a given executor.
255 * This function is used to launch a new coroutine.
257 * @param ex Identifies the executor that will run the coroutine. The new
258 * coroutine is implicitly given its own strand within this executor.
260 * @param function The coroutine function. The function must have the signature:
261 * @code void function(yield_context yield); @endcode
263 * @param attributes Boost.Coroutine attributes used to customise the coroutine.
265 template <typename Function, typename Executor>
266 void spawn(const Executor& ex,
267 BOOST_ASIO_MOVE_ARG(Function) function,
268 const boost::coroutines::attributes& attributes
269 = boost::coroutines::attributes(),
270 typename enable_if<is_executor<Executor>::value>::type* = 0);
272 /// Start a new stackful coroutine that executes on a given strand.
274 * This function is used to launch a new coroutine.
276 * @param ex Identifies the strand that will run the coroutine.
278 * @param function The coroutine function. The function must have the signature:
279 * @code void function(yield_context yield); @endcode
281 * @param attributes Boost.Coroutine attributes used to customise the coroutine.
283 template <typename Function, typename Executor>
284 void spawn(const strand<Executor>& ex,
285 BOOST_ASIO_MOVE_ARG(Function) function,
286 const boost::coroutines::attributes& attributes
287 = boost::coroutines::attributes());
289 /// Start a new stackful coroutine that executes in the context of a strand.
291 * This function is used to launch a new coroutine.
293 * @param s Identifies a strand. By starting multiple coroutines on the same
294 * strand, the implementation ensures that none of those coroutines can execute
297 * @param function The coroutine function. The function must have the signature:
298 * @code void function(yield_context yield); @endcode
300 * @param attributes Boost.Coroutine attributes used to customise the coroutine.
302 template <typename Function>
303 void spawn(const boost::asio::io_context::strand& s,
304 BOOST_ASIO_MOVE_ARG(Function) function,
305 const boost::coroutines::attributes& attributes
306 = boost::coroutines::attributes());
308 /// Start a new stackful coroutine that executes on a given execution context.
310 * This function is used to launch a new coroutine.
312 * @param ctx Identifies the execution context that will run the coroutine. The
313 * new coroutine is implicitly given its own strand within this execution
316 * @param function The coroutine function. The function must have the signature:
317 * @code void function(yield_context yield); @endcode
319 * @param attributes Boost.Coroutine attributes used to customise the coroutine.
321 template <typename Function, typename ExecutionContext>
322 void spawn(ExecutionContext& ctx,
323 BOOST_ASIO_MOVE_ARG(Function) function,
324 const boost::coroutines::attributes& attributes
325 = boost::coroutines::attributes(),
326 typename enable_if<is_convertible<
327 ExecutionContext&, execution_context&>::value>::type* = 0);
334 #include <boost/asio/detail/pop_options.hpp>
336 #include <boost/asio/impl/spawn.hpp>
338 #endif // BOOST_ASIO_SPAWN_HPP