co_spawn.hpp 16 KB

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