handler_work.hpp 14 KB

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