rcu_128 0.2.3

RCU (Read-Copy-Update) implementation for platforms supporting atomic 128-bit operations.
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
#![cfg(target_has_atomic = "128")]
#![feature(integer_atomics)]
#![no_std]
extern crate alloc;
use alloc::boxed::Box;
use parking_lot::RwLock;

use core::{
    hint,
    marker::PhantomData,
    ops::Deref,
    ptr::NonNull,
    sync::atomic::{AtomicU128, Ordering},
};

const COUNTER_MASK: u128 = 0xffff_ffff_ffff_ffff;

/// Exponential backoff for spin-wait loops.
///
/// Starts with `spin_loop` hints, then yields the thread after a threshold
/// to avoid burning CPU when waiting for long-lived readers.
struct Backoff {
    step: u32,
}

impl Backoff {
    fn new() -> Self {
        Self { step: 0 }
    }

    fn spin(&mut self) {
        for _ in 0..1u32 << self.step.min(6) {
            hint::spin_loop();
        }
        if self.step <= 6 {
            self.step += 1;
        }
    }
}

/// A guard that provides read access to a value in an `RcuCell`.
///
/// When this guard is dropped, it will signal that the read operation
/// is complete, allowing the `RcuCell` to manage its internal state
/// accordingly.
#[derive(Debug)]
pub struct RcuGuard<'a, T> {
    ptr: NonNull<T>,
    cell: &'a RcuCell<T>,
}

impl<T> Deref for RcuGuard<'_, T> {
    type Target = T;
    fn deref(&self) -> &T {
        // SAFETY: The pointer was obtained from a valid Box allocation in RcuCell::new,
        // write, or update. The value is kept alive because:
        // - If the value is still current (in ptr_counter_latest), it won't be freed
        //   until swapped out and all readers drain.
        // - If swapped out (in ptr_counter_to_clear), the writer spins until our
        //   guard's counter decrement, so the value is alive while this guard exists.
        unsafe { self.ptr.as_ref() }
    }
}

impl<T> Drop for RcuGuard<'_, T> {
    fn drop(&mut self) {
        // Try to decrement ptr_counter_latest first (fast path: value hasn't been swapped)
        let mut backoff = Backoff::new();
        loop {
            let ptr_counter = self.cell.ptr_counter_latest.load(Ordering::Acquire);
            if (ptr_counter >> 64) as usize == self.ptr.as_ptr() as usize {
                if self
                    .cell
                    .ptr_counter_latest
                    .compare_exchange_weak(
                        ptr_counter,
                        ptr_counter - 1,
                        Ordering::AcqRel,
                        Ordering::Relaxed,
                    )
                    .is_ok()
                {
                    return;
                }
            } else {
                // ptr_counter_latest has been updated, so we can't decrement it
                break;
            }
            backoff.spin();
        }
        // Slow path: value was swapped out, decrement ptr_counter_to_clear.
        // The writer that swapped our value out will (or has already) moved it
        // into ptr_counter_to_clear. We spin until it appears there.
        let mut backoff = Backoff::new();
        loop {
            let ptr_counter = self.cell.ptr_counter_to_clear.load(Ordering::Acquire);
            if (ptr_counter >> 64) as usize == self.ptr.as_ptr() as usize
                && self
                    .cell
                    .ptr_counter_to_clear
                    .compare_exchange_weak(
                        ptr_counter,
                        ptr_counter - 1,
                        Ordering::AcqRel,
                        Ordering::Relaxed,
                    )
                    .is_ok()
            {
                return;
            }
            backoff.spin();
        }
    }
}

/// A concurrent data structure that allows for safe, read-copy-update (RCU)
/// style access to its value.
///
/// # Grace period serialization
///
/// Only one old value can be pending reclamation at a time (single
/// `ptr_counter_to_clear` slot). If multiple writers call [`write`](RcuCell::write)
/// concurrently while readers hold guards to old values, their grace periods
/// are serialized. This is acceptable for read-heavy workloads but can cause
/// writer stalls under heavy write contention with long-lived readers.
#[derive(Debug)]
pub struct RcuCell<T> {
    ptr_counter_latest: AtomicU128,
    ptr_counter_to_clear: AtomicU128,
    data: PhantomData<T>,
    update_token: RwLock<()>,
}

impl<T: Default> Default for RcuCell<T> {
    fn default() -> Self {
        Self::new(Default::default())
    }
}

impl<T> Drop for RcuCell<T> {
    fn drop(&mut self) {
        // SAFETY: All RcuGuards borrow &RcuCell, so the borrow checker guarantees
        // they are all dropped before this runs. Therefore the counter is 0 and we
        // have exclusive ownership of the value. get_mut() is sound because &mut self.
        let ptr = (*self.ptr_counter_latest.get_mut() >> 64) as usize as *mut T;
        unsafe {
            let _ = Box::from_raw(ptr);
        }
    }
}

impl<T> RcuCell<T> {
    /// Creates a new `RcuCell` with the given initial value.
    ///
    /// # Example
    ///
    /// ```
    /// let rcu_cell = rcu_128::RcuCell::new(42);
    /// ```
    pub fn new(value: T) -> Self {
        Self {
            ptr_counter_latest: AtomicU128::new((Box::into_raw(Box::new(value)) as u128) << 64),
            ptr_counter_to_clear: AtomicU128::new(0),
            data: PhantomData,
            update_token: RwLock::new(()),
        }
    }

    /// Provides read access to the value stored in the `RcuCell`.
    ///
    /// This function returns an `RcuGuard`, which allows for safe,
    /// concurrent read access to the `RcuCell`'s value.
    ///
    /// Once all `RcuGuard` instances referencing a particular value are
    /// dropped, the value can be safely released during an update or write.
    ///
    /// # Example
    ///
    /// ```
    /// let rcu_cell = rcu_128::RcuCell::new(42);
    /// {
    ///     let guard = rcu_cell.read();
    ///     assert_eq!(*guard, 42);
    /// }
    /// ```
    pub fn read(&self) -> RcuGuard<'_, T> {
        // SAFETY: The upper 64 bits of ptr_counter_latest always hold a valid,
        // non-null pointer to a Box-allocated T. fetch_add atomically increments
        // the reader count, which prevents the writer from freeing this value
        // until we drop the guard.
        let ptr = unsafe {
            NonNull::new_unchecked(
                (self.ptr_counter_latest.fetch_add(1, Ordering::AcqRel) >> 64) as usize as *mut T,
            )
        };
        RcuGuard { cell: self, ptr }
    }

    /// Writes a new value into the `RcuCell`.
    ///
    /// The new value becomes immediately visible to subsequent readers.
    /// This method blocks until all readers of the old value have dropped
    /// their guards, then frees the old value.
    ///
    /// Multiple concurrent `write` calls are allowed (last-writer-wins).
    /// Use [`update`](RcuCell::update) if you need read-modify-write semantics.
    ///
    /// # Example
    ///
    /// ```
    /// let rcu_cell = rcu_128::RcuCell::new(42);
    /// rcu_cell.write(100);
    /// {
    ///     let guard = rcu_cell.read();
    ///     assert_eq!(*guard, 100);
    /// }
    /// ```
    pub fn write(&self, value: T) {
        let new_ptr_counter = (Box::into_raw(Box::new(value)) as u128) << 64;
        let token_shared = self.update_token.read();
        let old_ptr_counter = self
            .ptr_counter_latest
            .swap(new_ptr_counter, Ordering::AcqRel);
        drop(token_shared);
        self.clear(old_ptr_counter);
    }

    /// Updates the value stored in the `RcuCell` using a provided function.
    ///
    /// This function applies the given closure `f` to the current value,
    /// replacing it with the returned value. The closure runs under an
    /// exclusive lock, so concurrent `update` and `write` calls are
    /// serialized — the closure is guaranteed to run exactly once.
    ///
    /// # Example
    ///
    /// ```
    /// let rcu_cell = rcu_128::RcuCell::new(42);
    /// rcu_cell.update(|&old_value| old_value + 1);
    /// {
    ///     let guard = rcu_cell.read();
    ///     assert_eq!(*guard, 43);
    /// }
    /// ```
    pub fn update(&self, f: impl FnOnce(&T) -> T) {
        let token_exclusive = self.update_token.write();
        // SAFETY: The exclusive lock prevents any concurrent write/update from
        // swapping out ptr_counter_latest's pointer. Readers only obtain shared
        // references (&T) via guards, so no mutable aliasing occurs. The pointer
        // is valid because it was produced by Box::into_raw and hasn't been freed
        // (clear() only frees values after they're swapped out of ptr_counter_latest).
        let old_value =
            unsafe { &*((self.ptr_counter_latest.load(Ordering::Acquire) >> 64) as *const T) };
        let new_value = f(old_value);
        let new_ptr_counter = (Box::into_raw(Box::new(new_value)) as u128) << 64;
        let old_ptr_counter = self
            .ptr_counter_latest
            .swap(new_ptr_counter, Ordering::AcqRel);
        drop(token_exclusive);
        self.clear(old_ptr_counter);
    }

    /// Waits for all readers of the old value to finish, then frees it.
    fn clear(&self, old_ptr_counter: u128) {
        if old_ptr_counter & COUNTER_MASK == 0 {
            // No readers — release memory directly.
            // SAFETY: The pointer was produced by Box::into_raw. The counter is 0,
            // meaning no guards hold this pointer (the swap was atomic with the
            // reader's fetch_add, so any reader that incremented the count is
            // reflected here). It is safe to reclaim.
            unsafe {
                let _ = Box::from_raw((old_ptr_counter >> 64) as usize as *mut T);
            }
            return;
        }

        // Acquire the single reclamation slot. Only one old value can be
        // pending in ptr_counter_to_clear at a time. Other writers spin here
        // until the slot is available (grace period serialization).
        let mut backoff = Backoff::new();
        while self
            .ptr_counter_to_clear
            .compare_exchange_weak(0, old_ptr_counter, Ordering::AcqRel, Ordering::Relaxed)
            .is_err()
        {
            // Inner loop: read-only spin to avoid exclusive cache line access (MESI)
            while self.ptr_counter_to_clear.load(Ordering::Relaxed) != 0 {
                backoff.spin();
            }
        }

        // Wait for all readers of the old value to drop their guards.
        // Each guard drop decrements the counter in ptr_counter_to_clear.
        // No CAS needed here: once counter reaches 0, no other thread will
        // modify it (new readers get the latest pointer, not this one).
        let mut backoff = Backoff::new();
        while self.ptr_counter_to_clear.load(Ordering::Acquire) & COUNTER_MASK != 0 {
            backoff.spin();
        }
        // Clear the slot to allow other writers to reclaim their old values.
        self.ptr_counter_to_clear.store(0, Ordering::Release);
        // SAFETY: All readers have drained (counter == 0). The pointer was
        // produced by Box::into_raw and has not been freed elsewhere.
        unsafe {
            let _ = Box::from_raw((old_ptr_counter >> 64) as usize as *mut T);
        }
    }
}

#[cfg(test)]
mod tests {
    extern crate std;
    use super::*;
    extern crate alloc;
    use alloc::sync::Arc;
    use alloc::vec::Vec;
    use std::thread;

    #[test]
    fn basic_read_write() {
        let cell = RcuCell::new(42);
        assert_eq!(*cell.read(), 42);

        cell.write(100);
        assert_eq!(*cell.read(), 100);
    }

    #[test]
    fn update_applies_closure() {
        let cell = RcuCell::new(10);
        cell.update(|&v| v + 5);
        assert_eq!(*cell.read(), 15);

        cell.update(|&v| v * 2);
        assert_eq!(*cell.read(), 30);
    }

    #[test]
    fn default_trait() {
        let cell: RcuCell<i32> = RcuCell::default();
        assert_eq!(*cell.read(), 0);
    }

    #[test]
    fn multiple_guards_same_value() {
        let cell = RcuCell::new(42);
        let g1 = cell.read();
        let g2 = cell.read();
        let g3 = cell.read();
        assert_eq!(*g1, 42);
        assert_eq!(*g2, 42);
        assert_eq!(*g3, 42);
        drop(g1);
        drop(g2);
        drop(g3);
    }

    #[test]
    fn guard_sees_value_at_read_time() {
        let cell = Arc::new(RcuCell::new(1));
        let guard = cell.read();

        // Write from another thread (write blocks until guard drops)
        let cell2 = cell.clone();
        let handle = thread::spawn(move || {
            cell2.write(2);
        });

        // Guard still sees the old value
        assert_eq!(*guard, 1);
        drop(guard);
        handle.join().unwrap();
        assert_eq!(*cell.read(), 2);
    }

    #[test]
    fn drop_frees_value() {
        // Use Arc to verify the value is freed when RcuCell is dropped.
        let inner = Arc::new(42);
        let cell = RcuCell::new(inner.clone());
        assert_eq!(Arc::strong_count(&inner), 2);
        drop(cell);
        assert_eq!(Arc::strong_count(&inner), 1);
    }

    #[test]
    fn write_frees_old_value() {
        let v1 = Arc::new(1);
        let v2 = Arc::new(2);
        let cell = RcuCell::new(v1.clone());
        assert_eq!(Arc::strong_count(&v1), 2);

        cell.write(v2.clone());
        // old value should be freed since no guards held it
        assert_eq!(Arc::strong_count(&v1), 1);
        assert_eq!(Arc::strong_count(&v2), 2);
    }

    #[test]
    #[cfg(not(miri))] // requires threads + spin-wait, too slow for Miri
    fn old_value_freed_after_guard_drop() {
        let v1 = Arc::new(1);
        let cell = RcuCell::new(v1.clone());
        let guard = cell.read();
        assert_eq!(Arc::strong_count(&v1), 2);

        // Spawn a thread to write, which will block until we drop the guard
        let cell_ref = &cell;
        let v2 = Arc::new(2);
        let v2_clone = v2.clone();
        thread::scope(|s| {
            s.spawn(move || {
                cell_ref.write(v2_clone);
            });
            // Give the writer time to swap (but it will spin on clear)
            thread::sleep(std::time::Duration::from_millis(10));
            // v1 still alive because guard holds it
            assert_eq!(Arc::strong_count(&v1), 2);
            drop(guard);
        });
        // After scope, writer thread joined, v1 should be freed
        assert_eq!(Arc::strong_count(&v1), 1);
        assert_eq!(*cell.read(), v2);
    }

    #[test]
    #[cfg(not(miri))]
    fn concurrent_readers() {
        let cell = Arc::new(RcuCell::new(0u64));
        let mut handles = Vec::new();
        for _ in 0..4 {
            let cell = cell.clone();
            handles.push(thread::spawn(move || {
                for _ in 0..1000 {
                    let guard = cell.read();
                    let _ = *guard; // just read
                }
            }));
        }
        for h in handles {
            h.join().unwrap();
        }
    }

    #[test]
    #[cfg(not(miri))]
    fn concurrent_read_write() {
        let cell = Arc::new(RcuCell::new(0u64));

        thread::scope(|s| {
            // Writer
            let cell_w = cell.clone();
            s.spawn(move || {
                for i in 0..100 {
                    cell_w.write(i);
                }
            });

            // Readers
            for _ in 0..4 {
                let cell_r = cell.clone();
                s.spawn(move || {
                    for _ in 0..1000 {
                        let guard = cell_r.read();
                        let val = *guard;
                        assert!(val < 100);
                    }
                });
            }
        });
    }

    #[test]
    #[cfg(not(miri))]
    fn concurrent_updates() {
        let cell = Arc::new(RcuCell::new(0u64));

        thread::scope(|s| {
            for _ in 0..4 {
                let cell = cell.clone();
                s.spawn(move || {
                    for _ in 0..100 {
                        cell.update(|&v| v + 1);
                    }
                });
            }
        });

        assert_eq!(*cell.read(), 400);
    }

    #[test]
    #[cfg(not(miri))]
    fn stress_mixed_operations() {
        let cell = Arc::new(RcuCell::new(0u64));

        thread::scope(|s| {
            // Writers
            for _ in 0..2 {
                let cell = cell.clone();
                s.spawn(move || {
                    for i in 0..20 {
                        cell.write(i);
                    }
                });
            }

            // Updaters
            for _ in 0..2 {
                let cell = cell.clone();
                s.spawn(move || {
                    for _ in 0..20 {
                        cell.update(|&v| v.wrapping_add(1));
                    }
                });
            }

            // Readers with held guards
            for _ in 0..2 {
                let cell = cell.clone();
                s.spawn(move || {
                    let mut guards = Vec::new();
                    for i in 0..80 {
                        guards.push(cell.read());
                        if guards.len() > 4 {
                            guards.remove(0);
                        }
                        if i % 10 == 0 {
                            guards.clear();
                        }
                    }
                });
            }
        });
    }
}