io_context_strand.hpp 13 KB

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