defer.hpp 7.4 KB

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