post.hpp 7.2 KB

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