| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
| 3 |
|
// Copyright (c) 2026 Steve Gerbino
|
3 |
|
// Copyright (c) 2026 Steve Gerbino
|
| 4 |
|
|
4 |
|
|
| 5 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
5 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
| 6 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
6 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 7 |
|
|
7 |
|
|
| 8 |
|
// Official repository: https://github.com/cppalliance/corosio
|
8 |
|
// Official repository: https://github.com/cppalliance/corosio
|
| 9 |
|
|
9 |
|
|
| 10 |
|
|
10 |
|
|
| 11 |
|
#ifndef BOOST_COROSIO_IO_IO_TIMER_HPP
|
11 |
|
#ifndef BOOST_COROSIO_IO_IO_TIMER_HPP
|
| 12 |
|
#define BOOST_COROSIO_IO_IO_TIMER_HPP
|
12 |
|
#define BOOST_COROSIO_IO_IO_TIMER_HPP
|
| 13 |
|
|
13 |
|
|
| 14 |
|
#include <boost/corosio/detail/config.hpp>
|
14 |
|
#include <boost/corosio/detail/config.hpp>
|
| 15 |
|
#include <boost/corosio/io/io_object.hpp>
|
15 |
|
#include <boost/corosio/io/io_object.hpp>
|
| 16 |
|
#include <boost/capy/io_result.hpp>
|
16 |
|
#include <boost/capy/io_result.hpp>
|
| 17 |
|
#include <boost/capy/error.hpp>
|
17 |
|
#include <boost/capy/error.hpp>
|
| 18 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
18 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
| 19 |
|
#include <boost/capy/ex/io_env.hpp>
|
19 |
|
#include <boost/capy/ex/io_env.hpp>
|
| 20 |
|
|
20 |
|
|
| 21 |
|
|
21 |
|
|
| 22 |
|
|
22 |
|
|
| 23 |
|
|
23 |
|
|
| 24 |
|
|
24 |
|
|
| 25 |
|
|
25 |
|
|
| 26 |
|
|
26 |
|
|
| 27 |
|
|
27 |
|
|
| 28 |
|
namespace boost::corosio {
|
28 |
|
namespace boost::corosio {
|
| 29 |
|
|
29 |
|
|
| 30 |
|
/** Abstract base for asynchronous timers.
|
30 |
|
/** Abstract base for asynchronous timers.
|
| 31 |
|
|
31 |
|
|
| 32 |
|
Provides the common timer interface: `wait`, `cancel`, and
|
32 |
|
Provides the common timer interface: `wait`, `cancel`, and
|
| 33 |
|
`expiry`. Concrete classes like @ref timer add the ability
|
33 |
|
`expiry`. Concrete classes like @ref timer add the ability
|
| 34 |
|
to set expiry times and cancel individual waiters.
|
34 |
|
to set expiry times and cancel individual waiters.
|
| 35 |
|
|
35 |
|
|
| 36 |
|
|
36 |
|
|
| 37 |
|
|
37 |
|
|
| 38 |
|
|
38 |
|
|
| 39 |
|
|
39 |
|
|
| 40 |
|
|
40 |
|
|
| 41 |
|
|
41 |
|
|
| 42 |
|
class BOOST_COROSIO_DECL io_timer : public io_object
|
42 |
|
class BOOST_COROSIO_DECL io_timer : public io_object
|
| 43 |
|
|
43 |
|
|
| 44 |
|
|
44 |
|
|
| 45 |
|
|
45 |
|
|
| 46 |
|
|
46 |
|
|
| 47 |
|
|
47 |
|
|
| 48 |
|
mutable std::error_code ec_;
|
48 |
|
mutable std::error_code ec_;
|
| 49 |
|
|
49 |
|
|
| 50 |
|
explicit wait_awaitable(io_timer& t) noexcept : t_(t) {}
|
50 |
|
explicit wait_awaitable(io_timer& t) noexcept : t_(t) {}
|
| 51 |
|
|
51 |
|
|
| 52 |
|
bool await_ready() const noexcept
|
52 |
|
bool await_ready() const noexcept
|
| 53 |
|
|
53 |
|
|
| 54 |
|
return token_.stop_requested();
|
54 |
|
return token_.stop_requested();
|
| 55 |
|
|
55 |
|
|
| 56 |
|
|
56 |
|
|
| 57 |
|
capy::io_result<> await_resume() const noexcept
|
57 |
|
capy::io_result<> await_resume() const noexcept
|
| 58 |
|
|
58 |
|
|
| 59 |
|
if (token_.stop_requested())
|
59 |
|
if (token_.stop_requested())
|
| 60 |
|
return {capy::error::canceled};
|
60 |
|
return {capy::error::canceled};
|
| 61 |
|
|
61 |
|
|
| 62 |
|
|
62 |
|
|
| 63 |
|
|
63 |
|
|
| 64 |
|
auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
|
64 |
|
auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
|
| 65 |
|
-> std::coroutine_handle<>
|
65 |
|
-> std::coroutine_handle<>
|
| 66 |
|
|
66 |
|
|
| 67 |
|
token_ = env->stop_token;
|
67 |
|
token_ = env->stop_token;
|
| 68 |
|
|
68 |
|
|
| 69 |
|
// Inline fast path: already expired and not in the heap
|
69 |
|
// Inline fast path: already expired and not in the heap
|
| 70 |
|
if (impl.heap_index_ == implementation::npos &&
|
70 |
|
if (impl.heap_index_ == implementation::npos &&
|
| 71 |
|
(impl.expiry_ == (time_point::min)() ||
|
71 |
|
(impl.expiry_ == (time_point::min)() ||
|
| 72 |
|
impl.expiry_ <= clock_type::now()))
|
72 |
|
impl.expiry_ <= clock_type::now()))
|
| 73 |
|
|
73 |
|
|
| 74 |
|
|
74 |
|
|
| 75 |
|
|
75 |
|
|
| 76 |
|
|
76 |
|
|
| 77 |
|
return std::noop_coroutine();
|
77 |
|
return std::noop_coroutine();
|
| 78 |
|
|
78 |
|
|
| 79 |
|
return impl.wait(h, env->executor, std::move(token_), &ec_);
|
79 |
|
return impl.wait(h, env->executor, std::move(token_), &ec_);
|
| 80 |
|
|
80 |
|
|
| 81 |
|
|
81 |
|
|
| 82 |
|
|
82 |
|
|
| 83 |
|
|
83 |
|
|
| 84 |
|
struct implementation : io_object::implementation
|
84 |
|
struct implementation : io_object::implementation
|
| 85 |
|
|
85 |
|
|
| 86 |
|
static constexpr std::size_t npos =
|
86 |
|
static constexpr std::size_t npos =
|
| 87 |
|
(std::numeric_limits<std::size_t>::max)();
|
87 |
|
(std::numeric_limits<std::size_t>::max)();
|
| 88 |
|
|
88 |
|
|
| 89 |
|
std::chrono::steady_clock::time_point expiry_{};
|
89 |
|
std::chrono::steady_clock::time_point expiry_{};
|
| 90 |
|
std::size_t heap_index_ = npos;
|
90 |
|
std::size_t heap_index_ = npos;
|
| 91 |
|
bool might_have_pending_waits_ = false;
|
91 |
|
bool might_have_pending_waits_ = false;
|
| 92 |
|
|
92 |
|
|
| 93 |
|
virtual std::coroutine_handle<> wait(
|
93 |
|
virtual std::coroutine_handle<> wait(
|
| 94 |
|
|
94 |
|
|
| 95 |
|
|
95 |
|
|
| 96 |
|
|
96 |
|
|
| 97 |
|
|
97 |
|
|
| 98 |
|
|
98 |
|
|
| 99 |
|
|
99 |
|
|
| 100 |
|
/// The clock type used for time operations.
|
100 |
|
/// The clock type used for time operations.
|
| 101 |
|
using clock_type = std::chrono::steady_clock;
|
101 |
|
using clock_type = std::chrono::steady_clock;
|
| 102 |
|
|
102 |
|
|
| 103 |
|
/// The time point type for absolute expiry times.
|
103 |
|
/// The time point type for absolute expiry times.
|
| 104 |
|
using time_point = clock_type::time_point;
|
104 |
|
using time_point = clock_type::time_point;
|
| 105 |
|
|
105 |
|
|
| 106 |
|
/// The duration type for relative expiry times.
|
106 |
|
/// The duration type for relative expiry times.
|
| 107 |
|
using duration = clock_type::duration;
|
107 |
|
using duration = clock_type::duration;
|
| 108 |
|
|
108 |
|
|
| 109 |
|
/** Cancel all pending asynchronous wait operations.
|
109 |
|
/** Cancel all pending asynchronous wait operations.
|
| 110 |
|
|
110 |
|
|
| 111 |
|
All outstanding operations complete with an error code that
|
111 |
|
All outstanding operations complete with an error code that
|
| 112 |
|
compares equal to `capy::cond::canceled`.
|
112 |
|
compares equal to `capy::cond::canceled`.
|
| 113 |
|
|
113 |
|
|
| 114 |
|
@return The number of operations that were cancelled.
|
114 |
|
@return The number of operations that were cancelled.
|
| 115 |
|
|
115 |
|
|
| 116 |
|
|
116 |
|
|
| 117 |
|
|
117 |
|
|
| 118 |
|
if (!get().might_have_pending_waits_)
|
118 |
|
if (!get().might_have_pending_waits_)
|
| 119 |
|
|
119 |
|
|
| 120 |
|
|
120 |
|
|
| 121 |
|
|
121 |
|
|
| 122 |
|
|
122 |
|
|
| 123 |
|
/** Return the timer's expiry time as an absolute time.
|
123 |
|
/** Return the timer's expiry time as an absolute time.
|
| 124 |
|
|
124 |
|
|
| 125 |
|
@return The expiry time point. If no expiry has been set,
|
125 |
|
@return The expiry time point. If no expiry has been set,
|
| 126 |
|
returns a default-constructed time_point.
|
126 |
|
returns a default-constructed time_point.
|
| 127 |
|
|
127 |
|
|
| 128 |
|
time_point expiry() const noexcept
|
128 |
|
time_point expiry() const noexcept
|
| 129 |
|
|
129 |
|
|
| 130 |
|
|
130 |
|
|
| 131 |
|
|
131 |
|
|
| 132 |
|
|
132 |
|
|
| 133 |
|
/** Wait for the timer to expire.
|
133 |
|
/** Wait for the timer to expire.
|
| 134 |
|
|
134 |
|
|
| 135 |
|
Multiple coroutines may wait on the same timer concurrently.
|
135 |
|
Multiple coroutines may wait on the same timer concurrently.
|
| 136 |
|
When the timer expires, all waiters complete with success.
|
136 |
|
When the timer expires, all waiters complete with success.
|
| 137 |
|
|
137 |
|
|
| 138 |
|
The operation supports cancellation via `std::stop_token` through
|
138 |
|
The operation supports cancellation via `std::stop_token` through
|
| 139 |
|
the affine awaitable protocol. If the associated stop token is
|
139 |
|
the affine awaitable protocol. If the associated stop token is
|
| 140 |
|
triggered, only that waiter completes with an error that
|
140 |
|
triggered, only that waiter completes with an error that
|
| 141 |
|
compares equal to `capy::cond::canceled`; other waiters are
|
141 |
|
compares equal to `capy::cond::canceled`; other waiters are
|
| 142 |
|
|
142 |
|
|
| 143 |
|
|
143 |
|
|
| 144 |
|
@return An awaitable that completes with `io_result<>`.
|
144 |
|
@return An awaitable that completes with `io_result<>`.
|
| 145 |
|
|
145 |
|
|
| 146 |
|
|
146 |
|
|
| 147 |
|
|
147 |
|
|
| 148 |
|
return wait_awaitable(*this);
|
148 |
|
return wait_awaitable(*this);
|
| 149 |
|
|
149 |
|
|
| 150 |
|
|
150 |
|
|
| 151 |
|
|
151 |
|
|
| 152 |
|
/** Dispatch cancel to the concrete implementation.
|
152 |
|
/** Dispatch cancel to the concrete implementation.
|
| 153 |
|
|
153 |
|
|
| 154 |
|
@return The number of operations that were cancelled.
|
154 |
|
@return The number of operations that were cancelled.
|
| 155 |
|
|
155 |
|
|
| 156 |
|
virtual std::size_t do_cancel() = 0;
|
156 |
|
virtual std::size_t do_cancel() = 0;
|
| 157 |
|
|
157 |
|
|
| 158 |
|
explicit io_timer(handle h) noexcept : io_object(std::move(h)) {}
|
158 |
|
explicit io_timer(handle h) noexcept : io_object(std::move(h)) {}
|
| 159 |
|
|
159 |
|
|
| 160 |
|
|
160 |
|
|
| 161 |
|
io_timer(io_timer&& other) noexcept : io_object(std::move(other)) {}
|
161 |
|
io_timer(io_timer&& other) noexcept : io_object(std::move(other)) {}
|
| 162 |
|
|
162 |
|
|
| 163 |
|
|
163 |
|
|
| 164 |
|
io_timer& operator=(io_timer&& other) noexcept
|
164 |
|
io_timer& operator=(io_timer&& other) noexcept
|
| 165 |
|
|
165 |
|
|
| 166 |
|
|
166 |
|
|
| 167 |
|
h_ = std::move(other.h_);
|
167 |
|
h_ = std::move(other.h_);
|
| 168 |
|
|
168 |
|
|
| 169 |
|
|
169 |
|
|
| 170 |
|
|
170 |
|
|
| 171 |
|
io_timer(io_timer const&) = delete;
|
171 |
|
io_timer(io_timer const&) = delete;
|
| 172 |
|
io_timer& operator=(io_timer const&) = delete;
|
172 |
|
io_timer& operator=(io_timer const&) = delete;
|
| 173 |
|
|
173 |
|
|
| 174 |
|
/// Return the underlying implementation.
|
174 |
|
/// Return the underlying implementation.
|
| 175 |
|
implementation& get() const noexcept
|
175 |
|
implementation& get() const noexcept
|
| 176 |
|
|
176 |
|
|
| 177 |
|
return *static_cast<implementation*>(h_.get());
|
177 |
|
return *static_cast<implementation*>(h_.get());
|
| 178 |
|
|
178 |
|
|
| 179 |
|
|
179 |
|
|
| 180 |
|
|
180 |
|
|
| 181 |
|
} // namespace boost::corosio
|
181 |
|
} // namespace boost::corosio
|
| 182 |
|
|
182 |
|
|
| 183 |
|
|
183 |
|
|