Repository navigation
refactor: streamline ManualResetEvent and bounded broadcast waiters - #444
Merged
tisonkun merged 3 commits intoNov 21, 2026
Merged
Conversation
ManualResetEvent previously stored waiters in a doubly-linked WaitList and kept detached nodes alive after set(), forcing every woken or cancelled wait to reacquire the waiter lock solely to free its node.\n\nTrack unset cycles with a u64 generation alongside WakerSet and drain wakers in place before publishing is_set, so released waits complete and drop without locking while event::Waiter moves into auto_reset.
Three bounded broadcast backpressure tests previously polled blocked send futures only with Waker::noop(), verifying that a subsequent poll could publish without checking that reclaiming slots actually woke the parked wakers.\n\nPoll those producers with WakeCounter so missed sender notifications cannot pass silently.
Bounded broadcast backpressure wakes producers to recheck the shared backlog rather than reserving capacity, yet previously tracked parked senders through a separate internal Semaphore, an AtomicUsize upper-bound counter, and two broadcast-gated Semaphore methods.\n\nStore WaitList<RetryWaiter> inside the channel's existing Inner lock, shrinking BoundedSender::send futures by 16 B, eliminating spurious follower wakeups when a cancelled notified send races with a refilled slot, and removing the broadcast-only methods from internal::semaphore.
This was referenced Nov 21, 2026
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
ManualResetEvent's doubly-linkedWaitListwithWakerSetand au64generation counter, draining wakers in place before storingis_set = trueso released waits complete and drop without reacquiring the waiter lock, and foldevent::Waiterintoauto_reset.WakeCounteracross bounded broadcast backpressure tests so missed notifications cannot pass silently.broadcast::mpmc::bounded's senderSemaphoreandAtomicUsizecounter withWaitList<RetryWaiter>inside the channel's existingInnerlock, shrinkingBoundedSender::sendfutures by16 B(72 B -> 56 B), avoiding spurious follower wakeups when a cancelled notified send races with a refilled slot, and removing#[cfg(feature = "broadcast")]methods frominternal::semaphore::Semaphore.Design Notes
ManualResetEvent::setdetaches all registered wakers and advancesgenerationbefore publishingis_set = truewithReleaseordering. AnyWaitfuture observingis_set() == truetherefore has its slot already reclaimed and skips the lock on bothpollandDrop; if aresetlands before the woken task runs,generationstill identifies the completed cycle under the state lock without requiring 64-bit atomics on 32-bit targets.WakerSet::drain_intopopulates a caller-ownedWakerBatchin place, and broadcast send paths skip constructing aWakerBatchwhen no receivers are parked, avoiding544 Bstack moves on uncontended sends andsetcalls.aarch64-apple-darwin, 7 alternating paired runs)608d688)21cbe56)size_of::<ManualResetEvent>()80 B72 B-8 Bsize_of_val(&event.wait_owned())56 B40 B-16 Bsize_of_val(&bounded_tx.send(1usize))72 B56 B-16 Bprimitives::event::wait::wait_already_set/threads=1969 ms798 ms-17.6%primitives::event::wait::reused_waiter_handoff339 ms269 ms-20.6%primitives::event::wait::waiter_fan_out/16210 ms153 ms-27.1%primitives::event::wait::cancel_pending_reused332 ms301 ms-9.3%primitives::event::wait::set_registered/256186 ms132 ms-29.0%primitives::broadcast::mpmc::bounded::try_send_and_try_recv233 ms224 ms-3.9%primitives::broadcast::mpmc::bounded::full_channel_sender_handoff_cycle/4152 ms126 ms-17.1%primitives::broadcast::mpmc::bounded::deliver_to_waiting_receiver288 ms254 ms-11.8%primitives::broadcast::mpmc::bounded::cancel_blocked_send222 ms195 ms-12.2%primitives::broadcast::mpmc::unbounded::deliver_to_waiting_receiver280 ms241 ms-13.9%