1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// immediate.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_IMMEDIATE_HPP
#define BOOST_ASIO_IMMEDIATE_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/associated_immediate_executor.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace detail {
template <typename Executor>
class initiate_immediate
{
public:
typedef Executor executor_type;
explicit initiate_immediate(const Executor& ex)
: ex_(ex)
{
}
executor_type get_executor() const noexcept
{
return ex_;
}
template <typename CompletionHandler>
void operator()(CompletionHandler&& handler) const
{
typename associated_immediate_executor<
CompletionHandler, executor_type>::type ex =
(get_associated_immediate_executor)(handler, ex_);
(dispatch)(ex, static_cast<CompletionHandler&&>(handler));
}
private:
Executor ex_;
};
} // namespace detail
/// Launch a trivial asynchronous operation that completes immediately.
/**
* The async_immediate function is intended for use by composed operations,
* which can delegate to this operation in order to implement the correct
* semantics for immediate completion.
*
* @param ex The asynchronous operation's I/O executor.
*
* @param token The completion token.
*
* The completion handler is immediately submitted for execution by calling
* boost::asio::dispatch() on the handler's associated immediate executor.
*
* If the completion handler does not have a customised associated immediate
* executor, then the handler is submitted as if by calling boost::asio::post()
* on the supplied I/O executor.
*
* @par Completion Signature
* @code void() @endcode
*/
template <typename Executor,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
= default_completion_token_t<Executor>>
inline auto async_immediate(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_immediate<Executor>>(), token))
{
return async_initiate<NullaryToken, void()>(
detail::initiate_immediate<Executor>(ex), token);
}
/// Launch a trivial asynchronous operation that completes immediately.
/**
* The async_immediate function is intended for use by composed operations,
* which can delegate to this operation in order to implement the correct
* semantics for immediate completion.
*
* @param ex The execution context used to obtain the asynchronous operation's
* I/O executor.
*
* @param token The completion token.
*
* The completion handler is immediately submitted for execution by calling
* boost::asio::dispatch() on the handler's associated immediate executor.
*
* If the completion handler does not have a customised associated immediate
* executor, then the handler is submitted as if by calling boost::asio::post()
* on the I/O executor obtained from the supplied execution context.
*
* @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 async_immediate(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_immediate<
typename ExecutionContext::executor_type>>(), token))
{
return async_initiate<NullaryToken, void()>(
detail::initiate_immediate<
typename ExecutionContext::executor_type>(
ctx.get_executor()), token);
}
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IMMEDIATE_HPP