co_spawn.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. //
  2. // co_spawn.hpp
  3. // ~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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 BOOST_ASIO_CO_SPAWN_HPP
  11. #define BOOST_ASIO_CO_SPAWN_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  17. #include <boost/asio/awaitable.hpp>
  18. #include <boost/asio/execution/executor.hpp>
  19. #include <boost/asio/execution_context.hpp>
  20. #include <boost/asio/is_executor.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. template <typename T>
  26. struct awaitable_signature;
  27. template <typename T, typename Executor>
  28. struct awaitable_signature<awaitable<T, Executor>>
  29. {
  30. typedef void type(std::exception_ptr, T);
  31. };
  32. template <typename Executor>
  33. struct awaitable_signature<awaitable<void, Executor>>
  34. {
  35. typedef void type(std::exception_ptr);
  36. };
  37. } // namespace detail
  38. /// Spawn a new coroutined-based thread of execution.
  39. /**
  40. * @param ex The executor that will be used to schedule the new thread of
  41. * execution.
  42. *
  43. * @param a The boost::asio::awaitable object that is the result of calling the
  44. * coroutine's entry point function.
  45. *
  46. * @param token The completion token that will handle the notification that
  47. * the thread of execution has completed. The function signature of the
  48. * completion handler must be:
  49. * @code void handler(std::exception_ptr, T); @endcode
  50. *
  51. * @par Example
  52. * @code
  53. * boost::asio::awaitable<std::size_t> echo(tcp::socket socket)
  54. * {
  55. * std::size_t bytes_transferred = 0;
  56. *
  57. * try
  58. * {
  59. * char data[1024];
  60. * for (;;)
  61. * {
  62. * std::size_t n = co_await socket.async_read_some(
  63. * boost::asio::buffer(data), boost::asio::use_awaitable);
  64. *
  65. * co_await boost::asio::async_write(socket,
  66. * boost::asio::buffer(data, n), boost::asio::use_awaitable);
  67. *
  68. * bytes_transferred += n;
  69. * }
  70. * }
  71. * catch (const std::exception&)
  72. * {
  73. * }
  74. *
  75. * co_return bytes_transferred;
  76. * }
  77. *
  78. * // ...
  79. *
  80. * boost::asio::co_spawn(my_executor,
  81. * echo(std::move(my_tcp_socket)),
  82. * [](std::exception_ptr e, std::size_t n)
  83. * {
  84. * std::cout << "transferred " << n << "\n";
  85. * });
  86. * @endcode
  87. */
  88. template <typename Executor, typename T, typename AwaitableExecutor,
  89. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  90. void(std::exception_ptr, T)) CompletionToken
  91. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  92. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
  93. CompletionToken, void(std::exception_ptr, T))
  94. co_spawn(const Executor& ex, awaitable<T, AwaitableExecutor> a,
  95. CompletionToken&& token
  96. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
  97. typename constraint<
  98. (is_executor<Executor>::value || execution::is_executor<Executor>::value)
  99. && is_convertible<Executor, AwaitableExecutor>::value
  100. >::type = 0);
  101. /// Spawn a new coroutined-based thread of execution.
  102. /**
  103. * @param ex The executor that will be used to schedule the new thread of
  104. * execution.
  105. *
  106. * @param a The boost::asio::awaitable object that is the result of calling the
  107. * coroutine's entry point function.
  108. *
  109. * @param token The completion token that will handle the notification that
  110. * the thread of execution has completed. The function signature of the
  111. * completion handler must be:
  112. * @code void handler(std::exception_ptr); @endcode
  113. *
  114. * @par Example
  115. * @code
  116. * boost::asio::awaitable<void> echo(tcp::socket socket)
  117. * {
  118. * try
  119. * {
  120. * char data[1024];
  121. * for (;;)
  122. * {
  123. * std::size_t n = co_await socket.async_read_some(
  124. * boost::asio::buffer(data), boost::asio::use_awaitable);
  125. *
  126. * co_await boost::asio::async_write(socket,
  127. * boost::asio::buffer(data, n), boost::asio::use_awaitable);
  128. * }
  129. * }
  130. * catch (const std::exception& e)
  131. * {
  132. * std::cerr << "Exception: " << e.what() << "\n";
  133. * }
  134. * }
  135. *
  136. * // ...
  137. *
  138. * boost::asio::co_spawn(my_executor,
  139. * echo(std::move(my_tcp_socket)),
  140. * boost::asio::detached);
  141. * @endcode
  142. */
  143. template <typename Executor, typename AwaitableExecutor,
  144. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  145. void(std::exception_ptr)) CompletionToken
  146. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  147. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
  148. CompletionToken, void(std::exception_ptr))
  149. co_spawn(const Executor& ex, awaitable<void, AwaitableExecutor> a,
  150. CompletionToken&& token
  151. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
  152. typename constraint<
  153. (is_executor<Executor>::value || execution::is_executor<Executor>::value)
  154. && is_convertible<Executor, AwaitableExecutor>::value
  155. >::type = 0);
  156. /// Spawn a new coroutined-based thread of execution.
  157. /**
  158. * @param ctx An execution context that will provide the executor to be used to
  159. * schedule the new thread of execution.
  160. *
  161. * @param a The boost::asio::awaitable object that is the result of calling the
  162. * coroutine's entry point function.
  163. *
  164. * @param token The completion token that will handle the notification that
  165. * the thread of execution has completed. The function signature of the
  166. * completion handler must be:
  167. * @code void handler(std::exception_ptr); @endcode
  168. *
  169. * @par Example
  170. * @code
  171. * boost::asio::awaitable<std::size_t> echo(tcp::socket socket)
  172. * {
  173. * std::size_t bytes_transferred = 0;
  174. *
  175. * try
  176. * {
  177. * char data[1024];
  178. * for (;;)
  179. * {
  180. * std::size_t n = co_await socket.async_read_some(
  181. * boost::asio::buffer(data), boost::asio::use_awaitable);
  182. *
  183. * co_await boost::asio::async_write(socket,
  184. * boost::asio::buffer(data, n), boost::asio::use_awaitable);
  185. *
  186. * bytes_transferred += n;
  187. * }
  188. * }
  189. * catch (const std::exception&)
  190. * {
  191. * }
  192. *
  193. * co_return bytes_transferred;
  194. * }
  195. *
  196. * // ...
  197. *
  198. * boost::asio::co_spawn(my_io_context,
  199. * echo(std::move(my_tcp_socket)),
  200. * [](std::exception_ptr e, std::size_t n)
  201. * {
  202. * std::cout << "transferred " << n << "\n";
  203. * });
  204. * @endcode
  205. */
  206. template <typename ExecutionContext, typename T, typename AwaitableExecutor,
  207. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  208. void(std::exception_ptr, T)) CompletionToken
  209. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  210. typename ExecutionContext::executor_type)>
  211. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
  212. CompletionToken, void(std::exception_ptr, T))
  213. co_spawn(ExecutionContext& ctx, awaitable<T, AwaitableExecutor> a,
  214. CompletionToken&& token
  215. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  216. typename ExecutionContext::executor_type),
  217. typename constraint<
  218. is_convertible<ExecutionContext&, execution_context&>::value
  219. && is_convertible<typename ExecutionContext::executor_type,
  220. AwaitableExecutor>::value
  221. >::type = 0);
  222. /// Spawn a new coroutined-based thread of execution.
  223. /**
  224. * @param ctx An execution context that will provide the executor to be used to
  225. * schedule the new thread of execution.
  226. *
  227. * @param a The boost::asio::awaitable object that is the result of calling the
  228. * coroutine's entry point function.
  229. *
  230. * @param token The completion token that will handle the notification that
  231. * the thread of execution has completed. The function signature of the
  232. * completion handler must be:
  233. * @code void handler(std::exception_ptr); @endcode
  234. *
  235. * @par Example
  236. * @code
  237. * boost::asio::awaitable<void> echo(tcp::socket socket)
  238. * {
  239. * try
  240. * {
  241. * char data[1024];
  242. * for (;;)
  243. * {
  244. * std::size_t n = co_await socket.async_read_some(
  245. * boost::asio::buffer(data), boost::asio::use_awaitable);
  246. *
  247. * co_await boost::asio::async_write(socket,
  248. * boost::asio::buffer(data, n), boost::asio::use_awaitable);
  249. * }
  250. * }
  251. * catch (const std::exception& e)
  252. * {
  253. * std::cerr << "Exception: " << e.what() << "\n";
  254. * }
  255. * }
  256. *
  257. * // ...
  258. *
  259. * boost::asio::co_spawn(my_io_context,
  260. * echo(std::move(my_tcp_socket)),
  261. * boost::asio::detached);
  262. * @endcode
  263. */
  264. template <typename ExecutionContext, typename AwaitableExecutor,
  265. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  266. void(std::exception_ptr)) CompletionToken
  267. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  268. typename ExecutionContext::executor_type)>
  269. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
  270. CompletionToken, void(std::exception_ptr))
  271. co_spawn(ExecutionContext& ctx, awaitable<void, AwaitableExecutor> a,
  272. CompletionToken&& token
  273. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  274. typename ExecutionContext::executor_type),
  275. typename constraint<
  276. is_convertible<ExecutionContext&, execution_context&>::value
  277. && is_convertible<typename ExecutionContext::executor_type,
  278. AwaitableExecutor>::value
  279. >::type = 0);
  280. /// Spawn a new coroutined-based thread of execution.
  281. /**
  282. * @param ex The executor that will be used to schedule the new thread of
  283. * execution.
  284. *
  285. * @param f A nullary function object with a return type of the form
  286. * @c boost::asio::awaitable<R,E> that will be used as the coroutine's entry
  287. * point.
  288. *
  289. * @param token The completion token that will handle the notification that the
  290. * thread of execution has completed. If @c R is @c void, the function
  291. * signature of the completion handler must be:
  292. *
  293. * @code void handler(std::exception_ptr); @endcode
  294. * Otherwise, the function signature of the completion handler must be:
  295. * @code void handler(std::exception_ptr, R); @endcode
  296. *
  297. *
  298. * @par Example
  299. * @code
  300. * boost::asio::awaitable<std::size_t> echo(tcp::socket socket)
  301. * {
  302. * std::size_t bytes_transferred = 0;
  303. *
  304. * try
  305. * {
  306. * char data[1024];
  307. * for (;;)
  308. * {
  309. * std::size_t n = co_await socket.async_read_some(
  310. * boost::asio::buffer(data), boost::asio::use_awaitable);
  311. *
  312. * co_await boost::asio::async_write(socket,
  313. * boost::asio::buffer(data, n), boost::asio::use_awaitable);
  314. *
  315. * bytes_transferred += n;
  316. * }
  317. * }
  318. * catch (const std::exception&)
  319. * {
  320. * }
  321. *
  322. * co_return bytes_transferred;
  323. * }
  324. *
  325. * // ...
  326. *
  327. * boost::asio::co_spawn(my_executor,
  328. * [socket = std::move(my_tcp_socket)]() mutable
  329. * -> boost::asio::awaitable<void>
  330. * {
  331. * try
  332. * {
  333. * char data[1024];
  334. * for (;;)
  335. * {
  336. * std::size_t n = co_await socket.async_read_some(
  337. * boost::asio::buffer(data), boost::asio::use_awaitable);
  338. *
  339. * co_await boost::asio::async_write(socket,
  340. * boost::asio::buffer(data, n), boost::asio::use_awaitable);
  341. * }
  342. * }
  343. * catch (const std::exception& e)
  344. * {
  345. * std::cerr << "Exception: " << e.what() << "\n";
  346. * }
  347. * }, boost::asio::detached);
  348. * @endcode
  349. */
  350. template <typename Executor, typename F,
  351. BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature<
  352. typename result_of<F()>::type>::type) CompletionToken
  353. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  354. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken,
  355. typename detail::awaitable_signature<typename result_of<F()>::type>::type)
  356. co_spawn(const Executor& ex, F&& f,
  357. CompletionToken&& token
  358. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
  359. typename constraint<
  360. is_executor<Executor>::value || execution::is_executor<Executor>::value
  361. >::type = 0);
  362. /// Spawn a new coroutined-based thread of execution.
  363. /**
  364. * @param ctx An execution context that will provide the executor to be used to
  365. * schedule the new thread of execution.
  366. *
  367. * @param f A nullary function object with a return type of the form
  368. * @c boost::asio::awaitable<R,E> that will be used as the coroutine's entry
  369. * point.
  370. *
  371. * @param token The completion token that will handle the notification that the
  372. * thread of execution has completed. If @c R is @c void, the function
  373. * signature of the completion handler must be:
  374. *
  375. * @code void handler(std::exception_ptr); @endcode
  376. * Otherwise, the function signature of the completion handler must be:
  377. * @code void handler(std::exception_ptr, R); @endcode
  378. *
  379. *
  380. * @par Example
  381. * @code
  382. * boost::asio::awaitable<std::size_t> echo(tcp::socket socket)
  383. * {
  384. * std::size_t bytes_transferred = 0;
  385. *
  386. * try
  387. * {
  388. * char data[1024];
  389. * for (;;)
  390. * {
  391. * std::size_t n = co_await socket.async_read_some(
  392. * boost::asio::buffer(data), boost::asio::use_awaitable);
  393. *
  394. * co_await boost::asio::async_write(socket,
  395. * boost::asio::buffer(data, n), boost::asio::use_awaitable);
  396. *
  397. * bytes_transferred += n;
  398. * }
  399. * }
  400. * catch (const std::exception&)
  401. * {
  402. * }
  403. *
  404. * co_return bytes_transferred;
  405. * }
  406. *
  407. * // ...
  408. *
  409. * boost::asio::co_spawn(my_io_context,
  410. * [socket = std::move(my_tcp_socket)]() mutable
  411. * -> boost::asio::awaitable<void>
  412. * {
  413. * try
  414. * {
  415. * char data[1024];
  416. * for (;;)
  417. * {
  418. * std::size_t n = co_await socket.async_read_some(
  419. * boost::asio::buffer(data), boost::asio::use_awaitable);
  420. *
  421. * co_await boost::asio::async_write(socket,
  422. * boost::asio::buffer(data, n), boost::asio::use_awaitable);
  423. * }
  424. * }
  425. * catch (const std::exception& e)
  426. * {
  427. * std::cerr << "Exception: " << e.what() << "\n";
  428. * }
  429. * }, boost::asio::detached);
  430. * @endcode
  431. */
  432. template <typename ExecutionContext, typename F,
  433. BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature<
  434. typename result_of<F()>::type>::type) CompletionToken
  435. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  436. typename ExecutionContext::executor_type)>
  437. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken,
  438. typename detail::awaitable_signature<typename result_of<F()>::type>::type)
  439. co_spawn(ExecutionContext& ctx, F&& f,
  440. CompletionToken&& token
  441. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  442. typename ExecutionContext::executor_type),
  443. typename constraint<
  444. is_convertible<ExecutionContext&, execution_context&>::value
  445. >::type = 0);
  446. } // namespace asio
  447. } // namespace boost
  448. #include <boost/asio/detail/pop_options.hpp>
  449. #include <boost/asio/impl/co_spawn.hpp>
  450. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  451. #endif // BOOST_ASIO_CO_SPAWN_HPP