channel_operation.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // experimental/detail/channel_operation.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_EXPERIMENTAL_DETAIL_CHANNEL_OPERATION_HPP
  11. #define ASIO_EXPERIMENTAL_DETAIL_CHANNEL_OPERATION_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. #include "asio/associated_allocator.hpp"
  17. #include "asio/associated_executor.hpp"
  18. #include "asio/associated_immediate_executor.hpp"
  19. #include "asio/detail/initiate_post.hpp"
  20. #include "asio/detail/initiate_dispatch.hpp"
  21. #include "asio/detail/op_queue.hpp"
  22. #include "asio/detail/type_traits.hpp"
  23. #include "asio/execution/executor.hpp"
  24. #include "asio/execution/outstanding_work.hpp"
  25. #include "asio/executor_work_guard.hpp"
  26. #include "asio/prefer.hpp"
  27. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. namespace experimental {
  30. namespace detail {
  31. // Base class for all channel operations. A function pointer is used instead of
  32. // virtual functions to avoid the associated overhead.
  33. class channel_operation ASIO_INHERIT_TRACKED_HANDLER
  34. {
  35. public:
  36. template <typename Executor, typename = void, typename = void>
  37. class handler_work_base;
  38. template <typename Handler, typename IoExecutor, typename = void>
  39. class handler_work;
  40. void destroy()
  41. {
  42. func_(this, destroy_op, 0);
  43. }
  44. protected:
  45. enum action
  46. {
  47. destroy_op = 0,
  48. immediate_op = 1,
  49. post_op = 2,
  50. dispatch_op = 3,
  51. cancel_op = 4,
  52. close_op = 5
  53. };
  54. typedef void (*func_type)(channel_operation*, action, void*);
  55. channel_operation(func_type func)
  56. : next_(0),
  57. func_(func),
  58. cancellation_key_(0)
  59. {
  60. }
  61. // Prevents deletion through this type.
  62. ~channel_operation()
  63. {
  64. }
  65. friend class asio::detail::op_queue_access;
  66. channel_operation* next_;
  67. func_type func_;
  68. public:
  69. // The operation key used for targeted cancellation.
  70. void* cancellation_key_;
  71. };
  72. template <typename Executor, typename, typename>
  73. class channel_operation::handler_work_base
  74. {
  75. public:
  76. typedef decay_t<
  77. prefer_result_t<Executor,
  78. execution::outstanding_work_t::tracked_t
  79. >
  80. > executor_type;
  81. handler_work_base(int, const Executor& ex)
  82. : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
  83. {
  84. }
  85. const executor_type& get_executor() const noexcept
  86. {
  87. return executor_;
  88. }
  89. template <typename IoExecutor, typename Function, typename Handler>
  90. void post(const IoExecutor& io_exec, Function& function, Handler&)
  91. {
  92. (asio::detail::initiate_post_with_executor<IoExecutor>(io_exec))(
  93. static_cast<Function&&>(function));
  94. }
  95. template <typename Function, typename Handler>
  96. void dispatch(Function& function, Handler& handler)
  97. {
  98. associated_allocator_t<Handler> allocator =
  99. (get_associated_allocator)(handler);
  100. asio::prefer(executor_,
  101. execution::allocator(allocator)
  102. ).execute(static_cast<Function&&>(function));
  103. }
  104. private:
  105. executor_type executor_;
  106. };
  107. template <typename Executor>
  108. class channel_operation::handler_work_base<Executor,
  109. enable_if_t<
  110. execution::is_executor<Executor>::value
  111. >,
  112. enable_if_t<
  113. can_require<Executor, execution::blocking_t::never_t>::value
  114. >
  115. >
  116. {
  117. public:
  118. typedef decay_t<
  119. prefer_result_t<Executor,
  120. execution::outstanding_work_t::tracked_t
  121. >
  122. > executor_type;
  123. handler_work_base(int, const Executor& ex)
  124. : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
  125. {
  126. }
  127. const executor_type& get_executor() const noexcept
  128. {
  129. return executor_;
  130. }
  131. template <typename IoExecutor, typename Function, typename Handler>
  132. void post(const IoExecutor&, Function& function, Handler& handler)
  133. {
  134. associated_allocator_t<Handler> allocator =
  135. (get_associated_allocator)(handler);
  136. asio::prefer(
  137. asio::require(executor_, execution::blocking.never),
  138. execution::allocator(allocator)
  139. ).execute(static_cast<Function&&>(function));
  140. }
  141. template <typename Function, typename Handler>
  142. void dispatch(Function& function, Handler& handler)
  143. {
  144. associated_allocator_t<Handler> allocator =
  145. (get_associated_allocator)(handler);
  146. asio::prefer(executor_,
  147. execution::allocator(allocator)
  148. ).execute(static_cast<Function&&>(function));
  149. }
  150. private:
  151. executor_type executor_;
  152. };
  153. #if !defined(ASIO_NO_TS_EXECUTORS)
  154. template <typename Executor>
  155. class channel_operation::handler_work_base<Executor,
  156. enable_if_t<
  157. !execution::is_executor<Executor>::value
  158. >
  159. >
  160. {
  161. public:
  162. typedef Executor executor_type;
  163. handler_work_base(int, const Executor& ex)
  164. : work_(ex)
  165. {
  166. }
  167. executor_type get_executor() const noexcept
  168. {
  169. return work_.get_executor();
  170. }
  171. template <typename IoExecutor, typename Function, typename Handler>
  172. void post(const IoExecutor&, Function& function, Handler& handler)
  173. {
  174. associated_allocator_t<Handler> allocator =
  175. (get_associated_allocator)(handler);
  176. work_.get_executor().post(
  177. static_cast<Function&&>(function), allocator);
  178. }
  179. template <typename Function, typename Handler>
  180. void dispatch(Function& function, Handler& handler)
  181. {
  182. associated_allocator_t<Handler> allocator =
  183. (get_associated_allocator)(handler);
  184. work_.get_executor().dispatch(
  185. static_cast<Function&&>(function), allocator);
  186. }
  187. private:
  188. executor_work_guard<Executor> work_;
  189. };
  190. #endif // !defined(ASIO_NO_TS_EXECUTORS)
  191. template <typename Handler, typename IoExecutor, typename>
  192. class channel_operation::handler_work :
  193. channel_operation::handler_work_base<IoExecutor>,
  194. channel_operation::handler_work_base<
  195. associated_executor_t<Handler, IoExecutor>, IoExecutor>
  196. {
  197. public:
  198. typedef channel_operation::handler_work_base<IoExecutor> base1_type;
  199. typedef channel_operation::handler_work_base<
  200. associated_executor_t<Handler, IoExecutor>, IoExecutor>
  201. base2_type;
  202. handler_work(Handler& handler, const IoExecutor& io_ex) noexcept
  203. : base1_type(0, io_ex),
  204. base2_type(0, (get_associated_executor)(handler, io_ex))
  205. {
  206. }
  207. template <typename Function>
  208. void post(Function& function, Handler& handler)
  209. {
  210. base2_type::post(base1_type::get_executor(), function, handler);
  211. }
  212. template <typename Function>
  213. void dispatch(Function& function, Handler& handler)
  214. {
  215. base2_type::dispatch(function, handler);
  216. }
  217. template <typename Function>
  218. void immediate(Function& function, Handler& handler, ...)
  219. {
  220. typedef associated_immediate_executor_t<Handler,
  221. typename base1_type::executor_type> immediate_ex_type;
  222. immediate_ex_type immediate_ex = (get_associated_immediate_executor)(
  223. handler, base1_type::get_executor());
  224. (asio::detail::initiate_dispatch_with_executor<immediate_ex_type>(
  225. immediate_ex))(static_cast<Function&&>(function));
  226. }
  227. template <typename Function>
  228. void immediate(Function& function, Handler&,
  229. enable_if_t<
  230. is_same<
  231. typename associated_immediate_executor<
  232. conditional_t<false, Function, Handler>,
  233. typename base1_type::executor_type>::
  234. asio_associated_immediate_executor_is_unspecialised,
  235. void
  236. >::value
  237. >*)
  238. {
  239. (asio::detail::initiate_post_with_executor<
  240. typename base1_type::executor_type>(
  241. base1_type::get_executor()))(
  242. static_cast<Function&&>(function));
  243. }
  244. };
  245. template <typename Handler, typename IoExecutor>
  246. class channel_operation::handler_work<
  247. Handler, IoExecutor,
  248. enable_if_t<
  249. is_same<
  250. typename associated_executor<Handler,
  251. IoExecutor>::asio_associated_executor_is_unspecialised,
  252. void
  253. >::value
  254. >
  255. > : handler_work_base<IoExecutor>
  256. {
  257. public:
  258. typedef channel_operation::handler_work_base<IoExecutor> base1_type;
  259. handler_work(Handler&, const IoExecutor& io_ex) noexcept
  260. : base1_type(0, io_ex)
  261. {
  262. }
  263. template <typename Function>
  264. void post(Function& function, Handler& handler)
  265. {
  266. base1_type::post(base1_type::get_executor(), function, handler);
  267. }
  268. template <typename Function>
  269. void dispatch(Function& function, Handler& handler)
  270. {
  271. base1_type::dispatch(function, handler);
  272. }
  273. template <typename Function>
  274. void immediate(Function& function, Handler& handler, ...)
  275. {
  276. typedef associated_immediate_executor_t<Handler,
  277. typename base1_type::executor_type> immediate_ex_type;
  278. immediate_ex_type immediate_ex = (get_associated_immediate_executor)(
  279. handler, base1_type::get_executor());
  280. (asio::detail::initiate_dispatch_with_executor<immediate_ex_type>(
  281. immediate_ex))(static_cast<Function&&>(function));
  282. }
  283. template <typename Function>
  284. void immediate(Function& function, Handler& handler,
  285. enable_if_t<
  286. is_same<
  287. typename associated_immediate_executor<
  288. conditional_t<false, Function, Handler>,
  289. typename base1_type::executor_type>::
  290. asio_associated_immediate_executor_is_unspecialised,
  291. void
  292. >::value
  293. >*)
  294. {
  295. base1_type::post(base1_type::get_executor(), function, handler);
  296. }
  297. };
  298. } // namespace detail
  299. } // namespace experimental
  300. } // namespace asio
  301. #include "asio/detail/pop_options.hpp"
  302. #endif // ASIO_EXPERIMENTAL_DETAIL_CHANNEL_OPERATION_HPP