post.hpp 8.2 KB

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