strand.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // strand.hpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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/detail/strand_service.hpp"
  17. #include "asio/detail/wrapped_handler.hpp"
  18. #include "asio/io_service.hpp"
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. /// Provides serialised handler execution.
  22. /**
  23. * The io_service::strand class provides the ability to post and dispatch
  24. * handlers with the guarantee that none of those handlers will execute
  25. * concurrently.
  26. *
  27. * @par Order of handler invocation
  28. * Given:
  29. *
  30. * @li a strand object @c s
  31. *
  32. * @li an object @c a meeting completion handler requirements
  33. *
  34. * @li an object @c a1 which is an arbitrary copy of @c a made by the
  35. * implementation
  36. *
  37. * @li an object @c b meeting completion handler requirements
  38. *
  39. * @li an object @c b1 which is an arbitrary copy of @c b made by the
  40. * implementation
  41. *
  42. * if any of the following conditions are true:
  43. *
  44. * @li @c s.post(a) happens-before @c s.post(b)
  45. *
  46. * @li @c s.post(a) happens-before @c s.dispatch(b), where the latter is
  47. * performed outside the strand
  48. *
  49. * @li @c s.dispatch(a) happens-before @c s.post(b), where the former is
  50. * performed outside the strand
  51. *
  52. * @li @c s.dispatch(a) happens-before @c s.dispatch(b), where both are
  53. * performed outside the strand
  54. *
  55. * then @c asio_handler_invoke(a1, &a1) happens-before
  56. * @c asio_handler_invoke(b1, &b1).
  57. *
  58. * Note that in the following case:
  59. * @code async_op_1(..., s.wrap(a));
  60. * async_op_2(..., s.wrap(b)); @endcode
  61. * the completion of the first async operation will perform @c s.dispatch(a),
  62. * and the second will perform @c s.dispatch(b), but the order in which those
  63. * are performed is unspecified. That is, you cannot state whether one
  64. * happens-before the other. Therefore none of the above conditions are met and
  65. * no ordering guarantee is made.
  66. *
  67. * @par Thread Safety
  68. * @e Distinct @e objects: Safe.@n
  69. * @e Shared @e objects: Safe.
  70. *
  71. * @par Concepts:
  72. * Dispatcher.
  73. */
  74. class io_service::strand
  75. {
  76. public:
  77. /// Constructor.
  78. /**
  79. * Constructs the strand.
  80. *
  81. * @param io_service The io_service object that the strand will use to
  82. * dispatch handlers that are ready to be run.
  83. */
  84. explicit strand(asio::io_service& io_service)
  85. : service_(asio::use_service<
  86. asio::detail::strand_service>(io_service))
  87. {
  88. service_.construct(impl_);
  89. }
  90. /// Destructor.
  91. /**
  92. * Destroys a strand.
  93. *
  94. * Handlers posted through the strand that have not yet been invoked will
  95. * still be dispatched in a way that meets the guarantee of non-concurrency.
  96. */
  97. ~strand()
  98. {
  99. service_.destroy(impl_);
  100. }
  101. /// (Deprecated: use get_io_service().) Get the io_service associated with
  102. /// the strand.
  103. /**
  104. * This function may be used to obtain the io_service object that the strand
  105. * uses to dispatch handlers for asynchronous operations.
  106. *
  107. * @return A reference to the io_service object that the strand will use to
  108. * dispatch handlers. Ownership is not transferred to the caller.
  109. */
  110. asio::io_service& io_service()
  111. {
  112. return service_.get_io_service();
  113. }
  114. /// Get the io_service associated with the strand.
  115. /**
  116. * This function may be used to obtain the io_service object that the strand
  117. * uses to dispatch handlers for asynchronous operations.
  118. *
  119. * @return A reference to the io_service object that the strand will use to
  120. * dispatch handlers. Ownership is not transferred to the caller.
  121. */
  122. asio::io_service& get_io_service()
  123. {
  124. return service_.get_io_service();
  125. }
  126. /// Request the strand to invoke the given handler.
  127. /**
  128. * This function is used to ask the strand to execute the given handler.
  129. *
  130. * The strand object guarantees that handlers posted or dispatched through
  131. * the strand will not be executed concurrently. The handler may be executed
  132. * inside this function if the guarantee can be met. If this function is
  133. * called from within a handler that was posted or dispatched through the same
  134. * strand, then the new handler will be executed immediately.
  135. *
  136. * The strand's guarantee is in addition to the guarantee provided by the
  137. * underlying io_service. The io_service guarantees that the handler will only
  138. * be called in a thread in which the io_service's run member function is
  139. * currently being invoked.
  140. *
  141. * @param handler The handler to be called. The strand will make a copy of the
  142. * handler object as required. The function signature of the handler must be:
  143. * @code void handler(); @endcode
  144. */
  145. template <typename Handler>
  146. void dispatch(Handler handler)
  147. {
  148. service_.dispatch(impl_, handler);
  149. }
  150. /// Request the strand to invoke the given handler and return
  151. /// immediately.
  152. /**
  153. * This function is used to ask the strand to execute the given handler, but
  154. * without allowing the strand to call the handler from inside this function.
  155. *
  156. * The strand object guarantees that handlers posted or dispatched through
  157. * the strand will not be executed concurrently. The strand's guarantee is in
  158. * addition to the guarantee provided by the underlying io_service. The
  159. * io_service guarantees that the handler will only be called in a thread in
  160. * which the io_service's run member function is currently being invoked.
  161. *
  162. * @param handler The handler to be called. The strand will make a copy of the
  163. * handler object as required. The function signature of the handler must be:
  164. * @code void handler(); @endcode
  165. */
  166. template <typename Handler>
  167. void post(Handler handler)
  168. {
  169. service_.post(impl_, handler);
  170. }
  171. /// Create a new handler that automatically dispatches the wrapped handler
  172. /// on the strand.
  173. /**
  174. * This function is used to create a new handler function object that, when
  175. * invoked, will automatically pass the wrapped handler to the strand's
  176. * dispatch function.
  177. *
  178. * @param handler The handler to be wrapped. The strand will make a copy of
  179. * the handler object as required. The function signature of the handler must
  180. * be: @code void handler(A1 a1, ... An an); @endcode
  181. *
  182. * @return A function object that, when invoked, passes the wrapped handler to
  183. * the strand's dispatch function. Given a function object with the signature:
  184. * @code R f(A1 a1, ... An an); @endcode
  185. * If this function object is passed to the wrap function like so:
  186. * @code strand.wrap(f); @endcode
  187. * then the return value is a function object with the signature
  188. * @code void g(A1 a1, ... An an); @endcode
  189. * that, when invoked, executes code equivalent to:
  190. * @code strand.dispatch(boost::bind(f, a1, ... an)); @endcode
  191. */
  192. template <typename Handler>
  193. #if defined(GENERATING_DOCUMENTATION)
  194. unspecified
  195. #else
  196. detail::wrapped_handler<strand, Handler>
  197. #endif
  198. wrap(Handler handler)
  199. {
  200. return detail::wrapped_handler<io_service::strand, Handler>(*this, handler);
  201. }
  202. private:
  203. asio::detail::strand_service& service_;
  204. asio::detail::strand_service::implementation_type impl_;
  205. };
  206. /// Typedef for backwards compatibility.
  207. typedef asio::io_service::strand strand;
  208. } // namespace asio
  209. #include "asio/detail/pop_options.hpp"
  210. #endif // ASIO_STRAND_HPP