dispatch.hpp 7.1 KB

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