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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! [`AtomicSlot<T>`] — the crate's single confined `unsafe` organ (Phase 3b-II).
//!
//! This is the **only** module in the whole crate with
//! `#![allow(unsafe_code)]`. The crate is `#![forbid(unsafe_code)]` everywhere
//! else, so the structural promise "the `unsafe` is one module" is
//! **compiler-checked**, not asserted in prose.
//!
//! [`AtomicSlot<T>`] hides ALL pointer/`unsafe` work behind a minimal, total,
//! safe-to-use API. [`EpochRegion`](crate::concurrent::EpochRegion) is then
//! written in 100% safe code on top of it. Every `unsafe` block below carries a
//! `// SAFETY:` comment naming the invariant it relies on — no exceptions.
//!
//! ## Design
//!
//! A slot is a `(generation, value)` pair where `value` is a
//! `crossbeam_epoch::Atomic<T>` (null = vacant). Readers and writers coordinate
//! via a publication protocol:
//!
//! - A **writer** (holding the writer mutex) calls [`AtomicSlot::install`] to
//! publish a value (Release store) and returns the *current* generation. **Phase
//! 7b:** ANY thread (owner or remote) may call
//! [`AtomicSlot::try_evict_at`] to perform a generation-CAS-checked eviction
//! — the CAS is the single linearization point that prevents the
//! lost-live-value hazard (see the method's SAFETY proof).
//! - A **reader** calls [`AtomicSlot::read_with`] with the generation baked into
//! its handle: it loads the generation (Acquire), compares, then loads the
//! value pointer (Acquire) under a pinned epoch `Guard`. The generation/value
//! ordering guarantees a reader never sees a value belonging to a different
//! generation (no torn generation/value pair, no ABA).
//!
//! Reclamation is delegated to `crossbeam-epoch`: an evicted pointer is
//! scheduled for destruction via `guard.defer_destroy` and is freed only once
//! every reader that could still be holding it has unpinned. Readers therefore
//! dereference a pointer that is provably alive for the duration of their
//! pinned guard.
// The crate is `#![deny(unsafe_code)]` with `experimental` on (see
// `src/lib.rs`); this is the ONE documented exception: the confined `Hand`
// organ. `allow` lifts the crate-level `deny` for this file only, so the
// confinement is enforced structurally by the compiler — `unsafe` anywhere
// else is a hard error. (With no features the crate is `forbid` and this
// module is not compiled at all.)
use Ordering;
use ;
/// The generation a vacant slot starts at, and the lowest generation a handle
/// may carry. Install leaves the generation unchanged; only eviction bumps it.
const INITIAL_GENERATION: u32 = 0;
/// The outcome of a generation-checked eviction ([`AtomicSlot::try_evict_at`]).
///
/// This is the SINGLE linearization point of a removal: the
/// `compare_exchange(expected_gen → next)` on the slot's generation is the
/// atomic step that decides who owns the reclamation. See the `try_evict_at`
/// SAFETY argument for why this rules out the off-mutex "lost-live-value"
/// hazard (a remote remover that checks `generation == handle.gen` then swaps
/// value→null can destroy a NEWER value installed by the owner after an
/// intervening eviction — a use-after-free).
pub
/// A single slot of an [`EpochRegion`](crate::concurrent::EpochRegion): a
/// generation counter plus an epoch-managed value pointer (null = vacant).
///
/// This is the safe-to-use membrane over ALL pointer/`unsafe` work in the
/// crate. It exposes a minimal, total API:
///
/// - [`vacant`](Self::vacant) — construct a vacant slot.
/// - [`generation`](Self::generation) — read the current generation (Acquire).
/// - [`read_with`](Self::read_with) — lock-free read under a pinned guard.
/// - [`install`](Self::install) — writer-only publish (caller holds the writer
/// mutex); requires the slot be vacant.
/// - [`try_evict_at`](Self::try_evict_at) — generation-CAS-checked eviction
/// (Phase 7b); callable by ANY thread (owner or remote). The CAS is the
/// single linearization point of a removal.
///
/// `T` is stored on the heap behind a `crossbeam_epoch::Atomic<T>`; the slot
/// itself is plain data (an atomic `u32` and an atomic-pointer word) and is
/// `Send + Sync` for every `T` (the slot does not own a `T` until `install`,
/// and the pointed-to `T` is reclaimed by the epoch collector, not dropped by
/// the slot).
pub
// Hand-written `Send`/`Sync`: an `AtomicSlot<T>` does not own a `T` while
// vacant, and while occupied the `T` is shared (read-only to readers) and
// reclaimed by the epoch collector (not dropped by the slot). The slot is
// therefore `Send + Sync` for every `T` (matches `crossbeam_epoch::Atomic<T>`,
// which is unconditionally `Send + Sync`).
//
// PHASE 7b RE-AUDIT (relaxed "any thread may evict via try_evict_at" contract):
// pre-7b the ONLY mutator was the single writer holding the region's writer
// mutex. In 7b a REMOTE thread may also call `try_evict_at`, which performs a
// generation CAS + a value swap-to-null + `defer_destroy`. This does NOT
// broaden the aliasing surface: every mutation is still an atomic operation
// (`compare_exchange`, `swap`) on the atomic fields, and the reclamation is
// still routed through `crossbeam_epoch`'s `defer_destroy` (never a raw
// `drop`). The `try_evict_at` SAFETY proof establishes that exactly ONE thread
// can win the generation CAS at a given `expected_gen`, so exactly one thread
// schedules `defer_destroy` for the value published there — no double-free, no
// `&mut` racing a reader's `&`. The invariants below are therefore unchanged
// in substance; only the "single writer" framing widens to "the unique CAS
// winner among all evicting threads".
// SAFETY (Send): an `AtomicSlot<T>` may hold a heap `T` behind its `Atomic<T>`,
// so sending it to another thread sends that `T` — hence the `T: Send` bound.
// `T: Sync` is also required because readers on multiple threads share `&T`
// concurrently (see the Sync impl). With both bounds the impl matches exactly
// what `crossbeam_epoch::Atomic<T>: Send` requires, and is what the compiler
// would auto-derive — stated explicitly here for clarity at the unsafe seam.
// An UNBOUNDED impl would be unsound (it would let a non-`Send` `T`, e.g.
// `Rc`, cross threads).
unsafe
// SAFETY (Sync): `&AtomicSlot<T>` grants shared access to the generation
// counter (atomic) and the `Atomic<T>` pointer (lock-free under a guard).
// Readers run `read_with` concurrently, each obtaining a shared `&T` to the
// same value — sound only when `T: Sync`. Evicting threads (owner OR remote in
// 7b) mutate ONLY via atomic operations (`compare_exchange`, `swap`); no `&mut
// T` ever coexists with a reader `&T` (the pointer is swapped to null before
// `defer_destroy` is scheduled). `T: Send` is required because a value
// installed on one thread may be dropped (reclaimed) on another. Matches
// `Atomic<T>: Sync`.
unsafe
// `AtomicSlot<T>` has no hand-written `Drop`: its default drop drops the
// `Atomic<T>` handle without touching the pointee (which the `Atomic` does not
// own — the epoch collector does). LIVE values are dropped explicitly by
// [`EpochRegion`](crate::concurrent::EpochRegion)'s `Drop`, which calls
// [`AtomicSlot::drop_value`] on every slot under `&mut` exclusivity (upholding
// I5). Values already `remove`d are reclaimed by `crossbeam-epoch` at an epoch
// boundary; if the process exits before that boundary they may not run their
// destructors — the standard epoch-reclamation caveat, documented on the tier.