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