Skip to content

add tmc::asio_safe_socket, tmc::asio_safe_acceptor, tmc::asio_safe_timer - #273

Merged
tzcnt merged 15 commits into
mainfrom
asio_safe
Jul 15, 2026
Merged

tzcnt merged 15 commits into
mainfrom
asio_safe

Conversation

@tzcnt

@tzcnt tzcnt commented Jul 15, 2026 •

Copy link
Copy Markdown
Owner

Asio's objects not being thread-safe is annoying. The typical advice is to use an asio::strand (or tmc::ex_braid) to serialize the socket handlers; however, this serialization locks out other handlers during the entire duration of the handler, even for parts that don't touch the socket.

This PR adds tmc::asio_safe_* wrappers that are much more efficient; they only lock the minimum necessary critical section for each operation. For async operations, this is the operation initiation. For sync operations (e.g. cancel()), it's the whole operation. This makes the objects fully thread-safe, so you can read, write, close, etc. the socket from multiple threads without needing a strand/braid.

Note that this does not alter the other invariants, e.g. max 1 write + 1 read concurrently on a socket.

Every object exposes most of the same methods as the underlying Asio object. However, they are changed slightly:

  • all must be async for the lightweight mutex to work, so a synchronous cancel() becomes an awaitable instead
  • no blocking I/O operations are exposed
  • the tmc::aw_asio completion token is implied

Additionally, the socket doesn't implement the Asio stream concept directly, but you can access the underlying socket if you need to with .inner().

The implementation is quite efficient. I tested asio_http_server (1 I/O thread, 7 CPU threads) which currently does 865k RPS with the unsynchronized implementation. Switching to asio_safe_socket yielded 820k RPS. If we had to use strand/ex_braid here, it would be much more costly.


Implementation Background:

The minimal critical section is the operation initiation (the internal async_initiate() call). After that, it's safe for another thread to initiate another operation. We could shrink the critical section by entering and leaving a strand/braid; however, this causes the the task to enter the braid twice, both at initiation and at resumption:

// enter the braid manually
auto braidScope = co_await tmc::enter(serializer);

// exit the braid after initiation (task becomes suspended, braid may take another waiting task to execute)
co_await socket.async_read_some(bug, tmc::aw_asio);
// re-enter the braid after resumption

// exit the braid manually
co_await std::move(braidScope).exit();

The "re-enter after resumption" behavior occurs because the braid is an executor, and executor affinity causes the aw_asio completion token to send the continuation back to its its originating executor (the braid). So in order to lock only the necessary part - the initiation - without re-locking after resumption, we need a type that behaves like a braid but isn't an executor.

Implementation Details:

That type is tmc::detail::tiny_mutex, specially created for use within these safe wrappers. Its behavior is a bit unintuitive and correct operation is coupled to the awaitables, so it's only used internally. Its implemented as a locked run queue that calls .resume() on each task. When the .resume() call returns (the task is suspended or finished), the next item in the run queue may be executed. To resume a suspended task, its continuation is posted back to its native executor, not the run queue. Under this model, suspend == unlock, so if the task doesn't suspend, it doesn't unlock. So, the unlock is done two ways:

  • awaitables with tmc::aw_asio completion token always suspend (its await_ready returns false)
  • synchronous methods like cancel() don't suspend, so we manually unlock afterward

@tzcnt
tzcnt merged commit 6f18dfd into main Jul 15, 2026
5 of 53 checks passed
@tzcnt
tzcnt deleted the asio_safe branch July 15, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant