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
#[cfg(feature = "owning_ref")]
use owning_ref::StableAddress;
#[cfg(feature = "check")]
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{
    cell::UnsafeCell,
    ops::{Deref, DerefMut},
    panic::{RefUnwindSafe, UnwindSafe},
};

// Locking bits are copied from [parking_lot](https://github.com/Amanieu/parking_lot).
// If the reader count is zero: a writer is currently holding an exclusive lock.
// Otherwise: a writer is waiting for the remaining readers to exit the lock.
#[cfg(feature = "check")]
const WRITER_BIT: usize = 0b1000;
// Base unit for counting readers.
#[cfg(feature = "check")]
const ONE_READER: usize = 0b10000;

/// A read-write lock
#[derive(Debug)]
pub struct RwLock<T: ?Sized> {
    #[cfg(feature = "check")]
    state: AtomicUsize,
    value: UnsafeCell<T>,
}

impl<T> RefUnwindSafe for RwLock<T> where T: ?Sized {}
impl<T> UnwindSafe for RwLock<T> where T: ?Sized {}
unsafe impl<T> Send for RwLock<T> where T: ?Sized + Send {}
unsafe impl<T> Sync for RwLock<T> where T: ?Sized + Send + Sync {}

impl<T> From<T> for RwLock<T> {
    fn from(val: T) -> Self {
        Self::new(val)
    }
}

impl<T> Default for RwLock<T>
where
    T: ?Sized + Default,
{
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T> RwLock<T> {
    /// Create a new `RwLock`.
    pub const fn new(val: T) -> Self {
        Self {
            value: UnsafeCell::new(val),
            #[cfg(feature = "check")]
            state: AtomicUsize::new(0),
        }
    }

    /// Consume the `RwLock`, returning the inner value.
    pub fn into_inner(self) -> T {
        self.value.into_inner()
    }
}

impl<T> RwLock<T>
where
    T: ?Sized,
{
    /// Get a mutable reference of the inner value T. This is safe because we
    /// have the mutable reference of the lock.
    pub fn get_mut(&mut self) -> &mut T {
        self.value.get_mut()
    }

    /// Try write lock the `RwLock`, returns the write guard. Returns None if the
    /// `RwLock` is write locked.
    pub fn try_write<'a>(&'a self) -> Option<RwLockWriteGuard<'a, T>> {
        self.lock_exclusive()
            .then(|| RwLockWriteGuard { lock: self })
    }

    /// Write lock the `RwLock`, returns the write guard.
    ///
    /// # Panics
    ///
    /// If the `RwLock` is already write locked, this will panic if the `check`
    /// feature is turned on.
    pub fn write<'a>(&'a self) -> RwLockWriteGuard<'a, T> {
        if !self.lock_exclusive() {
            #[cfg(feature = "check")]
            panic!("The lock is already write locked")
        }

        RwLockWriteGuard { lock: self }
    }

    /// Try read lock the `RwLock`, returns the read guard. Returns None if the
    /// `RwLock` is write locked.
    pub fn try_read<'a>(&'a self) -> Option<RwLockReadGuard<'a, T>> {
        self.lock_shared().then(|| RwLockReadGuard { lock: self })
    }

    /// Read lock the `RwLock`, returns the read guard.
    ///
    /// # Panics
    ///
    /// If the `RwLock` is already write locked, this will panic if the check feature
    /// is turned on.
    pub fn read<'a>(&'a self) -> RwLockReadGuard<'a, T> {
        if !self.lock_shared() {
            #[cfg(feature = "check")]
            panic!("The lock is already write locked")
        }

        RwLockReadGuard { lock: self }
    }

    fn lock_exclusive(&self) -> bool {
        #[cfg(feature = "check")]
        {
            self.state
                .compare_exchange(0, WRITER_BIT, Ordering::Acquire, Ordering::Relaxed)
                .is_ok()
        }

        #[cfg(not(feature = "check"))]
        true
    }

    fn unlock_exclusive(&self) -> bool {
        #[cfg(feature = "check")]
        {
            self.state
                .compare_exchange(WRITER_BIT, 0, Ordering::Acquire, Ordering::Relaxed)
                .is_ok()
        }

        #[cfg(not(feature = "check"))]
        true
    }

    fn lock_shared(&self) -> bool {
        #[cfg(feature = "check")]
        loop {
            let state = self.state.load(Ordering::Relaxed);
            if state & WRITER_BIT != 0 {
                // is write locked
                return false;
            }

            if self
                .state
                .compare_exchange(
                    state,
                    state.checked_add(ONE_READER).expect("too many readers"),
                    Ordering::Acquire,
                    Ordering::Relaxed,
                )
                .is_ok()
            {
                break;
            }
        }

        true
    }

    fn unlock_shared(&self) {
        #[cfg(feature = "check")]
        self.state.fetch_sub(ONE_READER, Ordering::Release);
    }
}

pub struct RwLockWriteGuard<'a, T>
where
    T: ?Sized,
{
    lock: &'a RwLock<T>,
}

impl<'a, T> Deref for RwLockWriteGuard<'a, T>
where
    T: ?Sized,
{
    type Target = T;
    fn deref(&self) -> &T {
        unsafe { &*self.lock.value.get() }
    }
}

impl<'a, T> DerefMut for RwLockWriteGuard<'a, T>
where
    T: ?Sized,
{
    fn deref_mut(&mut self) -> &mut T {
        unsafe { &mut *self.lock.value.get() }
    }
}

impl<'a, T> Drop for RwLockWriteGuard<'a, T>
where
    T: ?Sized,
{
    fn drop(&mut self) {
        self.lock.unlock_exclusive();
    }
}

pub struct RwLockReadGuard<'a, T>
where
    T: ?Sized,
{
    lock: &'a RwLock<T>,
}

impl<'a, T> Deref for RwLockReadGuard<'a, T>
where
    T: ?Sized,
{
    type Target = T;
    fn deref(&self) -> &T {
        unsafe { &*self.lock.value.get() }
    }
}

impl<'a, T> Drop for RwLockReadGuard<'a, T>
where
    T: ?Sized,
{
    fn drop(&mut self) {
        self.lock.unlock_shared();
    }
}

#[cfg(feature = "owning_ref")]
unsafe impl<'a, T: 'a> StableAddress for RwLockReadGuard<'a, T> where T: ?Sized {}
#[cfg(feature = "owning_ref")]
unsafe impl<'a, T: 'a> StableAddress for RwLockWriteGuard<'a, T> where T: ?Sized {}