include/boost/corosio/native/detail/select/select_scheduler.hpp
87.0% Lines (160/184)
100.0% List of functions (13/13)
51.1% Branches (94/184)
Functions (13)
Function
Calls
Lines
Branches
Blocks
boost::corosio::detail::select_scheduler::register_signal_reader(int)
:136
41x
100.0%
–
100.0%
boost::corosio::detail::select_scheduler::select_scheduler(boost::capy::execution_context&, int)
:160
1404x
50.0%
25.0%
100.0%
boost::corosio::detail::select_scheduler::select_scheduler(boost::capy::execution_context&, int)::'lambda'(void*)::__invoke(void*)
:195
2452x
100.0%
–
100.0%
boost::corosio::detail::select_scheduler::select_scheduler(boost::capy::execution_context&, int)::'lambda'(void*)::operator void (*)(void*)() const
:195
702x
100.0%
–
100.0%
boost::corosio::detail::select_scheduler::select_scheduler(boost::capy::execution_context&, int)::'lambda'(void*)::operator()(void*) const
:195
2452x
100.0%
50.0%
100.0%
boost::corosio::detail::select_scheduler::~select_scheduler()
:207
2106x
100.0%
50.0%
100.0%
boost::corosio::detail::select_scheduler::shutdown()
:216
702x
100.0%
50.0%
100.0%
boost::corosio::detail::select_scheduler::register_descriptor(int, boost::corosio::detail::reactor_descriptor_state*) const
:225
3908x
87.5%
62.5%
52.0%
boost::corosio::detail::select_scheduler::deregister_descriptor(int) const
:263
3867x
92.3%
59.1%
78.0%
boost::corosio::detail::select_scheduler::notify_reactor() const
:285
2198x
100.0%
–
100.0%
boost::corosio::detail::select_scheduler::interrupt_reactor() const
:291
9842x
100.0%
–
100.0%
boost::corosio::detail::select_scheduler::calculate_timeout(long) const
:298
1514890x
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)
:331
1537602x
94.3%
63.2%
82.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 <new> | |||
| 45 | #include <unordered_map> | |||
| 46 | ||||
| 47 | namespace boost::corosio::detail { | |||
| 48 | ||||
| 49 | struct select_op; | |||
| 50 | ||||
| 51 | /** POSIX scheduler using select() for I/O multiplexing. | |||
| 52 | ||||
| 53 | This scheduler implements the scheduler interface using the POSIX select() | |||
| 54 | call for I/O event notification. It inherits the shared reactor threading | |||
| 55 | model from reactor_scheduler: signal state machine, inline completion | |||
| 56 | budget, work counting, and the do_one event loop. | |||
| 57 | ||||
| 58 | The design mirrors epoll_scheduler for behavioral consistency: | |||
| 59 | - Same single-reactor thread coordination model | |||
| 60 | - Same deferred I/O pattern (reactor marks ready; workers do I/O) | |||
| 61 | - Same timer integration pattern | |||
| 62 | ||||
| 63 | Known Limitations: | |||
| 64 | - FD_SETSIZE (~1024) limits maximum concurrent connections | |||
| 65 | - O(n) scanning: rebuilds fd_sets each iteration | |||
| 66 | - Level-triggered only (no edge-triggered mode) | |||
| 67 | ||||
| 68 | @par Thread Safety | |||
| 69 | All public member functions are thread-safe. | |||
| 70 | */ | |||
| 71 | class BOOST_COROSIO_DECL select_scheduler final : public reactor_scheduler | |||
| 72 | { | |||
| 73 | public: | |||
| 74 | /** Construct the scheduler. | |||
| 75 | ||||
| 76 | Creates a self-pipe for reactor interruption. | |||
| 77 | ||||
| 78 | @param ctx Reference to the owning execution_context. | |||
| 79 | @param concurrency_hint Hint for expected thread count (unused). | |||
| 80 | */ | |||
| 81 | select_scheduler(capy::execution_context& ctx, int concurrency_hint = -1); | |||
| 82 | ||||
| 83 | /// Destroy the scheduler. | |||
| 84 | ~select_scheduler() override; | |||
| 85 | ||||
| 86 | select_scheduler(select_scheduler const&) = delete; | |||
| 87 | select_scheduler& operator=(select_scheduler const&) = delete; | |||
| 88 | ||||
| 89 | /// Shut down the scheduler, draining pending operations. | |||
| 90 | void shutdown() override; | |||
| 91 | ||||
| 92 | /** Return the maximum file descriptor value supported. | |||
| 93 | ||||
| 94 | Returns FD_SETSIZE - 1, the maximum fd value that can be | |||
| 95 | monitored by select(). Operations with fd >= FD_SETSIZE | |||
| 96 | will fail with EINVAL. | |||
| 97 | ||||
| 98 | @return The maximum supported file descriptor value. | |||
| 99 | */ | |||
| 100 | static constexpr int max_fd() noexcept | |||
| 101 | { | |||
| 102 | return FD_SETSIZE - 1; | |||
| 103 | } | |||
| 104 | ||||
| 105 | /** Register a descriptor for persistent monitoring. | |||
| 106 | ||||
| 107 | The fd is added to the registered_descs_ map and will be | |||
| 108 | included in subsequent select() calls. The reactor is | |||
| 109 | interrupted so a blocked select() rebuilds its fd_sets. | |||
| 110 | ||||
| 111 | @param fd The file descriptor to register. | |||
| 112 | @param desc Pointer to descriptor state for this fd. | |||
| 113 | ||||
| 114 | @return The error if the fd cannot be tracked, otherwise a | |||
| 115 | default constructed error code. | |||
| 116 | */ | |||
| 117 | std::error_code | |||
| 118 | register_descriptor(int fd, reactor_descriptor_state* desc) const; | |||
| 119 | ||||
| 120 | /** Deregister a persistently registered descriptor. | |||
| 121 | ||||
| 122 | @param fd The file descriptor to deregister. | |||
| 123 | */ | |||
| 124 | void deregister_descriptor(int fd) const; | |||
| 125 | ||||
| 126 | /** Interrupt the reactor so it rebuilds its fd_sets. | |||
| 127 | ||||
| 128 | Called when a write, connect, or write-wait op is registered | |||
| 129 | after the reactor's snapshot was taken. Without this, | |||
| 130 | select() may block not watching for writability on the fd. | |||
| 131 | */ | |||
| 132 | void notify_reactor() const; | |||
| 133 | ||||
| 134 | /// Watch the read end of the POSIX signal self-pipe (see scheduler.hpp). | |||
| 135 | [[nodiscard]] std::error_code | |||
| 136 | 41x | register_signal_reader(int read_fd) override | ||
| 137 | { | |||
| 138 | 41x | return register_descriptor(read_fd, signal_pipe_reader_.arm()); | ||
| 139 | } | |||
| 140 | ||||
| 141 | private: | |||
| 142 | void | |||
| 143 | run_task(lock_type& lock, context_type* ctx, | |||
| 144 | long timeout_us) override; | |||
| 145 | void interrupt_reactor() const override; | |||
| 146 | long calculate_timeout(long requested_timeout_us) const; | |||
| 147 | ||||
| 148 | // Watches the global signal self-pipe's read end (armed lazily by | |||
| 149 | // register_signal_reader on the first signal registration). | |||
| 150 | reactor_signal_pipe_reader signal_pipe_reader_; | |||
| 151 | ||||
| 152 | // Self-pipe for interrupting select() | |||
| 153 | int pipe_fds_[2]; // [0]=read, [1]=write | |||
| 154 | ||||
| 155 | // Per-fd tracking for fd_set building | |||
| 156 | mutable std::unordered_map<int, reactor_descriptor_state*> registered_descs_; | |||
| 157 | mutable int max_fd_ = -1; | |||
| 158 | }; | |||
| 159 | ||||
| 160 | 2808x | inline select_scheduler::select_scheduler(capy::execution_context& ctx, int) | ||
| 161 | 702x | : pipe_fds_{-1, -1} | ||
| 162 | 702x | , max_fd_(-1) | ||
| 163 | 1404x | { | ||
| 164 |
2/4✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 702 times.
|
702x | if (::pipe(pipe_fds_) < 0) | |
| 165 | ✗ | detail::throw_system_error(make_err(errno), "pipe"); | ||
| 166 | ||||
| 167 |
2/2✓ Branch 0 taken 702 times.
✓ Branch 1 taken 1404 times.
|
2106x | for (int i = 0; i < 2; ++i) | |
| 168 | { | |||
| 169 |
1/2✓ Branch 0 taken 1404 times.
✗ Branch 1 not taken.
|
1404x | int flags = ::fcntl(pipe_fds_[i], F_GETFL, 0); | |
| 170 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1404 times.
|
1404x | if (flags == -1) | |
| 171 | { | |||
| 172 | ✗ | int errn = errno; | ||
| 173 | ✗ | ::close(pipe_fds_[0]); | ||
| 174 | ✗ | ::close(pipe_fds_[1]); | ||
| 175 | ✗ | detail::throw_system_error(make_err(errn), "fcntl F_GETFL"); | ||
| 176 | } | |||
| 177 |
2/4✓ Branch 0 taken 1404 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1404 times.
|
1404x | if (::fcntl(pipe_fds_[i], F_SETFL, flags | O_NONBLOCK) == -1) | |
| 178 | { | |||
| 179 | ✗ | int errn = errno; | ||
| 180 | ✗ | ::close(pipe_fds_[0]); | ||
| 181 | ✗ | ::close(pipe_fds_[1]); | ||
| 182 | ✗ | detail::throw_system_error(make_err(errn), "fcntl F_SETFL"); | ||
| 183 | } | |||
| 184 |
2/4✓ Branch 0 taken 1404 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1404 times.
|
1404x | if (::fcntl(pipe_fds_[i], F_SETFD, FD_CLOEXEC) == -1) | |
| 185 | { | |||
| 186 | ✗ | int errn = errno; | ||
| 187 | ✗ | ::close(pipe_fds_[0]); | ||
| 188 | ✗ | ::close(pipe_fds_[1]); | ||
| 189 | ✗ | detail::throw_system_error(make_err(errn), "fcntl F_SETFD"); | ||
| 190 | } | |||
| 191 | 1404x | } | ||
| 192 | ||||
| 193 |
1/2✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
|
702x | timer_svc_ = &get_timer_service(ctx, *this); | |
| 194 |
2/4✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 702 times.
✗ Branch 3 not taken.
|
1404x | timer_svc_->set_on_earliest_changed( | |
| 195 | 3154x | timer_service::callback(this, [](void* p) { | ||
| 196 | 2452x | static_cast<select_scheduler*>(p)->interrupt_reactor(); | ||
| 197 | 2452x | })); | ||
| 198 | ||||
| 199 |
1/2✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
|
702x | get_resolver_service(ctx, *this); | |
| 200 |
1/2✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
|
702x | get_signal_service(ctx, *this); | |
| 201 |
1/2✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
|
702x | get_stream_file_service(ctx, *this); | |
| 202 |
1/2✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
|
702x | get_random_access_file_service(ctx, *this); | |
| 203 | ||||
| 204 | 702x | completed_ops_.push(&task_op_); | ||
| 205 | 1404x | } | ||
| 206 | ||||
| 207 | 2106x | inline select_scheduler::~select_scheduler() | ||
| 208 | 1404x | { | ||
| 209 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
|
702x | if (pipe_fds_[0] >= 0) | |
| 210 |
1/2✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
|
702x | ::close(pipe_fds_[0]); | |
| 211 |
1/2✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
|
702x | if (pipe_fds_[1] >= 0) | |
| 212 |
1/2✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
|
702x | ::close(pipe_fds_[1]); | |
| 213 | 2106x | } | ||
| 214 | ||||
| 215 | inline void | |||
| 216 | 702x | select_scheduler::shutdown() | ||
| 217 | { | |||
| 218 | 702x | shutdown_drain(); | ||
| 219 | ||||
| 220 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
|
702x | if (pipe_fds_[1] >= 0) | |
| 221 | 702x | interrupt_reactor(); | ||
| 222 | 702x | } | ||
| 223 | ||||
| 224 | inline std::error_code | |||
| 225 | 3908x | select_scheduler::register_descriptor( | ||
| 226 | int fd, reactor_descriptor_state* desc) const | |||
| 227 | { | |||
| 228 |
2/4✓ Branch 0 taken 3908 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3908 times.
|
3908x | if (fd < 0 || fd >= FD_SETSIZE) | |
| 229 | ✗ | return make_err(EMFILE); | ||
| 230 | ||||
| 231 | 3908x | desc->registered_events = reactor_event_read | reactor_event_write; | ||
| 232 | 3908x | desc->fd = fd; | ||
| 233 | 3908x | desc->scheduler_ = this; | ||
| 234 | 3908x | desc->mutex.set_enabled(reactor_io_locking_); | ||
| 235 | 3908x | desc->ready_events_.store(0, std::memory_order_relaxed); | ||
| 236 | ||||
| 237 | { | |||
| 238 | 3908x | conditionally_enabled_mutex::scoped_lock lock(desc->mutex); | ||
| 239 | 3908x | desc->impl_ref_.reset(); | ||
| 240 | 3908x | desc->read_ready = false; | ||
| 241 | 3908x | desc->write_ready = false; | ||
| 242 | 3908x | } | ||
| 243 | ||||
| 244 | { | |||
| 245 | 3908x | mutex_type::scoped_lock lock(mutex_); | ||
| 246 | try | |||
| 247 | { | |||
| 248 |
1/2✓ Branch 0 taken 3908 times.
✗ Branch 1 not taken.
|
3908x | registered_descs_[fd] = desc; | |
| 249 | 3908x | } | ||
| 250 | catch (std::bad_alloc const&) | |||
| 251 | { | |||
| 252 | ✗ | return make_err(ENOMEM); | ||
| 253 | ✗ | } | ||
| 254 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 3898 times.
|
3908x | if (fd > max_fd_) | |
| 255 | 3898x | max_fd_ = fd; | ||
| 256 | 3908x | } | ||
| 257 | ||||
| 258 | 3908x | interrupt_reactor(); | ||
| 259 | 3908x | return {}; | ||
| 260 | 3908x | } | ||
| 261 | ||||
| 262 | inline void | |||
| 263 | 3867x | select_scheduler::deregister_descriptor(int fd) const | ||
| 264 | { | |||
| 265 | 3867x | mutex_type::scoped_lock lock(mutex_); | ||
| 266 | ||||
| 267 |
1/2✓ Branch 0 taken 3867 times.
✗ Branch 1 not taken.
|
3867x | auto it = registered_descs_.find(fd); | |
| 268 |
2/4✓ Branch 0 taken 3867 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3867 times.
✗ Branch 3 not taken.
|
3867x | if (it == registered_descs_.end()) | |
| 269 | ✗ | return; | ||
| 270 | ||||
| 271 |
1/2✓ Branch 0 taken 3867 times.
✗ Branch 1 not taken.
|
3867x | registered_descs_.erase(it); | |
| 272 | ||||
| 273 |
2/2✓ Branch 0 taken 158 times.
✓ Branch 1 taken 3709 times.
|
3867x | if (fd == max_fd_) | |
| 274 | { | |||
| 275 | 3709x | max_fd_ = pipe_fds_[0]; | ||
| 276 |
5/10✓ Branch 0 taken 7102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3393 times.
✓ Branch 3 taken 3709 times.
✓ Branch 4 taken 3393 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 3393 times.
✗ Branch 7 not taken.
|
7102x | for (auto& [registered_fd, state] : registered_descs_) | |
| 277 | { | |||
| 278 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 3344 times.
|
3393x | if (registered_fd > max_fd_) | |
| 279 | 3344x | max_fd_ = registered_fd; | ||
| 280 | } | |||
| 281 | 3709x | } | ||
| 282 | 3867x | } | ||
| 283 | ||||
| 284 | inline void | |||
| 285 | 2198x | select_scheduler::notify_reactor() const | ||
| 286 | { | |||
| 287 | 2198x | interrupt_reactor(); | ||
| 288 | 2198x | } | ||
| 289 | ||||
| 290 | inline void | |||
| 291 | 9842x | select_scheduler::interrupt_reactor() const | ||
| 292 | { | |||
| 293 | 9842x | char byte = 1; | ||
| 294 | 9842x | [[maybe_unused]] auto r = ::write(pipe_fds_[1], &byte, 1); | ||
| 295 | 9842x | } | ||
| 296 | ||||
| 297 | inline long | |||
| 298 | 1514890x | select_scheduler::calculate_timeout(long requested_timeout_us) const | ||
| 299 | { | |||
| 300 |
1/2✓ Branch 0 taken 1514890 times.
✗ Branch 1 not taken.
|
1514890x | if (requested_timeout_us == 0) | |
| 301 | ✗ | return 0; | ||
| 302 | ||||
| 303 | 1514890x | auto nearest = timer_svc_->nearest_expiry(); | ||
| 304 |
2/2✓ Branch 0 taken 1750 times.
✓ Branch 1 taken 1513140 times.
|
1514890x | if (nearest == timer_service::time_point::max()) | |
| 305 | 1750x | return requested_timeout_us; | ||
| 306 | ||||
| 307 | 1513140x | auto now = std::chrono::steady_clock::now(); | ||
| 308 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 1513089 times.
|
1513140x | if (nearest <= now) | |
| 309 | 51x | return 0; | ||
| 310 | ||||
| 311 | 1513089x | auto timer_timeout_us = | ||
| 312 | 1513089x | std::chrono::duration_cast<std::chrono::microseconds>(nearest - now) | ||
| 313 | 1513089x | .count(); | ||
| 314 | ||||
| 315 | 1513089x | constexpr auto long_max = | ||
| 316 | static_cast<long long>((std::numeric_limits<long>::max)()); | |||
| 317 | 1513089x | auto capped_timer_us = | ||
| 318 | 3026178x | (std::min)((std::max)(static_cast<long long>(timer_timeout_us), | ||
| 319 | 1513089x | static_cast<long long>(0)), | ||
| 320 | long_max); | |||
| 321 | ||||
| 322 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1513089 times.
|
1513089x | if (requested_timeout_us < 0) | |
| 323 | 1513089x | return static_cast<long>(capped_timer_us); | ||
| 324 | ||||
| 325 | ✗ | return static_cast<long>( | ||
| 326 | ✗ | (std::min)(static_cast<long long>(requested_timeout_us), | ||
| 327 | capped_timer_us)); | |||
| 328 | 1514890x | } | ||
| 329 | ||||
| 330 | inline void | |||
| 331 | 1537602x | select_scheduler::run_task( | ||
| 332 | lock_type& lock, context_type* ctx, long timeout_us) | |||
| 333 | { | |||
| 334 | 1537602x | long effective_timeout_us = | ||
| 335 |
2/2✓ Branch 0 taken 22712 times.
✓ Branch 1 taken 1514890 times.
|
1537602x | task_interrupted_ ? 0 : calculate_timeout(timeout_us); | |
| 336 | ||||
| 337 | // Snapshot registered descriptors while holding lock. | |||
| 338 | // Record which fds need write monitoring to avoid a hot loop: | |||
| 339 | // select is level-triggered so writable sockets (nearly always | |||
| 340 | // writable) would cause select() to return immediately every | |||
| 341 | // iteration if unconditionally added to write_fds. Membership | |||
| 342 | // stays opt-in: a parked write wait opts in the same way a | |||
| 343 | // parked write or connect op does. | |||
| 344 | struct fd_entry | |||
| 345 | { | |||
| 346 | int fd; | |||
| 347 | reactor_descriptor_state* desc; | |||
| 348 | bool needs_write; | |||
| 349 | }; | |||
| 350 | fd_entry snapshot[FD_SETSIZE]; | |||
| 351 | 1537602x | int snapshot_count = 0; | ||
| 352 | ||||
| 353 |
2/2✓ Branch 0 taken 2085419 times.
✓ Branch 1 taken 1537602 times.
|
3623021x | for (auto& [fd, desc] : registered_descs_) | |
| 354 | { | |||
| 355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2085419 times.
|
2085419x | if (snapshot_count < FD_SETSIZE) | |
| 356 | { | |||
| 357 | 2085419x | conditionally_enabled_mutex::scoped_lock desc_lock(desc->mutex); | ||
| 358 | 2085419x | snapshot[snapshot_count].fd = fd; | ||
| 359 | 2085419x | snapshot[snapshot_count].desc = desc; | ||
| 360 | 2085419x | snapshot[snapshot_count].needs_write = | ||
| 361 |
4/4✓ Branch 0 taken 2084196 times.
✓ Branch 1 taken 1223 times.
✓ Branch 2 taken 3278 times.
✓ Branch 3 taken 2080918 times.
|
2085419x | (desc->write_op || desc->connect_op || | |
| 362 | 2080918x | desc->wait_write_op); | ||
| 363 | 2085419x | ++snapshot_count; | ||
| 364 | 2085419x | } | ||
| 365 | } | |||
| 366 | ||||
| 367 |
2/2✓ Branch 0 taken 22711 times.
✓ Branch 1 taken 1514891 times.
|
1537602x | if (lock.owns_lock()) | |
| 368 | 1514891x | lock.unlock(); | ||
| 369 | ||||
| 370 | 1537602x | task_cleanup on_exit{this, &lock, ctx}; | ||
| 371 | ||||
| 372 | fd_set read_fds, write_fds, except_fds; | |||
| 373 | 1537602x | FD_ZERO(&read_fds); | ||
| 374 | 1537602x | FD_ZERO(&write_fds); | ||
| 375 | 1537602x | FD_ZERO(&except_fds); | ||
| 376 | ||||
| 377 |
1/2✓ Branch 0 taken 1537602 times.
✗ Branch 1 not taken.
|
1537602x | FD_SET(pipe_fds_[0], &read_fds); | |
| 378 | 1537602x | int nfds = pipe_fds_[0]; | ||
| 379 | ||||
| 380 |
2/2✓ Branch 0 taken 1537602 times.
✓ Branch 1 taken 2085419 times.
|
3623021x | for (int i = 0; i < snapshot_count; ++i) | |
| 381 | { | |||
| 382 | 2085419x | int fd = snapshot[i].fd; | ||
| 383 |
1/2✓ Branch 0 taken 2085419 times.
✗ Branch 1 not taken.
|
2085419x | FD_SET(fd, &read_fds); | |
| 384 |
2/2✓ Branch 0 taken 4511 times.
✓ Branch 1 taken 2080908 times.
|
2085419x | if (snapshot[i].needs_write) | |
| 385 |
1/2✓ Branch 0 taken 4511 times.
✗ Branch 1 not taken.
|
4511x | FD_SET(fd, &write_fds); | |
| 386 |
1/2✓ Branch 0 taken 2085419 times.
✗ Branch 1 not taken.
|
2085419x | FD_SET(fd, &except_fds); | |
| 387 |
2/2✓ Branch 0 taken 548220 times.
✓ Branch 1 taken 1537199 times.
|
2085419x | if (fd > nfds) | |
| 388 | 1537199x | nfds = fd; | ||
| 389 | 2085419x | } | ||
| 390 | ||||
| 391 | struct timeval tv; | |||
| 392 | 1537602x | struct timeval* tv_ptr = nullptr; | ||
| 393 |
2/2✓ Branch 0 taken 1746 times.
✓ Branch 1 taken 1535856 times.
|
1537602x | if (effective_timeout_us >= 0) | |
| 394 | { | |||
| 395 | 1535856x | tv.tv_sec = effective_timeout_us / 1000000; | ||
| 396 | 1535856x | tv.tv_usec = effective_timeout_us % 1000000; | ||
| 397 | 1535856x | tv_ptr = &tv; | ||
| 398 | 1535856x | } | ||
| 399 | ||||
| 400 |
1/2✓ Branch 0 taken 1537602 times.
✗ Branch 1 not taken.
|
1537602x | int ready = ::select(nfds + 1, &read_fds, &write_fds, &except_fds, tv_ptr); | |
| 401 | ||||
| 402 | // EINTR: signal interrupted select(), just retry. | |||
| 403 | // EBADF: an fd was closed between snapshot and select(); retry | |||
| 404 | // with a fresh snapshot from registered_descs_. | |||
| 405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1537602 times.
|
1537602x | if (ready < 0) | |
| 406 | { | |||
| 407 | ✗ | if (errno == EINTR || errno == EBADF) | ||
| 408 | ✗ | return; | ||
| 409 | ✗ | detail::throw_system_error(make_err(errno), "select"); | ||
| 410 | } | |||
| 411 | ||||
| 412 | // Process timers outside the lock | |||
| 413 |
1/2✓ Branch 0 taken 1537602 times.
✗ Branch 1 not taken.
|
1537602x | timer_svc_->process_expired(); | |
| 414 | ||||
| 415 | 1537602x | ready_queue local_ops; | ||
| 416 | ||||
| 417 |
2/2✓ Branch 0 taken 1532266 times.
✓ Branch 1 taken 5336 times.
|
1537602x | if (ready > 0) | |
| 418 | { | |||
| 419 |
3/4✓ Branch 0 taken 1532266 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4629 times.
✓ Branch 3 taken 1527637 times.
|
1532266x | if (FD_ISSET(pipe_fds_[0], &read_fds)) | |
| 420 | { | |||
| 421 | char buf[256]; | |||
| 422 |
3/4✓ Branch 0 taken 9258 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4629 times.
✓ Branch 3 taken 4629 times.
|
9258x | while (::read(pipe_fds_[0], buf, sizeof(buf)) > 0) | |
| 423 | { | |||
| 424 | } | |||
| 425 | 4629x | } | ||
| 426 | ||||
| 427 |
2/2✓ Branch 0 taken 2078766 times.
✓ Branch 1 taken 1532266 times.
|
3611032x | for (int i = 0; i < snapshot_count; ++i) | |
| 428 | { | |||
| 429 | 2078766x | int fd = snapshot[i].fd; | ||
| 430 | 2078766x | reactor_descriptor_state* desc = snapshot[i].desc; | ||
| 431 | ||||
| 432 | 2078766x | std::uint32_t flags = 0; | ||
| 433 |
3/4✓ Branch 0 taken 2078766 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 499049 times.
✓ Branch 3 taken 1579717 times.
|
2078766x | if (FD_ISSET(fd, &read_fds)) | |
| 434 | 1579717x | flags |= reactor_event_read; | ||
| 435 |
3/4✓ Branch 0 taken 2078766 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2076570 times.
✓ Branch 3 taken 2196 times.
|
2078766x | if (FD_ISSET(fd, &write_fds)) | |
| 436 | 2196x | flags |= reactor_event_write; | ||
| 437 |
2/4✓ Branch 0 taken 2078766 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2078766 times.
✗ Branch 3 not taken.
|
2078766x | if (FD_ISSET(fd, &except_fds)) | |
| 438 | ✗ | flags |= reactor_event_error; | ||
| 439 | ||||
| 440 |
2/2✓ Branch 0 taken 496864 times.
✓ Branch 1 taken 1581902 times.
|
2078766x | if (flags == 0) | |
| 441 | 496864x | continue; | ||
| 442 | ||||
| 443 | 1581902x | desc->add_ready_events(flags); | ||
| 444 | ||||
| 445 | 1581902x | bool expected = false; | ||
| 446 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1581902 times.
|
1581902x | if (desc->is_enqueued_.compare_exchange_strong( | |
| 447 | expected, true, std::memory_order_release, | |||
| 448 | std::memory_order_relaxed)) | |||
| 449 | { | |||
| 450 | 1581902x | local_ops.push(desc); | ||
| 451 | 1581902x | } | ||
| 452 | 1581902x | } | ||
| 453 | 1532266x | } | ||
| 454 | ||||
| 455 |
1/2✓ Branch 0 taken 1537602 times.
✗ Branch 1 not taken.
|
1537602x | lock.lock(); | |
| 456 | ||||
| 457 | 1537602x | completed_ops_.splice(local_ops); | ||
| 458 | 1537602x | } | ||
| 459 | ||||
| 460 | } // namespace boost::corosio::detail | |||
| 461 | ||||
| 462 | #endif // BOOST_COROSIO_HAS_SELECT | |||
| 463 | ||||
| 464 | #endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SCHEDULER_HPP | |||
| 465 |