dispatch.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // dispatch.hpp
  3. // ~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_DISPATCH_HPP
  11. #define BOOST_ASIO_DISPATCH_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/initiate_dispatch.hpp>
  18. #include <boost/asio/detail/type_traits.hpp>
  19. #include <boost/asio/execution_context.hpp>
  20. #include <boost/asio/execution/executor.hpp>
  21. #include <boost/asio/is_executor.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. /// Submits a completion token or function object for execution.
  26. /**
  27. * This function submits an object for execution using the object's associated
  28. * executor. The function object may be called from the current thread prior to
  29. * returning from <tt>dispatch()</tt>. Otherwise, it is queued for execution.
  30. *
  31. * @param token The @ref completion_token that will be used to produce a
  32. * completion handler. The function signature of the completion handler must be:
  33. * @code void handler(); @endcode
  34. *
  35. * @returns This function returns <tt>async_initiate<NullaryToken,
  36. * void()>(Init{}, token)</tt>, where @c Init is a function object type defined
  37. * as:
  38. *
  39. * @code class Init
  40. * {
  41. * public:
  42. * template <typename CompletionHandler>
  43. * void operator()(CompletionHandler&& completion_handler) const;
  44. * }; @endcode
  45. *
  46. * The function call operator of @c Init:
  47. *
  48. * @li Obtains the handler's associated executor object @c ex of type @c Ex by
  49. * performing @code auto ex = get_associated_executor(handler); @endcode
  50. *
  51. * @li Obtains the handler's associated allocator object @c alloc by performing
  52. * @code auto alloc = get_associated_allocator(handler); @endcode
  53. *
  54. * @li If <tt>execution::is_executor<Ex>::value</tt> is true, performs
  55. * @code prefer(ex, execution::allocator(alloc)).execute(
  56. * std::forward<CompletionHandler>(completion_handler)); @endcode
  57. *
  58. * @li If <tt>execution::is_executor<Ex>::value</tt> is false, performs
  59. * @code ex.dispatch(
  60. * std::forward<CompletionHandler>(completion_handler),
  61. * alloc); @endcode
  62. *
  63. * @par Completion Signature
  64. * @code void() @endcode
  65. */
  66. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken>
  67. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(NullaryToken, void()) dispatch(
  68. BOOST_ASIO_MOVE_ARG(NullaryToken) token)
  69. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  70. async_initiate<NullaryToken, void()>(
  71. declval<detail::initiate_dispatch>(), token)))
  72. {
  73. return async_initiate<NullaryToken, void()>(
  74. detail::initiate_dispatch(), token);
  75. }
  76. /// Submits a completion token or function object for execution.
  77. /**
  78. * This function submits an object for execution using the specified executor.
  79. * The function object may be called from the current thread prior to returning
  80. * from <tt>dispatch()</tt>. Otherwise, it is queued for execution.
  81. *
  82. * @param ex The target executor.
  83. *
  84. * @param token The @ref completion_token that will be used to produce a
  85. * completion handler. The function signature of the completion handler must be:
  86. * @code void handler(); @endcode
  87. *
  88. * @returns This function returns <tt>async_initiate<NullaryToken,
  89. * void()>(Init{ex}, token)</tt>, where @c Init is a function object type
  90. * defined as:
  91. *
  92. * @code class Init
  93. * {
  94. * public:
  95. * using executor_type = Executor;
  96. * explicit Init(const Executor& ex) : ex_(ex) {}
  97. * executor_type get_executor() const noexcept { return ex_; }
  98. * template <typename CompletionHandler>
  99. * void operator()(CompletionHandler&& completion_handler) const;
  100. * private:
  101. * Executor ex_; // exposition only
  102. * }; @endcode
  103. *
  104. * The function call operator of @c Init:
  105. *
  106. * @li Obtains the handler's associated executor object @c ex1 of type @c Ex1 by
  107. * performing @code auto ex1 = get_associated_executor(handler, ex); @endcode
  108. *
  109. * @li Obtains the handler's associated allocator object @c alloc by performing
  110. * @code auto alloc = get_associated_allocator(handler); @endcode
  111. *
  112. * @li If <tt>execution::is_executor<Ex1>::value</tt> is true, constructs a
  113. * function object @c f with a member @c executor_ that is initialised with
  114. * <tt>prefer(ex1, execution::outstanding_work.tracked)</tt>, a member @c
  115. * handler_ that is a decay-copy of @c completion_handler, and a function call
  116. * operator that performs:
  117. * @code auto a = get_associated_allocator(handler_);
  118. * prefer(executor_, execution::allocator(a)).execute(std::move(handler_));
  119. * @endcode
  120. *
  121. * @li If <tt>execution::is_executor<Ex1>::value</tt> is false, constructs a
  122. * function object @c f with a member @c work_ that is initialised with
  123. * <tt>make_work_guard(ex1)</tt>, a member @c handler_ that is a decay-copy of
  124. * @c completion_handler, and a function call operator that performs:
  125. * @code auto a = get_associated_allocator(handler_);
  126. * work_.get_executor().dispatch(std::move(handler_), a);
  127. * work_.reset(); @endcode
  128. *
  129. * @li If <tt>execution::is_executor<Ex>::value</tt> is true, performs
  130. * @code prefer(ex, execution::allocator(alloc)).execute(std::move(f)); @endcode
  131. *
  132. * @li If <tt>execution::is_executor<Ex>::value</tt> is false, performs
  133. * @code ex.dispatch(std::move(f), alloc); @endcode
  134. *
  135. * @par Completion Signature
  136. * @code void() @endcode
  137. */
  138. template <typename Executor,
  139. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
  140. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  141. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(NullaryToken, void()) dispatch(
  142. const Executor& ex,
  143. BOOST_ASIO_MOVE_ARG(NullaryToken) token
  144. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
  145. typename constraint<
  146. execution::is_executor<Executor>::value || is_executor<Executor>::value
  147. >::type = 0)
  148. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  149. async_initiate<NullaryToken, void()>(
  150. declval<detail::initiate_dispatch_with_executor<Executor> >(), token)))
  151. {
  152. return async_initiate<NullaryToken, void()>(
  153. detail::initiate_dispatch_with_executor<Executor>(ex), token);
  154. }
  155. /// Submits a completion token or function object for execution.
  156. /**
  157. * @param ctx An execution context, from which the target executor is obtained.
  158. *
  159. * @param token The @ref completion_token that will be used to produce a
  160. * completion handler. The function signature of the completion handler must be:
  161. * @code void handler(); @endcode
  162. *
  163. * @returns <tt>dispatch(ctx.get_executor(),
  164. * forward<NullaryToken>(token))</tt>.
  165. *
  166. * @par Completion Signature
  167. * @code void() @endcode
  168. */
  169. template <typename ExecutionContext,
  170. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
  171. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  172. typename ExecutionContext::executor_type)>
  173. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(NullaryToken, void()) dispatch(
  174. ExecutionContext& ctx,
  175. BOOST_ASIO_MOVE_ARG(NullaryToken) token
  176. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  177. typename ExecutionContext::executor_type),
  178. typename constraint<is_convertible<
  179. ExecutionContext&, execution_context&>::value>::type = 0)
  180. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  181. async_initiate<NullaryToken, void()>(
  182. declval<detail::initiate_dispatch_with_executor<
  183. typename ExecutionContext::executor_type> >(), token)))
  184. {
  185. return async_initiate<NullaryToken, void()>(
  186. detail::initiate_dispatch_with_executor<
  187. typename ExecutionContext::executor_type>(
  188. ctx.get_executor()), token);
  189. }
  190. } // namespace asio
  191. } // namespace boost
  192. #include <boost/asio/detail/pop_options.hpp>
  193. #endif // BOOST_ASIO_DISPATCH_HPP