sled/
lazy.rs

1//! This module exists because `lazy_static` causes TSAN to
2//! be very unhappy. We rely heavily on TSAN for finding
3//! races, so we don't use `lazy_static`.
4
5use std::sync::atomic::{
6    AtomicBool, AtomicPtr,
7    Ordering::{Acquire, SeqCst},
8};
9
10/// A lazily initialized value
11pub struct Lazy<T, F> {
12    value: AtomicPtr<T>,
13    init_mu: AtomicBool,
14    init: F,
15}
16
17impl<T, F> Lazy<T, F> {
18    /// Create a new Lazy
19    pub const fn new(init: F) -> Self
20    where
21        F: Sized,
22    {
23        Self {
24            value: AtomicPtr::new(std::ptr::null_mut()),
25            init_mu: AtomicBool::new(false),
26            init,
27        }
28    }
29}
30
31impl<T, F> Drop for Lazy<T, F> {
32    fn drop(&mut self) {
33        let value_ptr = self.value.load(Acquire);
34        if !value_ptr.is_null() {
35            #[allow(unsafe_code)]
36            unsafe {
37                drop(Box::from_raw(value_ptr))
38            }
39        }
40    }
41}
42
43impl<T, F> std::ops::Deref for Lazy<T, F>
44where
45    F: Fn() -> T,
46{
47    type Target = T;
48
49    fn deref(&self) -> &T {
50        {
51            let value_ptr = self.value.load(Acquire);
52            if !value_ptr.is_null() {
53                #[allow(unsafe_code)]
54                unsafe {
55                    return &*value_ptr;
56                }
57            }
58        }
59
60        // compare_and_swap returns the last value on success,
61        // or the current value on failure. We want to keep
62        // looping as long as it returns true, so we don't need
63        // any explicit conversion here.
64        while self.init_mu.compare_and_swap(false, true, SeqCst) {}
65
66        {
67            let value_ptr = self.value.load(Acquire);
68            // we need to check this again because
69            // maybe some other thread completed
70            // the initialization already.
71            if !value_ptr.is_null() {
72                let unlock = self.init_mu.swap(false, SeqCst);
73                assert!(unlock);
74                #[allow(unsafe_code)]
75                unsafe {
76                    return &*value_ptr;
77                }
78            }
79        }
80
81        {
82            let value = (self.init)();
83            let value_ptr = Box::into_raw(Box::new(value));
84
85            let old = self.value.swap(value_ptr, SeqCst);
86            assert!(old.is_null());
87
88            let unlock = self.init_mu.swap(false, SeqCst);
89            assert!(unlock);
90
91            #[allow(unsafe_code)]
92            unsafe {
93                &*value_ptr
94            }
95        }
96    }
97}