strand.hpp 7.4 KB

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