io_context_strand.hpp 13 KB

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