defer.hpp 8.0 KB

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