defer.hpp 8.5 KB

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