executor_work_guard.hpp 9.2 KB

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