defer.hpp 7.6 KB

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