co_spawn.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // impl/co_spawn.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_IMPL_CO_SPAWN_HPP
  11. #define BOOST_ASIO_IMPL_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. #include <boost/asio/awaitable.hpp>
  17. #include <boost/asio/dispatch.hpp>
  18. #include <boost/asio/post.hpp>
  19. #include <boost/asio/use_awaitable.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. template <typename T, typename Executor, typename F, typename Handler>
  25. awaitable<void, Executor> co_spawn_entry_point(
  26. awaitable<T, Executor>*, Executor ex, F f, Handler handler)
  27. {
  28. auto spawn_work = make_work_guard(ex);
  29. auto handler_work = make_work_guard(handler, ex);
  30. (void) co_await (post)(spawn_work.get_executor(),
  31. use_awaitable_t<Executor>{});
  32. bool done = false;
  33. try
  34. {
  35. T t = co_await f();
  36. done = true;
  37. (dispatch)(handler_work.get_executor(),
  38. [handler = std::move(handler), t = std::move(t)]() mutable
  39. {
  40. handler(std::exception_ptr(), std::move(t));
  41. });
  42. }
  43. catch (...)
  44. {
  45. if (done)
  46. throw;
  47. (dispatch)(handler_work.get_executor(),
  48. [handler = std::move(handler), e = std::current_exception()]() mutable
  49. {
  50. handler(e, T());
  51. });
  52. }
  53. }
  54. template <typename Executor, typename F, typename Handler>
  55. awaitable<void, Executor> co_spawn_entry_point(
  56. awaitable<void, Executor>*, Executor ex, F f, Handler handler)
  57. {
  58. auto spawn_work = make_work_guard(ex);
  59. auto handler_work = make_work_guard(handler, ex);
  60. (void) co_await (post)(spawn_work.get_executor(),
  61. use_awaitable_t<Executor>{});
  62. std::exception_ptr e = nullptr;
  63. try
  64. {
  65. co_await f();
  66. }
  67. catch (...)
  68. {
  69. e = std::current_exception();
  70. }
  71. (dispatch)(handler_work.get_executor(),
  72. [handler = std::move(handler), e]() mutable
  73. {
  74. handler(e);
  75. });
  76. }
  77. template <typename Executor>
  78. class initiate_co_spawn
  79. {
  80. public:
  81. typedef Executor executor_type;
  82. template <typename OtherExecutor>
  83. explicit initiate_co_spawn(const OtherExecutor& ex)
  84. : ex_(ex)
  85. {
  86. }
  87. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  88. {
  89. return ex_;
  90. }
  91. template <typename Handler, typename F>
  92. void operator()(Handler&& handler, F&& f) const
  93. {
  94. typedef typename result_of<F()>::type awaitable_type;
  95. auto a = (co_spawn_entry_point)(static_cast<awaitable_type*>(nullptr),
  96. ex_, std::forward<F>(f), std::forward<Handler>(handler));
  97. awaitable_handler<executor_type, void>(std::move(a), ex_).launch();
  98. }
  99. private:
  100. Executor ex_;
  101. };
  102. } // namespace detail
  103. template <typename Executor, typename F,
  104. BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature<
  105. typename result_of<F()>::type>::type) CompletionToken>
  106. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken,
  107. typename detail::awaitable_signature<typename result_of<F()>::type>::type)
  108. co_spawn(const Executor& ex, F&& f, CompletionToken&& token,
  109. typename enable_if<
  110. is_executor<Executor>::value
  111. >::type*)
  112. {
  113. return async_initiate<CompletionToken,
  114. typename detail::awaitable_signature<typename result_of<F()>::type>>(
  115. detail::initiate_co_spawn<
  116. typename result_of<F()>::type::executor_type>(ex),
  117. token, std::forward<F>(f));
  118. }
  119. template <typename ExecutionContext, typename F,
  120. BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature<
  121. typename result_of<F()>::type>::type) CompletionToken>
  122. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken,
  123. typename detail::awaitable_signature<typename result_of<F()>::type>::type)
  124. co_spawn(ExecutionContext& ctx, F&& f, CompletionToken&& token,
  125. typename enable_if<
  126. is_convertible<ExecutionContext&, execution_context&>::value
  127. >::type*)
  128. {
  129. return (co_spawn)(ctx.get_executor(), std::forward<F>(f),
  130. std::forward<CompletionToken>(token));
  131. }
  132. } // namespace asio
  133. } // namespace boost
  134. #include <boost/asio/detail/pop_options.hpp>
  135. #endif // BOOST_ASIO_IMPL_CO_SPAWN_HPP