strand.hpp 8.7 KB

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