executor_work_guard.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //
  2. // executor_work_guard.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_EXECUTOR_WORK_GUARD_HPP
  11. #define BOOST_ASIO_EXECUTOR_WORK_GUARD_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/type_traits.hpp>
  18. #include <boost/asio/execution.hpp>
  19. #include <boost/asio/is_executor.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. #if !defined(BOOST_ASIO_EXECUTOR_WORK_GUARD_DECL)
  24. #define BOOST_ASIO_EXECUTOR_WORK_GUARD_DECL
  25. template <typename Executor, typename = void, typename = void>
  26. class executor_work_guard;
  27. #endif // !defined(BOOST_ASIO_EXECUTOR_WORK_GUARD_DECL)
  28. #if defined(GENERATING_DOCUMENTATION)
  29. /// An object of type @c executor_work_guard controls ownership of outstanding
  30. /// executor work within a scope.
  31. template <typename Executor>
  32. class executor_work_guard
  33. {
  34. public:
  35. /// The underlying executor type.
  36. typedef Executor executor_type;
  37. /// Constructs a @c executor_work_guard object for the specified executor.
  38. /**
  39. * Stores a copy of @c e and calls <tt>on_work_started()</tt> on it.
  40. */
  41. explicit executor_work_guard(const executor_type& e) noexcept;
  42. /// Copy constructor.
  43. executor_work_guard(const executor_work_guard& other) noexcept;
  44. /// Move constructor.
  45. executor_work_guard(executor_work_guard&& other) noexcept;
  46. /// Destructor.
  47. /**
  48. * Unless the object has already been reset, or is in a moved-from state,
  49. * calls <tt>on_work_finished()</tt> on the stored executor.
  50. */
  51. ~executor_work_guard();
  52. /// Obtain the associated executor.
  53. executor_type get_executor() const noexcept;
  54. /// Whether the executor_work_guard object owns some outstanding work.
  55. bool owns_work() const noexcept;
  56. /// Indicate that the work is no longer outstanding.
  57. /**
  58. * Unless the object has already been reset, or is in a moved-from state,
  59. * calls <tt>on_work_finished()</tt> on the stored executor.
  60. */
  61. void reset() noexcept;
  62. };
  63. #endif // defined(GENERATING_DOCUMENTATION)
  64. #if !defined(GENERATING_DOCUMENTATION)
  65. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  66. template <typename Executor>
  67. class executor_work_guard<Executor,
  68. enable_if_t<
  69. is_executor<Executor>::value
  70. >>
  71. {
  72. public:
  73. typedef Executor executor_type;
  74. explicit executor_work_guard(const executor_type& e) noexcept
  75. : executor_(e),
  76. owns_(true)
  77. {
  78. executor_.on_work_started();
  79. }
  80. executor_work_guard(const executor_work_guard& other) noexcept
  81. : executor_(other.executor_),
  82. owns_(other.owns_)
  83. {
  84. if (owns_)
  85. executor_.on_work_started();
  86. }
  87. executor_work_guard(executor_work_guard&& other) noexcept
  88. : executor_(static_cast<Executor&&>(other.executor_)),
  89. owns_(other.owns_)
  90. {
  91. other.owns_ = false;
  92. }
  93. ~executor_work_guard()
  94. {
  95. if (owns_)
  96. executor_.on_work_finished();
  97. }
  98. executor_type get_executor() const noexcept
  99. {
  100. return executor_;
  101. }
  102. bool owns_work() const noexcept
  103. {
  104. return owns_;
  105. }
  106. void reset() noexcept
  107. {
  108. if (owns_)
  109. {
  110. executor_.on_work_finished();
  111. owns_ = false;
  112. }
  113. }
  114. private:
  115. // Disallow assignment.
  116. executor_work_guard& operator=(const executor_work_guard&);
  117. executor_type executor_;
  118. bool owns_;
  119. };
  120. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  121. template <typename Executor>
  122. class executor_work_guard<Executor,
  123. enable_if_t<
  124. !is_executor<Executor>::value
  125. >,
  126. enable_if_t<
  127. execution::is_executor<Executor>::value
  128. >>
  129. {
  130. public:
  131. typedef Executor executor_type;
  132. explicit executor_work_guard(const executor_type& e) noexcept
  133. : executor_(e),
  134. owns_(true)
  135. {
  136. new (&work_) work_type(boost::asio::prefer(executor_,
  137. execution::outstanding_work.tracked));
  138. }
  139. executor_work_guard(const executor_work_guard& other) noexcept
  140. : executor_(other.executor_),
  141. owns_(other.owns_)
  142. {
  143. if (owns_)
  144. {
  145. new (&work_) work_type(boost::asio::prefer(executor_,
  146. execution::outstanding_work.tracked));
  147. }
  148. }
  149. executor_work_guard(executor_work_guard&& other) noexcept
  150. : executor_(static_cast<Executor&&>(other.executor_)),
  151. owns_(other.owns_)
  152. {
  153. if (owns_)
  154. {
  155. new (&work_) work_type(
  156. static_cast<work_type&&>(
  157. *static_cast<work_type*>(
  158. static_cast<void*>(&other.work_))));
  159. other.owns_ = false;
  160. }
  161. }
  162. ~executor_work_guard()
  163. {
  164. if (owns_)
  165. static_cast<work_type*>(static_cast<void*>(&work_))->~work_type();
  166. }
  167. executor_type get_executor() const noexcept
  168. {
  169. return executor_;
  170. }
  171. bool owns_work() const noexcept
  172. {
  173. return owns_;
  174. }
  175. void reset() noexcept
  176. {
  177. if (owns_)
  178. {
  179. static_cast<work_type*>(static_cast<void*>(&work_))->~work_type();
  180. owns_ = false;
  181. }
  182. }
  183. private:
  184. // Disallow assignment.
  185. executor_work_guard& operator=(const executor_work_guard&);
  186. typedef decay_t<
  187. prefer_result_t<
  188. const executor_type&,
  189. execution::outstanding_work_t::tracked_t
  190. >
  191. > work_type;
  192. executor_type executor_;
  193. aligned_storage_t<sizeof(work_type), alignment_of<work_type>::value> work_;
  194. bool owns_;
  195. };
  196. #endif // !defined(GENERATING_DOCUMENTATION)
  197. /// Create an @ref executor_work_guard object.
  198. /**
  199. * @param ex An executor.
  200. *
  201. * @returns A work guard constructed with the specified executor.
  202. */
  203. template <typename Executor>
  204. BOOST_ASIO_NODISCARD inline executor_work_guard<Executor>
  205. make_work_guard(const Executor& ex,
  206. constraint_t<
  207. is_executor<Executor>::value || execution::is_executor<Executor>::value
  208. > = 0)
  209. {
  210. return executor_work_guard<Executor>(ex);
  211. }
  212. /// Create an @ref executor_work_guard object.
  213. /**
  214. * @param ctx An execution context, from which an executor will be obtained.
  215. *
  216. * @returns A work guard constructed with the execution context's executor,
  217. * obtained by performing <tt>ctx.get_executor()</tt>.
  218. */
  219. template <typename ExecutionContext>
  220. BOOST_ASIO_NODISCARD inline
  221. executor_work_guard<typename ExecutionContext::executor_type>
  222. make_work_guard(ExecutionContext& ctx,
  223. constraint_t<
  224. is_convertible<ExecutionContext&, execution_context&>::value
  225. > = 0)
  226. {
  227. return executor_work_guard<typename ExecutionContext::executor_type>(
  228. ctx.get_executor());
  229. }
  230. /// Create an @ref executor_work_guard object.
  231. /**
  232. * @param t An arbitrary object, such as a completion handler, for which the
  233. * associated executor will be obtained.
  234. *
  235. * @returns A work guard constructed with the associated executor of the object
  236. * @c t, which is obtained as if by calling <tt>get_associated_executor(t)</tt>.
  237. */
  238. template <typename T>
  239. BOOST_ASIO_NODISCARD inline
  240. executor_work_guard<
  241. typename constraint_t<
  242. !is_executor<T>::value
  243. && !execution::is_executor<T>::value
  244. && !is_convertible<T&, execution_context&>::value,
  245. associated_executor<T>
  246. >::type>
  247. make_work_guard(const T& t)
  248. {
  249. return executor_work_guard<associated_executor_t<T>>(
  250. associated_executor<T>::get(t));
  251. }
  252. /// Create an @ref executor_work_guard object.
  253. /**
  254. * @param t An arbitrary object, such as a completion handler, for which the
  255. * associated executor will be obtained.
  256. *
  257. * @param ex An executor to be used as the candidate object when determining the
  258. * associated executor.
  259. *
  260. * @returns A work guard constructed with the associated executor of the object
  261. * @c t, which is obtained as if by calling <tt>get_associated_executor(t,
  262. * ex)</tt>.
  263. */
  264. template <typename T, typename Executor>
  265. BOOST_ASIO_NODISCARD inline
  266. executor_work_guard<associated_executor_t<T, Executor>>
  267. make_work_guard(const T& t, const Executor& ex,
  268. constraint_t<
  269. is_executor<Executor>::value || execution::is_executor<Executor>::value
  270. > = 0)
  271. {
  272. return executor_work_guard<associated_executor_t<T, Executor>>(
  273. associated_executor<T, Executor>::get(t, ex));
  274. }
  275. /// Create an @ref executor_work_guard object.
  276. /**
  277. * @param t An arbitrary object, such as a completion handler, for which the
  278. * associated executor will be obtained.
  279. *
  280. * @param ctx An execution context, from which an executor is obtained to use as
  281. * the candidate object for determining the associated executor.
  282. *
  283. * @returns A work guard constructed with the associated executor of the object
  284. * @c t, which is obtained as if by calling <tt>get_associated_executor(t,
  285. * ctx.get_executor())</tt>.
  286. */
  287. template <typename T, typename ExecutionContext>
  288. BOOST_ASIO_NODISCARD inline executor_work_guard<
  289. associated_executor_t<T, typename ExecutionContext::executor_type>>
  290. make_work_guard(const T& t, ExecutionContext& ctx,
  291. constraint_t<
  292. !is_executor<T>::value
  293. > = 0,
  294. constraint_t<
  295. !execution::is_executor<T>::value
  296. > = 0,
  297. constraint_t<
  298. !is_convertible<T&, execution_context&>::value
  299. > = 0,
  300. constraint_t<
  301. is_convertible<ExecutionContext&, execution_context&>::value
  302. > = 0)
  303. {
  304. return executor_work_guard<
  305. associated_executor_t<T, typename ExecutionContext::executor_type>>(
  306. associated_executor<T, typename ExecutionContext::executor_type>::get(
  307. t, ctx.get_executor()));
  308. }
  309. } // namespace asio
  310. } // namespace boost
  311. #include <boost/asio/detail/pop_options.hpp>
  312. #endif // BOOST_ASIO_EXECUTOR_WORK_GUARD_HPP