dispatch.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // dispatch.hpp
  3. // ~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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. inline auto dispatch(NullaryToken&& token)
  68. -> decltype(
  69. async_initiate<NullaryToken, void()>(
  70. declval<detail::initiate_dispatch>(), token))
  71. {
  72. return async_initiate<NullaryToken, void()>(
  73. detail::initiate_dispatch(), token);
  74. }
  75. /// Submits a completion token or function object for execution.
  76. /**
  77. * This function submits an object for execution using the specified executor.
  78. * The function object may be called from the current thread prior to returning
  79. * from <tt>dispatch()</tt>. Otherwise, it is queued for execution.
  80. *
  81. * @param ex The target executor.
  82. *
  83. * @param token The @ref completion_token that will be used to produce a
  84. * completion handler. The function signature of the completion handler must be:
  85. * @code void handler(); @endcode
  86. *
  87. * @returns This function returns <tt>async_initiate<NullaryToken,
  88. * void()>(Init{ex}, token)</tt>, where @c Init is a function object type
  89. * defined as:
  90. *
  91. * @code class Init
  92. * {
  93. * public:
  94. * using executor_type = Executor;
  95. * explicit Init(const Executor& ex) : ex_(ex) {}
  96. * executor_type get_executor() const noexcept { return ex_; }
  97. * template <typename CompletionHandler>
  98. * void operator()(CompletionHandler&& completion_handler) const;
  99. * private:
  100. * Executor ex_; // exposition only
  101. * }; @endcode
  102. *
  103. * The function call operator of @c Init:
  104. *
  105. * @li Obtains the handler's associated executor object @c ex1 of type @c Ex1 by
  106. * performing @code auto ex1 = get_associated_executor(handler, ex); @endcode
  107. *
  108. * @li Obtains the handler's associated allocator object @c alloc by performing
  109. * @code auto alloc = get_associated_allocator(handler); @endcode
  110. *
  111. * @li If <tt>execution::is_executor<Ex1>::value</tt> is true, constructs a
  112. * function object @c f with a member @c executor_ that is initialised with
  113. * <tt>prefer(ex1, execution::outstanding_work.tracked)</tt>, a member @c
  114. * handler_ that is a decay-copy of @c completion_handler, and a function call
  115. * operator that performs:
  116. * @code auto a = get_associated_allocator(handler_);
  117. * prefer(executor_, execution::allocator(a)).execute(std::move(handler_));
  118. * @endcode
  119. *
  120. * @li If <tt>execution::is_executor<Ex1>::value</tt> is false, constructs a
  121. * function object @c f with a member @c work_ that is initialised with
  122. * <tt>make_work_guard(ex1)</tt>, a member @c handler_ that is a decay-copy of
  123. * @c completion_handler, and a function call operator that performs:
  124. * @code auto a = get_associated_allocator(handler_);
  125. * work_.get_executor().dispatch(std::move(handler_), a);
  126. * work_.reset(); @endcode
  127. *
  128. * @li If <tt>execution::is_executor<Ex>::value</tt> is true, performs
  129. * @code prefer(ex, execution::allocator(alloc)).execute(std::move(f)); @endcode
  130. *
  131. * @li If <tt>execution::is_executor<Ex>::value</tt> is false, performs
  132. * @code ex.dispatch(std::move(f), alloc); @endcode
  133. *
  134. * @par Completion Signature
  135. * @code void() @endcode
  136. */
  137. template <typename Executor,
  138. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
  139. = default_completion_token_t<Executor>>
  140. inline auto dispatch(const Executor& ex,
  141. NullaryToken&& token = default_completion_token_t<Executor>(),
  142. constraint_t<
  143. execution::is_executor<Executor>::value || is_executor<Executor>::value
  144. > = 0)
  145. -> decltype(
  146. async_initiate<NullaryToken, void()>(
  147. declval<detail::initiate_dispatch_with_executor<Executor>>(), token))
  148. {
  149. return async_initiate<NullaryToken, void()>(
  150. detail::initiate_dispatch_with_executor<Executor>(ex), token);
  151. }
  152. /// Submits a completion token or function object for execution.
  153. /**
  154. * @param ctx An execution context, from which the target executor is obtained.
  155. *
  156. * @param token The @ref completion_token that will be used to produce a
  157. * completion handler. The function signature of the completion handler must be:
  158. * @code void handler(); @endcode
  159. *
  160. * @returns <tt>dispatch(ctx.get_executor(),
  161. * forward<NullaryToken>(token))</tt>.
  162. *
  163. * @par Completion Signature
  164. * @code void() @endcode
  165. */
  166. template <typename ExecutionContext,
  167. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
  168. = default_completion_token_t<typename ExecutionContext::executor_type>>
  169. inline auto dispatch(ExecutionContext& ctx,
  170. NullaryToken&& token = default_completion_token_t<
  171. typename ExecutionContext::executor_type>(),
  172. constraint_t<
  173. is_convertible<ExecutionContext&, execution_context&>::value
  174. > = 0)
  175. -> decltype(
  176. async_initiate<NullaryToken, void()>(
  177. declval<detail::initiate_dispatch_with_executor<
  178. typename ExecutionContext::executor_type>>(), token))
  179. {
  180. return async_initiate<NullaryToken, void()>(
  181. detail::initiate_dispatch_with_executor<
  182. typename ExecutionContext::executor_type>(
  183. ctx.get_executor()), token);
  184. }
  185. } // namespace asio
  186. } // namespace boost
  187. #include <boost/asio/detail/pop_options.hpp>
  188. #endif // BOOST_ASIO_DISPATCH_HPP