dispatch.hpp 6.7 KB

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