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