handler_work.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. //
  2. // detail/handler_work.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 ASIO_DETAIL_HANDLER_WORK_HPP
  11. #define ASIO_DETAIL_HANDLER_WORK_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_executor.hpp"
  17. #include "asio/detail/handler_invoke_helpers.hpp"
  18. #include "asio/detail/type_traits.hpp"
  19. #include "asio/execution/allocator.hpp"
  20. #include "asio/execution/blocking.hpp"
  21. #include "asio/execution/execute.hpp"
  22. #include "asio/execution/executor.hpp"
  23. #include "asio/execution/outstanding_work.hpp"
  24. #include "asio/executor_work_guard.hpp"
  25. #include "asio/prefer.hpp"
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. class executor;
  29. class io_context;
  30. #if !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  31. class any_io_executor;
  32. #endif // !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  33. namespace execution {
  34. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  35. template <typename...> class any_executor;
  36. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  37. template <typename, typename, typename, typename, typename,
  38. typename, typename, typename, typename> class any_executor;
  39. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  40. } // namespace execution
  41. namespace detail {
  42. template <typename Executor, typename CandidateExecutor = void,
  43. typename IoContext = io_context,
  44. typename PolymorphicExecutor = executor, typename = void>
  45. class handler_work_base
  46. {
  47. public:
  48. explicit handler_work_base(int, int, const Executor& ex) ASIO_NOEXCEPT
  49. : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
  50. {
  51. }
  52. template <typename OtherExecutor>
  53. handler_work_base(bool /*base1_owns_work*/, const Executor& ex,
  54. const OtherExecutor& /*candidate*/) ASIO_NOEXCEPT
  55. : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
  56. {
  57. }
  58. handler_work_base(const handler_work_base& other) ASIO_NOEXCEPT
  59. : executor_(other.executor_)
  60. {
  61. }
  62. #if defined(ASIO_HAS_MOVE)
  63. handler_work_base(handler_work_base&& other) ASIO_NOEXCEPT
  64. : executor_(ASIO_MOVE_CAST(executor_type)(other.executor_))
  65. {
  66. }
  67. #endif // defined(ASIO_HAS_MOVE)
  68. bool owns_work() const ASIO_NOEXCEPT
  69. {
  70. return true;
  71. }
  72. template <typename Function, typename Handler>
  73. void dispatch(Function& function, Handler& handler)
  74. {
  75. execution::execute(
  76. asio::prefer(executor_,
  77. execution::blocking.possibly,
  78. execution::allocator((get_associated_allocator)(handler))),
  79. ASIO_MOVE_CAST(Function)(function));
  80. }
  81. private:
  82. typedef typename decay<
  83. typename prefer_result<Executor,
  84. execution::outstanding_work_t::tracked_t
  85. >::type
  86. >::type executor_type;
  87. executor_type executor_;
  88. };
  89. template <typename Executor, typename CandidateExecutor,
  90. typename IoContext, typename PolymorphicExecutor>
  91. class handler_work_base<Executor, CandidateExecutor,
  92. IoContext, PolymorphicExecutor,
  93. typename enable_if<
  94. !execution::is_executor<Executor>::value
  95. && (!is_same<Executor, PolymorphicExecutor>::value
  96. || !is_same<CandidateExecutor, void>::value)
  97. >::type>
  98. {
  99. public:
  100. explicit handler_work_base(int, int, const Executor& ex) ASIO_NOEXCEPT
  101. : executor_(ex),
  102. owns_work_(true)
  103. {
  104. executor_.on_work_started();
  105. }
  106. handler_work_base(bool /*base1_owns_work*/, const Executor& ex,
  107. const Executor& candidate) ASIO_NOEXCEPT
  108. : executor_(ex),
  109. owns_work_(ex != candidate)
  110. {
  111. if (owns_work_)
  112. executor_.on_work_started();
  113. }
  114. template <typename OtherExecutor>
  115. handler_work_base(bool /*base1_owns_work*/, const Executor& ex,
  116. const OtherExecutor& /*candidate*/) ASIO_NOEXCEPT
  117. : executor_(ex),
  118. owns_work_(true)
  119. {
  120. executor_.on_work_started();
  121. }
  122. handler_work_base(const handler_work_base& other) ASIO_NOEXCEPT
  123. : executor_(other.executor_),
  124. owns_work_(other.owns_work_)
  125. {
  126. if (owns_work_)
  127. executor_.on_work_started();
  128. }
  129. #if defined(ASIO_HAS_MOVE)
  130. handler_work_base(handler_work_base&& other) ASIO_NOEXCEPT
  131. : executor_(ASIO_MOVE_CAST(Executor)(other.executor_)),
  132. owns_work_(other.owns_work_)
  133. {
  134. other.owns_work_ = false;
  135. }
  136. #endif // defined(ASIO_HAS_MOVE)
  137. ~handler_work_base()
  138. {
  139. if (owns_work_)
  140. executor_.on_work_finished();
  141. }
  142. bool owns_work() const ASIO_NOEXCEPT
  143. {
  144. return owns_work_;
  145. }
  146. template <typename Function, typename Handler>
  147. void dispatch(Function& function, Handler& handler)
  148. {
  149. executor_.dispatch(ASIO_MOVE_CAST(Function)(function),
  150. asio::get_associated_allocator(handler));
  151. }
  152. private:
  153. Executor executor_;
  154. bool owns_work_;
  155. };
  156. template <typename Executor, typename IoContext, typename PolymorphicExecutor>
  157. class handler_work_base<Executor, void, IoContext, PolymorphicExecutor,
  158. typename enable_if<
  159. is_same<
  160. Executor,
  161. typename IoContext::executor_type
  162. >::value
  163. >::type>
  164. {
  165. public:
  166. explicit handler_work_base(int, int, const Executor&)
  167. {
  168. }
  169. bool owns_work() const ASIO_NOEXCEPT
  170. {
  171. return false;
  172. }
  173. template <typename Function, typename Handler>
  174. void dispatch(Function& function, Handler& handler)
  175. {
  176. // When using a native implementation, I/O completion handlers are
  177. // already dispatched according to the execution context's executor's
  178. // rules. We can call the function directly.
  179. asio_handler_invoke_helpers::invoke(function, handler);
  180. }
  181. };
  182. template <typename Executor, typename IoContext>
  183. class handler_work_base<Executor, void, IoContext, Executor>
  184. {
  185. public:
  186. explicit handler_work_base(int, int, const Executor& ex) ASIO_NOEXCEPT
  187. #if !defined(ASIO_NO_TYPEID)
  188. : executor_(
  189. ex.target_type() == typeid(typename IoContext::executor_type)
  190. ? Executor() : ex)
  191. #else // !defined(ASIO_NO_TYPEID)
  192. : executor_(ex)
  193. #endif // !defined(ASIO_NO_TYPEID)
  194. {
  195. if (executor_)
  196. executor_.on_work_started();
  197. }
  198. handler_work_base(bool /*base1_owns_work*/, const Executor& ex,
  199. const Executor& candidate) ASIO_NOEXCEPT
  200. : executor_(ex != candidate ? ex : Executor())
  201. {
  202. if (executor_)
  203. executor_.on_work_started();
  204. }
  205. template <typename OtherExecutor>
  206. handler_work_base(const Executor& ex,
  207. const OtherExecutor&) ASIO_NOEXCEPT
  208. : executor_(ex)
  209. {
  210. executor_.on_work_started();
  211. }
  212. handler_work_base(const handler_work_base& other) ASIO_NOEXCEPT
  213. : executor_(other.executor_)
  214. {
  215. if (executor_)
  216. executor_.on_work_started();
  217. }
  218. #if defined(ASIO_HAS_MOVE)
  219. handler_work_base(handler_work_base&& other) ASIO_NOEXCEPT
  220. : executor_(ASIO_MOVE_CAST(Executor)(other.executor_))
  221. {
  222. }
  223. #endif // defined(ASIO_HAS_MOVE)
  224. ~handler_work_base()
  225. {
  226. if (executor_)
  227. executor_.on_work_finished();
  228. }
  229. bool owns_work() const ASIO_NOEXCEPT
  230. {
  231. return !!executor_;
  232. }
  233. template <typename Function, typename Handler>
  234. void dispatch(Function& function, Handler& handler)
  235. {
  236. executor_.dispatch(ASIO_MOVE_CAST(Function)(function),
  237. asio::get_associated_allocator(handler));
  238. }
  239. private:
  240. Executor executor_;
  241. };
  242. template <
  243. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  244. typename... SupportableProperties,
  245. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  246. typename T1, typename T2, typename T3, typename T4, typename T5,
  247. typename T6, typename T7, typename T8, typename T9,
  248. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  249. typename CandidateExecutor, typename IoContext,
  250. typename PolymorphicExecutor>
  251. class handler_work_base<
  252. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  253. execution::any_executor<SupportableProperties...>,
  254. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  255. execution::any_executor<T1, T2, T3, T4, T5, T6, T7, T8, T9>,
  256. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  257. CandidateExecutor, IoContext, PolymorphicExecutor>
  258. {
  259. public:
  260. typedef
  261. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  262. execution::any_executor<SupportableProperties...>
  263. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  264. execution::any_executor<T1, T2, T3, T4, T5, T6, T7, T8, T9>
  265. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  266. executor_type;
  267. explicit handler_work_base(int, int,
  268. const executor_type& ex) ASIO_NOEXCEPT
  269. #if !defined(ASIO_NO_TYPEID)
  270. : executor_(
  271. ex.target_type() == typeid(typename IoContext::executor_type)
  272. ? executor_type()
  273. : asio::prefer(ex, execution::outstanding_work.tracked))
  274. #else // !defined(ASIO_NO_TYPEID)
  275. : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
  276. #endif // !defined(ASIO_NO_TYPEID)
  277. {
  278. }
  279. handler_work_base(bool base1_owns_work, const executor_type& ex,
  280. const executor_type& candidate) ASIO_NOEXCEPT
  281. : executor_(
  282. !base1_owns_work && ex == candidate
  283. ? executor_type()
  284. : asio::prefer(ex, execution::outstanding_work.tracked))
  285. {
  286. }
  287. template <typename OtherExecutor>
  288. handler_work_base(bool /*base1_owns_work*/, const executor_type& ex,
  289. const OtherExecutor& /*candidate*/) ASIO_NOEXCEPT
  290. : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
  291. {
  292. }
  293. handler_work_base(const handler_work_base& other) ASIO_NOEXCEPT
  294. : executor_(other.executor_)
  295. {
  296. }
  297. #if defined(ASIO_HAS_MOVE)
  298. handler_work_base(handler_work_base&& other) ASIO_NOEXCEPT
  299. : executor_(ASIO_MOVE_CAST(executor_type)(other.executor_))
  300. {
  301. }
  302. #endif // defined(ASIO_HAS_MOVE)
  303. bool owns_work() const ASIO_NOEXCEPT
  304. {
  305. return !!executor_;
  306. }
  307. template <typename Function, typename Handler>
  308. void dispatch(Function& function, Handler&)
  309. {
  310. execution::execute(
  311. asio::prefer(executor_, execution::blocking.possibly),
  312. ASIO_MOVE_CAST(Function)(function));
  313. }
  314. private:
  315. executor_type executor_;
  316. };
  317. #if !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  318. template <typename Executor, typename CandidateExecutor,
  319. typename IoContext, typename PolymorphicExecutor>
  320. class handler_work_base<
  321. Executor, CandidateExecutor,
  322. IoContext, PolymorphicExecutor,
  323. typename enable_if<
  324. is_same<
  325. Executor,
  326. any_io_executor
  327. >::value
  328. >::type>
  329. {
  330. public:
  331. typedef Executor executor_type;
  332. explicit handler_work_base(int, int,
  333. const executor_type& ex) ASIO_NOEXCEPT
  334. #if !defined(ASIO_NO_TYPEID)
  335. : executor_(
  336. ex.target_type() == typeid(typename IoContext::executor_type)
  337. ? executor_type()
  338. : asio::prefer(ex, execution::outstanding_work.tracked))
  339. #else // !defined(ASIO_NO_TYPEID)
  340. : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
  341. #endif // !defined(ASIO_NO_TYPEID)
  342. {
  343. }
  344. handler_work_base(bool base1_owns_work, const executor_type& ex,
  345. const executor_type& candidate) ASIO_NOEXCEPT
  346. : executor_(
  347. !base1_owns_work && ex == candidate
  348. ? executor_type()
  349. : asio::prefer(ex, execution::outstanding_work.tracked))
  350. {
  351. }
  352. template <typename OtherExecutor>
  353. handler_work_base(bool /*base1_owns_work*/, const executor_type& ex,
  354. const OtherExecutor& /*candidate*/) ASIO_NOEXCEPT
  355. : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
  356. {
  357. }
  358. handler_work_base(const handler_work_base& other) ASIO_NOEXCEPT
  359. : executor_(other.executor_)
  360. {
  361. }
  362. #if defined(ASIO_HAS_MOVE)
  363. handler_work_base(handler_work_base&& other) ASIO_NOEXCEPT
  364. : executor_(ASIO_MOVE_CAST(executor_type)(other.executor_))
  365. {
  366. }
  367. #endif // defined(ASIO_HAS_MOVE)
  368. bool owns_work() const ASIO_NOEXCEPT
  369. {
  370. return !!executor_;
  371. }
  372. template <typename Function, typename Handler>
  373. void dispatch(Function& function, Handler&)
  374. {
  375. execution::execute(
  376. asio::prefer(executor_, execution::blocking.possibly),
  377. ASIO_MOVE_CAST(Function)(function));
  378. }
  379. private:
  380. executor_type executor_;
  381. };
  382. #endif // !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  383. template <typename Handler, typename IoExecutor, typename = void>
  384. class handler_work :
  385. handler_work_base<IoExecutor>,
  386. handler_work_base<typename associated_executor<
  387. Handler, IoExecutor>::type, IoExecutor>
  388. {
  389. public:
  390. typedef handler_work_base<IoExecutor> base1_type;
  391. typedef handler_work_base<typename associated_executor<
  392. Handler, IoExecutor>::type, IoExecutor> base2_type;
  393. handler_work(Handler& handler, const IoExecutor& io_ex) ASIO_NOEXCEPT
  394. : base1_type(0, 0, io_ex),
  395. base2_type(base1_type::owns_work(),
  396. asio::get_associated_executor(handler, io_ex), io_ex)
  397. {
  398. }
  399. template <typename Function>
  400. void complete(Function& function, Handler& handler)
  401. {
  402. if (!base1_type::owns_work() && !base2_type::owns_work())
  403. {
  404. // When using a native implementation, I/O completion handlers are
  405. // already dispatched according to the execution context's executor's
  406. // rules. We can call the function directly.
  407. asio_handler_invoke_helpers::invoke(function, handler);
  408. }
  409. else
  410. {
  411. base2_type::dispatch(function, handler);
  412. }
  413. }
  414. };
  415. template <typename Handler, typename IoExecutor>
  416. class handler_work<
  417. Handler, IoExecutor,
  418. typename enable_if<
  419. is_same<
  420. typename associated_executor<Handler,
  421. IoExecutor>::asio_associated_executor_is_unspecialised,
  422. void
  423. >::value
  424. >::type> : handler_work_base<IoExecutor>
  425. {
  426. public:
  427. typedef handler_work_base<IoExecutor> base1_type;
  428. handler_work(Handler&, const IoExecutor& io_ex) ASIO_NOEXCEPT
  429. : base1_type(0, 0, io_ex)
  430. {
  431. }
  432. template <typename Function>
  433. void complete(Function& function, Handler& handler)
  434. {
  435. if (!base1_type::owns_work())
  436. {
  437. // When using a native implementation, I/O completion handlers are
  438. // already dispatched according to the execution context's executor's
  439. // rules. We can call the function directly.
  440. asio_handler_invoke_helpers::invoke(function, handler);
  441. }
  442. else
  443. {
  444. base1_type::dispatch(function, handler);
  445. }
  446. }
  447. };
  448. } // namespace detail
  449. } // namespace asio
  450. #include "asio/detail/pop_options.hpp"
  451. #endif // ASIO_DETAIL_HANDLER_WORK_HPP