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
// This code is forked from https://docs.rs/dashmap/7.0.0-rc2/src/dashmap/util.rs.html,
// available under the MIT license.
use parking_lot::RawRwLock;
pub type RwLock<T> = detail::RwLock<RawRwLock, T>;
pub type RwLockReadGuardDetached<'a> = detail::RwLockReadGuardDetached<'a, RawRwLock>;
pub type RwLockWriteGuardDetached<'a> = detail::RwLockWriteGuardDetached<'a, RawRwLock>;
mod detail {
use lock_api::RawRwLock;
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
pub struct RwLock<R, T> {
inner: lock_api::RwLock<R, T>,
}
impl<R: RawRwLock, T> RwLock<R, T> {
pub fn new(t: T) -> Self {
Self {
inner: lock_api::RwLock::new(t),
}
}
pub fn get_mut(&mut self) -> &mut T {
self.inner.get_mut()
}
pub fn read(&self) -> RwLockReadGuard<'_, R, T> {
// SAFETY: The shared guard derived from the raw lock is
// encapsulated into the resulting read guard.
let raw = unsafe { self.inner.raw() };
let guard = raw.lock_shared();
RwLockReadGuard {
rwlock: &self.inner,
guard: ManuallyDrop::new(guard),
_phantom: PhantomData,
}
}
pub fn write(&self) -> RwLockWriteGuard<'_, R, T> {
// SAFETY: The exclusive guard derived from the raw lock is
// encapsulated into the resulting write guard.
let raw = unsafe { self.inner.raw() };
let guard = raw.lock_exclusive();
RwLockWriteGuard {
rwlock: &self.inner,
guard: ManuallyDrop::new(guard),
_phantom: PhantomData,
}
}
}
pub struct RwLockReadGuard<'a, R: RawRwLock, T: 'a> {
rwlock: &'a lock_api::RwLock<R, T>,
guard: ManuallyDrop<R::SharedGuard>,
_phantom: PhantomData<&'a T>,
}
impl<'a, R: RawRwLock, T: 'a> Drop for RwLockReadGuard<'a, R, T> {
fn drop(&mut self) {
// SAFETY: It is safe to unlock the raw lock, see below.
let rwlock = unsafe { self.rwlock.raw() };
// SAFETY: self.guard isn't used again, as we are in the Drop
// implementation.
let guard = unsafe { ManuallyDrop::take(&mut self.guard) };
// SAFETY: All references to the locked data derived from this read
// guard are now out of scope, so it's safe to unlock. Indeed, the
// Deref implementation binds resulting references to this guard's
// lifetime.
unsafe {
rwlock.unlock_shared(guard);
}
}
}
impl<'a, R: RawRwLock, T: 'a> Deref for RwLockReadGuard<'a, R, T> {
type Target = T;
fn deref(&self) -> &T {
// SAFETY: Shared access is guaranteed for the whole output
// lifetime.
unsafe { &*self.rwlock.data_ptr() }
}
}
impl<'a, R: RawRwLock, T: 'a> RwLockReadGuard<'a, R, T> {
/// Separates the data from this [`RwLockReadGuard`].
///
/// # Safety
///
/// The data must not outlive the [`RwLockReadGuardDetached`].
pub unsafe fn detach(self) -> (RwLockReadGuardDetached<'a, R>, &'a T) {
// Inhibit this guard's Drop implementation, to avoid unlocking
// twice.
let mut this = ManuallyDrop::new(self);
// SAFETY: The caller promises that the returned reference isn't
// used beyond the detached guard's lifetime.
let data = unsafe { &*this.rwlock.data_ptr() };
// SAFETY: `this.guard` isn't used again, as `this` is wrapped in a
// ManuallyDrop.
let guard = unsafe { ManuallyDrop::take(&mut this.guard) };
let guard = RwLockReadGuardDetached {
// SAFETY: The caller promises that the returned reference isn't
// used beyond the detached guard's lifetime.
rwlock: unsafe { this.rwlock.raw() },
guard: ManuallyDrop::new(guard),
};
(guard, data)
}
}
/// A [`RwLockReadGuard`] without the data.
pub struct RwLockReadGuardDetached<'a, R: RawRwLock> {
rwlock: &'a R,
guard: ManuallyDrop<R::SharedGuard>,
}
impl<R: RawRwLock> Drop for RwLockReadGuardDetached<'_, R> {
fn drop(&mut self) {
// SAFETY: self.guard isn't used again, as we are in the Drop
// implementation.
let guard = unsafe { ManuallyDrop::take(&mut self.guard) };
// SAFETY: All references to the locked data derived from this read
// guard are now out of scope - as promised by the caller of
// RwLockReadGuard::detach() - so it's safe to unlock.
unsafe {
self.rwlock.unlock_shared(guard);
}
}
}
pub struct RwLockWriteGuard<'a, R: RawRwLock, T: 'a> {
rwlock: &'a lock_api::RwLock<R, T>,
guard: ManuallyDrop<R::ExclusiveGuard>,
_phantom: PhantomData<&'a mut T>,
}
impl<'a, R: RawRwLock, T: 'a> Drop for RwLockWriteGuard<'a, R, T> {
fn drop(&mut self) {
// SAFETY: It is safe to unlock the raw lock, see below.
let rwlock = unsafe { self.rwlock.raw() };
// SAFETY: self.guard isn't used again, as we are in the Drop
// implementation.
let guard = unsafe { ManuallyDrop::take(&mut self.guard) };
// SAFETY: All references to the locked data derived from this write
// guard are now out of scope, so it's safe to unlock. Indeed, the
// Deref and DerefMut implementations bind resulting references to
// this guard's lifetime.
unsafe {
rwlock.unlock_exclusive(guard);
}
}
}
impl<'a, R: RawRwLock, T: 'a> Deref for RwLockWriteGuard<'a, R, T> {
type Target = T;
fn deref(&self) -> &T {
// SAFETY: Exclusive access is guaranteed for the whole output
// lifetime.
unsafe { &*self.rwlock.data_ptr() }
}
}
impl<'a, R: RawRwLock, T: 'a> DerefMut for RwLockWriteGuard<'a, R, T> {
fn deref_mut(&mut self) -> &mut T {
// SAFETY: Exclusive access is guaranteed for the whole output
// lifetime.
unsafe { &mut *self.rwlock.data_ptr() }
}
}
impl<'a, R: RawRwLock, T: 'a> RwLockWriteGuard<'a, R, T> {
/// Separates the data from this [`RwLockWriteGuard`].
///
/// # Safety
///
/// The data must not outlive the [`RwLockWriteGuardDetached`].
pub unsafe fn detach(self) -> (RwLockWriteGuardDetached<'a, R>, &'a mut T) {
// Inhibit this guard's Drop implementation, to avoid unlocking
// twice.
let mut this = ManuallyDrop::new(self);
// SAFETY: The caller promises that the returned reference isn't
// used beyond the detached guard's lifetime.
let data = unsafe { &mut *this.rwlock.data_ptr() };
// SAFETY: `this.guard` isn't used again, as `this` is wrapped in a
// ManuallyDrop.
let guard = unsafe { ManuallyDrop::take(&mut this.guard) };
let guard = RwLockWriteGuardDetached {
// SAFETY: The caller promises that the returned reference isn't
// used beyond the detached guard's lifetime.
rwlock: unsafe { this.rwlock.raw() },
guard: ManuallyDrop::new(guard),
};
(guard, data)
}
}
/// A [`RwLockWriteGuard`] without the data.
pub struct RwLockWriteGuardDetached<'a, R: RawRwLock> {
rwlock: &'a R,
guard: ManuallyDrop<R::ExclusiveGuard>,
}
impl<R: RawRwLock> Drop for RwLockWriteGuardDetached<'_, R> {
fn drop(&mut self) {
// SAFETY: self.guard isn't used again, as we are in the Drop
// implementation.
let guard = unsafe { ManuallyDrop::take(&mut self.guard) };
// SAFETY: All references to the locked data derived from this write
// guard are now out of scope - as promised by the caller of
// RwLockWriteGuard::detach() - so it's safe to unlock.
unsafe {
self.rwlock.unlock_exclusive(guard);
}
}
}
}