io_context_strand.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // io_context_strand.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 BOOST_ASIO_IO_CONTEXT_STRAND_HPP
  11. #define BOOST_ASIO_IO_CONTEXT_STRAND_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. #if !defined(BOOST_ASIO_NO_EXTENSIONS) \
  17. && !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  18. #include <boost/asio/async_result.hpp>
  19. #include <boost/asio/detail/handler_type_requirements.hpp>
  20. #include <boost/asio/detail/strand_service.hpp>
  21. #include <boost/asio/detail/wrapped_handler.hpp>
  22. #include <boost/asio/io_context.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. /// Provides serialised handler execution.
  27. /**
  28. * The io_context::strand class provides the ability to post and dispatch
  29. * handlers with the guarantee that none of those handlers will execute
  30. * concurrently.
  31. *
  32. * @par Order of handler invocation
  33. * Given:
  34. *
  35. * @li a strand object @c s
  36. *
  37. * @li an object @c a meeting completion handler requirements
  38. *
  39. * @li an object @c a1 which is an arbitrary copy of @c a made by the
  40. * implementation
  41. *
  42. * @li an object @c b meeting completion handler requirements
  43. *
  44. * @li an object @c b1 which is an arbitrary copy of @c b made by the
  45. * implementation
  46. *
  47. * if any of the following conditions are true:
  48. *
  49. * @li @c s.post(a) happens-before @c s.post(b)
  50. *
  51. * @li @c s.post(a) happens-before @c s.dispatch(b), where the latter is
  52. * performed outside the strand
  53. *
  54. * @li @c s.dispatch(a) happens-before @c s.post(b), where the former is
  55. * performed outside the strand
  56. *
  57. * @li @c s.dispatch(a) happens-before @c s.dispatch(b), where both are
  58. * performed outside the strand
  59. *
  60. * then @c asio_handler_invoke(a1, &a1) happens-before
  61. * @c asio_handler_invoke(b1, &b1).
  62. *
  63. * Note that in the following case:
  64. * @code async_op_1(..., s.wrap(a));
  65. * async_op_2(..., s.wrap(b)); @endcode
  66. * the completion of the first async operation will perform @c s.dispatch(a),
  67. * and the second will perform @c s.dispatch(b), but the order in which those
  68. * are performed is unspecified. That is, you cannot state whether one
  69. * happens-before the other. Therefore none of the above conditions are met and
  70. * no ordering guarantee is made.
  71. *
  72. * @note The implementation makes no guarantee that handlers posted or
  73. * dispatched through different @c strand objects will be invoked concurrently.
  74. *
  75. * @par Thread Safety
  76. * @e Distinct @e objects: Safe.@n
  77. * @e Shared @e objects: Safe.
  78. *
  79. * @par Concepts:
  80. * Dispatcher.
  81. */
  82. class io_context::strand
  83. {
  84. private:
  85. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  86. struct initiate_dispatch;
  87. struct initiate_post;
  88. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  89. public:
  90. /// Constructor.
  91. /**
  92. * Constructs the strand.
  93. *
  94. * @param io_context The io_context object that the strand will use to
  95. * dispatch handlers that are ready to be run.
  96. */
  97. explicit strand(boost::asio::io_context& io_context)
  98. : service_(boost::asio::use_service<
  99. boost::asio::detail::strand_service>(io_context))
  100. {
  101. service_.construct(impl_);
  102. }
  103. /// Destructor.
  104. /**
  105. * Destroys a strand.
  106. *
  107. * Handlers posted through the strand that have not yet been invoked will
  108. * still be dispatched in a way that meets the guarantee of non-concurrency.
  109. */
  110. ~strand()
  111. {
  112. }
  113. /// Obtain the underlying execution context.
  114. boost::asio::io_context& context() const BOOST_ASIO_NOEXCEPT
  115. {
  116. return service_.get_io_context();
  117. }
  118. /// Inform the strand that it has some outstanding work to do.
  119. /**
  120. * The strand delegates this call to its underlying io_context.
  121. */
  122. void on_work_started() const BOOST_ASIO_NOEXCEPT
  123. {
  124. context().get_executor().on_work_started();
  125. }
  126. /// Inform the strand that some work is no longer outstanding.
  127. /**
  128. * The strand delegates this call to its underlying io_context.
  129. */
  130. void on_work_finished() const BOOST_ASIO_NOEXCEPT
  131. {
  132. context().get_executor().on_work_finished();
  133. }
  134. /// Request the strand to invoke the given function object.
  135. /**
  136. * This function is used to ask the strand to execute the given function
  137. * object on its underlying io_context. The function object will be executed
  138. * inside this function if the strand is not otherwise busy and if the
  139. * underlying io_context's executor's @c dispatch() function is also able to
  140. * execute the function before returning.
  141. *
  142. * @param f The function object to be called. The executor will make
  143. * a copy of the handler object as required. The function signature of the
  144. * function object must be: @code void function(); @endcode
  145. *
  146. * @param a An allocator that may be used by the executor to allocate the
  147. * internal storage needed for function invocation.
  148. */
  149. template <typename Function, typename Allocator>
  150. void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  151. {
  152. typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
  153. service_.dispatch(impl_, tmp);
  154. (void)a;
  155. }
  156. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  157. /// (Deprecated: Use boost::asio::dispatch().) Request the strand to invoke
  158. /// the given handler.
  159. /**
  160. * This function is used to ask the strand to execute the given handler.
  161. *
  162. * The strand object guarantees that handlers posted or dispatched through
  163. * the strand will not be executed concurrently. The handler may be executed
  164. * inside this function if the guarantee can be met. If this function is
  165. * called from within a handler that was posted or dispatched through the same
  166. * strand, then the new handler will be executed immediately.
  167. *
  168. * The strand's guarantee is in addition to the guarantee provided by the
  169. * underlying io_context. The io_context guarantees that the handler will only
  170. * be called in a thread in which the io_context's run member function is
  171. * currently being invoked.
  172. *
  173. * @param handler The handler to be called. The strand will make a copy of the
  174. * handler object as required. The function signature of the handler must be:
  175. * @code void handler(); @endcode
  176. */
  177. template <typename LegacyCompletionHandler>
  178. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(LegacyCompletionHandler, void ())
  179. dispatch(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
  180. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  181. async_initiate<LegacyCompletionHandler, void ()>(
  182. declval<initiate_dispatch>(), handler, this)))
  183. {
  184. return async_initiate<LegacyCompletionHandler, void ()>(
  185. initiate_dispatch(), handler, this);
  186. }
  187. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  188. /// Request the strand to invoke the given function object.
  189. /**
  190. * This function is used to ask the executor to execute the given function
  191. * object. The function object will never be executed inside this function.
  192. * Instead, it will be scheduled to run by the underlying io_context.
  193. *
  194. * @param f The function object to be called. The executor will make
  195. * a copy of the handler object as required. The function signature of the
  196. * function object must be: @code void function(); @endcode
  197. *
  198. * @param a An allocator that may be used by the executor to allocate the
  199. * internal storage needed for function invocation.
  200. */
  201. template <typename Function, typename Allocator>
  202. void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  203. {
  204. typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
  205. service_.post(impl_, tmp);
  206. (void)a;
  207. }
  208. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  209. /// (Deprecated: Use boost::asio::post().) Request the strand to invoke the
  210. /// given handler and return immediately.
  211. /**
  212. * This function is used to ask the strand to execute the given handler, but
  213. * without allowing the strand to call the handler from inside this function.
  214. *
  215. * The strand object guarantees that handlers posted or dispatched through
  216. * the strand will not be executed concurrently. The strand's guarantee is in
  217. * addition to the guarantee provided by the underlying io_context. The
  218. * io_context guarantees that the handler will only be called in a thread in
  219. * which the io_context's run member function is currently being invoked.
  220. *
  221. * @param handler The handler to be called. The strand will make a copy of the
  222. * handler object as required. The function signature of the handler must be:
  223. * @code void handler(); @endcode
  224. */
  225. template <typename LegacyCompletionHandler>
  226. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(LegacyCompletionHandler, void ())
  227. post(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
  228. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  229. async_initiate<LegacyCompletionHandler, void ()>(
  230. declval<initiate_post>(), handler, this)))
  231. {
  232. return async_initiate<LegacyCompletionHandler, void ()>(
  233. initiate_post(), handler, this);
  234. }
  235. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  236. /// Request the strand to invoke the given function object.
  237. /**
  238. * This function is used to ask the executor to execute the given function
  239. * object. The function object will never be executed inside this function.
  240. * Instead, it will be scheduled to run by the underlying io_context.
  241. *
  242. * @param f The function object to be called. The executor will make
  243. * a copy of the handler object as required. The function signature of the
  244. * function object must be: @code void function(); @endcode
  245. *
  246. * @param a An allocator that may be used by the executor to allocate the
  247. * internal storage needed for function invocation.
  248. */
  249. template <typename Function, typename Allocator>
  250. void defer(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  251. {
  252. typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
  253. service_.post(impl_, tmp);
  254. (void)a;
  255. }
  256. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  257. /// (Deprecated: Use boost::asio::bind_executor().) Create a new handler that
  258. /// automatically dispatches the wrapped handler on the strand.
  259. /**
  260. * This function is used to create a new handler function object that, when
  261. * invoked, will automatically pass the wrapped handler to the strand's
  262. * dispatch function.
  263. *
  264. * @param handler The handler to be wrapped. The strand will make a copy of
  265. * the handler object as required. The function signature of the handler must
  266. * be: @code void handler(A1 a1, ... An an); @endcode
  267. *
  268. * @return A function object that, when invoked, passes the wrapped handler to
  269. * the strand's dispatch function. Given a function object with the signature:
  270. * @code R f(A1 a1, ... An an); @endcode
  271. * If this function object is passed to the wrap function like so:
  272. * @code strand.wrap(f); @endcode
  273. * then the return value is a function object with the signature
  274. * @code void g(A1 a1, ... An an); @endcode
  275. * that, when invoked, executes code equivalent to:
  276. * @code strand.dispatch(boost::bind(f, a1, ... an)); @endcode
  277. */
  278. template <typename Handler>
  279. #if defined(GENERATING_DOCUMENTATION)
  280. unspecified
  281. #else
  282. detail::wrapped_handler<strand, Handler, detail::is_continuation_if_running>
  283. #endif
  284. wrap(Handler handler)
  285. {
  286. return detail::wrapped_handler<io_context::strand, Handler,
  287. detail::is_continuation_if_running>(*this, handler);
  288. }
  289. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  290. /// Determine whether the strand is running in the current thread.
  291. /**
  292. * @return @c true if the current thread is executing a handler that was
  293. * submitted to the strand using post(), dispatch() or wrap(). Otherwise
  294. * returns @c false.
  295. */
  296. bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT
  297. {
  298. return service_.running_in_this_thread(impl_);
  299. }
  300. /// Compare two strands for equality.
  301. /**
  302. * Two strands are equal if they refer to the same ordered, non-concurrent
  303. * state.
  304. */
  305. friend bool operator==(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
  306. {
  307. return a.impl_ == b.impl_;
  308. }
  309. /// Compare two strands for inequality.
  310. /**
  311. * Two strands are equal if they refer to the same ordered, non-concurrent
  312. * state.
  313. */
  314. friend bool operator!=(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
  315. {
  316. return a.impl_ != b.impl_;
  317. }
  318. private:
  319. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  320. struct initiate_dispatch
  321. {
  322. template <typename LegacyCompletionHandler>
  323. void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
  324. strand* self) const
  325. {
  326. // If you get an error on the following line it means that your
  327. // handler does not meet the documented type requirements for a
  328. // LegacyCompletionHandler.
  329. BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
  330. LegacyCompletionHandler, handler) type_check;
  331. detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
  332. self->service_.dispatch(self->impl_, handler2.value);
  333. }
  334. };
  335. struct initiate_post
  336. {
  337. template <typename LegacyCompletionHandler>
  338. void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
  339. strand* self) const
  340. {
  341. // If you get an error on the following line it means that your
  342. // handler does not meet the documented type requirements for a
  343. // LegacyCompletionHandler.
  344. BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
  345. LegacyCompletionHandler, handler) type_check;
  346. detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
  347. self->service_.post(self->impl_, handler2.value);
  348. }
  349. };
  350. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  351. boost::asio::detail::strand_service& service_;
  352. mutable boost::asio::detail::strand_service::implementation_type impl_;
  353. };
  354. } // namespace asio
  355. } // namespace boost
  356. #include <boost/asio/detail/pop_options.hpp>
  357. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  358. // && !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  359. #endif // BOOST_ASIO_IO_CONTEXT_STRAND_HPP