src/corosio/src/io_context.cpp
100.0% Lines (93/93)
100.0% List of functions (15/15)
73.1% Branches (19/26)
Functions (15)
Function
Calls
Lines
Branches
Blocks
boost::corosio::select_t::construct(boost::capy::execution_context&, unsigned int)
:74
836x
100.0%
–
100.0%
boost::corosio::kqueue_t::construct(boost::capy::execution_context&, unsigned int)
:92
1465x
100.0%
–
100.0%
boost::corosio::(anonymous namespace)::check_options(boost::corosio::io_context_options const&)
:153
37x
100.0%
75.0%
66.0%
boost::corosio::(anonymous namespace)::create_thread_pool(boost::capy::execution_context&, boost::corosio::io_context_options const&)
:174
2291x
100.0%
–
100.0%
boost::corosio::(anonymous namespace)::make_threading_config(boost::corosio::io_context_options const&)
:190
2291x
100.0%
–
100.0%
boost::corosio::(anonymous namespace)::apply_scheduler_options(boost::corosio::detail::scheduler&, boost::corosio::io_context_options const&, unsigned int)
:204
35x
100.0%
83.3%
91.0%
boost::corosio::(anonymous namespace)::finish_construction(boost::corosio::detail::scheduler&)
:256
2287x
100.0%
–
100.0%
boost::corosio::(anonymous namespace)::construct_default(boost::capy::execution_context&, unsigned int)
:265
631x
100.0%
–
100.0%
boost::corosio::io_context::io_context()
:280
614x
100.0%
–
100.0%
boost::corosio::io_context::io_context(unsigned int)
:285
1244x
100.0%
50.0%
100.0%
boost::corosio::io_context::io_context(boost::corosio::io_context_options const&, unsigned int)
:294
22x
100.0%
66.7%
100.0%
boost::corosio::io_context::apply_options_pre_(boost::corosio::io_context_options const&)
:309
37x
100.0%
–
100.0%
boost::corosio::io_context::apply_options_post_(boost::corosio::io_context_options const&, unsigned int)
:315
35x
100.0%
–
100.0%
boost::corosio::io_context::apply_threading_(boost::corosio::io_context_options const&)
:324
2256x
100.0%
–
100.0%
boost::corosio::io_context::~io_context()
:331
4392x
100.0%
–
100.0%
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2026 Steve Gerbino | |||
| 3 | // Copyright (c) 2026 Michael Vandeberg | |||
| 4 | // | |||
| 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) | |||
| 7 | // | |||
| 8 | // Official repository: https://github.com/cppalliance/corosio | |||
| 9 | // | |||
| 10 | ||||
| 11 | #include <boost/corosio/io_context.hpp> | |||
| 12 | #include <boost/corosio/backend.hpp> | |||
| 13 | #include <boost/corosio/detail/thread_pool.hpp> | |||
| 14 | ||||
| 15 | #include <algorithm> | |||
| 16 | #include <stdexcept> | |||
| 17 | #include <thread> | |||
| 18 | ||||
| 19 | #if BOOST_COROSIO_HAS_EPOLL | |||
| 20 | #include <boost/corosio/native/detail/epoll/epoll_types.hpp> | |||
| 21 | #endif | |||
| 22 | ||||
| 23 | #if BOOST_COROSIO_HAS_SELECT | |||
| 24 | #include <boost/corosio/native/detail/select/select_types.hpp> | |||
| 25 | #endif | |||
| 26 | ||||
| 27 | #if BOOST_COROSIO_HAS_KQUEUE | |||
| 28 | #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp> | |||
| 29 | #endif | |||
| 30 | ||||
| 31 | #if BOOST_COROSIO_HAS_URING | |||
| 32 | #include <boost/corosio/native/detail/uring/uring_acceptor_ops.hpp> | |||
| 33 | #include <boost/corosio/native/detail/uring/uring_buffer.hpp> | |||
| 34 | #include <boost/corosio/native/detail/uring/uring_dgram_ops.hpp> | |||
| 35 | #include <boost/corosio/native/detail/uring/uring_multishot_acceptor.hpp> | |||
| 36 | #include <boost/corosio/native/detail/uring/uring_random_access_file.hpp> | |||
| 37 | #include <boost/corosio/native/detail/uring/uring_scheduler.hpp> | |||
| 38 | #include <boost/corosio/native/detail/uring/uring_stream_file.hpp> | |||
| 39 | #include <boost/corosio/native/detail/uring/uring_types.hpp> | |||
| 40 | #endif | |||
| 41 | ||||
| 42 | #if BOOST_COROSIO_HAS_IOCP | |||
| 43 | #include <boost/corosio/native/detail/iocp/win_scheduler.hpp> | |||
| 44 | #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp> | |||
| 45 | #include <boost/corosio/native/detail/iocp/win_udp_service.hpp> | |||
| 46 | #include <boost/corosio/native/detail/iocp/win_local_stream_acceptor_service.hpp> | |||
| 47 | #include <boost/corosio/native/detail/iocp/win_signals.hpp> | |||
| 48 | #include <boost/corosio/native/detail/iocp/win_file_service.hpp> | |||
| 49 | #include <boost/corosio/native/detail/iocp/win_random_access_file_service.hpp> | |||
| 50 | #endif | |||
| 51 | ||||
| 52 | namespace boost::corosio { | |||
| 53 | ||||
| 54 | #if BOOST_COROSIO_HAS_EPOLL | |||
| 55 | detail::scheduler& | |||
| 56 | epoll_t::construct(capy::execution_context& ctx, unsigned concurrency_hint) | |||
| 57 | { | |||
| 58 | auto& sched = ctx.make_service<detail::epoll_scheduler>( | |||
| 59 | static_cast<int>(concurrency_hint)); | |||
| 60 | ||||
| 61 | ctx.make_service<detail::epoll_tcp_service>(); | |||
| 62 | ctx.make_service<detail::epoll_tcp_acceptor_service>(); | |||
| 63 | ctx.make_service<detail::epoll_udp_service>(); | |||
| 64 | ctx.make_service<detail::epoll_local_stream_service>(); | |||
| 65 | ctx.make_service<detail::epoll_local_stream_acceptor_service>(); | |||
| 66 | ctx.make_service<detail::epoll_local_datagram_service>(); | |||
| 67 | ||||
| 68 | return sched; | |||
| 69 | } | |||
| 70 | #endif | |||
| 71 | ||||
| 72 | #if BOOST_COROSIO_HAS_SELECT | |||
| 73 | detail::scheduler& | |||
| 74 | 836x | select_t::construct(capy::execution_context& ctx, unsigned concurrency_hint) | ||
| 75 | { | |||
| 76 | 1672x | auto& sched = ctx.make_service<detail::select_scheduler>( | ||
| 77 | 836x | static_cast<int>(concurrency_hint)); | ||
| 78 | ||||
| 79 | 836x | ctx.make_service<detail::select_tcp_service>(); | ||
| 80 | 836x | ctx.make_service<detail::select_tcp_acceptor_service>(); | ||
| 81 | 836x | ctx.make_service<detail::select_udp_service>(); | ||
| 82 | 836x | ctx.make_service<detail::select_local_stream_service>(); | ||
| 83 | 836x | ctx.make_service<detail::select_local_stream_acceptor_service>(); | ||
| 84 | 836x | ctx.make_service<detail::select_local_datagram_service>(); | ||
| 85 | ||||
| 86 | 836x | return sched; | ||
| 87 | } | |||
| 88 | #endif | |||
| 89 | ||||
| 90 | #if BOOST_COROSIO_HAS_KQUEUE | |||
| 91 | detail::scheduler& | |||
| 92 | 1465x | kqueue_t::construct(capy::execution_context& ctx, unsigned concurrency_hint) | ||
| 93 | { | |||
| 94 | 2930x | auto& sched = ctx.make_service<detail::kqueue_scheduler>( | ||
| 95 | 1465x | static_cast<int>(concurrency_hint)); | ||
| 96 | ||||
| 97 | 1465x | ctx.make_service<detail::kqueue_tcp_service>(); | ||
| 98 | 1465x | ctx.make_service<detail::kqueue_tcp_acceptor_service>(); | ||
| 99 | 1465x | ctx.make_service<detail::kqueue_udp_service>(); | ||
| 100 | 1465x | ctx.make_service<detail::kqueue_local_stream_service>(); | ||
| 101 | 1465x | ctx.make_service<detail::kqueue_local_stream_acceptor_service>(); | ||
| 102 | 1465x | ctx.make_service<detail::kqueue_local_datagram_service>(); | ||
| 103 | ||||
| 104 | 1465x | return sched; | ||
| 105 | } | |||
| 106 | #endif | |||
| 107 | ||||
| 108 | #if BOOST_COROSIO_HAS_IOCP | |||
| 109 | detail::scheduler& | |||
| 110 | iocp_t::construct(capy::execution_context& ctx, unsigned concurrency_hint) | |||
| 111 | { | |||
| 112 | auto& sched = ctx.make_service<detail::win_scheduler>( | |||
| 113 | static_cast<int>(concurrency_hint)); | |||
| 114 | ||||
| 115 | auto& tcp_svc = ctx.make_service<detail::win_tcp_service>(); | |||
| 116 | ctx.make_service<detail::win_tcp_acceptor_service>(tcp_svc); | |||
| 117 | ctx.make_service<detail::win_udp_service>(); | |||
| 118 | auto& local_svc = | |||
| 119 | ctx.make_service<detail::win_local_stream_service>(tcp_svc); | |||
| 120 | ctx.make_service<detail::win_local_stream_acceptor_service>(local_svc); | |||
| 121 | ctx.make_service<detail::win_signals>(); | |||
| 122 | ctx.make_service<detail::win_file_service>(); | |||
| 123 | ctx.make_service<detail::win_random_access_file_service>(); | |||
| 124 | ||||
| 125 | return sched; | |||
| 126 | } | |||
| 127 | #endif | |||
| 128 | ||||
| 129 | #if BOOST_COROSIO_HAS_URING | |||
| 130 | detail::scheduler& | |||
| 131 | uring_t::construct(capy::execution_context& ctx, unsigned concurrency_hint) | |||
| 132 | { | |||
| 133 | auto& sched = ctx.make_service<detail::uring_scheduler>( | |||
| 134 | static_cast<int>(concurrency_hint)); | |||
| 135 | ||||
| 136 | ctx.make_service<detail::uring_tcp_service>(); | |||
| 137 | ctx.make_service<detail::uring_tcp_acceptor_service>(); | |||
| 138 | ctx.make_service<detail::uring_local_stream_service>(); | |||
| 139 | ctx.make_service<detail::uring_local_stream_acceptor_service>(); | |||
| 140 | ctx.make_service<detail::uring_udp_service>(); | |||
| 141 | ctx.make_service<detail::uring_local_datagram_service>(); | |||
| 142 | ctx.make_service<detail::uring_stream_file_service>(sched); | |||
| 143 | ctx.make_service<detail::uring_random_access_file_service>(sched); | |||
| 144 | ||||
| 145 | return sched; | |||
| 146 | } | |||
| 147 | #endif | |||
| 148 | ||||
| 149 | namespace { | |||
| 150 | ||||
| 151 | // Reject options that construct() would otherwise act on. | |||
| 152 | void | |||
| 153 | 37x | check_options([[maybe_unused]] io_context_options const& opts) | ||
| 154 | { | |||
| 155 | #if BOOST_COROSIO_POSIX | |||
| 156 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 2 times.
|
37x | if (opts.thread_pool_size < 1) | |
| 157 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2x | throw std::invalid_argument("thread_pool_size must be at least 1"); | |
| 158 | #endif | |||
| 159 | 35x | } | ||
| 160 | ||||
| 161 | // Create the shared pool that runs blocking file and DNS work. Runs | |||
| 162 | // after construct() so the pool is newer than the scheduler its work | |||
| 163 | // items post completions to: services shut down newest first, and the | |||
| 164 | // pool must join its workers while that scheduler can still drain what | |||
| 165 | // the last of them posted. Only the service is built here; its workers | |||
| 166 | // wait for a first post, so a context that hands off no blocking work | |||
| 167 | // carries no thread for the pool it holds. | |||
| 168 | // | |||
| 169 | // Every io_context constructor has to reach here, and reach it before | |||
| 170 | // anything can call thread_pool_ref::get(): that is what keeps the | |||
| 171 | // binding from ever constructing a pool on an initiator's thread, and | |||
| 172 | // make_service throws on a duplicate if get() got there first. | |||
| 173 | void | |||
| 174 | 2291x | create_thread_pool( | ||
| 175 | capy::execution_context& ctx, | |||
| 176 | [[maybe_unused]] io_context_options const& opts) | |||
| 177 | { | |||
| 178 | #if BOOST_COROSIO_POSIX | |||
| 179 | 2291x | ctx.make_service<detail::thread_pool>(opts.thread_pool_size); | ||
| 180 | #else | |||
| 181 | // thread_pool_size is a POSIX file-service option; the IOCP | |||
| 182 | // backend uses the pool for DNS alone. | |||
| 183 | ctx.make_service<detail::thread_pool>(); | |||
| 184 | #endif | |||
| 185 | 2291x | } | ||
| 186 | ||||
| 187 | // Map the locking tier to the scheduler's threading facilities. one_thread is | |||
| 188 | // set only for the lockless tiers, where a single run thread is guaranteed. | |||
| 189 | detail::scheduler::threading_config | |||
| 190 | 2291x | make_threading_config(io_context_options const& opts) | ||
| 191 | { | |||
| 192 | 2291x | detail::scheduler::threading_config cfg; | ||
| 193 | 2291x | cfg.scheduler_locking = opts.locking != locking_mode::unsafe; | ||
| 194 | 2291x | cfg.reactor_io_locking = opts.locking == locking_mode::safe; | ||
| 195 | 2291x | cfg.one_thread = opts.locking != locking_mode::safe; | ||
| 196 | 2291x | return cfg; | ||
| 197 | } | |||
| 198 | ||||
| 199 | // Apply runtime tuning after construction. `concurrency_hint` is the effective | |||
| 200 | // hint (normalized to 1 for lockless tiers). Budget heuristic: with default | |||
| 201 | // budgets and hint > 1, disable the inline-completion fast path so multi-thread | |||
| 202 | // runs post everything for cross-thread work-stealing. | |||
| 203 | void | |||
| 204 | 35x | apply_scheduler_options( | ||
| 205 | [[maybe_unused]] detail::scheduler& sched, | |||
| 206 | [[maybe_unused]] io_context_options const& opts, | |||
| 207 | [[maybe_unused]] unsigned concurrency_hint) | |||
| 208 | { | |||
| 209 | 35x | sched.configure_threading(make_threading_config(opts)); | ||
| 210 | ||||
| 211 | #if BOOST_COROSIO_HAS_EPOLL || BOOST_COROSIO_HAS_KQUEUE || \ | |||
| 212 | BOOST_COROSIO_HAS_SELECT | |||
| 213 | // dynamic_cast — when io_uring is also linked, the runtime probe may | |||
| 214 | // have selected uring_scheduler instead of a reactor_scheduler. | |||
| 215 |
2/4✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
|
35x | if (auto* reactor = dynamic_cast<detail::reactor_scheduler*>(&sched)) | |
| 216 | { | |||
| 217 | // Detect "user kept the defaults" by comparing all three to the | |||
| 218 | // io_context-options-defined struct defaults. | |||
| 219 | 35x | io_context_options defaults; | ||
| 220 | 35x | bool budget_at_defaults = | ||
| 221 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 10 times.
|
35x | opts.inline_budget_initial == defaults.inline_budget_initial && | |
| 222 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 16 times.
|
25x | opts.inline_budget_max == defaults.inline_budget_max && | |
| 223 | 16x | opts.unassisted_budget == defaults.unassisted_budget; | ||
| 224 | ||||
| 225 | 35x | unsigned init = opts.inline_budget_initial; | ||
| 226 | 35x | unsigned max = opts.inline_budget_max; | ||
| 227 | 35x | unsigned ua = opts.unassisted_budget; | ||
| 228 | ||||
| 229 |
4/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 4 times.
|
35x | if (budget_at_defaults && concurrency_hint > 1) | |
| 230 | { | |||
| 231 | // Multi-thread default: disable budget (post-everything). | |||
| 232 | 4x | init = 0; | ||
| 233 | 4x | max = 0; | ||
| 234 | 4x | ua = 0; | ||
| 235 | 4x | } | ||
| 236 | ||||
| 237 | 35x | reactor->configure_reactor(opts.max_events_per_poll, init, max, ua); | ||
| 238 | 35x | } | ||
| 239 | #endif | |||
| 240 | ||||
| 241 | #if BOOST_COROSIO_HAS_URING | |||
| 242 | if (auto* uring_sched = dynamic_cast<detail::uring_scheduler*>(&sched)) | |||
| 243 | { | |||
| 244 | if (opts.enable_sqpoll) | |||
| 245 | uring_sched->configure_sqpoll( | |||
| 246 | true, opts.sq_thread_idle_ms, opts.sq_thread_cpu); | |||
| 247 | } | |||
| 248 | #endif | |||
| 249 | 35x | } | ||
| 250 | ||||
| 251 | // Bring up backend infrastructure whose setup depends on the options | |||
| 252 | // applied above. Runs last in every constructor: an io_context that | |||
| 253 | // constructs is usable, so a kernel that refuses the infrastructure is | |||
| 254 | // reported from the constructor and not from the first operation. | |||
| 255 | void | |||
| 256 | 2287x | finish_construction([[maybe_unused]] detail::scheduler& sched) | ||
| 257 | { | |||
| 258 | #if BOOST_COROSIO_HAS_URING | |||
| 259 | if (auto* uring_sched = dynamic_cast<detail::uring_scheduler*>(&sched)) | |||
| 260 | uring_sched->init_ring(); | |||
| 261 | #endif | |||
| 262 | 2287x | } | ||
| 263 | ||||
| 264 | detail::scheduler& | |||
| 265 | 631x | construct_default(capy::execution_context& ctx, unsigned concurrency_hint) | ||
| 266 | { | |||
| 267 | #if BOOST_COROSIO_HAS_IOCP | |||
| 268 | return iocp_t::construct(ctx, concurrency_hint); | |||
| 269 | #elif BOOST_COROSIO_HAS_EPOLL | |||
| 270 | return epoll_t::construct(ctx, concurrency_hint); | |||
| 271 | #elif BOOST_COROSIO_HAS_KQUEUE | |||
| 272 | 631x | return kqueue_t::construct(ctx, concurrency_hint); | ||
| 273 | #elif BOOST_COROSIO_HAS_SELECT | |||
| 274 | return select_t::construct(ctx, concurrency_hint); | |||
| 275 | #endif | |||
| 276 | } | |||
| 277 | ||||
| 278 | } // anonymous namespace | |||
| 279 | ||||
| 280 | 614x | io_context::io_context() | ||
| 281 | 614x | : io_context(std::max(1u, std::thread::hardware_concurrency())) | ||
| 282 | { | |||
| 283 | 614x | } | ||
| 284 | ||||
| 285 | 1244x | io_context::io_context(unsigned concurrency_hint) | ||
| 286 | 622x | : capy::execution_context(this) | ||
| 287 |
1/2✓ Branch 0 taken 622 times.
✗ Branch 1 not taken.
|
622x | , sched_(&construct_default(*this, concurrency_hint)) | |
| 288 | 622x | { | ||
| 289 | // Threading config only; the plain path leaves the reactor budget at its | |||
| 290 | // defaults (no options-ctor budget heuristic). | |||
| 291 |
1/2✓ Branch 0 taken 622 times.
✗ Branch 1 not taken.
|
622x | apply_threading_(io_context_options{}); | |
| 292 | 1244x | } | ||
| 293 | ||||
| 294 | 22x | io_context::io_context( | ||
| 295 | io_context_options const& opts_in, unsigned concurrency_hint) | |||
| 296 | 11x | : capy::execution_context(this) | ||
| 297 | 11x | , sched_(nullptr) | ||
| 298 | 11x | { | ||
| 299 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
|
11x | apply_options_pre_(opts_in); | |
| 300 | // Computed before construct_default so IOCP's completion port is created | |||
| 301 | // with the effective concurrency. | |||
| 302 | 9x | unsigned const eff = | ||
| 303 | 9x | detail::effective_concurrency_hint(opts_in, concurrency_hint); | ||
| 304 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9x | sched_ = &construct_default(*this, eff); | |
| 305 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9x | apply_options_post_(opts_in, eff); | |
| 306 | 22x | } | ||
| 307 | ||||
| 308 | void | |||
| 309 | 37x | io_context::apply_options_pre_(io_context_options const& opts) | ||
| 310 | { | |||
| 311 | 37x | check_options(opts); | ||
| 312 | 37x | } | ||
| 313 | ||||
| 314 | void | |||
| 315 | 35x | io_context::apply_options_post_( | ||
| 316 | io_context_options const& opts_in, unsigned concurrency_hint) | |||
| 317 | { | |||
| 318 | 35x | create_thread_pool(*this, opts_in); | ||
| 319 | 35x | apply_scheduler_options(*sched_, opts_in, concurrency_hint); | ||
| 320 | 35x | finish_construction(*sched_); | ||
| 321 | 35x | } | ||
| 322 | ||||
| 323 | void | |||
| 324 | 2256x | io_context::apply_threading_(io_context_options const& opts_in) | ||
| 325 | { | |||
| 326 | 2256x | create_thread_pool(*this, opts_in); | ||
| 327 | 2256x | sched_->configure_threading(make_threading_config(opts_in)); | ||
| 328 | 2256x | finish_construction(*sched_); | ||
| 329 | 2256x | } | ||
| 330 | ||||
| 331 | 4392x | io_context::~io_context() | ||
| 332 | 2105x | { | ||
| 333 | 2287x | shutdown(); | ||
| 334 | 2287x | destroy(); | ||
| 335 | 4392x | } | ||
| 336 | ||||
| 337 | } // namespace boost::corosio | |||
| 338 |