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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//! Sharded MPMC queue used by `Threadpool` to deliver `Runnable`s from
//! producers to worker threads.
//!
//! ## Architecture
//!
//! Up to [`MAX_SHARDS`] independent `parking_lot::Mutex<VecDeque<Runnable>>`
//! shards, plus a single shared `Condvar` for parking. Producers pick
//! a shard via the thread-local CPU cache (see [`crate::cpu`]) — a
//! producer running on core N consistently lands on shard `N & mask`,
//! which co-locates closures with the worker pinned to a nearby core
//! and keeps each producer-thread's pushes off the other producers'
//! shards.
//!
//! Each worker has a *preferred* shard equal to `worker_idx & mask`.
//! It pops from its own shard first; if empty, it scans the remaining
//! shards in cyclic order before parking. This is shard-scanning, not
//! work-stealing — there is no separate notion of victim/thief
//! queues and no per-task steal-retry spin.
//!
//! Number of shards is `num_workers.next_power_of_two().min(MAX_SHARDS)`,
//! so:
//!
//! * `workers == 1` → 1 shard, behaves identically to the single-queue
//! design (no scan cost, no extra mutexes).
//! * `workers ∈ {2, 3}` → 2-4 shards.
//! * `workers ≥ 5` → 8 shards (capped).
//!
//! The power-of-two count lets routing use a bitmask instead of a
//! modulo division.
//!
//! ## Lock-ordering and the parked-handshake
//!
//! Producers acquire the target shard's mutex, push, release, *then*
//! check the `parked` atomic. If any worker may be parked, the
//! producer acquires the `park` mutex briefly to call `notify_one`.
//! Workers, when parking, acquire the `park` mutex first, increment
//! `parked` *before* a final re-scan of all shards, then call
//! `cv.wait` (which atomically releases the `park` mutex).
//!
//! That ordering closes the lost-wakeup race two ways:
//!
//! 1. **Producer push before worker arms**: producer's release on the
//! shard mutex happens-before the worker's acquire of that shard
//! in the re-scan. Worker finds the push in the re-scan and
//! doesn't wait.
//! 2. **Producer push after worker arms**: the cross-variable
//! happens-before edge runs through the shard mutex, not directly
//! through the `parked` atomic. Specifically:
//!
//! * worker `parked.fetch_add(1, Release)` is *sequenced-before*
//! its re-scan acquire of `shard[idx]`;
//! * worker's `shard[idx]` unlock *synchronises-with* the
//! producer's later `shard[idx]` lock (mutex pair);
//! * producer's `shard[idx]` unlock is *sequenced-before* its
//! `parked.load(Acquire)`.
//!
//! By transitivity the worker's `fetch_add` happens-before the
//! producer's load, so the load is guaranteed to observe ≥1 and
//! the producer takes the `park.lock` + `notify_one` path. The
//! `Release`/`Acquire` ordering on `parked` itself only governs
//! coherence of the counter's value; the cross-variable visibility
//! that closes the race comes from the shard mutex.
//!
//! The producer's `park.lock` then blocks until the worker's
//! `cv.wait` atomically releases `park.lock` and parks, so the
//! `notify_one` always lands on a worker that is actually waiting.
//!
//! Lock-hold pairs never overlap on the producer side (shard then
//! park, with a release in between), so there is no AB-BA deadlock
//! even though the worker briefly holds `park` while acquiring
//! shards in the re-scan.
use Runnable;
use ;
use VecDeque;
use ;
use cratecpu;
/// Hard cap on the shard count. Picked empirically: 8 reduces
/// producer-side mutex contention to roughly tokio's blocking-pool
/// level on the benches that matter, while keeping the worst-case
/// scan a 64-byte fast loop. Bigger isn't free — every empty pop
/// walks all shards.
const MAX_SHARDS: usize = 8;
/// Shared work queue. `push` notifies one waiter; `pop_blocking`
/// scans then parks until a runnable arrives or shutdown is signalled.
pub