| 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_NATIVE_DETAIL_SELECT_SELECT_SCHEDULER_HPP
|
10 |
|
#ifndef BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SCHEDULER_HPP
|
| 11 |
|
#define BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SCHEDULER_HPP
|
11 |
|
#define BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SCHEDULER_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/corosio/detail/platform.hpp>
|
13 |
|
#include <boost/corosio/detail/platform.hpp>
|
| 14 |
|
|
14 |
|
|
| 15 |
|
#if BOOST_COROSIO_HAS_SELECT
|
15 |
|
#if BOOST_COROSIO_HAS_SELECT
|
| 16 |
|
|
16 |
|
|
| 17 |
|
#include <boost/corosio/detail/config.hpp>
|
17 |
|
#include <boost/corosio/detail/config.hpp>
|
| 18 |
|
#include <boost/capy/ex/execution_context.hpp>
|
18 |
|
#include <boost/capy/ex/execution_context.hpp>
|
| 19 |
|
|
19 |
|
|
| 20 |
|
#include <boost/corosio/native/detail/reactor/reactor_scheduler.hpp>
|
20 |
|
#include <boost/corosio/native/detail/reactor/reactor_scheduler.hpp>
|
| 21 |
|
|
21 |
|
|
| 22 |
|
#include <boost/corosio/native/detail/select/select_op.hpp>
|
22 |
|
#include <boost/corosio/native/detail/select/select_op.hpp>
|
| 23 |
|
#include <boost/corosio/detail/timer_service.hpp>
|
23 |
|
#include <boost/corosio/detail/timer_service.hpp>
|
| 24 |
|
#include <boost/corosio/native/detail/make_err.hpp>
|
24 |
|
#include <boost/corosio/native/detail/make_err.hpp>
|
| 25 |
|
#include <boost/corosio/native/detail/posix/posix_resolver_service.hpp>
|
25 |
|
#include <boost/corosio/native/detail/posix/posix_resolver_service.hpp>
|
| 26 |
|
#include <boost/corosio/native/detail/posix/posix_signal_service.hpp>
|
26 |
|
#include <boost/corosio/native/detail/posix/posix_signal_service.hpp>
|
| 27 |
|
|
27 |
|
|
| 28 |
|
#include <boost/corosio/detail/except.hpp>
|
28 |
|
#include <boost/corosio/detail/except.hpp>
|
| 29 |
|
|
29 |
|
|
| 30 |
|
|
30 |
|
|
| 31 |
|
|
31 |
|
|
| 32 |
|
|
32 |
|
|
| 33 |
|
|
33 |
|
|
| 34 |
|
|
34 |
|
|
| 35 |
|
|
35 |
|
|
| 36 |
|
|
36 |
|
|
| 37 |
|
|
37 |
|
|
| 38 |
|
|
38 |
|
|
| 39 |
|
|
39 |
|
|
| 40 |
|
|
40 |
|
|
| 41 |
|
|
41 |
|
|
| 42 |
|
namespace boost::corosio::detail {
|
42 |
|
namespace boost::corosio::detail {
|
| 43 |
|
|
43 |
|
|
| 44 |
|
|
44 |
|
|
| 45 |
|
struct select_descriptor_state;
|
45 |
|
struct select_descriptor_state;
|
| 46 |
|
|
46 |
|
|
| 47 |
|
/** POSIX scheduler using select() for I/O multiplexing.
|
47 |
|
/** POSIX scheduler using select() for I/O multiplexing.
|
| 48 |
|
|
48 |
|
|
| 49 |
|
This scheduler implements the scheduler interface using the POSIX select()
|
49 |
|
This scheduler implements the scheduler interface using the POSIX select()
|
| 50 |
|
call for I/O event notification. It inherits the shared reactor threading
|
50 |
|
call for I/O event notification. It inherits the shared reactor threading
|
| 51 |
|
model from reactor_scheduler_base: signal state machine, inline completion
|
51 |
|
model from reactor_scheduler_base: signal state machine, inline completion
|
| 52 |
|
budget, work counting, and the do_one event loop.
|
52 |
|
budget, work counting, and the do_one event loop.
|
| 53 |
|
|
53 |
|
|
| 54 |
|
The design mirrors epoll_scheduler for behavioral consistency:
|
54 |
|
The design mirrors epoll_scheduler for behavioral consistency:
|
| 55 |
|
- Same single-reactor thread coordination model
|
55 |
|
- Same single-reactor thread coordination model
|
| 56 |
|
- Same deferred I/O pattern (reactor marks ready; workers do I/O)
|
56 |
|
- Same deferred I/O pattern (reactor marks ready; workers do I/O)
|
| 57 |
|
- Same timer integration pattern
|
57 |
|
- Same timer integration pattern
|
| 58 |
|
|
58 |
|
|
| 59 |
|
|
59 |
|
|
| 60 |
|
- FD_SETSIZE (~1024) limits maximum concurrent connections
|
60 |
|
- FD_SETSIZE (~1024) limits maximum concurrent connections
|
| 61 |
|
- O(n) scanning: rebuilds fd_sets each iteration
|
61 |
|
- O(n) scanning: rebuilds fd_sets each iteration
|
| 62 |
|
- Level-triggered only (no edge-triggered mode)
|
62 |
|
- Level-triggered only (no edge-triggered mode)
|
| 63 |
|
|
63 |
|
|
| 64 |
|
|
64 |
|
|
| 65 |
|
All public member functions are thread-safe.
|
65 |
|
All public member functions are thread-safe.
|
| 66 |
|
|
66 |
|
|
| 67 |
|
class BOOST_COROSIO_DECL select_scheduler final : public reactor_scheduler_base
|
67 |
|
class BOOST_COROSIO_DECL select_scheduler final : public reactor_scheduler_base
|
| 68 |
|
|
68 |
|
|
| 69 |
|
|
69 |
|
|
| 70 |
|
/** Construct the scheduler.
|
70 |
|
/** Construct the scheduler.
|
| 71 |
|
|
71 |
|
|
| 72 |
|
Creates a self-pipe for reactor interruption.
|
72 |
|
Creates a self-pipe for reactor interruption.
|
| 73 |
|
|
73 |
|
|
| 74 |
|
@param ctx Reference to the owning execution_context.
|
74 |
|
@param ctx Reference to the owning execution_context.
|
| 75 |
|
@param concurrency_hint Hint for expected thread count (unused).
|
75 |
|
@param concurrency_hint Hint for expected thread count (unused).
|
| 76 |
|
|
76 |
|
|
| 77 |
|
select_scheduler(capy::execution_context& ctx, int concurrency_hint = -1);
|
77 |
|
select_scheduler(capy::execution_context& ctx, int concurrency_hint = -1);
|
| 78 |
|
|
78 |
|
|
| 79 |
|
/// Destroy the scheduler.
|
79 |
|
/// Destroy the scheduler.
|
| 80 |
|
~select_scheduler() override;
|
80 |
|
~select_scheduler() override;
|
| 81 |
|
|
81 |
|
|
| 82 |
|
select_scheduler(select_scheduler const&) = delete;
|
82 |
|
select_scheduler(select_scheduler const&) = delete;
|
| 83 |
|
select_scheduler& operator=(select_scheduler const&) = delete;
|
83 |
|
select_scheduler& operator=(select_scheduler const&) = delete;
|
| 84 |
|
|
84 |
|
|
| 85 |
|
/// Shut down the scheduler, draining pending operations.
|
85 |
|
/// Shut down the scheduler, draining pending operations.
|
| 86 |
|
void shutdown() override;
|
86 |
|
void shutdown() override;
|
| 87 |
|
|
87 |
|
|
| 88 |
|
/** Return the maximum file descriptor value supported.
|
88 |
|
/** Return the maximum file descriptor value supported.
|
| 89 |
|
|
89 |
|
|
| 90 |
|
Returns FD_SETSIZE - 1, the maximum fd value that can be
|
90 |
|
Returns FD_SETSIZE - 1, the maximum fd value that can be
|
| 91 |
|
monitored by select(). Operations with fd >= FD_SETSIZE
|
91 |
|
monitored by select(). Operations with fd >= FD_SETSIZE
|
| 92 |
|
|
92 |
|
|
| 93 |
|
|
93 |
|
|
| 94 |
|
@return The maximum supported file descriptor value.
|
94 |
|
@return The maximum supported file descriptor value.
|
| 95 |
|
|
95 |
|
|
| 96 |
|
static constexpr int max_fd() noexcept
|
96 |
|
static constexpr int max_fd() noexcept
|
| 97 |
|
|
97 |
|
|
| 98 |
|
|
98 |
|
|
| 99 |
|
|
99 |
|
|
| 100 |
|
|
100 |
|
|
| 101 |
|
/** Register a descriptor for persistent monitoring.
|
101 |
|
/** Register a descriptor for persistent monitoring.
|
| 102 |
|
|
102 |
|
|
| 103 |
|
The fd is added to the registered_descs_ map and will be
|
103 |
|
The fd is added to the registered_descs_ map and will be
|
| 104 |
|
included in subsequent select() calls. The reactor is
|
104 |
|
included in subsequent select() calls. The reactor is
|
| 105 |
|
interrupted so a blocked select() rebuilds its fd_sets.
|
105 |
|
interrupted so a blocked select() rebuilds its fd_sets.
|
| 106 |
|
|
106 |
|
|
| 107 |
|
@param fd The file descriptor to register.
|
107 |
|
@param fd The file descriptor to register.
|
| 108 |
|
@param desc Pointer to descriptor state for this fd.
|
108 |
|
@param desc Pointer to descriptor state for this fd.
|
| 109 |
|
|
109 |
|
|
| 110 |
|
void register_descriptor(int fd, select_descriptor_state* desc) const;
|
110 |
|
void register_descriptor(int fd, select_descriptor_state* desc) const;
|
| 111 |
|
|
111 |
|
|
| 112 |
|
/** Deregister a persistently registered descriptor.
|
112 |
|
/** Deregister a persistently registered descriptor.
|
| 113 |
|
|
113 |
|
|
| 114 |
|
@param fd The file descriptor to deregister.
|
114 |
|
@param fd The file descriptor to deregister.
|
| 115 |
|
|
115 |
|
|
| 116 |
|
void deregister_descriptor(int fd) const;
|
116 |
|
void deregister_descriptor(int fd) const;
|
| 117 |
|
|
117 |
|
|
| 118 |
|
/** Interrupt the reactor so it rebuilds its fd_sets.
|
118 |
|
/** Interrupt the reactor so it rebuilds its fd_sets.
|
| 119 |
|
|
119 |
|
|
| 120 |
|
Called when a write or connect op is registered after
|
120 |
|
Called when a write or connect op is registered after
|
| 121 |
|
the reactor's snapshot was taken. Without this, select()
|
121 |
|
the reactor's snapshot was taken. Without this, select()
|
| 122 |
|
may block not watching for writability on the fd.
|
122 |
|
may block not watching for writability on the fd.
|
| 123 |
|
|
123 |
|
|
| 124 |
|
void notify_reactor() const;
|
124 |
|
void notify_reactor() const;
|
| 125 |
|
|
125 |
|
|
| 126 |
|
|
126 |
|
|
| 127 |
|
|
127 |
|
|
| 128 |
|
run_task(std::unique_lock<std::mutex>& lock, context_type* ctx) override;
|
128 |
|
run_task(std::unique_lock<std::mutex>& lock, context_type* ctx) override;
|
| 129 |
|
void interrupt_reactor() const override;
|
129 |
|
void interrupt_reactor() const override;
|
| 130 |
|
long calculate_timeout(long requested_timeout_us) const;
|
130 |
|
long calculate_timeout(long requested_timeout_us) const;
|
| 131 |
|
|
131 |
|
|
| 132 |
|
// Self-pipe for interrupting select()
|
132 |
|
// Self-pipe for interrupting select()
|
| 133 |
|
int pipe_fds_[2]; // [0]=read, [1]=write
|
133 |
|
int pipe_fds_[2]; // [0]=read, [1]=write
|
| 134 |
|
|
134 |
|
|
| 135 |
|
// Per-fd tracking for fd_set building
|
135 |
|
// Per-fd tracking for fd_set building
|
| 136 |
|
mutable std::unordered_map<int, select_descriptor_state*> registered_descs_;
|
136 |
|
mutable std::unordered_map<int, select_descriptor_state*> registered_descs_;
|
| 137 |
|
mutable int max_fd_ = -1;
|
137 |
|
mutable int max_fd_ = -1;
|
| 138 |
|
|
138 |
|
|
| 139 |
|
|
139 |
|
|
| 140 |
|
inline select_scheduler::select_scheduler(capy::execution_context& ctx, int)
|
140 |
|
inline select_scheduler::select_scheduler(capy::execution_context& ctx, int)
|
| 141 |
|
|
141 |
|
|
| 142 |
|
|
142 |
|
|
| 143 |
|
|
143 |
|
|
| 144 |
|
if (::pipe(pipe_fds_) < 0)
|
144 |
|
if (::pipe(pipe_fds_) < 0)
|
| 145 |
|
detail::throw_system_error(make_err(errno), "pipe");
|
145 |
|
detail::throw_system_error(make_err(errno), "pipe");
|
| 146 |
|
|
146 |
|
|
| 147 |
|
for (int i = 0; i < 2; ++i)
|
147 |
|
for (int i = 0; i < 2; ++i)
|
| 148 |
|
|
148 |
|
|
| 149 |
|
int flags = ::fcntl(pipe_fds_[i], F_GETFL, 0);
|
149 |
|
int flags = ::fcntl(pipe_fds_[i], F_GETFL, 0);
|
| 150 |
|
|
150 |
|
|
| 151 |
|
|
151 |
|
|
| 152 |
|
|
152 |
|
|
| 153 |
|
|
153 |
|
|
| 154 |
|
|
154 |
|
|
| 155 |
|
detail::throw_system_error(make_err(errn), "fcntl F_GETFL");
|
155 |
|
detail::throw_system_error(make_err(errn), "fcntl F_GETFL");
|
| 156 |
|
|
156 |
|
|
| 157 |
|
if (::fcntl(pipe_fds_[i], F_SETFL, flags | O_NONBLOCK) == -1)
|
157 |
|
if (::fcntl(pipe_fds_[i], F_SETFL, flags | O_NONBLOCK) == -1)
|
| 158 |
|
|
158 |
|
|
| 159 |
|
|
159 |
|
|
| 160 |
|
|
160 |
|
|
| 161 |
|
|
161 |
|
|
| 162 |
|
detail::throw_system_error(make_err(errn), "fcntl F_SETFL");
|
162 |
|
detail::throw_system_error(make_err(errn), "fcntl F_SETFL");
|
| 163 |
|
|
163 |
|
|
| 164 |
|
if (::fcntl(pipe_fds_[i], F_SETFD, FD_CLOEXEC) == -1)
|
164 |
|
if (::fcntl(pipe_fds_[i], F_SETFD, FD_CLOEXEC) == -1)
|
| 165 |
|
|
165 |
|
|
| 166 |
|
|
166 |
|
|
| 167 |
|
|
167 |
|
|
| 168 |
|
|
168 |
|
|
| 169 |
|
detail::throw_system_error(make_err(errn), "fcntl F_SETFD");
|
169 |
|
detail::throw_system_error(make_err(errn), "fcntl F_SETFD");
|
| 170 |
|
|
170 |
|
|
| 171 |
|
|
171 |
|
|
| 172 |
|
|
172 |
|
|
| 173 |
|
timer_svc_ = &get_timer_service(ctx, *this);
|
173 |
|
timer_svc_ = &get_timer_service(ctx, *this);
|
| 174 |
|
timer_svc_->set_on_earliest_changed(
|
174 |
|
timer_svc_->set_on_earliest_changed(
|
| 175 |
|
timer_service::callback(this, [](void* p) {
|
175 |
|
timer_service::callback(this, [](void* p) {
|
| 176 |
|
static_cast<select_scheduler*>(p)->interrupt_reactor();
|
176 |
|
static_cast<select_scheduler*>(p)->interrupt_reactor();
|
| 177 |
|
|
177 |
|
|
| 178 |
|
|
178 |
|
|
| 179 |
|
get_resolver_service(ctx, *this);
|
179 |
|
get_resolver_service(ctx, *this);
|
| 180 |
|
get_signal_service(ctx, *this);
|
180 |
|
get_signal_service(ctx, *this);
|
| 181 |
|
|
181 |
|
|
| 182 |
|
completed_ops_.push(&task_op_);
|
182 |
|
completed_ops_.push(&task_op_);
|
| 183 |
|
|
183 |
|
|
| 184 |
|
|
184 |
|
|
| 185 |
|
inline select_scheduler::~select_scheduler()
|
185 |
|
inline select_scheduler::~select_scheduler()
|
| 186 |
|
|
186 |
|
|
| 187 |
|
|
187 |
|
|
| 188 |
|
|
188 |
|
|
| 189 |
|
|
189 |
|
|
| 190 |
|
|
190 |
|
|
| 191 |
|
|
191 |
|
|
| 192 |
|
|
192 |
|
|
| 193 |
|
|
193 |
|
|
| 194 |
|
select_scheduler::shutdown()
|
194 |
|
select_scheduler::shutdown()
|
| 195 |
|
|
195 |
|
|
| 196 |
|
|
196 |
|
|
| 197 |
|
|
197 |
|
|
| 198 |
|
|
198 |
|
|
| 199 |
|
|
199 |
|
|
| 200 |
|
|
200 |
|
|
| 201 |
|
|
201 |
|
|
| 202 |
|
|
202 |
|
|
| 203 |
|
select_scheduler::register_descriptor(
|
203 |
|
select_scheduler::register_descriptor(
|
| 204 |
|
int fd, select_descriptor_state* desc) const
|
204 |
|
int fd, select_descriptor_state* desc) const
|
| 205 |
|
|
205 |
|
|
| 206 |
|
if (fd < 0 || fd >= FD_SETSIZE)
|
206 |
|
if (fd < 0 || fd >= FD_SETSIZE)
|
| 207 |
|
detail::throw_system_error(make_err(EINVAL), "select: fd out of range");
|
207 |
|
detail::throw_system_error(make_err(EINVAL), "select: fd out of range");
|
| 208 |
|
|
208 |
|
|
| 209 |
|
desc->registered_events = reactor_event_read | reactor_event_write;
|
209 |
|
desc->registered_events = reactor_event_read | reactor_event_write;
|
| 210 |
|
|
210 |
|
|
| 211 |
|
|
211 |
|
|
| 212 |
|
desc->ready_events_.store(0, std::memory_order_relaxed);
|
212 |
|
desc->ready_events_.store(0, std::memory_order_relaxed);
|
| 213 |
|
|
213 |
|
|
| 214 |
|
|
214 |
|
|
| 215 |
|
std::lock_guard lock(desc->mutex);
|
215 |
|
std::lock_guard lock(desc->mutex);
|
| 216 |
|
|
216 |
|
|
| 217 |
|
desc->read_ready = false;
|
217 |
|
desc->read_ready = false;
|
| 218 |
|
desc->write_ready = false;
|
218 |
|
desc->write_ready = false;
|
| 219 |
|
|
219 |
|
|
| 220 |
|
|
220 |
|
|
| 221 |
|
|
221 |
|
|
| 222 |
|
std::lock_guard lock(mutex_);
|
222 |
|
std::lock_guard lock(mutex_);
|
| 223 |
|
registered_descs_[fd] = desc;
|
223 |
|
registered_descs_[fd] = desc;
|
| 224 |
|
|
224 |
|
|
| 225 |
|
|
225 |
|
|
| 226 |
|
|
226 |
|
|
| 227 |
|
|
227 |
|
|
| 228 |
|
|
228 |
|
|
| 229 |
|
|
229 |
|
|
| 230 |
|
|
230 |
|
|
| 231 |
|
|
231 |
|
|
| 232 |
|
select_scheduler::deregister_descriptor(int fd) const
|
232 |
|
select_scheduler::deregister_descriptor(int fd) const
|
| 233 |
|
|
233 |
|
|
| 234 |
|
std::lock_guard lock(mutex_);
|
234 |
|
std::lock_guard lock(mutex_);
|
| 235 |
|
|
235 |
|
|
| 236 |
|
auto it = registered_descs_.find(fd);
|
236 |
|
auto it = registered_descs_.find(fd);
|
| 237 |
|
if (it == registered_descs_.end())
|
237 |
|
if (it == registered_descs_.end())
|
| 238 |
|
|
238 |
|
|
| 239 |
|
|
239 |
|
|
| 240 |
|
registered_descs_.erase(it);
|
240 |
|
registered_descs_.erase(it);
|
| 241 |
|
|
241 |
|
|
| 242 |
|
|
242 |
|
|
| 243 |
|
|
243 |
|
|
| 244 |
|
|
244 |
|
|
| 245 |
|
for (auto& [registered_fd, state] : registered_descs_)
|
245 |
|
for (auto& [registered_fd, state] : registered_descs_)
|
| 246 |
|
|
246 |
|
|
| 247 |
|
if (registered_fd > max_fd_)
|
247 |
|
if (registered_fd > max_fd_)
|
| 248 |
|
|
248 |
|
|
| 249 |
|
|
249 |
|
|
| 250 |
|
|
250 |
|
|
| 251 |
|
|
251 |
|
|
| 252 |
|
|
252 |
|
|
| 253 |
|
|
253 |
|
|
| 254 |
|
select_scheduler::notify_reactor() const
|
254 |
|
select_scheduler::notify_reactor() const
|
| 255 |
|
|
255 |
|
|
| 256 |
|
|
256 |
|
|
| 257 |
|
|
257 |
|
|
| 258 |
|
|
258 |
|
|
| 259 |
|
|
259 |
|
|
| 260 |
|
select_scheduler::interrupt_reactor() const
|
260 |
|
select_scheduler::interrupt_reactor() const
|
| 261 |
|
|
261 |
|
|
| 262 |
|
|
262 |
|
|
| 263 |
|
[[maybe_unused]] auto r = ::write(pipe_fds_[1], &byte, 1);
|
263 |
|
[[maybe_unused]] auto r = ::write(pipe_fds_[1], &byte, 1);
|
| 264 |
|
|
264 |
|
|
| 265 |
|
|
265 |
|
|
| 266 |
|
|
266 |
|
|
| 267 |
|
select_scheduler::calculate_timeout(long requested_timeout_us) const
|
267 |
|
select_scheduler::calculate_timeout(long requested_timeout_us) const
|
| 268 |
|
|
268 |
|
|
| 269 |
|
if (requested_timeout_us == 0)
|
269 |
|
if (requested_timeout_us == 0)
|
| 270 |
|
|
270 |
|
|
| 271 |
|
|
271 |
|
|
| 272 |
|
auto nearest = timer_svc_->nearest_expiry();
|
272 |
|
auto nearest = timer_svc_->nearest_expiry();
|
| 273 |
|
if (nearest == timer_service::time_point::max())
|
273 |
|
if (nearest == timer_service::time_point::max())
|
| 274 |
|
return requested_timeout_us;
|
274 |
|
return requested_timeout_us;
|
| 275 |
|
|
275 |
|
|
| 276 |
|
auto now = std::chrono::steady_clock::now();
|
276 |
|
auto now = std::chrono::steady_clock::now();
|
| 277 |
|
|
277 |
|
|
| 278 |
|
|
278 |
|
|
| 279 |
|
|
279 |
|
|
| 280 |
|
|
280 |
|
|
| 281 |
|
std::chrono::duration_cast<std::chrono::microseconds>(nearest - now)
|
281 |
|
std::chrono::duration_cast<std::chrono::microseconds>(nearest - now)
|
| 282 |
|
|
282 |
|
|
| 283 |
|
|
283 |
|
|
| 284 |
|
constexpr auto long_max =
|
284 |
|
constexpr auto long_max =
|
| 285 |
|
static_cast<long long>((std::numeric_limits<long>::max)());
|
285 |
|
static_cast<long long>((std::numeric_limits<long>::max)());
|
| 286 |
|
|
286 |
|
|
| 287 |
|
(std::min)((std::max)(static_cast<long long>(timer_timeout_us),
|
287 |
|
(std::min)((std::max)(static_cast<long long>(timer_timeout_us),
|
| 288 |
|
static_cast<long long>(0)),
|
288 |
|
static_cast<long long>(0)),
|
| 289 |
|
|
289 |
|
|
| 290 |
|
|
290 |
|
|
| 291 |
|
if (requested_timeout_us < 0)
|
291 |
|
if (requested_timeout_us < 0)
|
| 292 |
|
return static_cast<long>(capped_timer_us);
|
292 |
|
return static_cast<long>(capped_timer_us);
|
| 293 |
|
|
293 |
|
|
| 294 |
|
return static_cast<long>(
|
294 |
|
return static_cast<long>(
|
| 295 |
|
(std::min)(static_cast<long long>(requested_timeout_us),
|
295 |
|
(std::min)(static_cast<long long>(requested_timeout_us),
|
| 296 |
|
|
296 |
|
|
| 297 |
|
|
297 |
|
|
| 298 |
|
|
298 |
|
|
| 299 |
|
|
299 |
|
|
| 300 |
|
select_scheduler::run_task(
|
300 |
|
select_scheduler::run_task(
|
| 301 |
|
std::unique_lock<std::mutex>& lock, context_type* ctx)
|
301 |
|
std::unique_lock<std::mutex>& lock, context_type* ctx)
|
| 302 |
|
|
302 |
|
|
| 303 |
|
long effective_timeout_us = task_interrupted_ ? 0 : calculate_timeout(-1);
|
303 |
|
long effective_timeout_us = task_interrupted_ ? 0 : calculate_timeout(-1);
|
| 304 |
|
|
304 |
|
|
| 305 |
|
// Snapshot registered descriptors while holding lock.
|
305 |
|
// Snapshot registered descriptors while holding lock.
|
| 306 |
|
// Record which fds need write monitoring to avoid a hot loop:
|
306 |
|
// Record which fds need write monitoring to avoid a hot loop:
|
| 307 |
|
// select is level-triggered so writable sockets (nearly always
|
307 |
|
// select is level-triggered so writable sockets (nearly always
|
| 308 |
|
// writable) would cause select() to return immediately every
|
308 |
|
// writable) would cause select() to return immediately every
|
| 309 |
|
// iteration if unconditionally added to write_fds.
|
309 |
|
// iteration if unconditionally added to write_fds.
|
| 310 |
|
|
310 |
|
|
| 311 |
|
|
311 |
|
|
| 312 |
|
|
312 |
|
|
| 313 |
|
select_descriptor_state* desc;
|
313 |
|
select_descriptor_state* desc;
|
| 314 |
|
|
314 |
|
|
| 315 |
|
|
315 |
|
|
| 316 |
|
fd_entry snapshot[FD_SETSIZE];
|
316 |
|
fd_entry snapshot[FD_SETSIZE];
|
| 317 |
|
|
317 |
|
|
| 318 |
|
|
318 |
|
|
| 319 |
|
for (auto& [fd, desc] : registered_descs_)
|
319 |
|
for (auto& [fd, desc] : registered_descs_)
|
| 320 |
|
|
320 |
|
|
| 321 |
|
if (snapshot_count < FD_SETSIZE)
|
321 |
|
if (snapshot_count < FD_SETSIZE)
|
| 322 |
|
|
322 |
|
|
| 323 |
|
std::lock_guard desc_lock(desc->mutex);
|
323 |
|
std::lock_guard desc_lock(desc->mutex);
|
| 324 |
|
snapshot[snapshot_count].fd = fd;
|
324 |
|
snapshot[snapshot_count].fd = fd;
|
| 325 |
|
snapshot[snapshot_count].desc = desc;
|
325 |
|
snapshot[snapshot_count].desc = desc;
|
| 326 |
|
snapshot[snapshot_count].needs_write =
|
326 |
|
snapshot[snapshot_count].needs_write =
|
| 327 |
|
(desc->write_op || desc->connect_op);
|
327 |
|
(desc->write_op || desc->connect_op);
|
| 328 |
|
|
328 |
|
|
| 329 |
|
|
329 |
|
|
| 330 |
|
|
330 |
|
|
| 331 |
|
|
331 |
|
|
| 332 |
|
|
332 |
|
|
| 333 |
|
|
333 |
|
|
| 334 |
|
|
334 |
|
|
| 335 |
|
task_cleanup on_exit{this, &lock, ctx};
|
335 |
|
task_cleanup on_exit{this, &lock, ctx};
|
| 336 |
|
|
336 |
|
|
| 337 |
|
fd_set read_fds, write_fds, except_fds;
|
337 |
|
fd_set read_fds, write_fds, except_fds;
|
| 338 |
|
|
338 |
|
|
| 339 |
|
|
339 |
|
|
| 340 |
|
|
340 |
|
|
| 341 |
|
|
341 |
|
|
| 342 |
|
FD_SET(pipe_fds_[0], &read_fds);
|
342 |
|
FD_SET(pipe_fds_[0], &read_fds);
|
| 343 |
|
|
343 |
|
|
| 344 |
|
|
344 |
|
|
| 345 |
|
for (int i = 0; i < snapshot_count; ++i)
|
345 |
|
for (int i = 0; i < snapshot_count; ++i)
|
| 346 |
|
|
346 |
|
|
| 347 |
|
|
347 |
|
|
| 348 |
|
|
348 |
|
|
| 349 |
|
if (snapshot[i].needs_write)
|
349 |
|
if (snapshot[i].needs_write)
|
| 350 |
|
|
350 |
|
|
| 351 |
|
|
351 |
|
|
| 352 |
|
|
352 |
|
|
| 353 |
|
|
353 |
|
|
| 354 |
|
|
354 |
|
|
| 355 |
|
|
355 |
|
|
| 356 |
|
|
356 |
|
|
| 357 |
|
struct timeval* tv_ptr = nullptr;
|
357 |
|
struct timeval* tv_ptr = nullptr;
|
| 358 |
|
if (effective_timeout_us >= 0)
|
358 |
|
if (effective_timeout_us >= 0)
|
| 359 |
|
|
359 |
|
|
| 360 |
|
tv.tv_sec = effective_timeout_us / 1000000;
|
360 |
|
tv.tv_sec = effective_timeout_us / 1000000;
|
| 361 |
|
tv.tv_usec = effective_timeout_us % 1000000;
|
361 |
|
tv.tv_usec = effective_timeout_us % 1000000;
|
| 362 |
|
|
362 |
|
|
| 363 |
|
|
363 |
|
|
| 364 |
|
|
364 |
|
|
| 365 |
|
int ready = ::select(nfds + 1, &read_fds, &write_fds, &except_fds, tv_ptr);
|
365 |
|
int ready = ::select(nfds + 1, &read_fds, &write_fds, &except_fds, tv_ptr);
|
| 366 |
|
|
366 |
|
|
| 367 |
|
// EINTR: signal interrupted select(), just retry.
|
367 |
|
// EINTR: signal interrupted select(), just retry.
|
| 368 |
|
// EBADF: an fd was closed between snapshot and select(); retry
|
368 |
|
// EBADF: an fd was closed between snapshot and select(); retry
|
| 369 |
|
// with a fresh snapshot from registered_descs_.
|
369 |
|
// with a fresh snapshot from registered_descs_.
|
| 370 |
|
|
370 |
|
|
| 371 |
|
|
371 |
|
|
| 372 |
|
if (errno == EINTR || errno == EBADF)
|
372 |
|
if (errno == EINTR || errno == EBADF)
|
| 373 |
|
|
373 |
|
|
| 374 |
|
detail::throw_system_error(make_err(errno), "select");
|
374 |
|
detail::throw_system_error(make_err(errno), "select");
|
| 375 |
|
|
375 |
|
|
| 376 |
|
|
376 |
|
|
| 377 |
|
// Process timers outside the lock
|
377 |
|
// Process timers outside the lock
|
| 378 |
|
timer_svc_->process_expired();
|
378 |
|
timer_svc_->process_expired();
|
| 379 |
|
|
379 |
|
|
| 380 |
|
|
380 |
|
|
| 381 |
|
|
381 |
|
|
| 382 |
|
|
382 |
|
|
| 383 |
|
|
383 |
|
|
| 384 |
|
if (FD_ISSET(pipe_fds_[0], &read_fds))
|
384 |
|
if (FD_ISSET(pipe_fds_[0], &read_fds))
|
| 385 |
|
|
385 |
|
|
| 386 |
|
|
386 |
|
|
| 387 |
|
while (::read(pipe_fds_[0], buf, sizeof(buf)) > 0)
|
387 |
|
while (::read(pipe_fds_[0], buf, sizeof(buf)) > 0)
|
| 388 |
|
|
388 |
|
|
| 389 |
|
|
389 |
|
|
| 390 |
|
|
390 |
|
|
| 391 |
|
|
391 |
|
|
| 392 |
|
for (int i = 0; i < snapshot_count; ++i)
|
392 |
|
for (int i = 0; i < snapshot_count; ++i)
|
| 393 |
|
|
393 |
|
|
| 394 |
|
|
394 |
|
|
| 395 |
|
select_descriptor_state* desc = snapshot[i].desc;
|
395 |
|
select_descriptor_state* desc = snapshot[i].desc;
|
| 396 |
|
|
396 |
|
|
| 397 |
|
|
397 |
|
|
| 398 |
|
if (FD_ISSET(fd, &read_fds))
|
398 |
|
if (FD_ISSET(fd, &read_fds))
|
| 399 |
|
flags |= reactor_event_read;
|
399 |
|
flags |= reactor_event_read;
|
| 400 |
|
if (FD_ISSET(fd, &write_fds))
|
400 |
|
if (FD_ISSET(fd, &write_fds))
|
| 401 |
|
flags |= reactor_event_write;
|
401 |
|
flags |= reactor_event_write;
|
| 402 |
|
if (FD_ISSET(fd, &except_fds))
|
402 |
|
if (FD_ISSET(fd, &except_fds))
|
| 403 |
|
flags |= reactor_event_error;
|
403 |
|
flags |= reactor_event_error;
|
| 404 |
|
|
404 |
|
|
| 405 |
|
|
405 |
|
|
| 406 |
|
|
406 |
|
|
| 407 |
|
|
407 |
|
|
| 408 |
|
desc->add_ready_events(flags);
|
408 |
|
desc->add_ready_events(flags);
|
| 409 |
|
|
409 |
|
|
| 410 |
|
|
410 |
|
|
| 411 |
|
if (desc->is_enqueued_.compare_exchange_strong(
|
411 |
|
if (desc->is_enqueued_.compare_exchange_strong(
|
| 412 |
|
expected, true, std::memory_order_release,
|
412 |
|
expected, true, std::memory_order_release,
|
| 413 |
|
std::memory_order_relaxed))
|
413 |
|
std::memory_order_relaxed))
|
| 414 |
|
|
414 |
|
|
| 415 |
|
|
415 |
|
|
| 416 |
|
|
416 |
|
|
| 417 |
|
|
417 |
|
|
| 418 |
|
|
418 |
|
|
| 419 |
|
|
419 |
|
|
| 420 |
|
|
420 |
|
|
| 421 |
|
|
421 |
|
|
| 422 |
|
|
422 |
|
|
| 423 |
|
completed_ops_.splice(local_ops);
|
423 |
|
completed_ops_.splice(local_ops);
|
| 424 |
|
|
424 |
|
|
| 425 |
|
|
425 |
|
|
| 426 |
|
} // namespace boost::corosio::detail
|
426 |
|
} // namespace boost::corosio::detail
|
| 427 |
|
|
427 |
|
|
| 428 |
|
#endif // BOOST_COROSIO_HAS_SELECT
|
428 |
|
#endif // BOOST_COROSIO_HAS_SELECT
|
| 429 |
|
|
429 |
|
|
| 430 |
|
#endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SCHEDULER_HPP
|
430 |
|
#endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SCHEDULER_HPP
|