work_dispatcher.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // detail/work_dispatcher.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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_DETAIL_WORK_DISPATCHER_HPP
  11. #define BOOST_ASIO_DETAIL_WORK_DISPATCHER_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/detail/bind_handler.hpp>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/associated_executor.hpp>
  19. #include <boost/asio/associated_allocator.hpp>
  20. #include <boost/asio/executor_work_guard.hpp>
  21. #include <boost/asio/execution/executor.hpp>
  22. #include <boost/asio/execution/allocator.hpp>
  23. #include <boost/asio/execution/blocking.hpp>
  24. #include <boost/asio/execution/outstanding_work.hpp>
  25. #include <boost/asio/prefer.hpp>
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. struct empty_work_function
  31. {
  32. void operator()() const noexcept
  33. {
  34. }
  35. };
  36. template <typename Function>
  37. struct work_result
  38. {
  39. typedef decay_t<result_of_t<decay_t<Function>()>> type;
  40. };
  41. template <typename Function>
  42. using work_result_t = typename work_result<Function>::type;
  43. template <typename Function, typename Handler,
  44. typename Executor, typename = void>
  45. struct is_work_dispatcher_required : true_type
  46. {
  47. };
  48. template <typename Handler, typename Executor>
  49. struct is_work_dispatcher_required<empty_work_function, Handler, Executor,
  50. enable_if_t<
  51. is_same<
  52. typename associated_executor<Handler,
  53. Executor>::asio_associated_executor_is_unspecialised,
  54. void
  55. >::value
  56. >> : false_type
  57. {
  58. };
  59. template <typename Function,
  60. bool IsVoid = is_void<result_of_t<Function()>>::value,
  61. bool IsClass = is_class<Function>::value>
  62. class work_dispatcher_function
  63. {
  64. Function function_;
  65. public:
  66. template <typename F>
  67. work_dispatcher_function(F&& function)
  68. : function_(static_cast<F&&>(function))
  69. {
  70. }
  71. work_dispatcher_function(const work_dispatcher_function& other)
  72. : function_(other.function_)
  73. {
  74. }
  75. work_dispatcher_function(work_dispatcher_function&& other)
  76. : function_(static_cast<Function&&>(other.function_))
  77. {
  78. }
  79. template <typename Handler>
  80. auto bind_result(Handler&& handler)
  81. -> decltype(
  82. boost::asio::detail::move_bind_handler(
  83. static_cast<Handler&&>(handler),
  84. static_cast<Function&&>(function_)()))
  85. {
  86. return boost::asio::detail::move_bind_handler(
  87. static_cast<Handler&&>(handler),
  88. static_cast<Function&&>(function_)());
  89. }
  90. };
  91. template <typename Function>
  92. class work_dispatcher_function<Function, false, true> : Function
  93. {
  94. public:
  95. template <typename F>
  96. work_dispatcher_function(F&& function)
  97. : Function(static_cast<F&&>(function))
  98. {
  99. }
  100. work_dispatcher_function(const work_dispatcher_function& other)
  101. : Function(static_cast<const Function&>(other))
  102. {
  103. }
  104. work_dispatcher_function(work_dispatcher_function&& other)
  105. : Function(static_cast<Function&&>(other))
  106. {
  107. }
  108. template <typename Handler>
  109. auto bind_result(Handler&& handler)
  110. -> decltype(
  111. boost::asio::detail::move_bind_handler(
  112. static_cast<Handler&&>(handler),
  113. static_cast<Function&&>(*static_cast<Function*>(this))()))
  114. {
  115. return boost::asio::detail::move_bind_handler(
  116. static_cast<Handler&&>(handler),
  117. static_cast<Function&&>(*static_cast<Function*>(this))());
  118. }
  119. };
  120. template <typename Function>
  121. class work_dispatcher_function<Function, true, false>
  122. {
  123. Function function_;
  124. public:
  125. template <typename F>
  126. work_dispatcher_function(F&& function)
  127. : function_(static_cast<Function&&>(function))
  128. {
  129. }
  130. work_dispatcher_function(const work_dispatcher_function& other)
  131. : function_(other.function_)
  132. {
  133. }
  134. work_dispatcher_function(work_dispatcher_function&& other)
  135. : function_(static_cast<Function&&>(other.function_))
  136. {
  137. }
  138. template <typename Handler>
  139. auto bind_result(Handler&& handler)
  140. -> decltype(boost::asio::detail::bind_handler(
  141. static_cast<Handler&&>(handler)))
  142. {
  143. static_cast<Function&&>(function_)();
  144. return boost::asio::detail::bind_handler(
  145. static_cast<Handler&&>(handler));
  146. }
  147. };
  148. template <typename Function>
  149. class work_dispatcher_function<Function, true, true> : Function
  150. {
  151. public:
  152. template <typename F>
  153. work_dispatcher_function(F&& function)
  154. : Function(static_cast<F&&>(function))
  155. {
  156. }
  157. work_dispatcher_function(const work_dispatcher_function& other)
  158. : Function(static_cast<const Function&>(other))
  159. {
  160. }
  161. work_dispatcher_function(work_dispatcher_function&& other)
  162. : Function(static_cast<Function&&>(other))
  163. {
  164. }
  165. template <typename Handler>
  166. auto bind_result(Handler&& handler)
  167. -> decltype(boost::asio::detail::bind_handler(
  168. static_cast<Handler&&>(handler)))
  169. {
  170. static_cast<Function&&>(*static_cast<Function*>(this))();
  171. return boost::asio::detail::bind_handler(
  172. static_cast<Handler&&>(handler));
  173. }
  174. };
  175. template <typename Function, typename Handler,
  176. typename Executor, typename = void>
  177. class work_dispatcher : work_dispatcher_function<Function>
  178. {
  179. public:
  180. template <typename F, typename CompletionHandler>
  181. work_dispatcher(F&& function, CompletionHandler&& handler,
  182. const Executor& handler_ex)
  183. : work_dispatcher_function<Function>(static_cast<F&&>(function)),
  184. handler_(static_cast<CompletionHandler&&>(handler)),
  185. executor_(boost::asio::prefer(handler_ex,
  186. execution::outstanding_work.tracked))
  187. {
  188. }
  189. work_dispatcher(const work_dispatcher& other)
  190. : work_dispatcher_function<Function>(other),
  191. handler_(other.handler_),
  192. executor_(other.executor_)
  193. {
  194. }
  195. work_dispatcher(work_dispatcher&& other)
  196. : work_dispatcher_function<Function>(
  197. static_cast<work_dispatcher_function<Function>&&>(other)),
  198. handler_(static_cast<Handler&&>(other.handler_)),
  199. executor_(static_cast<work_executor_type&&>(other.executor_))
  200. {
  201. }
  202. void operator()()
  203. {
  204. associated_allocator_t<Handler> alloc((get_associated_allocator)(handler_));
  205. boost::asio::prefer(executor_, execution::allocator(alloc)).execute(
  206. this->bind_result(static_cast<Handler&&>(handler_)));
  207. }
  208. private:
  209. typedef decay_t<
  210. prefer_result_t<const Executor&,
  211. execution::outstanding_work_t::tracked_t
  212. >
  213. > work_executor_type;
  214. Handler handler_;
  215. work_executor_type executor_;
  216. };
  217. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  218. template <typename Function, typename Handler, typename Executor>
  219. class work_dispatcher<Function, Handler, Executor,
  220. enable_if_t<!execution::is_executor<Executor>::value>>
  221. : work_dispatcher_function<Function>
  222. {
  223. public:
  224. template <typename F, typename CompletionHandler>
  225. work_dispatcher(F&& function, CompletionHandler&& handler,
  226. const Executor& handler_ex)
  227. : work_dispatcher_function<Function>(static_cast<F&&>(function)),
  228. work_(handler_ex),
  229. handler_(static_cast<CompletionHandler&&>(handler))
  230. {
  231. }
  232. work_dispatcher(const work_dispatcher& other)
  233. : work_dispatcher_function<Function>(other),
  234. work_(other.work_),
  235. handler_(other.handler_)
  236. {
  237. }
  238. work_dispatcher(work_dispatcher&& other)
  239. : work_dispatcher_function<Function>(
  240. static_cast<work_dispatcher_function<Function>&&>(*this)),
  241. work_(static_cast<executor_work_guard<Executor>&&>(other.work_)),
  242. handler_(static_cast<Handler&&>(other.handler_))
  243. {
  244. }
  245. void operator()()
  246. {
  247. associated_allocator_t<Handler> alloc((get_associated_allocator)(handler_));
  248. work_.get_executor().dispatch(
  249. this->bind_result(static_cast<Handler&&>(handler_)), alloc);
  250. work_.reset();
  251. }
  252. private:
  253. executor_work_guard<Executor> work_;
  254. Handler handler_;
  255. };
  256. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  257. } // namespace detail
  258. } // namespace asio
  259. } // namespace boost
  260. #include <boost/asio/detail/pop_options.hpp>
  261. #endif // BOOST_ASIO_DETAIL_WORK_DISPATCHER_HPP