strand.hpp 7.5 KB

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