| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2026 Steve Gerbino
|
2 |
|
// Copyright (c) 2026 Steve Gerbino
|
| 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/corosio
|
7 |
|
// Official repository: https://github.com/cppalliance/corosio
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_COROSIO_DETAIL_TIMEOUT_CORO_HPP
|
10 |
|
#ifndef BOOST_COROSIO_DETAIL_TIMEOUT_CORO_HPP
|
| 11 |
|
#define BOOST_COROSIO_DETAIL_TIMEOUT_CORO_HPP
|
11 |
|
#define BOOST_COROSIO_DETAIL_TIMEOUT_CORO_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/capy/concept/io_awaitable.hpp>
|
13 |
|
#include <boost/capy/concept/io_awaitable.hpp>
|
| 14 |
|
#include <boost/capy/ex/frame_allocator.hpp>
|
14 |
|
#include <boost/capy/ex/frame_allocator.hpp>
|
| 15 |
|
#include <boost/capy/ex/io_awaitable_promise_base.hpp>
|
15 |
|
#include <boost/capy/ex/io_awaitable_promise_base.hpp>
|
| 16 |
|
#include <boost/capy/ex/io_env.hpp>
|
16 |
|
#include <boost/capy/ex/io_env.hpp>
|
| 17 |
|
|
17 |
|
|
| 18 |
|
|
18 |
|
|
| 19 |
|
|
19 |
|
|
| 20 |
|
|
20 |
|
|
| 21 |
|
|
21 |
|
|
| 22 |
|
|
22 |
|
|
| 23 |
|
/* Self-destroying coroutine that awaits a timer and signals a
|
23 |
|
/* Self-destroying coroutine that awaits a timer and signals a
|
| 24 |
|
stop_source on expiry. Created suspended (initial_suspend =
|
24 |
|
stop_source on expiry. Created suspended (initial_suspend =
|
| 25 |
|
suspend_always); the caller sets an owned io_env copy then
|
25 |
|
suspend_always); the caller sets an owned io_env copy then
|
| 26 |
|
resumes, which runs synchronously until the timer wait suspends.
|
26 |
|
resumes, which runs synchronously until the timer wait suspends.
|
| 27 |
|
At final_suspend, suspend_never destroys the frame — the
|
27 |
|
At final_suspend, suspend_never destroys the frame — the
|
| 28 |
|
timeout_coro destructor is intentionally a no-op since the
|
28 |
|
timeout_coro destructor is intentionally a no-op since the
|
| 29 |
|
handle is dangling after self-destruction. If the coroutine is
|
29 |
|
handle is dangling after self-destruction. If the coroutine is
|
| 30 |
|
still suspended at shutdown, the timer service drains it via
|
30 |
|
still suspended at shutdown, the timer service drains it via
|
| 31 |
|
completion_op::destroy().
|
31 |
|
completion_op::destroy().
|
| 32 |
|
|
32 |
|
|
| 33 |
|
The promise reuses task<>'s transform_awaiter pattern (including
|
33 |
|
The promise reuses task<>'s transform_awaiter pattern (including
|
| 34 |
|
the MSVC symmetric-transfer workaround) to inject io_env into
|
34 |
|
the MSVC symmetric-transfer workaround) to inject io_env into
|
| 35 |
|
IoAwaitable co_await expressions. */
|
35 |
|
IoAwaitable co_await expressions. */
|
| 36 |
|
|
36 |
|
|
| 37 |
|
namespace boost::corosio::detail {
|
37 |
|
namespace boost::corosio::detail {
|
| 38 |
|
|
38 |
|
|
| 39 |
|
/** Fire-and-forget coroutine for the timeout side of cancel_at.
|
39 |
|
/** Fire-and-forget coroutine for the timeout side of cancel_at.
|
| 40 |
|
|
40 |
|
|
| 41 |
|
The coroutine awaits a timer and signals a stop_source if the
|
41 |
|
The coroutine awaits a timer and signals a stop_source if the
|
| 42 |
|
timer fires without being cancelled. It self-destroys at
|
42 |
|
timer fires without being cancelled. It self-destroys at
|
| 43 |
|
final_suspend via suspend_never.
|
43 |
|
final_suspend via suspend_never.
|
| 44 |
|
|
44 |
|
|
| 45 |
|
|
45 |
|
|
| 46 |
|
|
46 |
|
|
| 47 |
|
|
47 |
|
|
| 48 |
|
|
48 |
|
|
| 49 |
- |
|
49 |
+ |
struct promise_type : capy::io_awaitable_promise_base<promise_type>
|
| 50 |
- |
: capy::io_awaitable_promise_base<promise_type>
|
|
|
|
| 51 |
|
|
50 |
|
|
| 52 |
|
capy::io_env env_storage_;
|
51 |
|
capy::io_env env_storage_;
|
| 53 |
|
|
52 |
|
|
| 54 |
|
/** Store an owned copy of the environment.
|
53 |
|
/** Store an owned copy of the environment.
|
| 55 |
|
|
54 |
|
|
| 56 |
|
The timeout coroutine can outlive the cancel_at_awaitable
|
55 |
|
The timeout coroutine can outlive the cancel_at_awaitable
|
| 57 |
|
that created it, so it must own its env rather than
|
56 |
|
that created it, so it must own its env rather than
|
| 58 |
|
pointing to external storage.
|
57 |
|
pointing to external storage.
|
| 59 |
|
|
58 |
|
|
| 60 |
|
void set_env_owned(capy::io_env env)
|
59 |
|
void set_env_owned(capy::io_env env)
|
| 61 |
|
|
60 |
|
|
| 62 |
|
env_storage_ = std::move(env);
|
61 |
|
env_storage_ = std::move(env);
|
| 63 |
|
set_environment(&env_storage_);
|
62 |
|
set_environment(&env_storage_);
|
| 64 |
|
|
63 |
|
|
| 65 |
|
|
64 |
|
|
| 66 |
|
timeout_coro get_return_object() noexcept
|
65 |
|
timeout_coro get_return_object() noexcept
|
| 67 |
|
|
66 |
|
|
| 68 |
|
|
67 |
|
|
| 69 |
- |
std::coroutine_handle<promise_type>::from_promise(
|
68 |
+ |
std::coroutine_handle<promise_type>::from_promise(*this)};
|
| 70 |
- |
|
|
|
|
| 71 |
|
|
69 |
|
|
| 72 |
|
|
70 |
|
|
| 73 |
- |
std::suspend_always initial_suspend() noexcept { return {}; }
|
71 |
+ |
std::suspend_always initial_suspend() noexcept
|
| 74 |
- |
std::suspend_never final_suspend() noexcept { return {}; }
|
72 |
+ |
|
|
|
|
73 |
+ |
|
|
|
|
74 |
+ |
|
|
|
|
75 |
+ |
std::suspend_never final_suspend() noexcept
|
|
|
|
76 |
+ |
|
|
|
|
77 |
+ |
|
|
|
|
78 |
+ |
|
| 75 |
|
void return_void() noexcept {}
|
79 |
|
void return_void() noexcept {}
|
| 76 |
|
void unhandled_exception() noexcept {}
|
80 |
|
void unhandled_exception() noexcept {}
|
| 77 |
|
|
81 |
|
|
| 78 |
|
template<class Awaitable>
|
82 |
|
template<class Awaitable>
|
| 79 |
|
|
83 |
|
|
| 80 |
|
|
84 |
|
|
| 81 |
|
std::decay_t<Awaitable> a_;
|
85 |
|
std::decay_t<Awaitable> a_;
|
| 82 |
|
|
86 |
|
|
| 83 |
|
|
87 |
|
|
| 84 |
|
bool await_ready() noexcept
|
88 |
|
bool await_ready() noexcept
|
| 85 |
|
|
89 |
|
|
| 86 |
|
|
90 |
|
|
| 87 |
|
|
91 |
|
|
| 88 |
|
|
92 |
|
|
| 89 |
|
decltype(auto) await_resume()
|
93 |
|
decltype(auto) await_resume()
|
| 90 |
|
|
94 |
|
|
| 91 |
|
capy::set_current_frame_allocator(
|
95 |
|
capy::set_current_frame_allocator(
|
| 92 |
|
p_->environment()->frame_allocator);
|
96 |
|
p_->environment()->frame_allocator);
|
| 93 |
|
return a_.await_resume();
|
97 |
|
return a_.await_resume();
|
| 94 |
|
|
98 |
|
|
| 95 |
|
|
99 |
|
|
| 96 |
|
|
100 |
|
|
| 97 |
- |
|
101 |
+ |
auto await_suspend(std::coroutine_handle<Promise> h) noexcept
|
| 98 |
- |
std::coroutine_handle<Promise> h) noexcept
|
|
|
|
| 99 |
|
|
102 |
|
|
| 100 |
|
|
103 |
|
|
| 101 |
- |
|
104 |
+ |
using R = decltype(a_.await_suspend(h, p_->environment()));
|
| 102 |
- |
a_.await_suspend(h, p_->environment()));
|
105 |
+ |
if constexpr (std::is_same_v<R, std::coroutine_handle<>>)
|
| 103 |
- |
if constexpr (std::is_same_v<
|
106 |
+ |
a_.await_suspend(h, p_->environment()).resume();
|
| 104 |
- |
R, std::coroutine_handle<>>)
|
|
|
|
| 105 |
- |
a_.await_suspend(h, p_->environment())
|
|
|
|
| 106 |
- |
|
|
|
|
| 107 |
|
|
107 |
|
|
| 108 |
- |
|
108 |
+ |
return a_.await_suspend(h, p_->environment());
|
| 109 |
- |
|
|
|
|
| 110 |
|
|
109 |
|
|
| 111 |
|
return a_.await_suspend(h, p_->environment());
|
110 |
|
return a_.await_suspend(h, p_->environment());
|
| 112 |
|
|
111 |
|
|
| 113 |
|
|
112 |
|
|
| 114 |
|
|
113 |
|
|
| 115 |
|
|
114 |
|
|
| 116 |
|
template<class Awaitable>
|
115 |
|
template<class Awaitable>
|
| 117 |
|
auto transform_awaitable(Awaitable&& a)
|
116 |
|
auto transform_awaitable(Awaitable&& a)
|
| 118 |
|
|
117 |
|
|
| 119 |
|
using A = std::decay_t<Awaitable>;
|
118 |
|
using A = std::decay_t<Awaitable>;
|
| 120 |
|
if constexpr (capy::IoAwaitable<A>)
|
119 |
|
if constexpr (capy::IoAwaitable<A>)
|
| 121 |
|
|
120 |
|
|
| 122 |
|
return transform_awaiter<Awaitable>{
|
121 |
|
return transform_awaiter<Awaitable>{
|
| 123 |
|
std::forward<Awaitable>(a), this};
|
122 |
|
std::forward<Awaitable>(a), this};
|
| 124 |
|
|
123 |
|
|
| 125 |
|
|
124 |
|
|
| 126 |
|
|
125 |
|
|
| 127 |
- |
|
126 |
+ |
static_assert(sizeof(A) == 0, "requires IoAwaitable");
|
| 128 |
- |
sizeof(A) == 0, "requires IoAwaitable");
|
|
|
|
| 129 |
|
|
127 |
|
|
| 130 |
|
|
128 |
|
|
| 131 |
|
|
129 |
|
|
| 132 |
|
|
130 |
|
|
| 133 |
|
std::coroutine_handle<promise_type> h_;
|
131 |
|
std::coroutine_handle<promise_type> h_;
|
| 134 |
|
|
132 |
|
|
| 135 |
|
timeout_coro() noexcept : h_(nullptr) {}
|
133 |
|
timeout_coro() noexcept : h_(nullptr) {}
|
| 136 |
|
|
134 |
|
|
| 137 |
- |
|
135 |
+ |
explicit timeout_coro(std::coroutine_handle<promise_type> h) noexcept
|
| 138 |
- |
std::coroutine_handle<promise_type> h) noexcept
|
|
|
|
| 139 |
|
|
136 |
|
|
| 140 |
|
|
137 |
|
|
| 141 |
|
|
138 |
|
|
| 142 |
|
|
139 |
|
|
| 143 |
|
// Self-destroying via suspend_never at final_suspend
|
140 |
|
// Self-destroying via suspend_never at final_suspend
|
| 144 |
|
~timeout_coro() = default;
|
141 |
|
~timeout_coro() = default;
|
| 145 |
|
|
142 |
|
|
| 146 |
- |
timeout_coro(timeout_coro const&) = delete;
|
143 |
+ |
timeout_coro(timeout_coro const&) = delete;
|
| 147 |
|
timeout_coro& operator=(timeout_coro const&) = delete;
|
144 |
|
timeout_coro& operator=(timeout_coro const&) = delete;
|
| 148 |
|
|
145 |
|
|
| 149 |
- |
timeout_coro(timeout_coro&& o) noexcept
|
146 |
+ |
timeout_coro(timeout_coro&& o) noexcept : h_(o.h_)
|
| 150 |
- |
|
|
|
|
| 151 |
|
|
147 |
|
|
| 152 |
|
|
148 |
|
|
| 153 |
|
|
149 |
|
|
| 154 |
|
|
150 |
|
|
| 155 |
|
timeout_coro& operator=(timeout_coro&& o) noexcept
|
151 |
|
timeout_coro& operator=(timeout_coro&& o) noexcept
|
| 156 |
|
|
152 |
|
|
| 157 |
|
|
153 |
|
|
| 158 |
|
|
154 |
|
|
| 159 |
|
|
155 |
|
|
| 160 |
|
|
156 |
|
|
| 161 |
|
|
157 |
|
|
| 162 |
|
|
158 |
|
|
| 163 |
|
/** Create a fire-and-forget timeout coroutine.
|
159 |
|
/** Create a fire-and-forget timeout coroutine.
|
| 164 |
|
|
160 |
|
|
| 165 |
|
Wait on the timer. If it fires without cancellation, signal
|
161 |
|
Wait on the timer. If it fires without cancellation, signal
|
| 166 |
|
the stop source to cancel the paired inner operation.
|
162 |
|
the stop source to cancel the paired inner operation.
|
| 167 |
|
|
163 |
|
|
| 168 |
|
@tparam Timer Timer type (`timer` or `native_timer<B>`).
|
164 |
|
@tparam Timer Timer type (`timer` or `native_timer<B>`).
|
| 169 |
|
|
165 |
|
|
| 170 |
|
@param t The timer to wait on (must have expiry set).
|
166 |
|
@param t The timer to wait on (must have expiry set).
|
| 171 |
|
@param src Stop source to signal on timeout.
|
167 |
|
@param src Stop source to signal on timeout.
|
| 172 |
|
|
168 |
|
|
| 173 |
|
|
169 |
|
|
| 174 |
- |
timeout_coro make_timeout(Timer& t, std::stop_source src)
|
170 |
+ |
|
|
|
|
171 |
+ |
make_timeout(Timer& t, std::stop_source src)
|
| 175 |
|
|
172 |
|
|
| 176 |
|
auto [ec] = co_await t.wait();
|
173 |
|
auto [ec] = co_await t.wait();
|
| 177 |
|
|
174 |
|
|
| 178 |
|
|
175 |
|
|
| 179 |
|
|
176 |
|
|
| 180 |
|
|
177 |
|
|
| 181 |
|
} // namespace boost::corosio::detail
|
178 |
|
} // namespace boost::corosio::detail
|
| 182 |
|
|
179 |
|
|
| 183 |
|
|
180 |
|
|