Repository navigation
refactor(xpp): replace atomic waker slots with mutex in ScheduledIo - #201
Merged
Merged
Conversation
The previous implementation stored wakers as 4 atomic pointers (Schedule* + SpawnTaskBase* per direction) with manual release/acq_rel ordering. This was a lock-free reimplementation of a mutex but without the actual mutual exclusion — the Waker struct itself was subject to concurrent read/write data races. Replace with a mutex-protected Waiters struct (xpp::sys::Mutex<Waiters>), following Tokio's ScheduledIo design: - Readiness bitmask stays lock-free (atomic<uint8_t>) - Waker slots (reader/writer) behind a single mutex - Double-check-under-lock in poll_read/poll_write prevents lost wakes - wake_read/wake_write take the mutex before firing This eliminates the data race, reduces fields from 10 to 5, and removes 3 static helper functions (set_waker/clear_waker/fire_waker). Also add 15 unit tests covering all public methods: poll_read, poll_write, clear_readable, clear_writable, close_fd, is_closed, accessors, and concurrent poll+event. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
xpp::sys::Mutex<Waiters>, following Tokio's ScheduledIo designpoll_read/poll_writeprevents lost wakesMotivation
The previous
set_waker/fire_wakerhelpers storedWakeras two separate atomic pointers (Schedule*+SpawnTaskBase*) with manual release/acq_rel ordering. This was a lock-free reimplementation of a mutex but without actual mutual exclusion — theWakerstruct was subject to concurrent read/write data races. Tokio uses aMutex<Waiters>for exactly this reason.Test plan
ScheduledIoTestcases: accessors, poll_read (not ready, ready, clear+re-poll, waker wake), poll_write (ready, clear+re-poll, not ready), close_fd (sets closed, idempotent, wakes read/write), concurrent poll+eventctest -R xppin build (C++14)ctest -R xppin build-cxx20 (C++20)🤖 Generated with Claude Code