include/boost/corosio/native/native_io_context.hpp

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