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
#![doc = include_str!("../readme.md")]

use std::{collections::VecDeque, ops::Deref, ptr};

#[cfg(not(loom))]
use std::{
    cell::Cell,
    sync::{
        atomic::{AtomicPtr, AtomicU64, Ordering},
        Arc, Mutex, MutexGuard,
    },
};

#[cfg(loom)]
use loom::{
    cell::Cell,
    sync::{
        atomic::{AtomicPtr, AtomicU64, Ordering},
        Arc, Mutex, MutexGuard,
    },
};

/// Represents a read-copy-update for a specific value.
///
/// This is the write side, new read handles can be constructed by calling [`Rcu::reader`].
#[derive(Debug)]
pub struct Rcu<T> {
    epoch: u64,
    shared: Arc<Shared<T>>,
}

/// The reader handle for a value stored in an [`Rcu`].
///
/// Specific values can be read using [`Reader::read`]. Readers are `!Sync` and expected to be used
/// only on a single thread.
#[derive(Debug)]
pub struct Reader<T: 'static> {
    cache: Cell<&'static StampedValue<T>>,
    refs: Cell<usize>,
    state: ReaderState,
    shared: Arc<Shared<T>>,
}

#[derive(Debug)]
struct Shared<T> {
    ptr: Pointer<T>,
    reclaim: Mutex<VecDeque<Box<StampedValue<T>>>>,
    readers: Mutex<Vec<ReaderState>>,
}

#[derive(Debug)]
struct StampedValue<T> {
    value: T,
    epoch: u64,
}

#[derive(Debug, Clone)]
struct ReaderState(Arc<AtomicU64>);

#[derive(Debug)]
struct Pointer<T>(AtomicPtr<StampedValue<T>>);

#[derive(Debug)]
pub struct Guard<'a, T: 'static> {
    cache: &'a StampedValue<T>,
    reader: &'a Reader<T>,
}

impl<T: 'static> Default for Rcu<T>
where
    T: Default,
{
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T: 'static> Rcu<T> {
    /// Constructs a new RCU with an initial value.
    pub fn new(value: T) -> Self {
        Self {
            epoch: 1,
            shared: Arc::new(Shared {
                ptr: Pointer::new(StampedValue { value, epoch: 1 }),
                reclaim: Mutex::new(VecDeque::new()),
                readers: Mutex::new(Vec::new()),
            }),
        }
    }

    /// Registers a new [`Reader`] allowing values to be read.
    pub fn reader(&mut self) -> Reader<T> {
        Reader::new(self.shared.clone())
    }

    /// Write a new value making it available to all readers.
    ///
    /// Previously written values will be reclaimed when they are no longer accesed.
    pub fn write(&mut self, value: T) {
        // Records a new epoch associated with this value, not allowed to wrap around.
        self.epoch += 1;

        // Publish the value, we will attempt to reclaim the previous value.
        let next = StampedValue {
            epoch: self.epoch,
            value,
        };
        let prev = self.shared.ptr.swap(next);
        self.reclaim_queue().push_back(prev);

        // Immediately attempt to reclaim.
        self.try_reclaim();
    }

    /// Try and reclaim any values which are no longer in-use.
    ///
    /// Returns the number of values still waiting to be reclaimed.
    pub fn try_reclaim(&mut self) -> usize {
        let mut readers = self.shared.readers.lock().unwrap();

        // Trim readers which have been removed.
        readers.retain(|reader| reader.get() > ReaderState::NOT_IN_USE);

        let mut reclaim = self.reclaim_queue();

        // If there are no readers, we can reclaim everything.
        if readers.is_empty() {
            reclaim.clear();
        }

        // Check the minimum epoch across all active threads, removing records
        // that are below the minimum epoch.
        let min_epoch = readers
            .iter()
            .map(|r| r.get())
            .min()
            .unwrap_or(ReaderState::NOT_IN_USE);
        while let Some(candidate) = reclaim.pop_front() {
            if min_epoch > candidate.epoch {
                drop(candidate);
            } else {
                reclaim.push_front(candidate);
                // We short circuit, no point checking others with a higher epoch.
                return reclaim.len();
            }
        }
        0
    }

    fn reclaim_queue(&self) -> MutexGuard<'_, VecDeque<Box<StampedValue<T>>>> {
        // The reclaimer must be shared so we can drop any remaining values when the 'Arc<Shared>'
        // drops, but access to it should only be from this function. As a result we protect it with
        // a mutex but only rely on try_lock().
        self.shared
            .reclaim
            .try_lock()
            .expect("invalid shared reclaimer access")
    }
}

impl<T: 'static> Reader<T> {
    fn new(shared: Arc<Shared<T>>) -> Self {
        let value = shared.ptr.load();
        let mut readers = shared.readers.lock().unwrap();

        let state = ReaderState::new(value.epoch);
        readers.push(state.clone());

        Reader {
            shared: shared.clone(),
            refs: Cell::new(0),
            cache: Cell::new(value),
            state,
        }
    }

    /// Reads the latest value guarded to ensure that the pointer will not be reclaimed while the
    /// current reader has access.
    pub fn read(&self) -> Guard<'_, T> {
        // The read method provides a guard that allows deref access to one of the values written
        // by the writer previously. The invariant this method maintains, using ref-counts, is that
        // the epoch stamped on the current thread is always less than or equal to the epoch of the
        // last used value. As soon as the reclaimer sees an epoch for a specific thread, it can be
        // sure that no references with epochs 'below' the available epoch exist on that thread.

        let cache = if self.refs.get() == 0 {
            let value = self.shared.ptr.load();

            // Update the epoch to note that we are currently using this value. This uses release
            // ordering to ensure that loads when reclaiming will be ordered after this operation.
            self.state.set(value.epoch);

            // Cache the pointer in the current reader.
            self.cache.set(value);
            value
        } else {
            self.cache.get()
        };

        self.refs.set(self.refs.get() + 1);

        Guard {
            reader: self,
            cache,
        }
    }
}

impl<'a, T> Deref for Guard<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.cache.value
    }
}

impl<'a, T> Drop for Guard<'a, T> {
    fn drop(&mut self) {
        self.reader.refs.replace(self.reader.refs.get() - 1);
    }
}

impl<T> Drop for Reader<T> {
    fn drop(&mut self) {
        self.state.mark_dropped();
    }
}

impl<T> Pointer<T> {
    fn new(value: StampedValue<T>) -> Self {
        Self(AtomicPtr::new(Box::leak(Box::new(value))))
    }

    fn swap(&self, value: StampedValue<T>) -> Box<StampedValue<T>> {
        let ptr = Box::leak(Box::new(value));
        let prev = self.0.swap(ptr, Ordering::AcqRel);
        unsafe { Box::from_raw(prev) }
    }

    fn load(&self) -> &'static StampedValue<T> {
        unsafe { &*self.0.load(Ordering::Relaxed) }
    }
}

impl<T> Drop for Pointer<T> {
    fn drop(&mut self) {
        let prev = self.0.swap(ptr::null_mut(), Ordering::AcqRel);
        let _ = unsafe { Box::from_raw(prev) };
    }
}

impl ReaderState {
    const NOT_IN_USE: u64 = 0;

    fn new(epoch: u64) -> Self {
        Self(Arc::new(AtomicU64::new(epoch)))
    }

    fn mark_dropped(&self) {
        self.set(Self::NOT_IN_USE)
    }

    fn set(&self, epoch: u64) {
        self.0.store(epoch, Ordering::Release)
    }

    fn get(&self) -> u64 {
        self.0.load(Ordering::Acquire)
    }
}

/// Provides thread-local storage to read [`Rcu`] values.
///
/// When a new thread is initialized a new [`Reader`] will be created and stored in a slot for the
/// provided thread. Values will be published to the thread-local and access will be cheap
#[cfg(feature = "thread-local")]
pub struct ThreadLocal<T: Send + Sync + 'static> {
    shared: Arc<Shared<T>>,
    thread_local: thread_local::ThreadLocal<Reader<T>>,
}

#[cfg(feature = "thread-local")]
impl<T: Send + Sync + 'static> ThreadLocal<T> {
    pub fn new(rcu: &Rcu<T>) -> Self {
        Self {
            shared: rcu.shared.clone(),
            thread_local: thread_local::ThreadLocal::new(),
        }
    }

    /// Returns the element for the current thread, if it exists,
    pub fn get(&self) -> Option<Guard<'_, T>> {
        self.thread_local.get().map(|r| r.read())
    }

    /// Returns the element for the current thread, or creates it if it doesn't exist.
    pub fn get_or_init(&self) -> Guard<'_, T> {
        self.thread_local
            .get_or(|| Reader::new(self.shared.clone()))
            .read()
    }
}

#[cfg(test)]
#[cfg(loom)]
mod loom_tests {
    use loom::thread;

    use super::*;

    #[test]
    fn nested() {
        loom::model(|| {
            let mut rcu = Rcu::new(10);

            let rdr = rcu.reader();

            {
                let g = rdr.read();
                assert_eq!(10, *g);

                rcu.write(20);
                {
                    let g = rdr.read();
                    assert_eq!(10, *g);
                }
            }
        });
    }

    #[test]
    fn thread_nested() {
        loom::model(|| {
            let n = 2;
            let mut rcu = Rcu::new(0);
            let rdr = rcu.reader();
            let h = thread::spawn(move || {
                let v = rdr.read();
                assert!(*v < n);

                {
                    let g = rdr.read();
                    assert!(*g < n);
                }
            });
            for i in 0..n {
                rcu.write(i);
                loom::thread::yield_now();
            }
            h.join().unwrap();
        });
    }

    #[test]
    fn thread() {
        loom::model(|| {
            let n = 2;
            let mut rcu = Rcu::new(0);
            let rdr = rcu.reader();
            let h = thread::spawn(move || {
                for _ in 0..n {
                    let v = rdr.read();
                    assert!(*v < n);
                    loom::thread::yield_now();
                }
            });
            for i in 0..n {
                rcu.write(i);
                loom::thread::yield_now();
            }
            h.join().unwrap();
        });
    }

    #[test]
    fn thread_detached() {
        loom::model(|| {
            let n = 2;
            let mut rcu = Rcu::new(0);
            let rdr = rcu.reader();
            thread::spawn(move || {
                for _ in 0..n {
                    let v = rdr.read();
                    assert!(*v < n);
                    loom::thread::yield_now();
                }
            });
            for i in 0..n {
                rcu.write(i);
                loom::thread::yield_now();
            }
        });
    }
}

#[cfg(test)]
#[cfg(not(loom))]
mod tests {
    use std::{
        sync::{atomic::AtomicUsize, Condvar},
        thread,
        time::Duration,
    };

    use super::*;

    thread_local! {
        static REFS: AtomicUsize = AtomicUsize::new(0);
    }

    struct RefsCheck;

    impl RefsCheck {
        fn new() -> Self {
            REFS.with(|refs| {
                assert_eq!(refs.load(Ordering::SeqCst), 0);
            });
            Self
        }
    }

    impl Drop for RefsCheck {
        fn drop(&mut self) {
            REFS.with(|refs| {
                assert_eq!(refs.load(Ordering::SeqCst), 0);
            });
        }
    }

    #[derive(Debug)]
    struct RecordDrop(u32);

    impl RecordDrop {
        fn new(v: u32) -> Self {
            REFS.with(|refs| {
                refs.fetch_add(1, Ordering::SeqCst);
            });
            Self(v)
        }
    }

    impl Drop for RecordDrop {
        fn drop(&mut self) {
            REFS.with(|refs| {
                refs.fetch_sub(1, Ordering::SeqCst);
            });
        }
    }

    #[cfg(feature = "thread-local")]
    #[test]
    fn thread_local() {
        let mut rcu = Rcu::new(10);
        let tls = ThreadLocal::new(&rcu);

        thread::scope(|s| {
            s.spawn(|| {
                let _val = tls.get_or_init();
                assert!(tls.get().is_some());
            });
            s.spawn(|| {
                let _val = tls.get_or_init();
                assert!(tls.get().is_some());
            });
        });

        rcu.write(1);
    }

    #[test]
    fn send_check() {
        let mut rcu = Rcu::new(10);
        let rdr = rcu.reader();

        thread::spawn(move || {
            assert_eq!(10, *rdr.read());
        });
    }

    #[test]
    fn single_value() {
        let _refs = RefsCheck::new();

        let mut rcu = Rcu::new(RecordDrop::new(10));
        let rdr = rcu.reader();
        assert_eq!(10, rdr.read().0);
    }

    #[test]
    fn old_value() {
        let _refs = RefsCheck::new();

        let mut rcu = Rcu::new(RecordDrop::new(10));
        let rdr1 = rcu.reader();
        assert_eq!(10, rdr1.read().0);

        let rdr2 = rcu.reader();
        assert_eq!(10, rdr2.read().0);

        for i in 11..=20 {
            rcu.write(RecordDrop::new(i));
            assert_eq!(i, rdr1.read().0);
        }

        // because of the limitations of the current design, all values will
        // not be dropped until this point.
    }

    #[test]
    fn remove_readers() {
        let _refs = RefsCheck::new();

        let mut rcu = Rcu::new(RecordDrop::new(10));

        let rdr1 = rcu.reader();
        let rdr2 = rcu.reader();

        for i in 11..=20 {
            rcu.write(RecordDrop::new(i));
        }

        drop(rdr1);
        drop(rdr2);

        rcu.write(RecordDrop::new(30));
    }

    #[test]
    fn nested() {
        let _refs = RefsCheck::new();

        let mut rcu = Rcu::new(RecordDrop::new(10));

        let rdr = rcu.reader();

        {
            let handle = rdr.read();
            assert_eq!(10, handle.0);

            rcu.write(RecordDrop::new(20));

            {
                let handle = rdr.read();
                assert_eq!(10, handle.0);
            }

            let handle2 = rdr.read();
            assert_eq!(10, handle.0);
            assert_eq!(10, handle2.0);
        }

        assert_eq!(20, rdr.read().0);
    }

    #[test]
    fn nested_multi_threaded() {
        let _refs = RefsCheck::new();

        let notify = Arc::new((Mutex::new(false), Condvar::new()));

        let mut rcu = Rcu::new(RecordDrop::new(10));
        let rdr = rcu.reader();
        assert_eq!(10, rdr.read().0);

        let handles: Vec<_> = (0..10)
            .map(|_| {
                let rdr = rcu.reader();
                let notify = notify.clone();
                std::thread::spawn(move || {
                    let _refs = RefsCheck::new();

                    assert_eq!(10, rdr.read().0);
                    {
                        let handle = rdr.read();
                        assert_eq!(10, handle.0);
                    }

                    let (lock, cvar) = &*notify;
                    let mut started = lock.lock().unwrap();
                    while !*started {
                        started = cvar.wait(started).unwrap();
                    }

                    assert_eq!(20, rdr.read().0);
                    {
                        let handle = rdr.read();
                        assert_eq!(20, handle.0);
                    }
                })
            })
            .collect();

        thread::sleep(Duration::from_millis(10));
        rcu.write(RecordDrop::new(20));

        {
            let (lock, cvar) = &*notify;
            *lock.lock().unwrap() = true;
            cvar.notify_all();
        }

        handles.into_iter().for_each(|h| h.join().unwrap());
    }
}