| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2026 Michael Vandeberg
|
2 |
|
// Copyright (c) 2026 Michael Vandeberg
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
| 5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/cppalliance/capy
|
7 |
|
// Official repository: https://github.com/cppalliance/capy
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_CAPY_DELAY_HPP
|
10 |
|
#ifndef BOOST_CAPY_DELAY_HPP
|
| 11 |
|
#define BOOST_CAPY_DELAY_HPP
|
11 |
|
#define BOOST_CAPY_DELAY_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/capy/detail/config.hpp>
|
13 |
|
#include <boost/capy/detail/config.hpp>
|
| 14 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
14 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
| 15 |
|
#include <boost/capy/ex/io_env.hpp>
|
15 |
|
#include <boost/capy/ex/io_env.hpp>
|
| 16 |
|
#include <boost/capy/ex/detail/timer_service.hpp>
|
16 |
|
#include <boost/capy/ex/detail/timer_service.hpp>
|
| 17 |
|
|
17 |
|
|
| 18 |
|
|
18 |
|
|
| 19 |
|
|
19 |
|
|
| 20 |
|
|
20 |
|
|
| 21 |
|
|
21 |
|
|
| 22 |
|
|
22 |
|
|
| 23 |
|
|
23 |
|
|
| 24 |
|
|
24 |
|
|
| 25 |
|
|
25 |
|
|
| 26 |
|
|
26 |
|
|
| 27 |
|
|
27 |
|
|
| 28 |
|
/** IoAwaitable returned by @ref delay.
|
28 |
|
/** IoAwaitable returned by @ref delay.
|
| 29 |
|
|
29 |
|
|
| 30 |
|
Suspends the calling coroutine until the deadline elapses
|
30 |
|
Suspends the calling coroutine until the deadline elapses
|
| 31 |
|
or the environment's stop token is activated, whichever
|
31 |
|
or the environment's stop token is activated, whichever
|
| 32 |
|
comes first. Resumption is always posted through the
|
32 |
|
comes first. Resumption is always posted through the
|
| 33 |
|
executor, never inline on the timer thread.
|
33 |
|
executor, never inline on the timer thread.
|
| 34 |
|
|
34 |
|
|
| 35 |
|
Not intended to be named directly; use the @ref delay
|
35 |
|
Not intended to be named directly; use the @ref delay
|
| 36 |
|
factory function instead.
|
36 |
|
factory function instead.
|
| 37 |
|
|
37 |
|
|
| 38 |
|
|
38 |
|
|
| 39 |
|
|
39 |
|
|
| 40 |
|
If `stop_requested()` is true before suspension, the
|
40 |
|
If `stop_requested()` is true before suspension, the
|
| 41 |
|
coroutine resumes immediately without scheduling a timer.
|
41 |
|
coroutine resumes immediately without scheduling a timer.
|
| 42 |
|
If stop is requested while suspended, the stop callback
|
42 |
|
If stop is requested while suspended, the stop callback
|
| 43 |
|
claims the resume and posts it through the executor; the
|
43 |
|
claims the resume and posts it through the executor; the
|
| 44 |
|
pending timer is cancelled on the next `await_resume` or
|
44 |
|
pending timer is cancelled on the next `await_resume` or
|
| 45 |
|
|
45 |
|
|
| 46 |
|
|
46 |
|
|
| 47 |
|
|
47 |
|
|
| 48 |
|
|
48 |
|
|
| 49 |
|
A single `delay_awaitable` must not be awaited concurrently.
|
49 |
|
A single `delay_awaitable` must not be awaited concurrently.
|
| 50 |
|
Multiple independent `delay()` calls on the same
|
50 |
|
Multiple independent `delay()` calls on the same
|
| 51 |
|
execution_context are safe and share one timer thread.
|
51 |
|
execution_context are safe and share one timer thread.
|
| 52 |
|
|
52 |
|
|
| 53 |
|
|
53 |
|
|
| 54 |
|
|
54 |
|
|
| 55 |
|
|
55 |
|
|
| 56 |
|
|
56 |
|
|
| 57 |
|
std::chrono::nanoseconds dur_;
|
57 |
|
std::chrono::nanoseconds dur_;
|
| 58 |
|
|
58 |
|
|
| 59 |
|
detail::timer_service* ts_ = nullptr;
|
59 |
|
detail::timer_service* ts_ = nullptr;
|
| 60 |
|
detail::timer_service::timer_id tid_ = 0;
|
60 |
|
detail::timer_service::timer_id tid_ = 0;
|
| 61 |
|
|
61 |
|
|
| 62 |
|
// Declared before stop_cb_buf_: the callback
|
62 |
|
// Declared before stop_cb_buf_: the callback
|
| 63 |
|
// accesses these members, so they must still be
|
63 |
|
// accesses these members, so they must still be
|
| 64 |
|
// alive if the stop_cb_ destructor blocks.
|
64 |
|
// alive if the stop_cb_ destructor blocks.
|
| 65 |
|
std::atomic<bool> claimed_{false};
|
65 |
|
std::atomic<bool> claimed_{false};
|
| 66 |
|
|
66 |
|
|
| 67 |
|
bool stop_cb_active_ = false;
|
67 |
|
bool stop_cb_active_ = false;
|
| 68 |
|
|
68 |
|
|
| 69 |
|
|
69 |
|
|
| 70 |
|
|
70 |
|
|
| 71 |
|
|
71 |
|
|
| 72 |
|
|
72 |
|
|
| 73 |
|
std::coroutine_handle<> h_;
|
73 |
|
std::coroutine_handle<> h_;
|
| 74 |
|
|
74 |
|
|
| 75 |
|
void operator()() const noexcept
|
75 |
|
void operator()() const noexcept
|
| 76 |
|
|
76 |
|
|
| 77 |
|
if(!self_->claimed_.exchange(
|
77 |
|
if(!self_->claimed_.exchange(
|
| 78 |
|
true, std::memory_order_acq_rel))
|
78 |
|
true, std::memory_order_acq_rel))
|
| 79 |
|
|
79 |
|
|
| 80 |
|
|
80 |
|
|
| 81 |
|
|
81 |
|
|
| 82 |
|
|
82 |
|
|
| 83 |
|
|
83 |
|
|
| 84 |
|
|
84 |
|
|
| 85 |
|
|
85 |
|
|
| 86 |
|
using stop_cb_t = std::stop_callback<cancel_fn>;
|
86 |
|
using stop_cb_t = std::stop_callback<cancel_fn>;
|
| 87 |
|
|
87 |
|
|
| 88 |
|
// Aligned storage for the stop callback.
|
88 |
|
// Aligned storage for the stop callback.
|
| 89 |
|
// Declared last: its destructor may block while
|
89 |
|
// Declared last: its destructor may block while
|
| 90 |
|
// the callback accesses the members above.
|
90 |
|
// the callback accesses the members above.
|
| 91 |
|
|
91 |
|
|
| 92 |
|
|
92 |
|
|
| 93 |
|
# pragma warning(disable: 4324)
|
93 |
|
# pragma warning(disable: 4324)
|
| 94 |
|
|
94 |
|
|
| 95 |
|
|
95 |
|
|
| 96 |
|
unsigned char stop_cb_buf_[sizeof(stop_cb_t)];
|
96 |
|
unsigned char stop_cb_buf_[sizeof(stop_cb_t)];
|
| 97 |
|
|
97 |
|
|
| 98 |
|
|
98 |
|
|
| 99 |
|
|
99 |
|
|
| 100 |
|
|
100 |
|
|
| 101 |
|
stop_cb_t& stop_cb_() noexcept
|
101 |
|
stop_cb_t& stop_cb_() noexcept
|
| 102 |
|
|
102 |
|
|
| 103 |
|
return *reinterpret_cast<stop_cb_t*>(stop_cb_buf_);
|
103 |
|
return *reinterpret_cast<stop_cb_t*>(stop_cb_buf_);
|
| 104 |
|
|
104 |
|
|
| 105 |
|
|
105 |
|
|
| 106 |
|
|
106 |
|
|
| 107 |
|
explicit delay_awaitable(std::chrono::nanoseconds dur) noexcept
|
107 |
|
explicit delay_awaitable(std::chrono::nanoseconds dur) noexcept
|
| 108 |
|
|
108 |
|
|
| 109 |
|
|
109 |
|
|
| 110 |
|
|
110 |
|
|
| 111 |
|
|
111 |
|
|
| 112 |
|
/// @pre The stop callback must not be active
|
112 |
|
/// @pre The stop callback must not be active
|
| 113 |
|
/// (i.e. the object has not yet been awaited).
|
113 |
|
/// (i.e. the object has not yet been awaited).
|
| 114 |
|
delay_awaitable(delay_awaitable&& o) noexcept
|
114 |
|
delay_awaitable(delay_awaitable&& o) noexcept
|
| 115 |
|
|
115 |
|
|
| 116 |
|
|
116 |
|
|
| 117 |
|
|
117 |
|
|
| 118 |
|
, claimed_(o.claimed_.load(std::memory_order_relaxed))
|
118 |
|
, claimed_(o.claimed_.load(std::memory_order_relaxed))
|
| 119 |
|
|
119 |
|
|
| 120 |
|
, stop_cb_active_(std::exchange(o.stop_cb_active_, false))
|
120 |
|
, stop_cb_active_(std::exchange(o.stop_cb_active_, false))
|
| 121 |
|
|
121 |
|
|
| 122 |
|
|
122 |
|
|
| 123 |
|
|
123 |
|
|
| 124 |
|
|
124 |
|
|
| 125 |
|
|
125 |
|
|
| 126 |
|
|
126 |
|
|
| 127 |
|
|
127 |
|
|
| 128 |
|
|
128 |
|
|
| 129 |
|
|
129 |
|
|
| 130 |
|
|
130 |
|
|
| 131 |
|
|
131 |
|
|
| 132 |
|
delay_awaitable(delay_awaitable const&) = delete;
|
132 |
|
delay_awaitable(delay_awaitable const&) = delete;
|
| 133 |
|
delay_awaitable& operator=(delay_awaitable const&) = delete;
|
133 |
|
delay_awaitable& operator=(delay_awaitable const&) = delete;
|
| 134 |
|
delay_awaitable& operator=(delay_awaitable&&) = delete;
|
134 |
|
delay_awaitable& operator=(delay_awaitable&&) = delete;
|
| 135 |
|
|
135 |
|
|
| 136 |
|
bool await_ready() const noexcept
|
136 |
|
bool await_ready() const noexcept
|
| 137 |
|
|
137 |
|
|
| 138 |
|
return dur_.count() <= 0;
|
138 |
|
return dur_.count() <= 0;
|
| 139 |
|
|
139 |
|
|
| 140 |
|
|
140 |
|
|
| 141 |
|
|
141 |
|
|
| 142 |
|
|
142 |
|
|
| 143 |
|
std::coroutine_handle<> h,
|
143 |
|
std::coroutine_handle<> h,
|
| 144 |
|
io_env const* env) noexcept
|
144 |
|
io_env const* env) noexcept
|
| 145 |
|
|
145 |
|
|
| 146 |
|
// Already stopped: resume immediately
|
146 |
|
// Already stopped: resume immediately
|
| 147 |
|
if(env->stop_token.stop_requested())
|
147 |
|
if(env->stop_token.stop_requested())
|
| 148 |
|
|
148 |
|
|
| 149 |
|
|
149 |
|
|
| 150 |
|
|
150 |
|
|
| 151 |
|
|
151 |
|
|
| 152 |
|
|
152 |
|
|
| 153 |
|
ts_ = &env->executor.context().use_service<detail::timer_service>();
|
153 |
|
ts_ = &env->executor.context().use_service<detail::timer_service>();
|
| 154 |
|
|
154 |
|
|
| 155 |
|
// Schedule timer (won't fire inline since deadline is in the future)
|
155 |
|
// Schedule timer (won't fire inline since deadline is in the future)
|
| 156 |
|
tid_ = ts_->schedule_after(dur_,
|
156 |
|
tid_ = ts_->schedule_after(dur_,
|
| 157 |
|
[this, h, ex = env->executor]()
|
157 |
|
[this, h, ex = env->executor]()
|
| 158 |
|
|
158 |
|
|
| 159 |
|
|
159 |
|
|
| 160 |
|
true, std::memory_order_acq_rel))
|
160 |
|
true, std::memory_order_acq_rel))
|
| 161 |
|
|
161 |
|
|
| 162 |
|
|
162 |
|
|
| 163 |
|
|
163 |
|
|
| 164 |
|
|
164 |
|
|
| 165 |
|
|
165 |
|
|
| 166 |
|
// Register stop callback (may fire inline)
|
166 |
|
// Register stop callback (may fire inline)
|
| 167 |
|
::new(stop_cb_buf_) stop_cb_t(
|
167 |
|
::new(stop_cb_buf_) stop_cb_t(
|
| 168 |
|
|
168 |
|
|
| 169 |
|
cancel_fn{this, env->executor, h});
|
169 |
|
cancel_fn{this, env->executor, h});
|
| 170 |
|
|
170 |
|
|
| 171 |
|
|
171 |
|
|
| 172 |
|
return std::noop_coroutine();
|
172 |
|
return std::noop_coroutine();
|
| 173 |
|
|
173 |
|
|
| 174 |
|
|
174 |
|
|
| 175 |
|
void await_resume() noexcept
|
175 |
|
void await_resume() noexcept
|
| 176 |
|
|
176 |
|
|
| 177 |
|
|
177 |
|
|
| 178 |
|
|
178 |
|
|
| 179 |
|
|
179 |
|
|
| 180 |
|
|
180 |
|
|
| 181 |
|
|
181 |
|
|
| 182 |
|
|
182 |
|
|
| 183 |
|
|
183 |
|
|
| 184 |
|
|
184 |
|
|
| 185 |
|
|
185 |
|
|
| 186 |
|
|
186 |
|
|
| 187 |
|
/** Suspend the current coroutine for a duration.
|
187 |
|
/** Suspend the current coroutine for a duration.
|
| 188 |
|
|
188 |
|
|
| 189 |
|
Returns an IoAwaitable that completes at or after the
|
189 |
|
Returns an IoAwaitable that completes at or after the
|
| 190 |
|
specified duration, or earlier if the environment's stop
|
190 |
|
specified duration, or earlier if the environment's stop
|
| 191 |
|
token is activated. Completion is always normal (void
|
191 |
|
token is activated. Completion is always normal (void
|
| 192 |
|
return); no exception is thrown on cancellation.
|
192 |
|
return); no exception is thrown on cancellation.
|
| 193 |
|
|
193 |
|
|
| 194 |
|
Zero or negative durations complete synchronously without
|
194 |
|
Zero or negative durations complete synchronously without
|
| 195 |
|
|
195 |
|
|
| 196 |
|
|
196 |
|
|
| 197 |
|
|
197 |
|
|
| 198 |
|
|
198 |
|
|
| 199 |
|
co_await delay(std::chrono::milliseconds(100));
|
199 |
|
co_await delay(std::chrono::milliseconds(100));
|
| 200 |
|
|
200 |
|
|
| 201 |
|
|
201 |
|
|
| 202 |
|
@param dur The duration to wait.
|
202 |
|
@param dur The duration to wait.
|
| 203 |
|
|
203 |
|
|
| 204 |
|
@return A @ref delay_awaitable whose `await_resume`
|
204 |
|
@return A @ref delay_awaitable whose `await_resume`
|
| 205 |
|
|
205 |
|
|
| 206 |
|
|
206 |
|
|
| 207 |
|
|
207 |
|
|
| 208 |
|
|
208 |
|
|
| 209 |
|
@see timeout, delay_awaitable
|
209 |
|
@see timeout, delay_awaitable
|
| 210 |
|
|
210 |
|
|
| 211 |
|
template<typename Rep, typename Period>
|
211 |
|
template<typename Rep, typename Period>
|
| 212 |
|
|
212 |
|
|
| 213 |
|
delay(std::chrono::duration<Rep, Period> dur) noexcept
|
213 |
|
delay(std::chrono::duration<Rep, Period> dur) noexcept
|
| 214 |
|
|
214 |
|
|
| 215 |
|
|
215 |
|
|
| 216 |
|
std::chrono::duration_cast<std::chrono::nanoseconds>(dur)};
|
216 |
|
std::chrono::duration_cast<std::chrono::nanoseconds>(dur)};
|
| 217 |
|
|
217 |
|
|
| 218 |
|
|
218 |
|
|
| 219 |
|
|
219 |
|
|
| 220 |
|
|
220 |
|
|
| 221 |
|
|
221 |
|
|
| 222 |
|
|
222 |
|
|