post.hpp 7.1 KB

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