99.44% Lines (178/179) 100.00% Functions (13/13)
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   27  
28   #include <boost/corosio/detail/except.hpp> 28   #include <boost/corosio/detail/except.hpp>
29   29  
30   #include <sys/select.h> 30   #include <sys/select.h>
31   #include <unistd.h> 31   #include <unistd.h>
32   #include <errno.h> 32   #include <errno.h>
33   #include <fcntl.h> 33   #include <fcntl.h>
34   34  
35   #include <atomic> 35   #include <atomic>
36   #include <chrono> 36   #include <chrono>
37   #include <cstdint> 37   #include <cstdint>
38   #include <limits> 38   #include <limits>
39   #include <mutex> 39   #include <mutex>
40   #include <new> 40   #include <new>
41   #include <unordered_map> 41   #include <unordered_map>
42   42  
43   namespace boost::corosio::detail { 43   namespace boost::corosio::detail {
44   44  
45   struct select_op; 45   struct select_op;
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: signal state machine, inline completion 51   model from reactor_scheduler: 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   Known Limitations: 59   Known Limitations:
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   @par Thread Safety 64   @par Thread Safety
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 67   class BOOST_COROSIO_DECL select_scheduler final : public reactor_scheduler
68   { 68   {
69   public: 69   public:
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   will fail with EINVAL. 92   will fail with EINVAL.
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   return FD_SETSIZE - 1; 98   return FD_SETSIZE - 1;
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   @return The error if the fd cannot be tracked, otherwise a 110   @return The error if the fd cannot be tracked, otherwise a
111   default constructed error code. 111   default constructed error code.
112   */ 112   */
113   std::error_code 113   std::error_code
114   register_descriptor(int fd, reactor_descriptor_state* desc) const; 114   register_descriptor(int fd, reactor_descriptor_state* desc) const;
115   115  
116   /// No-op: write readiness is watched from registration on. 116   /// No-op: write readiness is watched from registration on.
117   std::error_code 117   std::error_code
HITCBC 118   2744 ensure_write_registered(int, reactor_descriptor_state*) const noexcept 118   2768 ensure_write_registered(int, reactor_descriptor_state*) const noexcept
119   { 119   {
HITCBC 120   2744 return {}; 120   2768 return {};
121   } 121   }
122   122  
123   /** Deregister a persistently registered descriptor. 123   /** Deregister a persistently registered descriptor.
124   124  
125   @param fd The file descriptor to deregister. 125   @param fd The file descriptor to deregister.
126   */ 126   */
127   void deregister_descriptor(int fd) const; 127   void deregister_descriptor(int fd) const;
128   128  
129   /** Interrupt the reactor so it rebuilds its fd_sets. 129   /** Interrupt the reactor so it rebuilds its fd_sets.
130   130  
131   Called when a write, connect, or write-wait op is registered 131   Called when a write, connect, or write-wait op is registered
132   after the reactor's snapshot was taken. Without this, 132   after the reactor's snapshot was taken. Without this,
133   select() may block not watching for writability on the fd. 133   select() may block not watching for writability on the fd.
134   */ 134   */
135   void notify_reactor() const; 135   void notify_reactor() const;
136   136  
137   /// Watch the read end of the POSIX signal self-pipe (see scheduler.hpp). 137   /// Watch the read end of the POSIX signal self-pipe (see scheduler.hpp).
HITCBC 138   271 [[nodiscard]] std::error_code register_signal_reader(int read_fd) override 138   271 [[nodiscard]] std::error_code register_signal_reader(int read_fd) override
139   { 139   {
HITCBC 140   271 return register_descriptor(read_fd, signal_pipe_reader_.arm()); 140   271 return register_descriptor(read_fd, signal_pipe_reader_.arm());
141   } 141   }
142   142  
143   private: 143   private:
144   void run_task(lock_type& lock, context_type& ctx, long timeout_us) override; 144   void run_task(lock_type& lock, context_type& ctx, long timeout_us) override;
145   void interrupt_reactor() const override; 145   void interrupt_reactor() const override;
146   146  
147   /// Rebuild the poll set a blocked select() was given. 147   /// Rebuild the poll set a blocked select() was given.
HITCBC 148   454 void notify_reparked() const override 148   482 void notify_reparked() const override
149   { 149   {
HITCBC 150   454 interrupt_reactor(); 150   482 interrupt_reactor();
HITCBC 151   454 } 151   482 }
152   long calculate_timeout(long requested_timeout_us) const; 152   long calculate_timeout(long requested_timeout_us) const;
153   153  
154   // Watches the global signal self-pipe's read end (armed lazily by 154   // Watches the global signal self-pipe's read end (armed lazily by
155   // register_signal_reader on the first signal registration). 155   // register_signal_reader on the first signal registration).
156   reactor_signal_pipe_reader signal_pipe_reader_; 156   reactor_signal_pipe_reader signal_pipe_reader_;
157   157  
158   // Self-pipe for interrupting select() 158   // Self-pipe for interrupting select()
159   int pipe_fds_[2]; // [0]=read, [1]=write 159   int pipe_fds_[2]; // [0]=read, [1]=write
160   160  
161   // Per-fd tracking for fd_set building 161   // Per-fd tracking for fd_set building
162   mutable std::unordered_map<int, reactor_descriptor_state*> 162   mutable std::unordered_map<int, reactor_descriptor_state*>
163   registered_descs_; 163   registered_descs_;
164   mutable int max_fd_ = -1; 164   mutable int max_fd_ = -1;
165   }; 165   };
166   166  
HITCBC 167   1952 inline select_scheduler::select_scheduler(capy::execution_context& ctx, int) 167   1952 inline select_scheduler::select_scheduler(capy::execution_context& ctx, int)
HITCBC 168   1952 : pipe_fds_{-1, -1} 168   1952 : pipe_fds_{-1, -1}
HITCBC 169   1952 , max_fd_(-1) 169   1952 , max_fd_(-1)
170   { 170   {
HITCBC 171   1952 if (::pipe(pipe_fds_) < 0) 171   1952 if (::pipe(pipe_fds_) < 0)
HITCBC 172   1 detail::throw_system_error(make_err(errno), "pipe"); 172   1 detail::throw_system_error(make_err(errno), "pipe");
173   173  
HITCBC 174   5844 for (int i = 0; i < 2; ++i) 174   5844 for (int i = 0; i < 2; ++i)
175   { 175   {
HITCBC 176   3899 int flags = ::fcntl(pipe_fds_[i], F_GETFL, 0); 176   3899 int flags = ::fcntl(pipe_fds_[i], F_GETFL, 0);
HITCBC 177   3899 if (flags == -1) 177   3899 if (flags == -1)
178   { 178   {
HITCBC 179   2 int errn = errno; 179   2 int errn = errno;
HITCBC 180   2 ::close(pipe_fds_[0]); 180   2 ::close(pipe_fds_[0]);
HITCBC 181   2 ::close(pipe_fds_[1]); 181   2 ::close(pipe_fds_[1]);
HITCBC 182   2 detail::throw_system_error(make_err(errn), "fcntl F_GETFL"); 182   2 detail::throw_system_error(make_err(errn), "fcntl F_GETFL");
183   } 183   }
HITCBC 184   3897 if (::fcntl(pipe_fds_[i], F_SETFL, flags | O_NONBLOCK) == -1) 184   3897 if (::fcntl(pipe_fds_[i], F_SETFL, flags | O_NONBLOCK) == -1)
185   { 185   {
HITCBC 186   2 int errn = errno; 186   2 int errn = errno;
HITCBC 187   2 ::close(pipe_fds_[0]); 187   2 ::close(pipe_fds_[0]);
HITCBC 188   2 ::close(pipe_fds_[1]); 188   2 ::close(pipe_fds_[1]);
HITCBC 189   2 detail::throw_system_error(make_err(errn), "fcntl F_SETFL"); 189   2 detail::throw_system_error(make_err(errn), "fcntl F_SETFL");
190   } 190   }
HITCBC 191   3895 if (::fcntl(pipe_fds_[i], F_SETFD, FD_CLOEXEC) == -1) 191   3895 if (::fcntl(pipe_fds_[i], F_SETFD, FD_CLOEXEC) == -1)
192   { 192   {
HITCBC 193   2 int errn = errno; 193   2 int errn = errno;
HITCBC 194   2 ::close(pipe_fds_[0]); 194   2 ::close(pipe_fds_[0]);
HITCBC 195   2 ::close(pipe_fds_[1]); 195   2 ::close(pipe_fds_[1]);
HITCBC 196   2 detail::throw_system_error(make_err(errn), "fcntl F_SETFD"); 196   2 detail::throw_system_error(make_err(errn), "fcntl F_SETFD");
197   } 197   }
198   } 198   }
199   199  
HITCBC 200   1945 timer_svc_ = &get_timer_service(ctx, *this); 200   1945 timer_svc_ = &get_timer_service(ctx, *this);
HITCBC 201   1945 timer_svc_->set_on_earliest_changed( 201   1945 timer_svc_->set_on_earliest_changed(
HITCBC 202   5130 timer_service::callback(this, [](void* p) { 202   5160 timer_service::callback(this, [](void* p) {
HITCBC 203   3185 static_cast<select_scheduler*>(p)->interrupt_reactor(); 203   3215 static_cast<select_scheduler*>(p)->interrupt_reactor();
HITCBC 204   3185 })); 204   3215 }));
205   205  
HITCBC 206   1945 completed_ops_.push(&task_op_); 206   1945 completed_ops_.push(&task_op_);
HITCBC 207   1966 } 207   1966 }
208   208  
HITCBC 209   3890 inline select_scheduler::~select_scheduler() 209   3890 inline select_scheduler::~select_scheduler()
210   { 210   {
HITCBC 211   1945 if (pipe_fds_[0] >= 0) 211   1945 if (pipe_fds_[0] >= 0)
HITCBC 212   1945 ::close(pipe_fds_[0]); 212   1945 ::close(pipe_fds_[0]);
HITCBC 213   1945 if (pipe_fds_[1] >= 0) 213   1945 if (pipe_fds_[1] >= 0)
HITCBC 214   1945 ::close(pipe_fds_[1]); 214   1945 ::close(pipe_fds_[1]);
HITCBC 215   3890 } 215   3890 }
216   216  
217   inline void 217   inline void
HITCBC 218   1945 select_scheduler::shutdown() 218   1945 select_scheduler::shutdown()
219   { 219   {
HITCBC 220   1945 shutdown_drain(); 220   1945 shutdown_drain();
221   221  
HITCBC 222   1945 if (pipe_fds_[1] >= 0) 222   1945 if (pipe_fds_[1] >= 0)
HITCBC 223   1945 interrupt_reactor(); 223   1945 interrupt_reactor();
HITCBC 224   1945 } 224   1945 }
225   225  
226   inline std::error_code 226   inline std::error_code
HITCBC 227   9887 select_scheduler::register_descriptor( 227   10011 select_scheduler::register_descriptor(
228   int fd, reactor_descriptor_state* desc) const 228   int fd, reactor_descriptor_state* desc) const
229   { 229   {
HITCBC 230   9887 if (fd < 0 || fd >= FD_SETSIZE) 230   10011 if (fd < 0 || fd >= FD_SETSIZE)
HITCBC 231   1 return make_err(EMFILE); 231   1 return make_err(EMFILE);
232   232  
233   // A completion queued by an earlier registration of the same state 233   // A completion queued by an earlier registration of the same state
234   // may still be running, so the fields it reads change under its 234   // may still be running, so the fields it reads change under its
235   // mutex, and the mutex mode is only written when it differs. 235   // mutex, and the mutex mode is only written when it differs.
HITCBC 236   9886 if (desc->mutex.enabled() != reactor_io_locking_) 236   10010 if (desc->mutex.enabled() != reactor_io_locking_)
HITCBC 237   3 desc->mutex.set_enabled(reactor_io_locking_); 237   3 desc->mutex.set_enabled(reactor_io_locking_);
238   { 238   {
HITCBC 239   9886 conditionally_enabled_mutex::scoped_lock lock(desc->mutex); 239   10010 conditionally_enabled_mutex::scoped_lock lock(desc->mutex);
HITCBC 240   9886 desc->registered_events = reactor_event_read | reactor_event_write; 240   10010 desc->registered_events = reactor_event_read | reactor_event_write;
HITCBC 241   9886 desc->unpollable = false; // the state is reused across adoptions 241   10010 desc->unpollable = false; // the state is reused across adoptions
HITCBC 242   9886 desc->fd = fd; 242   10010 desc->fd = fd;
HITCBC 243   9886 desc->scheduler_ = this; 243   10010 desc->scheduler_ = this;
HITCBC 244   9886 desc->ready_events_.store(0, std::memory_order_release); 244   10010 desc->ready_events_.store(0, std::memory_order_release);
245   // object_ref_ is not touched: a queued invocation from an 245   // object_ref_ is not touched: a queued invocation from an
246   // earlier registration still owns the reference in it. 246   // earlier registration still owns the reference in it.
HITCBC 247   9886 desc->read_ready = false; 247   10010 desc->read_ready = false;
HITCBC 248   9886 desc->write_ready = false; 248   10010 desc->write_ready = false;
HITCBC 249   9886 } 249   10010 }
250   250  
251   { 251   {
HITCBC 252   9886 mutex_type::scoped_lock lock(mutex_); 252   10010 mutex_type::scoped_lock lock(mutex_);
253   try 253   try
254   { 254   {
HITCBC 255   9886 registered_descs_[fd] = desc; 255   10010 registered_descs_[fd] = desc;
256   } 256   }
HITCBC 257   1 catch (std::bad_alloc const&) 257   1 catch (std::bad_alloc const&)
258   { 258   {
HITCBC 259   1 return make_err(ENOMEM); 259   1 return make_err(ENOMEM);
HITCBC 260   1 } 260   1 }
HITCBC 261   9885 if (fd > max_fd_) 261   10009 if (fd > max_fd_)
HITCBC 262   9396 max_fd_ = fd; 262   9499 max_fd_ = fd;
HITCBC 263   9886 } 263   10010 }
264   264  
HITCBC 265   9885 interrupt_reactor(); 265   10009 interrupt_reactor();
HITCBC 266   9885 return {}; 266   10009 return {};
267   } 267   }
268   268  
269   inline void 269   inline void
HITCBC 270   9615 select_scheduler::deregister_descriptor(int fd) const 270   9739 select_scheduler::deregister_descriptor(int fd) const
271   { 271   {
HITCBC 272   9615 mutex_type::scoped_lock lock(mutex_); 272   9739 mutex_type::scoped_lock lock(mutex_);
273   273  
HITCBC 274   9615 auto it = registered_descs_.find(fd); 274   9739 auto it = registered_descs_.find(fd);
HITCBC 275   9615 if (it == registered_descs_.end()) 275   9739 if (it == registered_descs_.end())
MISUBC 276   ✗ return; 276   ✗ return;
277   277  
HITCBC 278   9615 registered_descs_.erase(it); 278   9739 registered_descs_.erase(it);
279   279  
HITCBC 280   9615 if (fd == max_fd_) 280   9739 if (fd == max_fd_)
281   { 281   {
HITCBC 282   7696 max_fd_ = pipe_fds_[0]; 282   7796 max_fd_ = pipe_fds_[0];
HITCBC 283   12766 for (auto& [registered_fd, state] : registered_descs_) 283   13038 for (auto& [registered_fd, state] : registered_descs_)
284   { 284   {
HITCBC 285   5070 if (registered_fd > max_fd_) 285   5242 if (registered_fd > max_fd_)
HITCBC 286   4868 max_fd_ = registered_fd; 286   4979 max_fd_ = registered_fd;
287   } 287   }
288   } 288   }
HITCBC 289   9615 } 289   9739 }
290   290  
291   inline void 291   inline void
HITCBC 292   6777 select_scheduler::notify_reactor() const 292   6829 select_scheduler::notify_reactor() const
293   { 293   {
HITCBC 294   6777 interrupt_reactor(); 294   6829 interrupt_reactor();
HITCBC 295   6777 } 295   6829 }
296   296  
297   inline void 297   inline void
HITCBC 298   25540 select_scheduler::interrupt_reactor() const 298   25599 select_scheduler::interrupt_reactor() const
299   { 299   {
HITCBC 300   25540 char byte = 1; 300   25599 char byte = 1;
HITCBC 301   25540 [[maybe_unused]] auto r = ::write(pipe_fds_[1], &byte, 1); 301   25599 [[maybe_unused]] auto r = ::write(pipe_fds_[1], &byte, 1);
HITCBC 302   25540 } 302   25599 }
303   303  
304   inline long 304   inline long
HITCBC 305   14530 select_scheduler::calculate_timeout(long requested_timeout_us) const 305   81669 select_scheduler::calculate_timeout(long requested_timeout_us) const
306   { 306   {
HITCBC 307   14530 if (requested_timeout_us == 0) 307   81669 if (requested_timeout_us == 0)
308   return 0; // LCOV_EXCL_LINE run_task passes 0 via task_interrupted_, never through this argument 308   return 0; // LCOV_EXCL_LINE run_task passes 0 via task_interrupted_, never through this argument
309   309  
HITCBC 310   14530 auto nearest = timer_svc_->nearest_expiry(); 310   81669 auto nearest = timer_svc_->nearest_expiry();
HITCBC 311   14530 if (nearest == timer_service::time_point::max()) 311   81669 if (nearest == timer_service::time_point::max())
HITCBC 312   7981 return requested_timeout_us; 312   75053 return requested_timeout_us;
313   313  
HITCBC 314   6549 auto now = std::chrono::steady_clock::now(); 314   6616 auto now = std::chrono::steady_clock::now();
HITCBC 315   6549 if (nearest <= now) 315   6616 if (nearest <= now)
HITCBC 316   69 return 0; 316   51 return 0;
317   317  
318   auto timer_timeout_us = 318   auto timer_timeout_us =
HITCBC 319   6480 std::chrono::duration_cast<std::chrono::microseconds>(nearest - now) 319   6565 std::chrono::duration_cast<std::chrono::microseconds>(nearest - now)
HITCBC 320   6480 .count(); 320   6565 .count();
321   321  
HITCBC 322   6480 constexpr auto long_max = 322   6565 constexpr auto long_max =
323   static_cast<long long>((std::numeric_limits<long>::max)()); 323   static_cast<long long>((std::numeric_limits<long>::max)());
324   auto capped_timer_us = 324   auto capped_timer_us =
HITCBC 325   6480 (std::min)((std::max)(static_cast<long long>(timer_timeout_us), 325   6565 (std::min)((std::max)(static_cast<long long>(timer_timeout_us),
HITCBC 326   6480 static_cast<long long>(0)), 326   6565 static_cast<long long>(0)),
HITCBC 327   6480 long_max); 327   6565 long_max);
328   328  
HITCBC 329   6480 if (requested_timeout_us < 0) 329   6565 if (requested_timeout_us < 0)
HITCBC 330   6474 return static_cast<long>(capped_timer_us); 330   6559 return static_cast<long>(capped_timer_us);
331   331  
332   return static_cast<long>( 332   return static_cast<long>(
HITCBC 333   6 (std::min)(static_cast<long long>(requested_timeout_us), 333   6 (std::min)(static_cast<long long>(requested_timeout_us),
HITCBC 334   6 capped_timer_us)); 334   6 capped_timer_us));
335   } 335   }
336   336  
337   inline void 337   inline void
HITCBC 338   140771 select_scheduler::run_task(lock_type& lock, context_type& ctx, long timeout_us) 338   210590 select_scheduler::run_task(lock_type& lock, context_type& ctx, long timeout_us)
339   { 339   {
340   long effective_timeout_us = 340   long effective_timeout_us =
HITCBC 341   140771 task_interrupted_ ? 0 : calculate_timeout(timeout_us); 341   210590 task_interrupted_ ? 0 : calculate_timeout(timeout_us);
342   342  
343   // Snapshot registered descriptors while holding lock. 343   // Snapshot registered descriptors while holding lock.
344   // Record which directions each fd needs monitored to avoid a hot 344   // Record which directions each fd needs monitored to avoid a hot
345   // loop: select is level-triggered, so a writable socket (nearly 345   // loop: select is level-triggered, so a writable socket (nearly
346   // always writable) or an always-readable fd (/dev/zero, a pipe at 346   // always writable) or an always-readable fd (/dev/zero, a pipe at
347   // EOF) would return select() immediately every iteration if 347   // EOF) would return select() immediately every iteration if
348   // unconditionally added. Membership in both sets is opt-in: a 348   // unconditionally added. Membership in both sets is opt-in: a
349   // parked op or wait in a direction opts that direction in. The 349   // parked op or wait in a direction opts that direction in. The
350   // exceptional set is opt-in too, for any parked op or wait: 350   // exceptional set is opt-in too, for any parked op or wait:
351   // Darwin reports a character device (/dev/zero, /dev/null) as 351   // Darwin reports a character device (/dev/zero, /dev/null) as
352   // exceptional on every call. 352   // exceptional on every call.
353   struct fd_entry 353   struct fd_entry
354   { 354   {
355   int fd; 355   int fd;
356   reactor_descriptor_state* desc; 356   reactor_descriptor_state* desc;
357   std::uint32_t want; 357   std::uint32_t want;
358   }; 358   };
359   fd_entry snapshot[FD_SETSIZE]; 359   fd_entry snapshot[FD_SETSIZE];
HITCBC 360   140771 int snapshot_count = 0; 360   210590 int snapshot_count = 0;
361   361  
362   // The caller may have released the lock to hand queued work to a 362   // The caller may have released the lock to hand queued work to a
363   // peer, and deregister_descriptor() erases from this map under it. 363   // peer, and deregister_descriptor() erases from this map under it.
HITCBC 364   140771 if (!lock.owns_lock()) 364   210590 if (!lock.owns_lock())
HITCBC 365   126238 lock.lock(); 365   128918 lock.lock();
366   366  
HITCBC 367   389684 for (auto& [fd, desc] : registered_descs_) 367   537480 for (auto& [fd, desc] : registered_descs_)
368   { 368   {
HITCBC 369   248913 if (snapshot_count < FD_SETSIZE) 369   326890 if (snapshot_count < FD_SETSIZE)
370   { 370   {
HITCBC 371   248913 conditionally_enabled_mutex::scoped_lock desc_lock(desc->mutex); 371   326890 conditionally_enabled_mutex::scoped_lock desc_lock(desc->mutex);
HITCBC 372   248913 snapshot[snapshot_count].fd = fd; 372   326890 snapshot[snapshot_count].fd = fd;
HITCBC 373   248913 snapshot[snapshot_count].desc = desc; 373   326890 snapshot[snapshot_count].desc = desc;
HITCBC 374   248913 snapshot[snapshot_count].want = 374   326890 snapshot[snapshot_count].want =
HITCBC 375   248913 ((desc->read_op || desc->wait_read_op) ? reactor_event_read 375   326890 ((desc->read_op || desc->wait_read_op) ? reactor_event_read
HITCBC 376   248913 : 0) | 376   326890 : 0) |
HITCBC 377   248533 ((desc->write_op || desc->connect_op || desc->wait_write_op) 377   326510 ((desc->write_op || desc->connect_op || desc->wait_write_op)
HITCBC 378   497446 ? reactor_event_write 378   653400 ? reactor_event_write
HITCBC 379   248913 : 0) | 379   326890 : 0) |
HITCBC 380   248913 (desc->wait_error_op ? reactor_event_error : 0); 380   326890 (desc->wait_error_op ? reactor_event_error : 0);
HITCBC 381   248913 ++snapshot_count; 381   326890 ++snapshot_count;
HITCBC 382   248913 } 382   326890 }
383   } 383   }
384   384  
HITCBC 385   140771 if (lock.owns_lock()) 385   210590 if (lock.owns_lock())
HITCBC 386   140770 lock.unlock(); 386   210589 lock.unlock();
387   387  
HITCBC 388   140771 task_cleanup on_exit{this, &lock, ctx}; 388   210590 task_cleanup on_exit{this, &lock, ctx};
389   389  
390   fd_set read_fds, write_fds, except_fds; 390   fd_set read_fds, write_fds, except_fds;
HITCBC 391   2393107 FD_ZERO(&read_fds); 391   3580030 FD_ZERO(&read_fds);
HITCBC 392   2393107 FD_ZERO(&write_fds); 392   3580030 FD_ZERO(&write_fds);
HITCBC 393   2393107 FD_ZERO(&except_fds); 393   3580030 FD_ZERO(&except_fds);
394   394  
HITCBC 395   140771 FD_SET(pipe_fds_[0], &read_fds); 395   210590 FD_SET(pipe_fds_[0], &read_fds);
HITCBC 396   140771 int nfds = pipe_fds_[0]; 396   210590 int nfds = pipe_fds_[0];
397   397  
HITCBC 398   389684 for (int i = 0; i < snapshot_count; ++i) 398   537480 for (int i = 0; i < snapshot_count; ++i)
399   { 399   {
HITCBC 400   248913 int fd = snapshot[i].fd; 400   326890 int fd = snapshot[i].fd;
HITCBC 401   248913 if (snapshot[i].want & reactor_event_read) 401   326890 if (snapshot[i].want & reactor_event_read)
HITCBC 402   115589 FD_SET(fd, &read_fds); 402   183192 FD_SET(fd, &read_fds);
HITCBC 403   248913 if (snapshot[i].want & reactor_event_write) 403   326890 if (snapshot[i].want & reactor_event_write)
HITCBC 404   3018 FD_SET(fd, &write_fds); 404   3042 FD_SET(fd, &write_fds);
HITCBC 405   248913 if (snapshot[i].want != 0) 405   326890 if (snapshot[i].want != 0)
HITCBC 406   118897 FD_SET(fd, &except_fds); 406   186524 FD_SET(fd, &except_fds);
HITCBC 407   248913 if (fd > nfds) 407   326890 if (fd > nfds)
HITCBC 408   138642 nfds = fd; 408   208504 nfds = fd;
409   } 409   }
410   410  
411   struct timeval tv; 411   struct timeval tv;
HITCBC 412   140771 struct timeval* tv_ptr = nullptr; 412   210590 struct timeval* tv_ptr = nullptr;
HITCBC 413   140771 if (effective_timeout_us >= 0) 413   210590 if (effective_timeout_us >= 0)
414   { 414   {
HITCBC 415   133474 tv.tv_sec = effective_timeout_us / 1000000; 415   136127 tv.tv_sec = effective_timeout_us / 1000000;
HITCBC 416   133474 tv.tv_usec = effective_timeout_us % 1000000; 416   136127 tv.tv_usec = effective_timeout_us % 1000000;
HITCBC 417   133474 tv_ptr = &tv; 417   136127 tv_ptr = &tv;
418   } 418   }
419   419  
HITCBC 420   140771 int ready = ::select(nfds + 1, &read_fds, &write_fds, &except_fds, tv_ptr); 420   210590 int ready = ::select(nfds + 1, &read_fds, &write_fds, &except_fds, tv_ptr);
421   421  
422   // EINTR: signal interrupted select(), just retry. 422   // EINTR: signal interrupted select(), just retry.
423   // EBADF: an fd was closed between snapshot and select(); retry 423   // EBADF: an fd was closed between snapshot and select(); retry
424   // with a fresh snapshot from registered_descs_. 424   // with a fresh snapshot from registered_descs_.
425   // Both fall through with no ready descriptors rather than 425   // Both fall through with no ready descriptors rather than
426   // returning: the caller handed this function an owned lock that 426   // returning: the caller handed this function an owned lock that
427   // only the epilogue below re-acquires. 427   // only the epilogue below re-acquires.
HITCBC 428   140771 if (ready < 0) 428   210590 if (ready < 0)
429   { 429   {
HITCBC 430   3 if (errno != EINTR && errno != EBADF) 430   3 if (errno != EINTR && errno != EBADF)
HITCBC 431   1 detail::throw_system_error(make_err(errno), "select"); 431   1 detail::throw_system_error(make_err(errno), "select");
HITCBC 432   2 ready = 0; 432   2 ready = 0;
433   } 433   }
434   434  
435   // Process timers outside the lock 435   // Process timers outside the lock
HITCBC 436   140770 timer_svc_->process_expired(); 436   210589 timer_svc_->process_expired();
437   437  
HITCBC 438   140770 ready_queue local_ops; 438   210589 ready_queue local_ops;
439   439  
HITCBC 440   140770 if (ready > 0) 440   210589 if (ready > 0)
441   { 441   {
HITCBC 442   14761 if (FD_ISSET(pipe_fds_[0], &read_fds)) 442   81941 if (FD_ISSET(pipe_fds_[0], &read_fds))
443   { 443   {
444   char buf[256]; 444   char buf[256];
HITCBC 445   22673 while (::read(pipe_fds_[0], buf, sizeof(buf)) > 0) 445   22377 while (::read(pipe_fds_[0], buf, sizeof(buf)) > 0)
446   { 446   {
447   } 447   }
448   } 448   }
449   449  
HITCBC 450   97607 for (int i = 0; i < snapshot_count; ++i) 450   236601 for (int i = 0; i < snapshot_count; ++i)
451   { 451   {
HITCBC 452   82846 int fd = snapshot[i].fd; 452   154660 int fd = snapshot[i].fd;
HITCBC 453   82846 reactor_descriptor_state* desc = snapshot[i].desc; 453   154660 reactor_descriptor_state* desc = snapshot[i].desc;
454   454  
HITCBC 455   82846 std::uint32_t flags = 0; 455   154660 std::uint32_t flags = 0;
HITCBC 456   82846 if (FD_ISSET(fd, &read_fds)) 456   154660 if (FD_ISSET(fd, &read_fds))
HITCBC 457   6173 flags |= reactor_event_read; 457   73655 flags |= reactor_event_read;
HITCBC 458   82846 if (FD_ISSET(fd, &write_fds)) 458   154660 if (FD_ISSET(fd, &write_fds))
HITCBC 459   2724 flags |= reactor_event_write; 459   2748 flags |= reactor_event_write;
HITCBC 460   82846 if (FD_ISSET(fd, &except_fds)) 460   154660 if (FD_ISSET(fd, &except_fds))
HITCBC 461   9 flags |= reactor_event_error; 461   13 flags |= reactor_event_error;
462   462  
HITCBC 463   82846 if (flags == 0) 463   154660 if (flags == 0)
HITCBC 464   73947 continue; 464   78255 continue;
465   465  
HITCBC 466   8899 desc->add_ready_events(flags); 466   76405 desc->add_ready_events(flags);
467   467  
HITCBC 468   8899 bool expected = false; 468   76405 bool expected = false;
HITCBC 469   8899 if (desc->is_enqueued_.compare_exchange_strong( 469   76405 if (desc->is_enqueued_.compare_exchange_strong(
470   expected, true, std::memory_order_release, 470   expected, true, std::memory_order_release,
471   std::memory_order_relaxed)) 471   std::memory_order_relaxed))
472   { 472   {
HITCBC 473   6271 local_ops.push(desc); 473   6356 local_ops.push(desc);
474   } 474   }
475   } 475   }
476   } 476   }
477   477  
HITCBC 478   140770 lock.lock(); 478   210589 lock.lock();
479   479  
HITCBC 480   140770 completed_ops_.splice(local_ops); 480   210589 completed_ops_.splice(local_ops);
HITCBC 481   140771 } 481   210590 }
482   482  
483   } // namespace boost::corosio::detail 483   } // namespace boost::corosio::detail
484   484  
485   #endif // BOOST_COROSIO_HAS_SELECT 485   #endif // BOOST_COROSIO_HAS_SELECT
486   486  
487   #endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SCHEDULER_HPP 487   #endif // BOOST_COROSIO_NATIVE_DETAIL_SELECT_SELECT_SCHEDULER_HPP