lockfree 0.5.1

This crate provides concurrent data structures and a solution to the ABA problem as an alternative of hazard pointers
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
use super::{
    bucket::{Bucket, Garbage, GetRes, InsertRes},
    guard::{ReadGuard, Removed},
    insertion::{Inserter, Insertion},
};
use incin::{Incinerator, Pause};
use owned_alloc::{Cache, OwnedAlloc, UninitAlloc};
use std::{
    borrow::Borrow,
    fmt,
    marker::PhantomData,
    ptr::{null_mut, NonNull},
    sync::{
        atomic::{
            AtomicPtr,
            Ordering::{self, *},
        },
        Arc,
    },
};

const BITS: usize = 8;

// If you remove this alignment, don't remove it. Please, set it to 2.
#[repr(align(64))]
pub struct Table<K, V> {
    nodes: [Node<K, V>; 1 << BITS],
}

impl<K, V> Table<K, V> {
    pub fn new_alloc() -> OwnedAlloc<Self> {
        // Safe because it calls a correctly a function which correctly
        // initializes uninitialized memory with, indeed, uninitialized memory.
        unsafe {
            UninitAlloc::<Self>::new().init_in_place(|val| val.init_in_place())
        }
    }

    // Unsafe because passing ininitialized memory may cause leaks.
    #[inline]
    pub unsafe fn init_in_place(&mut self) {
        for node in &mut self.nodes as &mut [_] {
            (node as *mut Node<K, V>).write(Node::new())
        }
    }

    // Unsafe because the incinerator needs to be paused and there are no
    // guarantees the passed pause comes from the incinerator used with the map
    // by other threads. Map implementation guarantees that.
    pub unsafe fn get<'map, Q>(
        &self,
        key: &Q,
        hash: u64,
        pause: Pause<'map, Garbage<K, V>>,
    ) -> Option<ReadGuard<'map, K, V>>
    where
        Q: ?Sized + Ord,
        K: Borrow<Q>,
    {
        let mut shifted = hash;
        let mut table = self;

        loop {
            // Compute the index from the shifted hash's lower bits.
            let index = shifted as usize & (1 << BITS) - 1;
            let loaded = table.nodes[index].atomic.load(Acquire);

            // Null means we have nothing.
            if loaded.is_null() {
                break None;
            }

            // Cleared lower bit means this is a bucket.
            if loaded as usize & 1 == 0 {
                let bucket = &*(loaded as *mut Bucket<K, V>);

                // This bucket only matters if it has the same hash we do.
                if bucket.hash() != hash {
                    break None;
                }

                break match bucket.get(key, pause) {
                    // Success.
                    GetRes::Found(pair) => Some(pair),

                    // Not here.
                    GetRes::NotFound => None,

                    // Delete the bucket completely.
                    GetRes::Delete(pause) => {
                        let res = table.nodes[index].atomic.compare_exchange(
                            loaded,
                            null_mut(),
                            Relaxed,
                            Relaxed,
                        );

                        if res.is_ok() {
                            let alloc = OwnedAlloc::from_raw(
                                NonNull::new_unchecked(loaded as *mut _),
                            );
                            // Needs to be destroyed by the incinerator as it is
                            // shared.
                            pause.add_to_incin(Garbage::Bucket(alloc));
                        }

                        None
                    },
                };
            }

            // If none of other cases have been confirmed, the only remaining
            // case is a branching table. Let's try to look at it.
            table = &*((loaded as usize & !1) as *mut Self);
            // Shifting the hash so we test some other bits.
            shifted >>= BITS;
        }
    }

    // Unsafe because the incinerator needs to be paused and there are no
    // guarantees the passed pause comes from the incinerator used with the map
    // by other threads. Map implementation guarantees that.
    #[inline(never)]
    pub unsafe fn insert<I>(
        &self,
        mut inserter: I,
        hash: u64,
        pause: &Pause<Garbage<K, V>>,
        incin: &Arc<Incinerator<Garbage<K, V>>>,
    ) -> Insertion<K, V, I>
    where
        I: Inserter<K, V>,
        K: Ord,
    {
        let mut table = self;
        let mut shifted = hash;
        let mut depth = 1;
        let mut tbl_cache = Cache::<OwnedAlloc<Self>>::new();

        // Compute the index from the shifted hash's lower bits.
        let mut index = shifted as usize & (1 << BITS) - 1;
        // Load what is in the index before trying to insert.
        let mut loaded = table.nodes[index].atomic.load(Acquire);

        loop {
            if loaded.is_null() {
                // Let's test the found conditions.
                inserter.input(None);
                let pair = match inserter.pointer() {
                    // The inserter accepted the conditions.
                    Some(nnptr) => nnptr,
                    // The inserter rejected the conditions.
                    None => break Insertion::Failed(inserter),
                };

                // Allocation of a bucket containing a single entry. Our pair.
                let bucket = Bucket::new(hash, pair);
                let bucket_nnptr = OwnedAlloc::new(bucket).into_raw();

                // We try to put it in the index.
                let res = table.nodes[index].atomic.compare_exchange(
                    loaded,
                    bucket_nnptr.as_ptr() as *mut (),
                    AcqRel,
                    Acquire,
                );

                match res {
                    Ok(_) => {
                        // Let's not forget to prevent the inserter from
                        // deallocating the pointer.
                        inserter.take_pointer();
                        break Insertion::Created;
                    },

                    Err(new) => {
                        // If we failed this try, we have to clean up.
                        let mut bucket = OwnedAlloc::from_raw(bucket_nnptr);
                        bucket.take_first();
                        loaded = new;
                    },
                }
            } else if loaded as usize & 1 == 0 {
                // We keep pointers to Buckets with the lower bit cleared.
                let bucket = &*(loaded as *mut Bucket<K, V>);

                // If the hash of the bucket is equal to ours, there is no need
                // for us to branch. Actually, we must not do it. We must insert
                // in the bucket.
                if bucket.hash() == hash {
                    match bucket.insert(inserter, pause, incin) {
                        InsertRes::Created => break Insertion::Created,

                        InsertRes::Updated(old) => {
                            break Insertion::Updated(old)
                        },

                        InsertRes::Failed(inserter) => {
                            break Insertion::Failed(inserter)
                        },

                        // This means we must delete the bucket entirely. And
                        // try again, obviously.
                        InsertRes::Delete(returned) => {
                            let ptr = &table.nodes[index].atomic;
                            let res = ptr.compare_exchange(
                                loaded,
                                null_mut(),
                                AcqRel,
                                Acquire,
                            );

                            match res {
                                Ok(_) => {
                                    let alloc = OwnedAlloc::from_raw(
                                        NonNull::new_unchecked(
                                            loaded as *mut _,
                                        ),
                                    );
                                    incin.add(Garbage::Bucket(alloc));
                                    loaded = null_mut()
                                },

                                Err(new) => {
                                    loaded = new;
                                },
                            }

                            inserter = returned;
                        },
                    }
                } else {
                    // In the case hashes aren't equal, we will branch!
                    let new_table = tbl_cache.take_or(|| Self::new_alloc());
                    let other_shifted = bucket.hash() >> (depth * BITS);
                    let other_index = other_shifted as usize & (1 << BITS) - 1;

                    // Placing the found bucket into the new table first.
                    new_table.nodes[other_index].atomic.store(loaded, Relaxed);

                    let new_table_nnptr = new_table.into_raw();
                    let res = table.nodes[index].atomic.compare_exchange(
                        loaded,
                        // Note we mark the lower bit!
                        (new_table_nnptr.as_ptr() as usize | 1) as *mut (),
                        AcqRel,
                        Acquire,
                    );

                    match res {
                        Ok(_) => {
                            // If we succeeded, let's act like we found another
                            // table in this index.
                            depth += 1;
                            table = &*new_table_nnptr.as_ptr();
                            shifted >>= BITS;
                            // Compute the index from the shifted hash's lower
                            // bits.
                            index = shifted as usize & (1 << BITS) - 1;
                            // Load what is in the index before trying to
                            // insert.
                            loaded = table.nodes[index].atomic.load(Acquire);
                        },

                        Err(new) => {
                            // If we failed -> clean up! And store the
                            // allocation in
                            // some cache, since allocating a table can be
                            // really expensive due
                            // to it's size.
                            let new_table =
                                OwnedAlloc::from_raw(new_table_nnptr);
                            new_table.nodes[other_index]
                                .atomic
                                .store(null_mut(), Relaxed);
                            tbl_cache.store(new_table);
                            loaded = new;
                        },
                    }
                }
            } else {
                // If none of other cases have been confirmed, the only
                // remaining case is a branching table. Let's
                // try to look at it.
                depth += 1;
                table = &*((loaded as usize & !1) as *mut Self);
                shifted >>= BITS;

                // Compute the index from the shifted hash's lower
                // bits.
                index = shifted as usize & (1 << BITS) - 1;
                // Load what is in the index before trying to
                // insert.
                loaded = table.nodes[index].atomic.load(Acquire);
            }
        }
    }

    // Unsafe because the incinerator needs to be paused and there are no
    // guarantees the passed pause comes from the incinerator used with the map
    // by other threads. Map implementation guarantees that.
    pub unsafe fn remove<Q, F>(
        &self,
        key: &Q,
        interactive: F,
        hash: u64,
        pause: &Pause<Garbage<K, V>>,
        incin: &Arc<Incinerator<Garbage<K, V>>>,
    ) -> Option<Removed<K, V>>
    where
        Q: ?Sized + Ord,
        K: Borrow<Q>,
        F: FnMut(&(K, V)) -> bool,
    {
        let mut table = self;
        let mut shifted = hash;

        loop {
            // Compute the index from the shifted hash's lower bits.
            let index = shifted as usize & (1 << BITS) - 1;
            // Let's load to see what is in there.
            let loaded = table.nodes[index].atomic.load(Acquire);

            // Null means we have nothing.
            if loaded.is_null() {
                break None;
            }

            // Cleared lower bit means this is a bucket.
            if loaded as usize & 1 == 0 {
                let bucket = &*(loaded as *mut Bucket<K, V>);

                // This bucket only matters if it has the same hash we do.
                if bucket.hash() != hash {
                    break None;
                }

                let res = bucket.remove(key, interactive, pause, incin);

                // If this field is true it means the whole bucket must be
                // removed. Regardless of failure or success.
                if res.delete {
                    let res = table.nodes[index].atomic.compare_exchange(
                        loaded,
                        null_mut(),
                        Relaxed,
                        Relaxed,
                    );

                    if res.is_ok() {
                        let alloc = OwnedAlloc::from_raw(
                            NonNull::new_unchecked(loaded as *mut _),
                        );
                        incin.add(Garbage::Bucket(alloc));
                    }
                }
                break res.pair;
            }

            // If none of other cases have been confirmed, the only remaining
            // case is a branching table. Let's try to look at it.
            table = &*((loaded as usize & !1) as *mut Self);
            // Shifting the hash so we test some other bits.
            shifted >>= BITS;
        }
    }

    // Unsafe because calling this function and using the table again later will
    // cause undefined behavior.
    #[inline]
    pub unsafe fn free_nodes(
        &mut self,
        tbl_stack: &mut Vec<OwnedAlloc<Table<K, V>>>,
    ) {
        for node in &self.nodes as &[Node<K, V>] {
            Node::free_ptr(node.atomic.load(Relaxed), tbl_stack);
        }
    }

    #[inline]
    pub fn clear(&mut self, tbl_stack: &mut Vec<OwnedAlloc<Table<K, V>>>) {
        for node in &self.nodes as &[Node<K, V>] {
            // This should be safe because we store only proper pointers.
            unsafe {
                Node::free_ptr(
                    node.atomic.swap(null_mut(), Relaxed),
                    tbl_stack,
                );
            }
        }
    }

    pub fn optimize_space(&mut self) -> OptSpaceRes<K, V> {
        let mut removed = 0usize;
        let mut last_bucket = None;

        for node in &self.nodes as &[Node<K, V>] {
            let loaded = node.atomic.load(Relaxed);

            if loaded.is_null() {
                removed += 1;
            } else if loaded as usize & 1 == 0 {
                let bucket_ptr = loaded as *mut Bucket<K, V>;
                // This is safe because:
                //
                // 1. We have exclusive reference to the table.
                //
                // 2. We proper allocate pointers stored in the table.
                //
                // 3. Bucket pointers are not marked and we checked for it.
                if unsafe { (*bucket_ptr).is_empty() } {
                    node.atomic.store(null_mut(), Release);
                    removed += 1;

                    // This is safe because we have exclusive reference to the
                    // map. Also, we remove the bucket from the table so no one
                    // else will find it.
                    unsafe {
                        OwnedAlloc::from_raw(NonNull::new_unchecked(
                            bucket_ptr,
                        ));
                    }
                } else {
                    // Safe because of the same things in the list above. Also,
                    // we checked for null already, we can by-pass this check.
                    let nnptr = unsafe { NonNull::new_unchecked(bucket_ptr) };
                    last_bucket = Some(nnptr);
                }
            } else {
                let table_ptr = (loaded as usize & !1) as *mut Table<K, V>;

                // This is safe because:
                //
                // 1. We have exclusive reference to the table.
                //
                // 2. We proper allocate pointers stored in the table.
                //
                // 3. Table pointers are marked and we checked for it.
                //
                // 4. We cleared the marked bit.
                match unsafe { &mut *table_ptr }.optimize_space() {
                    OptSpaceRes::NoOpt => (),

                    OptSpaceRes::Remove => {
                        node.atomic.store(null_mut(), Relaxed);
                        // This is safe because we have exclusive reference to
                        // the map. Also, we remove the inner table from the
                        // outer table so no one else will find it.
                        unsafe {
                            let nnptr = NonNull::new_unchecked(table_ptr);
                            OwnedAlloc::from_raw(nnptr);
                        }
                        removed += 1;
                    },

                    OptSpaceRes::TableToBucket(bucket) => {
                        unsafe {
                            // This is safe because we have exclusive reference
                            // to the map. Also, we remove the inner table from
                            // the outer table so no one else will find it.
                            let nnptr = NonNull::new_unchecked(table_ptr);
                            OwnedAlloc::from_raw(nnptr);
                        }
                        node.atomic.store(bucket.as_ptr() as *mut _, Relaxed)
                    },
                }
            }
        }

        match (last_bucket, self.nodes.len() - removed) {
            (Some(nnptr), 1) => OptSpaceRes::TableToBucket(nnptr),

            (_, 0) => OptSpaceRes::Remove,

            _ => OptSpaceRes::NoOpt,
        }
    }

    pub fn load_index(
        &self,
        index: usize,
        ordering: Ordering,
    ) -> Option<*mut ()> {
        self.nodes.get(index).map(|node| node.atomic.load(ordering))
    }
}

impl<K, V> fmt::Debug for Table<K, V> {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        write!(
            fmtr,
            "Table {} nodes: {:?} {}",
            '{', &self.nodes as &[Node<K, V>], '}'
        )
    }
}

struct Node<K, V> {
    // First lower bit is 0 for leaf and 1 for branch
    atomic: AtomicPtr<()>,
    _marker: PhantomData<(K, V)>,
}

impl<K, V> Node<K, V> {
    // Unsafe because it is *pretty easy* to make undefined behavior out of this
    // because the pointer does not have even a fixed type.
    unsafe fn free_ptr(
        ptr: *mut (),
        tbl_stack: &mut Vec<OwnedAlloc<Table<K, V>>>,
    ) {
        if ptr.is_null() {
            return;
        }

        if ptr as usize & 1 == 0 {
            OwnedAlloc::from_raw(NonNull::new_unchecked(
                ptr as *mut Bucket<K, V>,
            ));
        } else {
            let table_ptr = (ptr as usize & !1) as *mut Table<K, V>;

            debug_assert!(!table_ptr.is_null());
            tbl_stack
                .push(OwnedAlloc::from_raw(NonNull::new_unchecked(table_ptr)));
        }
    }
}

impl<K, V> Node<K, V> {
    fn new() -> Self {
        Self { atomic: AtomicPtr::new(null_mut()), _marker: PhantomData }
    }
}

impl<K, V> fmt::Debug for Node<K, V> {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        write!(fmtr, "Node {} pointer: {:?} {}", '{', self.atomic, '}')
    }
}

pub enum OptSpaceRes<K, V> {
    NoOpt,
    Remove,
    TableToBucket(NonNull<Bucket<K, V>>),
}