dispatch.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // impl/dispatch.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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_DISPATCH_HPP
  11. #define BOOST_ASIO_IMPL_DISPATCH_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/associated_allocator.hpp>
  17. #include <boost/asio/associated_executor.hpp>
  18. #include <boost/asio/detail/work_dispatcher.hpp>
  19. #include <boost/asio/execution/allocator.hpp>
  20. #include <boost/asio/execution/blocking.hpp>
  21. #include <boost/asio/prefer.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. class initiate_dispatch
  27. {
  28. public:
  29. template <typename CompletionHandler>
  30. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  31. typename enable_if<
  32. execution::is_executor<
  33. typename associated_executor<
  34. typename decay<CompletionHandler>::type
  35. >::type
  36. >::value
  37. >::type* = 0) const
  38. {
  39. typedef typename decay<CompletionHandler>::type handler_t;
  40. typename associated_executor<handler_t>::type ex(
  41. (get_associated_executor)(handler));
  42. typename associated_allocator<handler_t>::type alloc(
  43. (get_associated_allocator)(handler));
  44. execution::execute(
  45. boost::asio::prefer(ex,
  46. execution::blocking.possibly,
  47. execution::allocator(alloc)),
  48. boost::asio::detail::bind_handler(
  49. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)));
  50. }
  51. template <typename CompletionHandler>
  52. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  53. typename enable_if<
  54. !execution::is_executor<
  55. typename associated_executor<
  56. typename decay<CompletionHandler>::type
  57. >::type
  58. >::value
  59. >::type* = 0) const
  60. {
  61. typedef typename decay<CompletionHandler>::type handler_t;
  62. typename associated_executor<handler_t>::type ex(
  63. (get_associated_executor)(handler));
  64. typename associated_allocator<handler_t>::type alloc(
  65. (get_associated_allocator)(handler));
  66. ex.dispatch(boost::asio::detail::bind_handler(
  67. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
  68. }
  69. };
  70. template <typename Executor>
  71. class initiate_dispatch_with_executor
  72. {
  73. public:
  74. typedef Executor executor_type;
  75. explicit initiate_dispatch_with_executor(const Executor& ex)
  76. : ex_(ex)
  77. {
  78. }
  79. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  80. {
  81. return ex_;
  82. }
  83. template <typename CompletionHandler>
  84. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  85. typename enable_if<
  86. execution::is_executor<
  87. typename conditional<true, executor_type, CompletionHandler>::type
  88. >::value
  89. >::type* = 0,
  90. typename enable_if<
  91. !detail::is_work_dispatcher_required<
  92. typename decay<CompletionHandler>::type,
  93. Executor
  94. >::value
  95. >::type* = 0) const
  96. {
  97. typedef typename decay<CompletionHandler>::type handler_t;
  98. typename associated_allocator<handler_t>::type alloc(
  99. (get_associated_allocator)(handler));
  100. execution::execute(
  101. boost::asio::prefer(ex_,
  102. execution::blocking.possibly,
  103. execution::allocator(alloc)),
  104. boost::asio::detail::bind_handler(
  105. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)));
  106. }
  107. template <typename CompletionHandler>
  108. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  109. typename enable_if<
  110. execution::is_executor<
  111. typename conditional<true, executor_type, CompletionHandler>::type
  112. >::value
  113. >::type* = 0,
  114. typename enable_if<
  115. detail::is_work_dispatcher_required<
  116. typename decay<CompletionHandler>::type,
  117. Executor
  118. >::value
  119. >::type* = 0) const
  120. {
  121. typedef typename decay<CompletionHandler>::type handler_t;
  122. typedef typename associated_executor<
  123. handler_t, Executor>::type handler_ex_t;
  124. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  125. typename associated_allocator<handler_t>::type alloc(
  126. (get_associated_allocator)(handler));
  127. execution::execute(
  128. boost::asio::prefer(ex_,
  129. execution::blocking.possibly,
  130. execution::allocator(alloc)),
  131. detail::work_dispatcher<handler_t, handler_ex_t>(
  132. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler), handler_ex));
  133. }
  134. template <typename CompletionHandler>
  135. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  136. typename enable_if<
  137. !execution::is_executor<
  138. typename conditional<true, executor_type, CompletionHandler>::type
  139. >::value
  140. >::type* = 0,
  141. typename enable_if<
  142. !detail::is_work_dispatcher_required<
  143. typename decay<CompletionHandler>::type,
  144. Executor
  145. >::value
  146. >::type* = 0) const
  147. {
  148. typedef typename decay<CompletionHandler>::type handler_t;
  149. typename associated_allocator<handler_t>::type alloc(
  150. (get_associated_allocator)(handler));
  151. ex_.dispatch(boost::asio::detail::bind_handler(
  152. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
  153. }
  154. template <typename CompletionHandler>
  155. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler,
  156. typename enable_if<
  157. !execution::is_executor<
  158. typename conditional<true, executor_type, CompletionHandler>::type
  159. >::value
  160. >::type* = 0,
  161. typename enable_if<
  162. detail::is_work_dispatcher_required<
  163. typename decay<CompletionHandler>::type,
  164. Executor
  165. >::value
  166. >::type* = 0) const
  167. {
  168. typedef typename decay<CompletionHandler>::type handler_t;
  169. typedef typename associated_executor<
  170. handler_t, Executor>::type handler_ex_t;
  171. handler_ex_t handler_ex((get_associated_executor)(handler, ex_));
  172. typename associated_allocator<handler_t>::type alloc(
  173. (get_associated_allocator)(handler));
  174. ex_.dispatch(detail::work_dispatcher<handler_t, handler_ex_t>(
  175. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler),
  176. handler_ex), alloc);
  177. }
  178. private:
  179. Executor ex_;
  180. };
  181. } // namespace detail
  182. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken>
  183. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(NullaryToken, void()) dispatch(
  184. BOOST_ASIO_MOVE_ARG(NullaryToken) token)
  185. {
  186. return async_initiate<NullaryToken, void()>(
  187. detail::initiate_dispatch(), token);
  188. }
  189. template <typename Executor,
  190. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken>
  191. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(NullaryToken, void()) dispatch(
  192. const Executor& ex, BOOST_ASIO_MOVE_ARG(NullaryToken) token,
  193. typename constraint<
  194. execution::is_executor<Executor>::value || is_executor<Executor>::value
  195. >::type)
  196. {
  197. return async_initiate<NullaryToken, void()>(
  198. detail::initiate_dispatch_with_executor<Executor>(ex), token);
  199. }
  200. template <typename ExecutionContext,
  201. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken>
  202. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(NullaryToken, void()) dispatch(
  203. ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(NullaryToken) token,
  204. typename constraint<is_convertible<
  205. ExecutionContext&, execution_context&>::value>::type)
  206. {
  207. return async_initiate<NullaryToken, void()>(
  208. detail::initiate_dispatch_with_executor<
  209. typename ExecutionContext::executor_type>(
  210. ctx.get_executor()), token);
  211. }
  212. } // namespace asio
  213. } // namespace boost
  214. #include <boost/asio/detail/pop_options.hpp>
  215. #endif // BOOST_ASIO_IMPL_DISPATCH_HPP