strand.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // 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_STRAND_HPP
  11. #define ASIO_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_service.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. /// Provides serialised handler execution.
  24. /**
  25. * The io_service::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_service::strand
  80. {
  81. public:
  82. /// Constructor.
  83. /**
  84. * Constructs the strand.
  85. *
  86. * @param io_service The io_service object that the strand will use to
  87. * dispatch handlers that are ready to be run.
  88. */
  89. explicit strand(asio::io_service& io_service)
  90. : service_(asio::use_service<
  91. asio::detail::strand_service>(io_service))
  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. /// Get the io_service associated with the strand.
  106. /**
  107. * This function may be used to obtain the io_service object that the strand
  108. * uses to dispatch handlers for asynchronous operations.
  109. *
  110. * @return A reference to the io_service object that the strand will use to
  111. * dispatch handlers. Ownership is not transferred to the caller.
  112. */
  113. asio::io_service& get_io_service()
  114. {
  115. return service_.get_io_service();
  116. }
  117. /// Request the strand to invoke the given handler.
  118. /**
  119. * This function is used to ask the strand to execute the given handler.
  120. *
  121. * The strand object guarantees that handlers posted or dispatched through
  122. * the strand will not be executed concurrently. The handler may be executed
  123. * inside this function if the guarantee can be met. If this function is
  124. * called from within a handler that was posted or dispatched through the same
  125. * strand, then the new handler will be executed immediately.
  126. *
  127. * The strand's guarantee is in addition to the guarantee provided by the
  128. * underlying io_service. The io_service guarantees that the handler will only
  129. * be called in a thread in which the io_service's run member function is
  130. * currently being invoked.
  131. *
  132. * @param handler The handler to be called. The strand will make a copy of the
  133. * handler object as required. The function signature of the handler must be:
  134. * @code void handler(); @endcode
  135. */
  136. template <typename CompletionHandler>
  137. ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
  138. dispatch(ASIO_MOVE_ARG(CompletionHandler) handler)
  139. {
  140. // If you get an error on the following line it means that your handler does
  141. // not meet the documented type requirements for a CompletionHandler.
  142. ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
  143. detail::async_result_init<
  144. CompletionHandler, void ()> init(
  145. ASIO_MOVE_CAST(CompletionHandler)(handler));
  146. service_.dispatch(impl_, init.handler);
  147. return init.result.get();
  148. }
  149. /// Request the strand to invoke the given handler and return
  150. /// immediately.
  151. /**
  152. * This function is used to ask the strand to execute the given handler, but
  153. * without allowing the strand to call the handler from inside this function.
  154. *
  155. * The strand object guarantees that handlers posted or dispatched through
  156. * the strand will not be executed concurrently. The strand's guarantee is in
  157. * addition to the guarantee provided by the underlying io_service. The
  158. * io_service guarantees that the handler will only be called in a thread in
  159. * which the io_service's run member function is currently being invoked.
  160. *
  161. * @param handler The handler to be called. The strand will make a copy of the
  162. * handler object as required. The function signature of the handler must be:
  163. * @code void handler(); @endcode
  164. */
  165. template <typename CompletionHandler>
  166. ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
  167. post(ASIO_MOVE_ARG(CompletionHandler) handler)
  168. {
  169. // If you get an error on the following line it means that your handler does
  170. // not meet the documented type requirements for a CompletionHandler.
  171. ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
  172. detail::async_result_init<
  173. CompletionHandler, void ()> init(
  174. ASIO_MOVE_CAST(CompletionHandler)(handler));
  175. service_.post(impl_, init.handler);
  176. return init.result.get();
  177. }
  178. /// Create a new handler that automatically dispatches the wrapped handler
  179. /// on the strand.
  180. /**
  181. * This function is used to create a new handler function object that, when
  182. * invoked, will automatically pass the wrapped handler to the strand's
  183. * dispatch function.
  184. *
  185. * @param handler The handler to be wrapped. The strand will make a copy of
  186. * the handler object as required. The function signature of the handler must
  187. * be: @code void handler(A1 a1, ... An an); @endcode
  188. *
  189. * @return A function object that, when invoked, passes the wrapped handler to
  190. * the strand's dispatch function. Given a function object with the signature:
  191. * @code R f(A1 a1, ... An an); @endcode
  192. * If this function object is passed to the wrap function like so:
  193. * @code strand.wrap(f); @endcode
  194. * then the return value is a function object with the signature
  195. * @code void g(A1 a1, ... An an); @endcode
  196. * that, when invoked, executes code equivalent to:
  197. * @code strand.dispatch(boost::bind(f, a1, ... an)); @endcode
  198. */
  199. template <typename Handler>
  200. #if defined(GENERATING_DOCUMENTATION)
  201. unspecified
  202. #else
  203. detail::wrapped_handler<strand, Handler, detail::is_continuation_if_running>
  204. #endif
  205. wrap(Handler handler)
  206. {
  207. return detail::wrapped_handler<io_service::strand, Handler,
  208. detail::is_continuation_if_running>(*this, handler);
  209. }
  210. /// Determine whether the strand is running in the current thread.
  211. /**
  212. * @return @c true if the current thread is executing a handler that was
  213. * submitted to the strand using post(), dispatch() or wrap(). Otherwise
  214. * returns @c false.
  215. */
  216. bool running_in_this_thread() const
  217. {
  218. return service_.running_in_this_thread(impl_);
  219. }
  220. private:
  221. asio::detail::strand_service& service_;
  222. asio::detail::strand_service::implementation_type impl_;
  223. };
  224. /// (Deprecated: Use asio::io_service::strand.) Typedef for backwards
  225. /// compatibility.
  226. typedef asio::io_service::strand strand;
  227. } // namespace asio
  228. #include "asio/detail/pop_options.hpp"
  229. #endif // ASIO_STRAND_HPP