coro.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // experimental/coro.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2021-2022 Klemens D. Morgenstern
  6. // (klemens dot morgenstern at gmx dot net)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_EXPERIMENTAL_CORO_HPP
  12. #define ASIO_EXPERIMENTAL_CORO_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #include "asio/dispatch.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/error_code.hpp"
  20. #include "asio/experimental/coro_traits.hpp"
  21. #include "asio/experimental/detail/coro_promise_allocator.hpp"
  22. #include "asio/experimental/detail/partial_promise.hpp"
  23. #include "asio/experimental/use_coro.hpp"
  24. #include "asio/post.hpp"
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. namespace experimental {
  28. namespace detail {
  29. template <typename T, typename Coroutine>
  30. struct coro_with_arg;
  31. } // namespace detail
  32. /// The main type of a resumable coroutine.
  33. /**
  34. * Template parameter @c Yield specifies type or signature used by co_yield,
  35. * @c Return specifies the type used for co_return, and @c Executor specifies
  36. * the underlying executor type.
  37. */
  38. template <typename Yield = void, typename Return = void,
  39. typename Executor = any_io_executor>
  40. struct coro
  41. {
  42. /// The traits of the coroutine. See asio::experimental::coro_traits
  43. /// for details.
  44. using traits = coro_traits<Yield, Return, Executor>;
  45. /// The value that can be passed into a symmetrical cororoutine. @c void if
  46. /// asymmetrical.
  47. using input_type = typename traits::input_type;
  48. /// The type that can be passed out through a co_yield.
  49. using yield_type = typename traits::yield_type;
  50. /// The type that can be passed out through a co_return.
  51. using return_type = typename traits::return_type;
  52. /// The type received by a co_await or async_resume. Its a combination of
  53. /// yield and return.
  54. using result_type = typename traits::result_type;
  55. /// The signature used by the async_resume.
  56. using signature_type = typename traits::signature_type;
  57. /// Whether or not the coroutine is noexcept.
  58. constexpr static bool is_noexcept = traits::is_noexcept;
  59. /// The error type of the coroutine. Void for noexcept
  60. using error_type = typename traits::error_type;
  61. /// Completion handler type used by async_resume.
  62. using completion_handler = typename traits::completion_handler;
  63. /// The internal promise-type of the coroutine.
  64. using promise_type = detail::coro_promise<Yield, Return, Executor>;
  65. #if !defined(GENERATING_DOCUMENTATION)
  66. template <typename T, typename Coroutine>
  67. friend struct detail::coro_with_arg;
  68. #endif // !defined(GENERATING_DOCUMENTATION)
  69. /// The executor type.
  70. using executor_type = Executor;
  71. #if !defined(GENERATING_DOCUMENTATION)
  72. friend struct detail::coro_promise<Yield, Return, Executor>;
  73. #endif // !defined(GENERATING_DOCUMENTATION)
  74. /// The default constructor, gives an invalid coroutine.
  75. coro() = default;
  76. /// Move constructor.
  77. coro(coro&& lhs) noexcept
  78. : coro_(std::exchange(lhs.coro_, nullptr))
  79. {
  80. }
  81. coro(const coro &) = delete;
  82. /// Move assignment.
  83. coro& operator=(coro&& lhs) noexcept
  84. {
  85. std::swap(coro_, lhs.coro_);
  86. return *this;
  87. }
  88. coro& operator=(const coro&) = delete;
  89. /// Destructor. Destroys the coroutine, if it holds a valid one.
  90. /**
  91. * @note This does not cancel an active coroutine. Destructing a resumable
  92. * coroutine, i.e. one with a call to async_resume that has not completed, is
  93. * undefined behaviour.
  94. */
  95. ~coro()
  96. {
  97. if (coro_ != nullptr)
  98. {
  99. struct destroyer
  100. {
  101. detail::coroutine_handle<promise_type> handle;
  102. destroyer(const detail::coroutine_handle<promise_type>& handle)
  103. : handle(handle)
  104. { }
  105. destroyer(destroyer&& lhs)
  106. : handle(std::exchange(lhs.handle, nullptr))
  107. {
  108. }
  109. destroyer(const destroyer&) = delete;
  110. void operator()() {}
  111. ~destroyer()
  112. {
  113. if (handle)
  114. handle.destroy();
  115. }
  116. };
  117. auto handle =
  118. detail::coroutine_handle<promise_type>::from_promise(*coro_);
  119. if (handle)
  120. asio::dispatch(coro_->get_executor(), destroyer{handle});
  121. }
  122. }
  123. /// Get the used executor.
  124. executor_type get_executor() const
  125. {
  126. if (coro_)
  127. return coro_->get_executor();
  128. if constexpr (std::is_default_constructible_v<Executor>)
  129. return Executor{};
  130. else
  131. throw std::logic_error("Coroutine has no executor");
  132. }
  133. /// Resume the coroutine.
  134. /**
  135. * @param token The completion token of the async resume.
  136. *
  137. * @attention Calling an invalid coroutine with a noexcept signature is
  138. * undefined behaviour.
  139. *
  140. * @note This overload is only available for coroutines without an input
  141. * value.
  142. */
  143. template <typename CompletionToken>
  144. requires std::is_void_v<input_type>
  145. auto async_resume(CompletionToken&& token) &
  146. {
  147. return async_initiate<CompletionToken,
  148. typename traits::completion_handler>(
  149. initiate_async_resume(this), token);
  150. }
  151. /// Resume the coroutine.
  152. /**
  153. * @param token The completion token of the async resume.
  154. *
  155. * @attention Calling an invalid coroutine with a noexcept signature is
  156. * undefined behaviour.
  157. *
  158. * @note This overload is only available for coroutines with an input value.
  159. */
  160. template <typename CompletionToken, detail::convertible_to<input_type> T>
  161. auto async_resume(T&& ip, CompletionToken&& token) &
  162. {
  163. return async_initiate<CompletionToken,
  164. typename traits::completion_handler>(
  165. initiate_async_resume(this), token, std::forward<T>(ip));
  166. }
  167. /// Operator used for coroutines without input value.
  168. auto operator co_await() requires (std::is_void_v<input_type>)
  169. {
  170. return awaitable_t{*this};
  171. }
  172. /// Operator used for coroutines with input value.
  173. /**
  174. * @param ip The input value
  175. *
  176. * @returns An awaitable handle.
  177. *
  178. * @code
  179. * coro<void> push_values(coro<double(int)> c)
  180. * {
  181. * std::optional<double> res = co_await c(42);
  182. * }
  183. * @endcode
  184. */
  185. template <detail::convertible_to<input_type> T>
  186. auto operator()(T&& ip)
  187. {
  188. return detail::coro_with_arg<std::decay_t<T>, coro>{
  189. std::forward<T>(ip), *this};
  190. }
  191. /// Check whether the coroutine is open, i.e. can be resumed.
  192. bool is_open() const
  193. {
  194. if (coro_)
  195. {
  196. auto handle =
  197. detail::coroutine_handle<promise_type>::from_promise(*coro_);
  198. return handle && !handle.done();
  199. }
  200. else
  201. return false;
  202. }
  203. /// Check whether the coroutine is open, i.e. can be resumed.
  204. explicit operator bool() const { return is_open(); }
  205. private:
  206. struct awaitable_t;
  207. struct initiate_async_resume;
  208. explicit coro(promise_type* const cr) : coro_(cr) {}
  209. promise_type* coro_{nullptr};
  210. };
  211. } // namespace experimental
  212. } // namespace asio
  213. #include "asio/detail/pop_options.hpp"
  214. #include "asio/experimental/impl/coro.hpp"
  215. #endif // ASIO_EXPERIMENTAL_CORO_HPP