| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | use super::LockClassKey;
|
| 9 | use crate::{
|
| 10 | str::{CStr, CStrExt as _},
|
| 11 | types::{NotThreadSafe, Opaque, ScopeGuard},
|
| 12 | };
|
| 13 | use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin};
|
| 14 | use pin_init::{pin_data, pin_init, PinInit, Wrapper};
|
| 15 |
|
| 16 | pub mod mutex;
|
| 17 | pub mod spinlock;
|
| 18 |
|
| 19 | pub(super) mod global;
|
| 20 | pub use global::{GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
|
| 21 |
|
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | pub unsafe trait Backend {
|
| 38 | |
| 39 | type State;
|
| 40 |
|
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | type GuardState;
|
| 46 |
|
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | unsafe fn init(
|
| 54 | ptr: *mut Self::State,
|
| 55 | name: *const crate::ffi::c_char,
|
| 56 | key: *mut bindings::lock_class_key,
|
| 57 | );
|
| 58 |
|
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | #[must_use]
|
| 65 | unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState;
|
| 66 |
|
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState>;
|
| 73 |
|
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | unsafe fn unlock(ptr: *mut Self::State, guard_state: &Self::GuardState);
|
| 80 |
|
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) {
|
| 88 | |
| 89 | *guard_state = unsafe { Self::lock(ptr) };
|
| 90 | }
|
| 91 |
|
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | unsafe fn assert_is_held(ptr: *mut Self::State);
|
| 98 | }
|
| 99 |
|
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | #[repr(C)]
|
| 105 | #[pin_data]
|
| 106 | pub struct Lock<T: ?Sized, B: Backend> {
|
| 107 | |
| 108 | #[pin]
|
| 109 | state: Opaque<B::State>,
|
| 110 |
|
| 111 | |
| 112 | |
| 113 | |
| 114 | #[pin]
|
| 115 | _pin: PhantomPinned,
|
| 116 |
|
| 117 | |
| 118 | #[pin]
|
| 119 | pub(crate) data: UnsafeCell<T>,
|
| 120 | }
|
| 121 |
|
| 122 | |
| 123 | unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {}
|
| 124 |
|
| 125 | |
| 126 | |
| 127 | unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
|
| 128 |
|
| 129 | impl<T, B: Backend> Lock<T, B> {
|
| 130 | |
| 131 | pub fn new(
|
| 132 | t: impl PinInit<T>,
|
| 133 | name: &'static CStr,
|
| 134 | key: Pin<&'static LockClassKey>,
|
| 135 | ) -> impl PinInit<Self> {
|
| 136 | pin_init!(Self {
|
| 137 | data <- UnsafeCell::pin_init(t),
|
| 138 | _pin: PhantomPinned,
|
| 139 | |
| 140 | |
| 141 | state <- Opaque::ffi_init(|slot| unsafe {
|
| 142 | B::init(slot, name.as_char_ptr(), key.as_ptr())
|
| 143 | }),
|
| 144 | })
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | impl<B: Backend> Lock<(), B> {
|
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | #[inline]
|
| 160 | pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self {
|
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | unsafe { &*ptr.cast() }
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | impl<T: ?Sized, B: Backend> Lock<T, B> {
|
| 172 | |
| 173 | #[inline]
|
| 174 | pub fn lock(&self) -> Guard<'_, T, B> {
|
| 175 | |
| 176 | |
| 177 | let state = unsafe { B::lock(self.state.get()) };
|
| 178 | |
| 179 | unsafe { Guard::new(self, state) }
|
| 180 | }
|
| 181 |
|
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | #[must_use = "if unused, the lock will be immediately unlocked"]
|
| 187 | #[inline]
|
| 188 | pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
|
| 189 | |
| 190 | |
| 191 | unsafe { B::try_lock(self.state.get()).map(|state| Guard::new(self, state)) }
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | #[must_use = "the lock unlocks immediately when the guard is unused"]
|
| 201 | pub struct Guard<'a, T: ?Sized, B: Backend> {
|
| 202 | pub(crate) lock: &'a Lock<T, B>,
|
| 203 | pub(crate) state: B::GuardState,
|
| 204 | _not_send: NotThreadSafe,
|
| 205 | }
|
| 206 |
|
| 207 | |
| 208 | unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
|
| 209 |
|
| 210 | impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
|
| 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 | pub fn lock_ref(&self) -> &'a Lock<T, B> {
|
| 238 | self.lock
|
| 239 | }
|
| 240 |
|
| 241 | pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
|
| 242 | |
| 243 | unsafe { B::unlock(self.lock.state.get(), &self.state) };
|
| 244 |
|
| 245 | let _relock = ScopeGuard::new(||
|
| 246 | |
| 247 | unsafe { B::relock(self.lock.state.get(), &mut self.state) });
|
| 248 |
|
| 249 | cb()
|
| 250 | }
|
| 251 |
|
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | pub fn as_mut(&mut self) -> Pin<&mut T> {
|
| 273 | |
| 274 | unsafe { Pin::new_unchecked(&mut *self.lock.data.get()) }
|
| 275 | }
|
| 276 | }
|
| 277 |
|
| 278 | impl<T: ?Sized, B: Backend> core::ops::Deref for Guard<'_, T, B> {
|
| 279 | type Target = T;
|
| 280 |
|
| 281 | #[inline]
|
| 282 | fn deref(&self) -> &Self::Target {
|
| 283 | |
| 284 | unsafe { &*self.lock.data.get() }
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B>
|
| 289 | where
|
| 290 | T: Unpin,
|
| 291 | {
|
| 292 | #[inline]
|
| 293 | fn deref_mut(&mut self) -> &mut Self::Target {
|
| 294 | |
| 295 | unsafe { &mut *self.lock.data.get() }
|
| 296 | }
|
| 297 | }
|
| 298 |
|
| 299 | impl<T: ?Sized, B: Backend> Drop for Guard<'_, T, B> {
|
| 300 | #[inline]
|
| 301 | fn drop(&mut self) {
|
| 302 | |
| 303 | unsafe { B::unlock(self.lock.state.get(), &self.state) };
|
| 304 | }
|
| 305 | }
|
| 306 |
|
| 307 | impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
|
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | #[inline]
|
| 314 | pub unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self {
|
| 315 | |
| 316 | unsafe { B::assert_is_held(lock.state.get()) };
|
| 317 |
|
| 318 | Self {
|
| 319 | lock,
|
| 320 | state,
|
| 321 | _not_send: NotThreadSafe,
|
| 322 | }
|
| 323 | }
|
| 324 | }
|