| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- //
- // post.hpp
- // ~~~~~~~~
- //
- // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef BOOST_ASIO_POST_HPP
- #define BOOST_ASIO_POST_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <boost/asio/detail/config.hpp>
- #include <boost/asio/async_result.hpp>
- #include <boost/asio/detail/initiate_post.hpp>
- #include <boost/asio/detail/type_traits.hpp>
- #include <boost/asio/execution_context.hpp>
- #include <boost/asio/execution/blocking.hpp>
- #include <boost/asio/execution/executor.hpp>
- #include <boost/asio/is_executor.hpp>
- #include <boost/asio/require.hpp>
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- /// Submits a completion token or function object for execution.
- /**
- * This function submits an object for execution using the object's associated
- * executor. The function object is queued for execution, and is never called
- * from the current thread prior to returning from <tt>post()</tt>.
- *
- * The use of @c post(), rather than @ref defer(), indicates the caller's
- * preference that the function object be eagerly queued for execution.
- *
- * @param token The @ref completion_token that will be used to produce a
- * completion handler. The function signature of the completion handler must be:
- * @code void handler(); @endcode
- *
- * @returns This function returns <tt>async_initiate<NullaryToken,
- * void()>(Init{}, token)</tt>, where @c Init is a function object type defined
- * as:
- *
- * @code class Init
- * {
- * public:
- * template <typename CompletionHandler>
- * void operator()(CompletionHandler&& completion_handler) const;
- * }; @endcode
- *
- * The function call operator of @c Init:
- *
- * @li Obtains the handler's associated executor object @c ex of type @c Ex by
- * performing
- * @code auto ex = get_associated_executor(completion_handler); @endcode
- *
- * @li Obtains the handler's associated allocator object @c alloc by performing
- * @code auto alloc = get_associated_allocator(completion_handler); @endcode
- *
- * @li If <tt>execution::is_executor<Ex>::value</tt> is true, performs
- * @code prefer(
- * require(ex, execution::blocking.never),
- * execution::relationship.fork,
- * execution::allocator(alloc)
- * ).execute(std::forward<CompletionHandler>(completion_handler)); @endcode
- *
- * @li If <tt>execution::is_executor<Ex>::value</tt> is false, performs
- * @code ex.post(
- * std::forward<CompletionHandler>(completion_handler),
- * alloc); @endcode
- *
- * @par Completion Signature
- * @code void() @endcode
- */
- template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken = deferred_t>
- inline auto post(NullaryToken&& token = deferred_t())
- -> decltype(
- async_initiate<NullaryToken, void()>(
- declval<detail::initiate_post>(), token))
- {
- return async_initiate<NullaryToken, void()>(
- detail::initiate_post(), token);
- }
- /// Submits a completion token or function object for execution.
- /**
- * This function submits an object for execution using the specified executor.
- * The function object is queued for execution, and is never called from the
- * current thread prior to returning from <tt>post()</tt>.
- *
- * The use of @c post(), rather than @ref defer(), indicates the caller's
- * preference that the function object be eagerly queued for execution.
- *
- * @param ex The target executor.
- *
- * @param token The @ref completion_token that will be used to produce a
- * completion handler. The function signature of the completion handler must be:
- * @code void handler(); @endcode
- *
- * @returns This function returns <tt>async_initiate<NullaryToken,
- * void()>(Init{ex}, token)</tt>, where @c Init is a function object type
- * defined as:
- *
- * @code class Init
- * {
- * public:
- * using executor_type = Executor;
- * explicit Init(const Executor& ex) : ex_(ex) {}
- * executor_type get_executor() const noexcept { return ex_; }
- * template <typename CompletionHandler>
- * void operator()(CompletionHandler&& completion_handler) const;
- * private:
- * Executor ex_; // exposition only
- * }; @endcode
- *
- * The function call operator of @c Init:
- *
- * @li Obtains the handler's associated executor object @c ex1 of type @c Ex1 by
- * performing
- * @code auto ex1 = get_associated_executor(completion_handler, ex); @endcode
- *
- * @li Obtains the handler's associated allocator object @c alloc by performing
- * @code auto alloc = get_associated_allocator(completion_handler); @endcode
- *
- * @li If <tt>execution::is_executor<Ex1>::value</tt> is true, constructs a
- * function object @c f with a member @c executor_ that is initialised with
- * <tt>prefer(ex1, execution::outstanding_work.tracked)</tt>, a member @c
- * handler_ that is a decay-copy of @c completion_handler, and a function call
- * operator that performs:
- * @code auto a = get_associated_allocator(handler_);
- * prefer(executor_, execution::allocator(a)).execute(std::move(handler_));
- * @endcode
- *
- * @li If <tt>execution::is_executor<Ex1>::value</tt> is false, constructs a
- * function object @c f with a member @c work_ that is initialised with
- * <tt>make_work_guard(ex1)</tt>, a member @c handler_ that is a decay-copy of
- * @c completion_handler, and a function call operator that performs:
- * @code auto a = get_associated_allocator(handler_);
- * work_.get_executor().dispatch(std::move(handler_), a);
- * work_.reset(); @endcode
- *
- * @li If <tt>execution::is_executor<Executor>::value</tt> is true, performs
- * @code prefer(
- * require(ex, execution::blocking.never),
- * execution::relationship.fork,
- * execution::allocator(alloc)
- * ).execute(std::move(f)); @endcode
- *
- * @li If <tt>execution::is_executor<Executor>::value</tt> is false, performs
- * @code ex.post(std::move(f), alloc); @endcode
- *
- * @par Completion Signature
- * @code void() @endcode
- */
- template <typename Executor,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
- = default_completion_token_t<Executor>>
- inline auto post(const Executor& ex,
- NullaryToken&& token = default_completion_token_t<Executor>(),
- constraint_t<
- (execution::is_executor<Executor>::value
- && can_require<Executor, execution::blocking_t::never_t>::value)
- || is_executor<Executor>::value
- > = 0)
- -> decltype(
- async_initiate<NullaryToken, void()>(
- declval<detail::initiate_post_with_executor<Executor>>(),
- token, detail::empty_work_function()))
- {
- return async_initiate<NullaryToken, void()>(
- detail::initiate_post_with_executor<Executor>(ex),
- token, detail::empty_work_function());
- }
- /// Submits a completion token or function object for execution.
- /**
- * @param ctx An execution context, from which the target executor is obtained.
- *
- * @param token The @ref completion_token that will be used to produce a
- * completion handler. The function signature of the completion handler must be:
- * @code void handler(); @endcode
- *
- * @returns <tt>post(ctx.get_executor(), forward<NullaryToken>(token))</tt>.
- *
- * @par Completion Signature
- * @code void() @endcode
- */
- template <typename ExecutionContext,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
- = default_completion_token_t<typename ExecutionContext::executor_type>>
- inline auto post(ExecutionContext& ctx,
- NullaryToken&& token = default_completion_token_t<
- typename ExecutionContext::executor_type>(),
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value
- > = 0)
- -> decltype(
- async_initiate<NullaryToken, void()>(
- declval<detail::initiate_post_with_executor<
- typename ExecutionContext::executor_type>>(),
- token, detail::empty_work_function()))
- {
- return async_initiate<NullaryToken, void()>(
- detail::initiate_post_with_executor<
- typename ExecutionContext::executor_type>(ctx.get_executor()),
- token, detail::empty_work_function());
- }
- /// Submits a function to be run on a specified target executor, and after
- /// completion submits the completion handler.
- /**
- * This function submits a function object for execution on the specified
- * executor. The function object is queued for execution, and is never called
- * from the current thread prior to returning from <tt>post()</tt>. After the
- * submitted function completes, the completion handler is dispatched to run on
- * its associated executor.
- *
- * The use of @c post(), rather than @ref defer(), indicates the caller's
- * preference that the function object be eagerly queued for execution.
- *
- * @param function A nullary function to be executed on the target executor.
- *
- * @param ex The target executor.
- *
- * @param token The @ref completion_token that will be used to produce a
- * completion handler. The function signature of the completion handler must be:
- * @code void handler(); @endcode
- *
- * @returns This function returns <tt>async_initiate<NullaryToken,
- * void()>(Init{ex}, token, forward<Function>(function))</tt>, where @c Init is
- * a function object type defined as:
- *
- * @code class Init
- * {
- * public:
- * using executor_type = Executor;
- * explicit Init(const Executor& ex) : ex_(ex) {}
- * executor_type get_executor() const noexcept { return ex_; }
- * template <typename CompletionHandler>
- * void operator()(CompletionHandler&& completion_handler,
- * Function&& function) const;
- * private:
- * Executor ex_; // exposition only
- * }; @endcode
- *
- * The function call operator of @c Init:
- *
- * @li Obtains the handler's associated executor object @c ex1 of type @c Ex1 by
- * performing
- * @code auto ex1 = get_associated_executor(completion_handler, ex); @endcode
- *
- * @li Obtains the handler's associated allocator object @c alloc by performing
- * @code auto alloc = get_associated_allocator(completion_handler); @endcode
- *
- * @li If <tt>execution::is_executor<Ex1>::value</tt> is true, constructs a
- * function object wrapper @c f with a member @c executor_ that is initialised
- * with <tt>prefer(ex1, execution::outstanding_work.tracked)</tt>, a member @c
- * function_ that is a decay-copy of @c function, a member @c handler_ that is a
- * decay-copy of @c completion_handler, and a function call operator that
- * performs:
- * @code std::move(function_)();
- * auto a = get_associated_allocator(handler_);
- * prefer(executor_, execution::allocator(a)).execute(std::move(handler_));
- * @endcode
- *
- * @li If <tt>execution::is_executor<Ex1>::value</tt> is false, constructs a
- * function object wrapper @c f with a member @c work_ that is initialised with
- * <tt>make_work_guard(ex1)</tt>, a member @c function_ that is a decay-copy of
- * @c function, a member @c handler_ that is a decay-copy of @c
- * completion_handler, and a function call operator that performs:
- * @code std::move(function_)();
- * auto a = get_associated_allocator(handler_);
- * work_.get_executor().dispatch(std::move(handler_), a);
- * work_.reset(); @endcode
- *
- * @li If <tt>execution::is_executor<Executor>::value</tt> is true, performs
- * @code prefer(
- * require(ex, execution::blocking.never),
- * execution::relationship.fork,
- * execution::allocator(alloc)
- * ).execute(std::move(f)); @endcode
- *
- * @li If <tt>execution::is_executor<Executor>::value</tt> is false, performs
- * @code ex.post(std::move(f), alloc); @endcode
- *
- * @note If the function object throws an exception, that exception is allowed
- * to propagate to the target executor. The behaviour in this case is dependent
- * on the executor. For example, boost::asio::io_context will allow the
- * exception to propagate to the caller that runs the @c io_context, whereas
- * boost::asio::thread_pool will call @c std::terminate.
- *
- * @par Example
- * This @c post overload may be used to submit long running work to a thread
- * pool and, once complete, continue execution on an associated completion
- * executor, such as a coroutine's associated executor:
- * @code boost::asio::awaitable<void> my_coroutine()
- * {
- * // ...
- *
- * co_await boost::asio::post(
- * []{
- * perform_expensive_computation();
- * },
- * my_thread_pool);
- *
- * // handle result on the coroutine's associated executor
- * } @endcode
- *
- * @par Completion Signature
- * @code void() @endcode
- */
- template <typename Function, typename Executor,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
- = default_completion_token_t<Executor>>
- inline auto post(Function&& function, const Executor& ex,
- NullaryToken&& token = default_completion_token_t<Executor>(),
- constraint_t<
- is_void<result_of_t<decay_t<Function>()>>::value
- > = 0,
- constraint_t<
- (execution::is_executor<Executor>::value
- && can_require<Executor, execution::blocking_t::never_t>::value)
- || is_executor<Executor>::value
- > = 0)
- -> decltype(
- async_initiate<NullaryToken, void()>(
- declval<detail::initiate_post_with_executor<Executor>>(),
- token, static_cast<Function&&>(function)))
- {
- return async_initiate<NullaryToken, void()>(
- detail::initiate_post_with_executor<Executor>(ex),
- token, static_cast<Function&&>(function));
- }
- /// Submits a function to be run on a specified target executor, and passes the
- /// result to a completion handler.
- /**
- * This function submits a function object for execution on the specified
- * executor. The function object is queued for execution, and is never called
- * from the current thread prior to returning from <tt>post()</tt>. After the
- * submitted function completes, the completion handler is dispatched along with
- * the function's result, to run on its associated executor.
- *
- * The use of @c post(), rather than @ref defer(), indicates the caller's
- * preference that the function object be eagerly queued for execution.
- *
- * @param function A nullary function to be executed on the target executor.
- *
- * @param ex The target executor.
- *
- * @param token The @ref completion_token that will be used to produce a
- * completion handler. The function signature of the completion handler must be:
- * @code void handler(decay_t<result_of_t<decay_t<Function>()>>); @endcode
- *
- * @returns This function returns <tt>async_initiate<CompletionToken,
- * void()>(Init{ex}, token)</tt>, where @c Init is a function object type
- * defined as:
- *
- * @code class Init
- * {
- * public:
- * using executor_type = Executor;
- * explicit Init(const Executor& ex) : ex_(ex) {}
- * executor_type get_executor() const noexcept { return ex_; }
- * template <typename CompletionHandler>
- * void operator()(CompletionHandler&& completion_handler,
- * Function&& function) const;
- * private:
- * Executor ex_; // exposition only
- * }; @endcode
- *
- * The function call operator of @c Init:
- *
- * @li Obtains the handler's associated executor object @c ex1 of type @c Ex1 by
- * performing
- * @code auto ex1 = get_associated_executor(completion_handler, ex); @endcode
- *
- * @li Obtains the handler's associated allocator object @c alloc by performing
- * @code auto alloc = get_associated_allocator(completion_handler); @endcode
- *
- * @li If <tt>execution::is_executor<Ex1>::value</tt> is true, constructs a
- * function object wrapper @c f with a member @c executor_ that is initialised
- * with <tt>prefer(ex1, execution::outstanding_work.tracked)</tt>, a member @c
- * function_ that is a decay-copy of @c function, a member @c handler_ that is a
- * decay-copy of @c completion_handler, and a function call operator that
- * performs:
- * @code auto result = std::move(function_)();
- * auto a = get_associated_allocator(handler_);
- * prefer(executor_, execution::allocator(a)).execute(
- * std::bind(std::move(handler_), std::move(result)));
- * @endcode
- *
- * @li If <tt>execution::is_executor<Ex1>::value</tt> is false, constructs a
- * function object wrapper @c f with a member @c work_ that is initialised with
- * <tt>make_work_guard(ex1)</tt>, a member @c function_ that is a decay-copy of
- * @c function, a member @c handler_ that is a decay-copy of @c
- * completion_handler, and a function call operator that performs:
- * @code auto result = std::move(function_)();
- * auto a = get_associated_allocator(handler_);
- * work_.get_executor().dispatch(
- * std::bind(std::move(handler_), std::move(result)), a);
- * work_.reset(); @endcode
- *
- * @li If <tt>execution::is_executor<Executor>::value</tt> is true, performs
- * @code prefer(
- * require(ex, execution::blocking.never),
- * execution::relationship.fork,
- * execution::allocator(alloc)
- * ).execute(std::move(f)); @endcode
- *
- * @li If <tt>execution::is_executor<Executor>::value</tt> is false, performs
- * @code ex.post(std::move(f), alloc); @endcode
- *
- * @note If the function object throws an exception, that exception is allowed
- * to propagate to the target executor. The behaviour in this case is dependent
- * on the executor. For example, boost::asio::io_context will allow the
- * exception to propagate to the caller that runs the @c io_context, whereas
- * boost::asio::thread_pool will call @c std::terminate.
- *
- * @par Example
- * This @c post overload may be used to submit long running work to a thread
- * pool and, once complete, continue execution on an associated completion
- * executor, such as a coroutine's associated executor:
- * @code boost::asio::awaitable<void> my_coroutine()
- * {
- * // ...
- *
- * int result = co_await boost::asio::post(
- * []{
- * return perform_expensive_computation();
- * },
- * my_thread_pool);
- *
- * // handle result on the coroutine's associated executor
- * } @endcode
- *
- * @par Completion Signature
- * @code void(decay_t<result_of_t<decay_t<Function>()>>) @endcode
- */
- template <typename Function, typename Executor,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(
- void(decay_t<result_of_t<decay_t<Function>()>>)) CompletionToken
- = default_completion_token_t<Executor>>
- inline auto post(Function&& function, const Executor& ex,
- CompletionToken&& token = default_completion_token_t<Executor>(),
- constraint_t<
- !is_void<result_of_t<decay_t<Function>()>>::value
- > = 0,
- constraint_t<
- (execution::is_executor<Executor>::value
- && can_require<Executor, execution::blocking_t::never_t>::value)
- || is_executor<Executor>::value
- > = 0)
- -> decltype(
- async_initiate<CompletionToken, void(detail::work_result_t<Function>)>(
- declval<detail::initiate_post_with_executor<Executor>>(),
- token, static_cast<Function&&>(function)))
- {
- return async_initiate<CompletionToken, void(detail::work_result_t<Function>)>(
- detail::initiate_post_with_executor<Executor>(ex),
- token, static_cast<Function&&>(function));
- }
- /// Submits a function to be run on a specified execution context, and after
- /// completion submits the completion handler.
- /**
- * @param function A nullary function to be executed on the target executor.
- *
- * @param ctx An execution context, from which the target executor is obtained.
- *
- * @param token The @ref completion_token that will be used to produce a
- * completion handler. The function signature of the completion handler must be:
- * @code void handler(); @endcode
- *
- * @returns <tt>post(forward<Function>(function), ctx.get_executor(),
- * forward<NullaryToken>(token))</tt>.
- *
- * @note If the function object throws an exception, that exception is allowed
- * to propagate to the target executor. The behaviour in this case is dependent
- * on the executor. For example, boost::asio::io_context will allow the
- * exception to propagate to the caller that runs the @c io_context, whereas
- * boost::asio::thread_pool will call @c std::terminate.
- *
- * @par Completion Signature
- * @code void() @endcode
- */
- template <typename Function, typename ExecutionContext,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
- = default_completion_token_t<typename ExecutionContext::executor_type>>
- inline auto post(Function&& function, ExecutionContext& ctx,
- NullaryToken&& token = default_completion_token_t<
- typename ExecutionContext::executor_type>(),
- constraint_t<
- is_void<result_of_t<decay_t<Function>()>>::value
- > = 0,
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value
- > = 0)
- -> decltype(
- async_initiate<NullaryToken, void()>(
- declval<detail::initiate_post_with_executor<
- typename ExecutionContext::executor_type>>(),
- token, static_cast<Function&&>(function)))
- {
- return async_initiate<NullaryToken, void()>(
- detail::initiate_post_with_executor<
- typename ExecutionContext::executor_type>(ctx.get_executor()),
- token, static_cast<Function&&>(function));
- }
- /// Submits a function to be run on a specified execution context, and passes
- /// the result to a completion handler.
- /**
- * @param function A nullary function to be executed on the target executor.
- *
- * @param ctx An execution context, from which the target executor is obtained.
- *
- * @param token The @ref completion_token that will be used to produce a
- * completion handler. The function signature of the completion handler must be:
- * @code void handler(); @endcode
- *
- * @returns <tt>post(forward<Function>(function), ctx.get_executor(),
- * forward<CompletionToken>(token))</tt>.
- *
- * @note If the function object throws an exception, that exception is allowed
- * to propagate to the target executor. The behaviour in this case is dependent
- * on the executor. For example, boost::asio::io_context will allow the
- * exception to propagate to the caller that runs the @c io_context, whereas
- * boost::asio::thread_pool will call @c std::terminate.
- *
- * @par Completion Signature
- * @code void(decay_t<result_of_t<decay_t<Function>()>>) @endcode
- */
- template <typename Function, typename ExecutionContext,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(
- void(decay_t<result_of_t<decay_t<Function>()>>)) CompletionToken
- = default_completion_token_t<typename ExecutionContext::executor_type>>
- inline auto post(Function&& function, ExecutionContext& ctx,
- CompletionToken&& token = default_completion_token_t<
- typename ExecutionContext::executor_type>(),
- constraint_t<
- !is_void<result_of_t<decay_t<Function>()>>::value
- > = 0,
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value
- > = 0)
- -> decltype(
- async_initiate<CompletionToken, void(detail::work_result_t<Function>)>(
- declval<detail::initiate_post_with_executor<
- typename ExecutionContext::executor_type>>(),
- token, static_cast<Function&&>(function)))
- {
- return async_initiate<CompletionToken, void(detail::work_result_t<Function>)>(
- detail::initiate_post_with_executor<
- typename ExecutionContext::executor_type>(ctx.get_executor()),
- token, static_cast<Function&&>(function));
- }
- } // namespace asio
- } // namespace boost
- #include <boost/asio/detail/pop_options.hpp>
- #endif // BOOST_ASIO_POST_HPP
|