1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! Producer-side wake gate shared by the send paths and both endpoint drops.
use Mutex;
use ;
use Waker;
use crateCHANNEL_STORE_LOAD_ORDER;
/// Wake every parked thread and registered async waker after a publication.
///
/// The caller has just published state consumers must observe — a produced
/// element or the `closed` flag — and gates the wake on counters that are read
/// *outside* the registration locks. Counter registration and the publication
/// live in different locations, so the two sides form a store-buffer (Dekker)
/// pair:
///
/// | | consumer (registration) | producer (this gate) |
/// |---|---|---|
/// | first | `parked_count` / `waker_count` increment | publish (produce / close) |
/// | then | ring / `closed` re-check | counter gate load below |
///
/// Without a `StoreLoad` barrier on both sides, both "then" loads may observe
/// stale values at once: this gate reads a zero counter and skips the wake
/// while the consumer's re-check misses the publication and it parks with the
/// message already delivered — the last-message hang. `moirai-sync`'s
/// `FutexMutex` (`lock_slow`/`unlock` in `src/sync/futex_mutex.rs`) documents
/// and fences this exact pattern on its `waiters`/`locked` pair; the
/// the sequentially-consistent fence here follows it and pairs with the fence
/// each registration site executes between its counter increment and re-check.
///
/// Wakers are drained under the registry lock but woken after it is released:
/// `Waker::wake` may execute the woken task inline on this thread (the
/// executor polls inline under injector saturation), and that poll may
/// re-enter this channel's registration lock — waking under the lock would
/// self-deadlock. Unparking follows the same shape so both critical sections
/// stay minimal.
///
/// Every produce takes this gate, never only the one that found the ring
/// empty: the producer reads the consumer cursor before it publishes, and the
/// consumer can drain the items ahead of the produce in that window, find the
/// new slot unpublished, register, and park. Occupancy observed before the
/// publication therefore cannot excuse the barrier or the wake — the same
/// interleaving `tests/loom_mpmc_waiter.rs`
/// (`occupancy_read_before_publish_loses_the_wakeup`) enumerates for the
/// bounded MPMC notifier.
pub