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