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