spawn.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // spawn.hpp
  3. // ~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef ASIO_SPAWN_HPP
  11. #define ASIO_SPAWN_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <boost/coroutine/all.hpp>
  17. #include "asio/detail/weak_ptr.hpp"
  18. #include "asio/detail/wrapped_handler.hpp"
  19. #include "asio/io_service.hpp"
  20. #include "asio/strand.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. /// Context object the represents the currently executing coroutine.
  24. /**
  25. * The basic_yield_context class is used to represent the currently executing
  26. * stackful coroutine. A basic_yield_context may be passed as a handler to an
  27. * asynchronous operation. For example:
  28. *
  29. * @code template <typename Handler>
  30. * void my_coroutine(basic_yield_context<Handler> yield)
  31. * {
  32. * ...
  33. * std::size_t n = my_socket.async_read_some(buffer, yield);
  34. * ...
  35. * } @endcode
  36. *
  37. * The initiating function (async_read_some in the above example) suspends the
  38. * current coroutine. The coroutine is resumed when the asynchronous operation
  39. * completes, and the result of the operation is returned.
  40. */
  41. template <typename Handler>
  42. class basic_yield_context
  43. {
  44. public:
  45. /// The coroutine callee type, used by the implementation.
  46. /**
  47. * When using Boost.Coroutine v1, this type is:
  48. * @code typename coroutine<void()> @endcode
  49. * When using Boost.Coroutine v2 (unidirectional coroutines), this type is:
  50. * @code push_coroutine<void> @endcode
  51. */
  52. #if defined(GENERATING_DOCUMENTATION)
  53. typedef implementation_defined callee_type;
  54. #elif defined(BOOST_COROUTINES_UNIDIRECT) || defined(BOOST_COROUTINES_V2)
  55. typedef boost::coroutines::push_coroutine<void> callee_type;
  56. #else
  57. typedef boost::coroutines::coroutine<void()> callee_type;
  58. #endif
  59. /// The coroutine caller type, used by the implementation.
  60. /**
  61. * When using Boost.Coroutine v1, this type is:
  62. * @code typename coroutine<void()>::caller_type @endcode
  63. * When using Boost.Coroutine v2 (unidirectional coroutines), this type is:
  64. * @code pull_coroutine<void> @endcode
  65. */
  66. #if defined(GENERATING_DOCUMENTATION)
  67. typedef implementation_defined caller_type;
  68. #elif defined(BOOST_COROUTINES_UNIDIRECT) || defined(BOOST_COROUTINES_V2)
  69. typedef boost::coroutines::pull_coroutine<void> caller_type;
  70. #else
  71. typedef boost::coroutines::coroutine<void()>::caller_type caller_type;
  72. #endif
  73. /// Construct a yield context to represent the specified coroutine.
  74. /**
  75. * Most applications do not need to use this constructor. Instead, the
  76. * spawn() function passes a yield context as an argument to the coroutine
  77. * function.
  78. */
  79. basic_yield_context(
  80. const detail::weak_ptr<callee_type>& coro,
  81. caller_type& ca, Handler& handler)
  82. : coro_(coro),
  83. ca_(ca),
  84. handler_(handler),
  85. ec_(0)
  86. {
  87. }
  88. /// Return a yield context that sets the specified error_code.
  89. /**
  90. * By default, when a yield context is used with an asynchronous operation, a
  91. * non-success error_code is converted to system_error and thrown. This
  92. * operator may be used to specify an error_code object that should instead be
  93. * set with the asynchronous operation's result. For example:
  94. *
  95. * @code template <typename Handler>
  96. * void my_coroutine(basic_yield_context<Handler> yield)
  97. * {
  98. * ...
  99. * std::size_t n = my_socket.async_read_some(buffer, yield[ec]);
  100. * if (ec)
  101. * {
  102. * // An error occurred.
  103. * }
  104. * ...
  105. * } @endcode
  106. */
  107. basic_yield_context operator[](asio::error_code& ec) const
  108. {
  109. basic_yield_context tmp(*this);
  110. tmp.ec_ = &ec;
  111. return tmp;
  112. }
  113. #if defined(GENERATING_DOCUMENTATION)
  114. private:
  115. #endif // defined(GENERATING_DOCUMENTATION)
  116. detail::weak_ptr<callee_type> coro_;
  117. caller_type& ca_;
  118. Handler& handler_;
  119. asio::error_code* ec_;
  120. };
  121. #if defined(GENERATING_DOCUMENTATION)
  122. /// Context object that represents the currently executing coroutine.
  123. typedef basic_yield_context<unspecified> yield_context;
  124. #else // defined(GENERATING_DOCUMENTATION)
  125. typedef basic_yield_context<
  126. detail::wrapped_handler<
  127. io_service::strand, void(*)(),
  128. detail::is_continuation_if_running> > yield_context;
  129. #endif // defined(GENERATING_DOCUMENTATION)
  130. /**
  131. * @defgroup spawn asio::spawn
  132. *
  133. * @brief Start a new stackful coroutine.
  134. *
  135. * The spawn() function is a high-level wrapper over the Boost.Coroutine
  136. * library. This function enables programs to implement asynchronous logic in a
  137. * synchronous manner, as illustrated by the following example:
  138. *
  139. * @code asio::spawn(my_strand, do_echo);
  140. *
  141. * // ...
  142. *
  143. * void do_echo(asio::yield_context yield)
  144. * {
  145. * try
  146. * {
  147. * char data[128];
  148. * for (;;)
  149. * {
  150. * std::size_t length =
  151. * my_socket.async_read_some(
  152. * asio::buffer(data), yield);
  153. *
  154. * asio::async_write(my_socket,
  155. * asio::buffer(data, length), yield);
  156. * }
  157. * }
  158. * catch (std::exception& e)
  159. * {
  160. * // ...
  161. * }
  162. * } @endcode
  163. */
  164. /*@{*/
  165. /// Start a new stackful coroutine, calling the specified handler when it
  166. /// completes.
  167. /**
  168. * This function is used to launch a new coroutine.
  169. *
  170. * @param handler A handler to be called when the coroutine exits. More
  171. * importantly, the handler provides an execution context (via the the handler
  172. * invocation hook) for the coroutine. The handler must have the signature:
  173. * @code void handler(); @endcode
  174. *
  175. * @param function The coroutine function. The function must have the signature:
  176. * @code void function(basic_yield_context<Handler> yield); @endcode
  177. *
  178. * @param attributes Boost.Coroutine attributes used to customise the coroutine.
  179. */
  180. template <typename Handler, typename Function>
  181. void spawn(ASIO_MOVE_ARG(Handler) handler,
  182. ASIO_MOVE_ARG(Function) function,
  183. const boost::coroutines::attributes& attributes
  184. = boost::coroutines::attributes());
  185. /// Start a new stackful coroutine, inheriting the execution context of another.
  186. /**
  187. * This function is used to launch a new coroutine.
  188. *
  189. * @param ctx Identifies the current coroutine as a parent of the new
  190. * coroutine. This specifies that the new coroutine should inherit the
  191. * execution context of the parent. For example, if the parent coroutine is
  192. * executing in a particular strand, then the new coroutine will execute in the
  193. * same strand.
  194. *
  195. * @param function The coroutine function. The function must have the signature:
  196. * @code void function(basic_yield_context<Handler> yield); @endcode
  197. *
  198. * @param attributes Boost.Coroutine attributes used to customise the coroutine.
  199. */
  200. template <typename Handler, typename Function>
  201. void spawn(basic_yield_context<Handler> ctx,
  202. ASIO_MOVE_ARG(Function) function,
  203. const boost::coroutines::attributes& attributes
  204. = boost::coroutines::attributes());
  205. /// Start a new stackful coroutine that executes in the context of a strand.
  206. /**
  207. * This function is used to launch a new coroutine.
  208. *
  209. * @param strand Identifies a strand. By starting multiple coroutines on the
  210. * same strand, the implementation ensures that none of those coroutines can
  211. * execute simultaneously.
  212. *
  213. * @param function The coroutine function. The function must have the signature:
  214. * @code void function(yield_context yield); @endcode
  215. *
  216. * @param attributes Boost.Coroutine attributes used to customise the coroutine.
  217. */
  218. template <typename Function>
  219. void spawn(asio::io_service::strand strand,
  220. ASIO_MOVE_ARG(Function) function,
  221. const boost::coroutines::attributes& attributes
  222. = boost::coroutines::attributes());
  223. /// Start a new stackful coroutine that executes on a given io_service.
  224. /**
  225. * This function is used to launch a new coroutine.
  226. *
  227. * @param io_service Identifies the io_service that will run the coroutine. The
  228. * new coroutine is implicitly given its own strand within this io_service.
  229. *
  230. * @param function The coroutine function. The function must have the signature:
  231. * @code void function(yield_context yield); @endcode
  232. *
  233. * @param attributes Boost.Coroutine attributes used to customise the coroutine.
  234. */
  235. template <typename Function>
  236. void spawn(asio::io_service& io_service,
  237. ASIO_MOVE_ARG(Function) function,
  238. const boost::coroutines::attributes& attributes
  239. = boost::coroutines::attributes());
  240. /*@}*/
  241. } // namespace asio
  242. #include "asio/detail/pop_options.hpp"
  243. #include "asio/impl/spawn.hpp"
  244. #endif // ASIO_SPAWN_HPP