100.00% Lines (45/45) 100.00% Functions (12/12)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Steve Gerbino 2   // Copyright (c) 2026 Steve Gerbino
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 4   // Distributed under the Boost Software License, Version 1.0. (See accompanying
5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/corosio 7   // Official repository: https://github.com/cppalliance/corosio
8   // 8   //
9   9  
10   #ifndef BOOST_COROSIO_NATIVE_NATIVE_IO_CONTEXT_HPP 10   #ifndef BOOST_COROSIO_NATIVE_NATIVE_IO_CONTEXT_HPP
11   #define BOOST_COROSIO_NATIVE_NATIVE_IO_CONTEXT_HPP 11   #define BOOST_COROSIO_NATIVE_NATIVE_IO_CONTEXT_HPP
12   12  
13   #include <boost/corosio/io_context.hpp> 13   #include <boost/corosio/io_context.hpp>
14   #include <boost/corosio/backend.hpp> 14   #include <boost/corosio/backend.hpp>
15   15  
16   #ifndef BOOST_COROSIO_MRDOCS 16   #ifndef BOOST_COROSIO_MRDOCS
17   #if BOOST_COROSIO_HAS_EPOLL 17   #if BOOST_COROSIO_HAS_EPOLL
18   #include <boost/corosio/native/detail/epoll/epoll_scheduler.hpp> 18   #include <boost/corosio/native/detail/epoll/epoll_scheduler.hpp>
19   #endif 19   #endif
20   20  
21   #if BOOST_COROSIO_HAS_SELECT 21   #if BOOST_COROSIO_HAS_SELECT
22   #include <boost/corosio/native/detail/select/select_scheduler.hpp> 22   #include <boost/corosio/native/detail/select/select_scheduler.hpp>
23   #endif 23   #endif
24   24  
25   #if BOOST_COROSIO_HAS_KQUEUE 25   #if BOOST_COROSIO_HAS_KQUEUE
26   #include <boost/corosio/native/detail/kqueue/kqueue_scheduler.hpp> 26   #include <boost/corosio/native/detail/kqueue/kqueue_scheduler.hpp>
27   #endif 27   #endif
28   28  
29   #if BOOST_COROSIO_HAS_IOCP 29   #if BOOST_COROSIO_HAS_IOCP
30   #include <boost/corosio/native/detail/iocp/win_scheduler.hpp> 30   #include <boost/corosio/native/detail/iocp/win_scheduler.hpp>
31   #endif 31   #endif
  32 +
  33 + #if BOOST_COROSIO_HAS_IO_URING
  34 + #include <boost/corosio/native/detail/io_uring/io_uring_scheduler.hpp>
  35 + #endif
32   #endif // !BOOST_COROSIO_MRDOCS 36   #endif // !BOOST_COROSIO_MRDOCS
33   37  
34   namespace boost::corosio { 38   namespace boost::corosio {
35   39  
36   /** An I/O context with devirtualized event loop methods. 40   /** An I/O context with devirtualized event loop methods.
37   41  
38   This class template inherits from @ref io_context and shadows 42   This class template inherits from @ref io_context and shadows
39   all public methods with versions that call the concrete 43   all public methods with versions that call the concrete
40   scheduler directly, bypassing virtual dispatch. No new state 44   scheduler directly, bypassing virtual dispatch. No new state
41   is added. 45   is added.
42   46  
43   A `native_io_context` IS-A `io_context` and can be passed 47   A `native_io_context` IS-A `io_context` and can be passed
44   anywhere an `io_context&` is accepted, in which case virtual 48   anywhere an `io_context&` is accepted, in which case virtual
45   dispatch is used transparently. 49   dispatch is used transparently.
46   50  
47   @tparam Backend A backend tag value (e.g., `epoll`, 51   @tparam Backend A backend tag value (e.g., `epoll`,
48   `iocp`) whose type provides `scheduler_type`. 52   `iocp`) whose type provides `scheduler_type`.
49   53  
50   @par Thread Safety 54   @par Thread Safety
51   Same as the underlying context type. 55   Same as the underlying context type.
52   56  
53   @par Example 57   @par Example
54   @code 58   @code
55   #include <boost/corosio/native/native_io_context.hpp> 59   #include <boost/corosio/native/native_io_context.hpp>
56   60  
57   native_io_context<epoll> ctx; 61   native_io_context<epoll> ctx;
58   ctx.poll(); // devirtualized call 62   ctx.poll(); // devirtualized call
59   @endcode 63   @endcode
60   64  
61   @see io_context, epoll_t, iocp_t 65   @see io_context, epoll_t, iocp_t
62   */ 66   */
63   template<auto Backend> 67   template<auto Backend>
64   class native_io_context : public io_context 68   class native_io_context : public io_context
65   { 69   {
66   using backend_type = decltype(Backend); 70   using backend_type = decltype(Backend);
67   using scheduler_type = typename backend_type::scheduler_type; 71   using scheduler_type = typename backend_type::scheduler_type;
68   72  
HITCBC 69   42 scheduler_type& sched() noexcept 73   42 scheduler_type& sched() noexcept
70   { 74   {
HITCBC 71   42 return *static_cast<scheduler_type*>(this->sched_); 75   42 return *static_cast<scheduler_type*>(this->sched_);
72   } 76   }
73   77  
74   public: 78   public:
75   /** Construct with default concurrency. */ 79   /** Construct with default concurrency. */
HITCBC 76   18 native_io_context() : io_context(Backend) {} 80   18 native_io_context() : io_context(Backend) {}
77   81  
78   /** Construct with a concurrency hint. 82   /** Construct with a concurrency hint.
79   83  
80   @param concurrency_hint Hint for the number of threads that 84   @param concurrency_hint Hint for the number of threads that
81   will call `run()`. 85   will call `run()`.
82   */ 86   */
HITCBC 83   2 explicit native_io_context(unsigned concurrency_hint) 87   2 explicit native_io_context(unsigned concurrency_hint)
HITCBC 84   2 : io_context(Backend, concurrency_hint) 88   2 : io_context(Backend, concurrency_hint)
85   { 89   {
HITCBC 86   2 } 90   2 }
87   91  
88   /** Construct with runtime tuning options. 92   /** Construct with runtime tuning options.
89   93  
90   @param opts Runtime options controlling scheduler and 94   @param opts Runtime options controlling scheduler and
91   service behavior. 95   service behavior.
92   @param concurrency_hint Hint for the number of threads that 96   @param concurrency_hint Hint for the number of threads that
93   will call `run()`. 97   will call `run()`.
94   */ 98   */
HITCBC 95   2 explicit native_io_context( 99   2 explicit native_io_context(
96   io_context_options const& opts, 100   io_context_options const& opts,
97   unsigned concurrency_hint = std::thread::hardware_concurrency()) 101   unsigned concurrency_hint = std::thread::hardware_concurrency())
HITCBC 98   2 : io_context(Backend, opts, concurrency_hint) 102   2 : io_context(Backend, opts, concurrency_hint)
99   { 103   {
HITCBC 100   2 } 104   2 }
101   105  
102   // Non-copyable, non-movable 106   // Non-copyable, non-movable
103   native_io_context(native_io_context const&) = delete; 107   native_io_context(native_io_context const&) = delete;
104   native_io_context& operator=(native_io_context const&) = delete; 108   native_io_context& operator=(native_io_context const&) = delete;
105   109  
106   /// Signal the context to stop processing. 110   /// Signal the context to stop processing.
HITCBC 107   2 void stop() 111   2 void stop()
108   { 112   {
HITCBC 109   2 sched().stop(); 113   2 sched().stop();
HITCBC 110   2 } 114   2 }
111   115  
112   /// Return whether the context has been stopped. 116   /// Return whether the context has been stopped.
HITCBC 113   22 bool stopped() const noexcept 117   22 bool stopped() const noexcept
114   { 118   {
HITCBC 115   22 return const_cast<native_io_context*>(this)->sched().stopped(); 119   22 return const_cast<native_io_context*>(this)->sched().stopped();
116   } 120   }
117   121  
118   /// Restart the context after being stopped. 122   /// Restart the context after being stopped.
HITCBC 119   2 void restart() 123   2 void restart()
120   { 124   {
HITCBC 121   2 sched().restart(); 125   2 sched().restart();
HITCBC 122   2 } 126   2 }
123   127  
124   /** Process all pending work items. 128   /** Process all pending work items.
125   129  
126   @return The number of handlers executed. 130   @return The number of handlers executed.
127   */ 131   */
HITCBC 128   2 std::size_t run() 132   2 std::size_t run()
129   { 133   {
HITCBC 130   2 return sched().run(); 134   2 return sched().run();
131   } 135   }
132   136  
133   /** Process at most one pending work item. 137   /** Process at most one pending work item.
134   138  
135   @return The number of handlers executed (0 or 1). 139   @return The number of handlers executed (0 or 1).
136   */ 140   */
137   std::size_t run_one() 141   std::size_t run_one()
138   { 142   {
139   return sched().run_one(); 143   return sched().run_one();
140   } 144   }
141   145  
142   /** Process work items for the specified duration. 146   /** Process work items for the specified duration.
143   147  
144   @param rel_time The duration for which to process work. 148   @param rel_time The duration for which to process work.
145   149  
146   @return The number of handlers executed. 150   @return The number of handlers executed.
147   */ 151   */
148   template<class Rep, class Period> 152   template<class Rep, class Period>
HITCBC 149   4 std::size_t run_for(std::chrono::duration<Rep, Period> const& rel_time) 153   4 std::size_t run_for(std::chrono::duration<Rep, Period> const& rel_time)
150   { 154   {
HITCBC 151   4 return run_until(std::chrono::steady_clock::now() + rel_time); 155   4 return run_until(std::chrono::steady_clock::now() + rel_time);
152   } 156   }
153   157  
154   /** Process work items until the specified time. 158   /** Process work items until the specified time.
155   159  
156   @param abs_time The time point until which to process work. 160   @param abs_time The time point until which to process work.
157   161  
158   @return The number of handlers executed. 162   @return The number of handlers executed.
159   */ 163   */
160   template<class Clock, class Duration> 164   template<class Clock, class Duration>
161   std::size_t 165   std::size_t
HITCBC 162   4 run_until(std::chrono::time_point<Clock, Duration> const& abs_time) 166   4 run_until(std::chrono::time_point<Clock, Duration> const& abs_time)
163   { 167   {
HITCBC 164   4 std::size_t n = 0; 168   4 std::size_t n = 0;
HITCBC 165   6 while (run_one_until(abs_time)) 169   6 while (run_one_until(abs_time))
HITCBC 166   2 if (n != (std::numeric_limits<std::size_t>::max)()) 170   2 if (n != (std::numeric_limits<std::size_t>::max)())
HITCBC 167   2 ++n; 171   2 ++n;
HITCBC 168   4 return n; 172   4 return n;
169   } 173   }
170   174  
171   /** Process at most one work item for the specified duration. 175   /** Process at most one work item for the specified duration.
172   176  
173   @param rel_time The duration for which the call may block. 177   @param rel_time The duration for which the call may block.
174   178  
175   @return The number of handlers executed (0 or 1). 179   @return The number of handlers executed (0 or 1).
176   */ 180   */
177   template<class Rep, class Period> 181   template<class Rep, class Period>
178   std::size_t run_one_for(std::chrono::duration<Rep, Period> const& rel_time) 182   std::size_t run_one_for(std::chrono::duration<Rep, Period> const& rel_time)
179   { 183   {
180   return run_one_until(std::chrono::steady_clock::now() + rel_time); 184   return run_one_until(std::chrono::steady_clock::now() + rel_time);
181   } 185   }
182   186  
183   /** Process at most one work item until the specified time. 187   /** Process at most one work item until the specified time.
184   188  
185   @param abs_time The time point until which the call may block. 189   @param abs_time The time point until which the call may block.
186   190  
187   @return The number of handlers executed (0 or 1). 191   @return The number of handlers executed (0 or 1).
188   */ 192   */
189   template<class Clock, class Duration> 193   template<class Clock, class Duration>
190   std::size_t 194   std::size_t
HITCBC 191   10 run_one_until(std::chrono::time_point<Clock, Duration> const& abs_time) 195   10 run_one_until(std::chrono::time_point<Clock, Duration> const& abs_time)
192   { 196   {
HITCBC 193   10 typename Clock::time_point now = Clock::now(); 197   10 typename Clock::time_point now = Clock::now();
HITCBC 194   2 for (;;) 198   2 for (;;)
195   { 199   {
HITCBC 196   12 auto rel_time = abs_time - now; 200   12 auto rel_time = abs_time - now;
197   using rel_type = decltype(rel_time); 201   using rel_type = decltype(rel_time);
HITCBC 198   12 if (rel_time < rel_type::zero()) 202   12 if (rel_time < rel_type::zero())
HITCBC 199   2 rel_time = rel_type::zero(); 203   2 rel_time = rel_type::zero();
HITCBC 200   10 else if (rel_time > std::chrono::seconds(1)) 204   10 else if (rel_time > std::chrono::seconds(1))
HITCBC 201   2 rel_time = std::chrono::seconds(1); 205   2 rel_time = std::chrono::seconds(1);
202   206  
HITCBC 203   24 std::size_t s = sched().wait_one( 207   24 std::size_t s = sched().wait_one(
204   static_cast<long>( 208   static_cast<long>(
HITCBC 205   12 std::chrono::duration_cast<std::chrono::microseconds>( 209   12 std::chrono::duration_cast<std::chrono::microseconds>(
206   rel_time) 210   rel_time)
HITCBC 207   12 .count())); 211   12 .count()));
208   212  
HITCBC 209   12 if (s || stopped()) 213   12 if (s || stopped())
HITCBC 210   10 return s; 214   10 return s;
211   215  
HITCBC 212   4 now = Clock::now(); 216   4 now = Clock::now();
HITCBC 213   4 if (now >= abs_time) 217   4 if (now >= abs_time)
HITCBC 214   2 return 0; 218   2 return 0;
215   } 219   }
216   } 220   }
217   221  
218   /** Process all ready work items without blocking. 222   /** Process all ready work items without blocking.
219   223  
220   @return The number of handlers executed. 224   @return The number of handlers executed.
221   */ 225   */
HITCBC 222   2 std::size_t poll() 226   2 std::size_t poll()
223   { 227   {
HITCBC 224   2 return sched().poll(); 228   2 return sched().poll();
225   } 229   }
226   230  
227   /** Process at most one ready work item without blocking. 231   /** Process at most one ready work item without blocking.
228   232  
229   @return The number of handlers executed (0 or 1). 233   @return The number of handlers executed (0 or 1).
230   */ 234   */
231   std::size_t poll_one() 235   std::size_t poll_one()
232   { 236   {
233   return sched().poll_one(); 237   return sched().poll_one();
234   } 238   }
235   }; 239   };
236   240  
237   } // namespace boost::corosio 241   } // namespace boost::corosio
238   242  
239   #endif // BOOST_COROSIO_NATIVE_NATIVE_IO_CONTEXT_HPP 243   #endif // BOOST_COROSIO_NATIVE_NATIVE_IO_CONTEXT_HPP