Skip to content

fix(io_context): decouple concurrency_hint == 1 from lockless mode (#…#316

Merged
mvandeberg merged 1 commit into
cppalliance:develop-2from
mvandeberg:pr/310-fix-concurrency-hint
Jul 15, 2026
Merged

fix(io_context): decouple concurrency_hint == 1 from lockless mode (#…#316
mvandeberg merged 1 commit into
cppalliance:develop-2from
mvandeberg:pr/310-fix-concurrency-hint

Conversation

@mvandeberg

@mvandeberg mvandeberg commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

…310)

A concurrency_hint of 1 silently enabled full single-threaded lockless mode: scheduler mutex/condvar, per-descriptor locks, IOCP dispatch mutex, and io_uring ring/dispatch mutexes were all disabled, plus io_uring got SINGLE_ISSUER/DEFER_TASKRUN. This made cross-thread post() undefined behavior, contrary to asio (where a plain integer hint never disables locking) and to what migrating users expect.

A hint now only tunes performance and never changes the safety contract:

  • Remove the three hint == 1 -> single_threaded coupling sites (backend-tag template ctor, io_context(unsigned) ctor, and normalize_options, which is deleted along with the now-dead configure_single_threaded_() member).
  • Rename the public opt-in io_context_options::single_threaded -> single_threaded_lockless so the lockless mode is loud and named, mirroring asio's ASIO_CONCURRENCY_HINT_UNSAFE convention. It remains the sole way to enable lockless mode (still gating the io_uring SINGLE_ISSUER flags).
  • Default ctor clamp max(2u, hardware_concurrency()) -> max(1u, ...): the "2" floor existed only to dodge the removed trap; floor at 1 still guards a 0 return.

Tests: the resolver/stream_file single-threaded restriction tests switch to explicit opts.single_threaded_lockless; new testHintOneIsThreadSafe proves cross-thread post() into a hint == 1 context is TSan-clean across all backends (verified: fails under TSan with the coupling restored).

Docs: header Doxygen (option, Thread Safety, default ctor) plus the io-context and configuration guide pages updated.

Fixes #310

@cppalliance-bot

cppalliance-bot commented Jul 10, 2026

Copy link
Copy Markdown

An automated preview of the documentation is available at https://316.corosio.prtest3.cppalliance.org/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-07-15 15:42:05 UTC

@mvandeberg
mvandeberg force-pushed the pr/310-fix-concurrency-hint branch 2 times, most recently from 88bcce9 to 9141d96 Compare July 10, 2026 22:48
@mvandeberg
mvandeberg marked this pull request as ready for review July 13, 2026 16:33
@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.89%. Comparing base (19d76f3) to head (9141d96).
⚠️ Report is 1 commits behind head on develop-2.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           develop-2     #316   +/-   ##
==========================================
  Coverage      77.89%   77.89%           
==========================================
  Files             96       96           
  Lines           7139     7139           
  Branches        1744     1744           
==========================================
  Hits            5561     5561           
  Misses          1076     1076           
  Partials         502      502           
Files with missing lines Coverage Δ
include/boost/corosio/io_context.hpp 96.87% <ø> (ø)
src/corosio/src/io_context.cpp 95.83% <ø> (ø)

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 19d76f3...9141d96. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mvandeberg
mvandeberg force-pushed the pr/310-fix-concurrency-hint branch 2 times, most recently from 65d8102 to 470dbfb Compare July 15, 2026 15:07
…er forces lockless mode (cppalliance#310)

`concurrency_hint` is now purely a performance hint (how many threads are
expected to call `run()`); it never changes the thread-safety contract. In
particular `concurrency_hint == 1` keeps full thread safety and full
multi-thread utilization.

Thread safety is governed by a new `io_context_options::locking` tier
(`locking_mode` enum), replacing the `single_threaded` bool and mirroring
Boost.Asio's SAFE / UNSAFE_IO / UNSAFE:

- `safe` (default): all locks enabled; any thread may run and post.
- `unsafe_io`: per-descriptor I/O locks off, scheduler locking kept; the
  context must be single-threaded, but resolver and POSIX file services
  remain available.
- `unsafe`: all locks off; single-threaded, and DNS/POSIX file I/O return
  `operation_not_supported`.

Implementation:

- Scheduler threading is configured through a decomposed `threading_config`
  (scheduler_locking / reactor_io_locking / one_thread), replacing the
  coarse `configure_single_threaded` / `is_single_threaded`. The reactor,
  io_uring, and IOCP schedulers honor it.
- `one_thread` engages a single-run-thread fast path (eliding inter-run-thread
  wakeups); it is set only for the lockless tiers, so the safe tier retains
  full multi-thread utilization at any hint.
- The effective concurrency hint is normalized to 1 for the lockless tiers,
  driving the reactor inline-completion budget heuristic and, on IOCP,
  `NumberOfConcurrentThreads`.
- Resolver and POSIX file services gate `operation_not_supported` on
  scheduler locking being disabled (the `unsafe` tier only), so they stay
  available under `unsafe_io`.
- io_uring `SINGLE_ISSUER`/`DEFER_TASKRUN` and the eventfd wake elision are
  keyed on the ring being a single user (the lockless tiers).
- The default constructor uses `max(1u, hardware_concurrency())`.

Tests cover the tiers (unsafe_io keeps services available; unsafe restricts
them), hint==1 thread safety and peer utilization across multiple run
threads, and effective-hint normalization. Benchmarks, guide pages, and
docstrings are updated for the new option.

Fixes cppalliance#310.
@mvandeberg
mvandeberg force-pushed the pr/310-fix-concurrency-hint branch from 470dbfb to 5194a70 Compare July 15, 2026 15:35
@mvandeberg
mvandeberg merged commit cb5cf39 into cppalliance:develop-2 Jul 15, 2026
110 of 118 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Beast2 Jul 15, 2026
@mvandeberg
mvandeberg deleted the pr/310-fix-concurrency-hint branch July 15, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants