concurrent-slotmap 0.2.0-alpha.1

A lock-free concurrent slotmap
Documentation
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//! This module implements the [Hyaline-1 memory reclamation technique].
//!
//! [Hyaline-1 memory reclamation technique]: https://arxiv.org/pdf/1905.07903

use crate::{slot::Vec, Slot, SHARD_COUNT};
use alloc::alloc::{alloc, dealloc, handle_alloc_error, realloc, Layout};
use core::{
    cell::{Cell, UnsafeCell},
    fmt,
    marker::PhantomData,
    mem::{self, ManuallyDrop},
    panic::RefUnwindSafe,
    ptr, slice,
    sync::atomic::{
        self, AtomicPtr, AtomicUsize,
        Ordering::{Acquire, Relaxed, Release, SeqCst},
    },
};
use std::{num::NonZeroUsize, thread};
use thread_local::ThreadLocal;

// SAFETY: `usize` and `*mut Node` have the same layout.
// TODO: Replace with `ptr::without_provanance_mut` once we bump the MSRV.
#[allow(clippy::useless_transmute)]
const INACTIVE: *mut Node = unsafe { mem::transmute(usize::MAX) };

const MIN_RETIRED_LEN: usize = 64;

pub struct CollectorHandle {
    ptr: *mut Collector,
}

// SAFETY: `Collector` is `Send + Sync` and its lifetime is enforced with reference counting.
unsafe impl Send for CollectorHandle {}

// SAFETY: `Collector` is `Send + Sync` and its lifetime is enforced with reference counting.
unsafe impl Sync for CollectorHandle {}

impl Default for CollectorHandle {
    fn default() -> Self {
        Self::new()
    }
}

impl CollectorHandle {
    #[must_use]
    pub fn new() -> Self {
        if SHARD_COUNT.load(Relaxed) == 0 {
            let num_cpus = thread::available_parallelism()
                .map(NonZeroUsize::get)
                .unwrap_or(1);
            SHARD_COUNT.store(num_cpus.next_power_of_two(), Relaxed);
        }

        let ptr = Box::into_raw(Box::new(Collector {
            retirement_lists: ThreadLocal::new(),
            handle_count: AtomicUsize::new(1),
        }));

        // SAFETY: We made sure that initialize the `handle_count` to `1` such that the handle's
        // drop implementation cannot drop the `Collector` while another handle still exists.
        unsafe { CollectorHandle { ptr } }
    }

    /// # Safety
    ///
    /// The returned `Guard` must not outlive any collection that uses this collector. It would
    /// result in the `Guard`'s drop implementation attempting to free slots from the already freed
    /// collection, resulting in a Use-After-Free. You must ensure that the `Guard` has its
    /// lifetime bound to the collections it protects.
    #[inline]
    #[must_use]
    pub unsafe fn pin(&self) -> Guard<'_> {
        let mut is_fresh_entry = false;

        let retirement_list = self.collector().retirement_lists.get_or(|| {
            is_fresh_entry = true;

            crate::set_shard_index();

            RetirementList {
                head: AtomicPtr::new(INACTIVE),
                // SAFETY: The collector cannot be dropped until all handles have been dropped, at
                // which point it is impossible to access this handle.
                collector: ManuallyDrop::new(unsafe { ptr::read(self) }),
                guard_count: Cell::new(0),
                batch: UnsafeCell::new(LocalBatch::new()),
            }
        });

        if is_fresh_entry {
            atomic::fence(SeqCst);
        }

        retirement_list.pin()
    }

    #[inline]
    fn collector(&self) -> &Collector {
        // SAFETY: The pointer is valid.
        unsafe { &*self.ptr }
    }
}

impl Clone for CollectorHandle {
    #[inline]
    fn clone(&self) -> Self {
        #[allow(clippy::cast_sign_loss)]
        if self.collector().handle_count.fetch_add(1, Relaxed) > isize::MAX as usize {
            std::process::abort();
        }

        // SAFETY: We incremented the `handle_count` above such that the handle's drop
        // implementation cannot drop the `Collector` while another handle still exists.
        unsafe { CollectorHandle { ptr: self.ptr } }
    }
}

impl fmt::Debug for CollectorHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CollectorHandle").finish_non_exhaustive()
    }
}

impl PartialEq for CollectorHandle {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.ptr == other.ptr
    }
}

impl Eq for CollectorHandle {}

impl Drop for CollectorHandle {
    #[inline]
    fn drop(&mut self) {
        if self.collector().handle_count.fetch_sub(1, Release) == 1 {
            atomic::fence(Acquire);

            // SAFETY: The handle count has gone to zero, which means that no other threads can
            // register a new handle. The `Acquire` fence above ensures that the drop is
            // synchronized with the above decrement, such that no access to the `Collector` can be
            // ordered after the drop.
            let _ = unsafe { Box::from_raw(self.ptr) };
        }
    }
}

struct Collector {
    /// Per-thread retirement lists.
    retirement_lists: ThreadLocal<RetirementList>,
    /// The number of `CollectorHandle`s that exist.
    handle_count: AtomicUsize,
}

/// The retirement list tracking retired batches.
#[repr(align(128))]
pub(crate) struct RetirementList {
    /// The list of retired batches.
    head: AtomicPtr<Node>,
    /// The parent collector.
    collector: ManuallyDrop<CollectorHandle>,
    /// The number of `Guard`s that exist.
    guard_count: Cell<usize>,
    /// The current batch being prepared.
    batch: UnsafeCell<LocalBatch>,
}

// SAFETY: The cells are never accessed concurrently.
unsafe impl Sync for RetirementList {}

// While `RetirementList` does contain interior mutability, this is never exposed to the user and
// no invariant can be broken.
impl RefUnwindSafe for RetirementList {}

impl RetirementList {
    #[inline]
    pub(crate) fn pin(&self) -> Guard<'_> {
        let guard_count = self.guard_count.get();
        self.guard_count.set(guard_count.checked_add(1).unwrap());

        if guard_count == 0 {
            // SAFETY: The guard count was zero, which means that we couldn't have entered already.
            unsafe { self.enter() };
        }

        // SAFETY:
        // * We incremented the `guard_count` above such that the guard's drop implementation cannot
        //   unpin the participant while another guard still exists.
        // * We made sure to pin the participant if it wasn't already.
        unsafe { Guard::new(self) }
    }

    #[inline]
    unsafe fn enter(&self) {
        self.head.store(ptr::null_mut(), Relaxed);
        atomic::fence(SeqCst);
    }

    #[inline]
    unsafe fn defer_reclaim(
        &self,
        index: u32,
        slots: *const u8,
        reclaim: unsafe fn(u32, *const u8),
    ) {
        // SAFETY: The caller must ensure that this method isn't called concurrently.
        let batch = unsafe { &mut *self.batch.get() };

        // SAFETY: The caller must ensure that `index` is valid and that it is not reachable
        // anymore, that `slots` is a valid pointer to the allocation of `Slot`s, and that `reclaim`
        // is safe to call with the `index` and `slots`.
        unsafe { batch.push(index, slots, reclaim) };

        if batch.len() == MIN_RETIRED_LEN {
            // SAFETY: The caller must ensure that this method isn't called concurrently.
            unsafe { self.retire() };
        }
    }

    #[inline(never)]
    unsafe fn retire(&self) {
        // SAFETY: The caller must ensure that this method isn't called concurrently.
        let batch = unsafe { &mut *self.batch.get() };

        if batch.is_empty() {
            return;
        }

        let mut batch = mem::take(batch);
        let retired_len = batch.len();
        let mut len = 0;

        // SAFETY: `batch.len()` is the number of retired slots in the batch.
        unsafe { batch.set_retired_len(retired_len) };

        atomic::fence(SeqCst);

        for retirement_list in &self.collector.collector().retirement_lists {
            if retirement_list.head.load(Relaxed) == INACTIVE {
                continue;
            }

            if len >= retired_len {
                // SAFETY: This node is never getting retired since it is ouside of `retired_len`.
                unsafe { batch.push(0, ptr::null(), |_, _| {}) };
            }

            // SAFETY: We made sure that the index is in bounds above.
            let node = unsafe { batch.as_mut_slice().get_unchecked_mut(len) };

            node.link.retirement_list = &retirement_list.head;

            len += 1;
        }

        let nodes = batch.as_mut_ptr();
        let batch = batch.into_raw();

        atomic::fence(Acquire);

        #[allow(clippy::mut_range_bound)]
        'outer: for node_index in 0..len {
            // SAFETY: The pointer is valid and the index is in bounds.
            let node = unsafe { nodes.add(node_index) };

            // SAFETY: The pointer is valid.
            unsafe { (*node).batch = batch };

            // SAFETY:
            // * The `node` pointer is valid.
            // * We pushed the `retirement_list` union variant into the batch above.
            // * The `retirement_list` pointer must have staid valid because `ThreadLocal` entries
            //   are never deinitialized.
            let list = unsafe { &*(*node).link.retirement_list };

            let mut head = list.load(Relaxed);

            loop {
                if head == INACTIVE {
                    atomic::fence(Acquire);
                    len -= 1;
                    continue 'outer;
                }

                // SAFETY: The pointer is valid.
                unsafe { (*node).link.next = head };

                match list.compare_exchange_weak(head, node, Release, Relaxed) {
                    Ok(_) => break,
                    Err(new_head) => head = new_head,
                }
            }
        }

        // SAFETY: The pointer is valid.
        if unsafe { (*batch).ref_count.fetch_add(len, Release) }.wrapping_add(len) == 0 {
            // SAFETY: We had the last reference.
            unsafe { self.reclaim(batch) };
        }
    }

    #[inline]
    unsafe fn leave(&self) {
        let head = self.head.swap(INACTIVE, Release);

        if !head.is_null() {
            // SAFETY: The caller must ensure that this method isn't called concurrently and that
            // there are no more references to any retired slots.
            unsafe { self.traverse(head) };
        }
    }

    #[cold]
    unsafe fn traverse(&self, mut head: *mut Node) {
        atomic::fence(Acquire);

        while !head.is_null() {
            // SAFETY: We always push valid pointers into the list.
            let batch = unsafe { (*head).batch };

            // SAFETY: We always push valid pointers into the list, and when doing so, we make sure
            // that the `next` union variant is initialized.
            let next = unsafe { (*head).link.next };

            // SAFETY: The caller must ensure that there are no more references to any retired
            // slots.
            let ref_count = unsafe { (*batch).ref_count.fetch_sub(1, Release) }.wrapping_sub(1);

            if ref_count == 0 {
                // SAFETY: We had the last reference.
                unsafe { self.reclaim(batch) };
            }

            head = next;
        }
    }

    unsafe fn reclaim(&self, batch: *mut Batch) {
        atomic::fence(Acquire);

        // SAFETY: The caller must ensure that we own the batch.
        let mut batch = unsafe { LocalBatch::from_raw(batch) };

        for node in batch.retired_as_mut_slice() {
            // SAFETY:
            // * We own the batch, which means that no more references to the slots can exist.
            // * We always push indices of existing vacant slots into the list.
            // * `node.slots` is the same pointer that was used when pushing the node.
            unsafe { (node.reclaim)(node.index, node.slots) };
        }
    }
}

impl Drop for Collector {
    fn drop(&mut self) {
        atomic::fence(Acquire);

        for retirement_list in &mut self.retirement_lists {
            let batch = retirement_list.batch.get_mut();

            if batch.is_empty() {
                continue;
            }

            for node in batch.retired_as_mut_slice() {
                // SAFETY:
                // * We have mutable access to the batch, which means that no more references to the
                //   slots can exist.
                // * We always push indices of existing vacant slots into the list.
                // * `node.slots` is the same pointer that was used when pushing the node.
                unsafe { (node.reclaim)(node.index, node.slots) };
            }
        }
    }
}

/// A batch of retired slots.
#[repr(C)]
struct Batch {
    /// The number of threads that can still access the slots in this batch.
    ref_count: AtomicUsize,
    /// The capacity of `nodes`.
    capacity: usize,
    /// The number of `nodes`.
    len: usize,
    /// The number of `nodes` that should be retired.
    retired_len: usize,
    /// An inline allocation of `capacity` nodes with `len` being intialized.
    nodes: [Node; 0],
}

/// A node in the retirement list.
struct Node {
    link: NodeLink,
    /// The batch that this node is a part of.
    batch: *mut Batch,
    /// The index of the retired slot.
    index: u32,
    /// A pointer to the allocation of `slot::Slot<V>`s.
    slots: *const u8,
    /// The reclamation function for the retired slot.
    reclaim: unsafe fn(u32, *const u8),
}

union NodeLink {
    /// The retirement list.
    retirement_list: *const AtomicPtr<Node>,
    /// The next node in the retirement list.
    next: *mut Node,
}

struct LocalBatch {
    ptr: *mut Batch,
}

// SAFETY: We own the batch and access to it is synchronized using mutable references.
unsafe impl Send for LocalBatch {}

// SAFETY: We own the batch and access to it is synchronized using mutable references.
unsafe impl Sync for LocalBatch {}

impl Default for LocalBatch {
    fn default() -> Self {
        LocalBatch::new()
    }
}

impl LocalBatch {
    const MIN_CAP: usize = 4;

    fn new() -> Self {
        let layout = layout_for_capacity(Self::MIN_CAP);

        // SAFETY: The layout is not zero-sized.
        let ptr = unsafe { alloc(layout) }.cast::<Batch>();

        if ptr.is_null() {
            handle_alloc_error(layout);
        }

        // SAFETY: We successfully allocated the batch.
        unsafe {
            *ptr::addr_of_mut!((*ptr).ref_count) = AtomicUsize::new(0);
            *ptr::addr_of_mut!((*ptr).capacity) = Self::MIN_CAP;
            *ptr::addr_of_mut!((*ptr).len) = 0;
            *ptr::addr_of_mut!((*ptr).retired_len) = 0;
        }

        LocalBatch { ptr }
    }

    #[inline]
    unsafe fn from_raw(ptr: *mut Batch) -> Self {
        LocalBatch { ptr }
    }

    #[inline]
    fn into_raw(self) -> *mut Batch {
        ManuallyDrop::new(self).ptr
    }

    #[inline]
    fn capacity(&self) -> usize {
        // SAFETY: The pointer is valid.
        unsafe { (*self.ptr).capacity }
    }

    #[inline]
    fn len(&self) -> usize {
        // SAFETY: The pointer is valid.
        unsafe { (*self.ptr).len }
    }

    #[inline]
    fn retired_len(&self) -> usize {
        // SAFETY: The pointer is valid.
        unsafe { (*self.ptr).retired_len }
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    fn as_mut_ptr(&mut self) -> *mut Node {
        // SAFETY: The pointer is valid.
        unsafe { ptr::addr_of_mut!((*self.ptr).nodes) }.cast()
    }

    fn as_mut_slice(&mut self) -> &mut [Node] {
        // SAFETY: The pointer is valid and the caller of `LocalBatch::set_len` must ensure that the
        // length is the correct number of nodes that are initialized.
        unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len()) }
    }

    #[inline]
    fn retired_as_mut_slice(&mut self) -> &mut [Node] {
        // SAFETY: The pointer is valid and the caller of `LocalBatch::set_retired_len` must ensure
        // that the length is the correct number of nodes that should be retired.
        unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.retired_len()) }
    }

    #[inline]
    unsafe fn push(&mut self, index: u32, slots: *const u8, reclaim: unsafe fn(u32, *const u8)) {
        let len = self.len();

        if len == self.capacity() {
            self.grow_one();
        }

        let node = Node {
            link: NodeLink {
                retirement_list: ptr::null(),
            },
            batch: ptr::null_mut(),
            index,
            slots,
            reclaim,
        };

        // SAFETY: We made sure that the index is in bounds above.
        unsafe { self.as_mut_ptr().add(len).write(node) };

        // SAFETY: We wrote the new element above.
        unsafe { self.set_len(len + 1) };
    }

    #[inline(never)]
    fn grow_one(&mut self) {
        let capacity = self.capacity();
        let new_capacity = capacity * 2;
        let layout = layout_for_capacity(capacity);
        let new_layout = layout_for_capacity(new_capacity);

        // SAFETY:
        // * `self.ptr` was allocated via the global allocator.
        // * `layout` is the current layout.
        // * `new_layout.size()`, when rounded up to the nearest multiple of `new_layout.align()`,
        //   cannot overflow `isize` since we used `Layout` for the layout calculation.
        let new_ptr = unsafe { realloc(self.ptr.cast(), layout, new_layout.size()) };

        if new_ptr.is_null() {
            handle_alloc_error(new_layout);
        }

        self.ptr = new_ptr.cast();

        // SAFETY: The pointer is valid.
        unsafe { (*self.ptr).capacity = new_capacity };
    }

    #[inline]
    unsafe fn set_len(&mut self, len: usize) {
        // SAFETY: The pointer is valid.
        unsafe { (*self.ptr).len = len };
    }

    #[inline]
    unsafe fn set_retired_len(&mut self, len: usize) {
        // SAFETY: The pointer is valid.
        unsafe { (*self.ptr).retired_len = len };
    }
}

impl Drop for LocalBatch {
    fn drop(&mut self) {
        let layout = layout_for_capacity(self.capacity());

        // SAFETY:
        // * `self.ptr` was allocated using the global allocator.
        // * `layout` is the layout of the allocation.
        unsafe { dealloc(self.ptr.cast(), layout) };
    }
}

fn layout_for_capacity(capacity: usize) -> Layout {
    Layout::new::<Batch>()
        .extend(Layout::array::<Node>(capacity).unwrap())
        .unwrap()
        .0
}

pub struct Guard<'a> {
    retirement_list: &'a RetirementList,
    marker: PhantomData<*const ()>,
}

impl<'a> Guard<'a> {
    #[inline]
    unsafe fn new(retirement_list: &'a RetirementList) -> Self {
        Guard {
            retirement_list,
            marker: PhantomData,
        }
    }

    #[inline]
    #[must_use]
    pub fn collector(&self) -> &CollectorHandle {
        &self.retirement_list.collector
    }

    #[inline]
    pub(crate) unsafe fn defer_reclaim<V>(&self, index: u32, slots: &Vec<V>) {
        let slots = slots.as_ptr().cast();
        let reclaim = transmute_reclaim_fp(crate::reclaim::<V>);

        // SAFETY:
        // * `Guard` is `!Send + !Sync`, so this cannot be called concurrently.
        // * The caller must ensure that `index` is valid and that it is not reachable anymore.
        // * `slots` is a valid pointer to the allocation of `Slot<V>`s.
        // * The caller must ensure that `reclaim` is safe to call with the `index` and `slots`.
        unsafe { self.retirement_list.defer_reclaim(index, slots, reclaim) };
    }

    #[inline]
    pub(crate) unsafe fn defer_reclaim_invalidated<V>(&self, index: u32, slots: &Vec<V>) {
        let slots = slots.as_ptr().cast();
        let reclaim = transmute_reclaim_fp(crate::reclaim_invalidated::<V>);

        // SAFETY:
        // * `Guard` is `!Send + !Sync`, so this cannot be called concurrently.
        // * The caller must ensure that `index` is valid and that it is not reachable anymore.
        // * `slots` is a valid pointer to the allocation of `Slot<V>`s.
        // * The caller must ensure that `reclaim` is safe to call with the `index` and `slots`.
        unsafe { self.retirement_list.defer_reclaim(index, slots, reclaim) }
    }

    #[inline]
    pub fn flush(&self) {
        // SAFETY: `Guard` is `!Send + !Sync`, so this cannot be called concurrently.
        unsafe { self.retirement_list.retire() };
    }
}

fn transmute_reclaim_fp<V>(fp: unsafe fn(u32, *const Slot<V>)) -> unsafe fn(u32, *const u8) {
    // SAFETY: Pointers have the same ABI for all sized pointees.
    unsafe { mem::transmute::<unsafe fn(u32, *const Slot<V>), unsafe fn(u32, *const u8)>(fp) }
}

impl fmt::Debug for Guard<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Guard").finish_non_exhaustive()
    }
}

impl Clone for Guard<'_> {
    #[inline]
    fn clone(&self) -> Self {
        let guard_count = self.retirement_list.guard_count.get();
        self.retirement_list
            .guard_count
            .set(guard_count.checked_add(1).unwrap());

        // SAFETY:
        // * We incremented the `guard_count` above, such that the guard's drop implementation
        //   cannot unpin the participant while another guard still exists.
        // * The participant is already pinned as this guard's existence is a proof of that.
        unsafe { Guard::new(self.retirement_list) }
    }
}

impl Drop for Guard<'_> {
    #[inline]
    fn drop(&mut self) {
        let guard_count = self.retirement_list.guard_count.get();
        self.retirement_list.guard_count.set(guard_count - 1);

        if guard_count == 1 {
            // SAFETY:
            // * `Guard` is `!Send + !Sync`, so this cannot be called concurrently.
            // * We are dropping the last guard, so there cannot be any more references to any
            //   retired slots.
            unsafe { self.retirement_list.leave() };
        }
    }
}