-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwlock.rs
125 lines (105 loc) · 3.1 KB
/
rwlock.rs
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
use core::cell::UnsafeCell;
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;
use core::sync::atomic::{AtomicUsize, Ordering};
pub struct RwLock<T> {
data: UnsafeCell<T>,
lock: AtomicUsize,
}
pub struct RwLockReadGuard<'a, T> {
lock: &'a AtomicUsize,
data: NonNull<T>,
}
pub struct RwLockWriteGuard<'a, T> {
lock: &'a RwLock<T>,
}
unsafe impl<T: Send> Send for RwLock<T> {}
unsafe impl<T: Send + Sync> Sync for RwLock<T> {}
unsafe impl<T: Sync> Send for RwLockReadGuard<'_, T> {}
unsafe impl<T: Sync> Sync for RwLockReadGuard<'_, T> {}
unsafe impl<T: Send + Sync> Send for RwLockWriteGuard<'_, T> {}
unsafe impl<T: Send + Sync> Sync for RwLockWriteGuard<'_, T> {}
impl<T> RwLock<T> {
pub const fn new(t: T) -> Self {
Self {
lock: AtomicUsize::new(0),
data: UnsafeCell::new(t),
}
}
}
impl<T> RwLock<T> {
pub fn dump_lock_state(&self) {
eprintln!("lock: {:b}", self.lock.load(Ordering::Relaxed));
eprintln!("{:p}", self);
}
const WRITE_LOCK_MASK: usize = 1 << (usize::BITS - 1);
pub fn try_read(&self) -> Option<RwLockReadGuard<T>> {
let val = self.lock.fetch_add(1, Ordering::Acquire);
if (val & Self::WRITE_LOCK_MASK) == 0 {
// was not already locked as Write so we're good.
Some(RwLockReadGuard {
lock: &self.lock,
data: unsafe { NonNull::new_unchecked(self.data.get()) },
})
} else {
// failed to lock. reset
let _ = self.lock.fetch_sub(1, Ordering::Release);
None
}
}
pub fn try_write(&self) -> Option<RwLockWriteGuard<T>> {
let val = self.lock.fetch_or(Self::WRITE_LOCK_MASK, Ordering::AcqRel);
if (val & Self::WRITE_LOCK_MASK) == 0 {
Some(RwLockWriteGuard { lock: &self })
} else {
// no need to undo what we did.
None
}
}
pub fn read(&self) -> RwLockReadGuard<T> {
loop {
match self.try_read() {
Some(g) => break g,
None => core::hint::spin_loop(),
}
}
}
pub fn write(&self) -> RwLockWriteGuard<T> {
loop {
match self.try_write() {
Some(g) => break g,
None => core::hint::spin_loop(),
}
}
}
}
impl<'a, T> Deref for RwLockReadGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { self.data.as_ref() }
}
}
impl<'a, T> Deref for RwLockWriteGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.lock.data.get() }
}
}
impl<'a, T> DerefMut for RwLockWriteGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.lock.data.get() }
}
}
impl<'a, T> Drop for RwLockReadGuard<'a, T> {
fn drop(&mut self) {
self.lock.fetch_sub(1, Ordering::Release);
}
}
impl<'a, T> Drop for RwLockWriteGuard<'a, T> {
fn drop(&mut self) {
let val = self
.lock
.lock
.fetch_xor(RwLock::<T>::WRITE_LOCK_MASK, Ordering::Release);
}
}