Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
13KB

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