dispatch.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 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/type_traits.hpp>
  18. #include <boost/asio/execution_context.hpp>
  19. #include <boost/asio/execution/executor.hpp>
  20. #include <boost/asio/is_executor.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  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 execution::execute(
  55. * prefer(ex,
  56. * execution::blocking.possibly,
  57. * execution::allocator(alloc)),
  58. * std::forward<CompletionHandler>(completion_handler)); @endcode
  59. *
  60. * @li If <tt>execution::is_executor<Ex>::value</tt> is false, performs
  61. * @code ex.dispatch(
  62. * std::forward<CompletionHandler>(completion_handler),
  63. * alloc); @endcode
  64. *
  65. * @par Completion Signature
  66. * @code void() @endcode
  67. */
  68. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken>
  69. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(NullaryToken, void()) dispatch(
  70. BOOST_ASIO_MOVE_ARG(NullaryToken) token);
  71. /// Submits a completion token or function object for execution.
  72. /**
  73. * This function submits an object for execution using the specified executor.
  74. * The function object may be called from the current thread prior to returning
  75. * from <tt>dispatch()</tt>. Otherwise, it is queued for execution.
  76. *
  77. * @param ex The target executor.
  78. *
  79. * @param token The @ref completion_token that will be used to produce a
  80. * completion handler. The function signature of the completion handler must be:
  81. * @code void handler(); @endcode
  82. *
  83. * @returns This function returns <tt>async_initiate<NullaryToken,
  84. * void()>(Init{ex}, token)</tt>, where @c Init is a function object type
  85. * defined as:
  86. *
  87. * @code class Init
  88. * {
  89. * public:
  90. * using executor_type = Executor;
  91. * explicit Init(const Executor& ex) : ex_(ex) {}
  92. * executor_type get_executor() const noexcept { return ex_; }
  93. * template <typename CompletionHandler>
  94. * void operator()(CompletionHandler&& completion_handler) const;
  95. * private:
  96. * Executor ex_; // exposition only
  97. * }; @endcode
  98. *
  99. * The function call operator of @c Init:
  100. *
  101. * @li Obtains the handler's associated executor object @c ex1 of type @c Ex1 by
  102. * performing @code auto ex1 = get_associated_executor(handler, ex); @endcode
  103. *
  104. * @li Obtains the handler's associated allocator object @c alloc by performing
  105. * @code auto alloc = get_associated_allocator(handler); @endcode
  106. *
  107. * @li If <tt>execution::is_executor<Ex1>::value</tt> is true, constructs a
  108. * function object @c f with a member @c executor_ that is initialised with
  109. * <tt>prefer(ex1, execution::outstanding_work.tracked)</tt>, a member @c
  110. * handler_ that is a decay-copy of @c completion_handler, and a function call
  111. * operator that performs:
  112. * @code auto a = get_associated_allocator(handler_);
  113. * execution::execute(
  114. * prefer(executor_,
  115. * execution::blocking.possibly,
  116. * execution::allocator(a)),
  117. * std::move(handler_)); @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 execution::execute(
  129. * prefer(ex,
  130. * execution::blocking.possibly,
  131. * execution::allocator(alloc)),
  132. * std::move(f)); @endcode
  133. *
  134. * @li If <tt>execution::is_executor<Ex>::value</tt> is false, performs
  135. * @code ex.dispatch(std::move(f), alloc); @endcode
  136. *
  137. * @par Completion Signature
  138. * @code void() @endcode
  139. */
  140. template <typename Executor,
  141. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
  142. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  143. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(NullaryToken, void()) dispatch(
  144. const Executor& ex,
  145. BOOST_ASIO_MOVE_ARG(NullaryToken) token
  146. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
  147. typename constraint<
  148. execution::is_executor<Executor>::value || is_executor<Executor>::value
  149. >::type = 0);
  150. /// Submits a completion token or function object for execution.
  151. /**
  152. * @param ctx An execution context, from which the target executor is obtained.
  153. *
  154. * @param token The @ref completion_token that will be used to produce a
  155. * completion handler. The function signature of the completion handler must be:
  156. * @code void handler(); @endcode
  157. *
  158. * @returns <tt>dispatch(ctx.get_executor(),
  159. * forward<NullaryToken>(token))</tt>.
  160. *
  161. * @par Completion Signature
  162. * @code void() @endcode
  163. */
  164. template <typename ExecutionContext,
  165. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
  166. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  167. typename ExecutionContext::executor_type)>
  168. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(NullaryToken, void()) dispatch(
  169. ExecutionContext& ctx,
  170. BOOST_ASIO_MOVE_ARG(NullaryToken) token
  171. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  172. typename ExecutionContext::executor_type),
  173. typename constraint<is_convertible<
  174. ExecutionContext&, execution_context&>::value>::type = 0);
  175. } // namespace asio
  176. } // namespace boost
  177. #include <boost/asio/detail/pop_options.hpp>
  178. #include <boost/asio/impl/dispatch.hpp>
  179. #endif // BOOST_ASIO_DISPATCH_HPP