| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | use super::LockClassKey;
|
| 9 | use crate::{init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard};
|
| 10 | use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned};
|
| 11 | use macros::pin_data;
|
| 12 |
|
| 13 | pub mod mutex;
|
| 14 | pub mod spinlock;
|
| 15 |
|
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | pub unsafe trait Backend {
|
| 32 | |
| 33 | type State;
|
| 34 |
|
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | type GuardState;
|
| 40 |
|
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | unsafe fn init(
|
| 48 | ptr: *mut Self::State,
|
| 49 | name: *const core::ffi::c_char,
|
| 50 | key: *mut bindings::lock_class_key,
|
| 51 | );
|
| 52 |
|
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | #[must_use]
|
| 59 | unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState;
|
| 60 |
|
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | unsafe fn unlock(ptr: *mut Self::State, guard_state: &Self::GuardState);
|
| 67 |
|
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) {
|
| 75 | |
| 76 | *guard_state = unsafe { Self::lock(ptr) };
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | #[pin_data]
|
| 85 | pub struct Lock<T: ?Sized, B: Backend> {
|
| 86 | |
| 87 | #[pin]
|
| 88 | state: Opaque<B::State>,
|
| 89 |
|
| 90 | |
| 91 | |
| 92 | |
| 93 | #[pin]
|
| 94 | _pin: PhantomPinned,
|
| 95 |
|
| 96 | |
| 97 | pub(crate) data: UnsafeCell<T>,
|
| 98 | }
|
| 99 |
|
| 100 | |
| 101 | unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {}
|
| 102 |
|
| 103 | |
| 104 | |
| 105 | unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
|
| 106 |
|
| 107 | impl<T, B: Backend> Lock<T, B> {
|
| 108 | |
| 109 | pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> {
|
| 110 | pin_init!(Self {
|
| 111 | data: UnsafeCell::new(t),
|
| 112 | _pin: PhantomPinned,
|
| 113 | |
| 114 | |
| 115 | state <- Opaque::ffi_init(|slot| unsafe {
|
| 116 | B::init(slot, name.as_char_ptr(), key.as_ptr())
|
| 117 | }),
|
| 118 | })
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | impl<T: ?Sized, B: Backend> Lock<T, B> {
|
| 123 | |
| 124 | pub fn lock(&self) -> Guard<'_, T, B> {
|
| 125 | |
| 126 | |
| 127 | let state = unsafe { B::lock(self.state.get()) };
|
| 128 | |
| 129 | unsafe { Guard::new(self, state) }
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | #[must_use = "the lock unlocks immediately when the guard is unused"]
|
| 139 | pub struct Guard<'a, T: ?Sized, B: Backend> {
|
| 140 | pub(crate) lock: &'a Lock<T, B>,
|
| 141 | pub(crate) state: B::GuardState,
|
| 142 | _not_send: PhantomData<*mut ()>,
|
| 143 | }
|
| 144 |
|
| 145 | |
| 146 | unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
|
| 147 |
|
| 148 | impl<T: ?Sized, B: Backend> Guard<'_, T, B> {
|
| 149 | pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
|
| 150 | |
| 151 | unsafe { B::unlock(self.lock.state.get(), &self.state) };
|
| 152 |
|
| 153 | |
| 154 | let _relock =
|
| 155 | ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &mut self.state) });
|
| 156 |
|
| 157 | cb()
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | impl<T: ?Sized, B: Backend> core::ops::Deref for Guard<'_, T, B> {
|
| 162 | type Target = T;
|
| 163 |
|
| 164 | fn deref(&self) -> &Self::Target {
|
| 165 | |
| 166 | unsafe { &*self.lock.data.get() }
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B> {
|
| 171 | fn deref_mut(&mut self) -> &mut Self::Target {
|
| 172 | |
| 173 | unsafe { &mut *self.lock.data.get() }
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | impl<T: ?Sized, B: Backend> Drop for Guard<'_, T, B> {
|
| 178 | fn drop(&mut self) {
|
| 179 | |
| 180 | unsafe { B::unlock(self.lock.state.get(), &self.state) };
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
|
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | pub(crate) unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self {
|
| 191 | Self {
|
| 192 | lock,
|
| 193 | state,
|
| 194 | _not_send: PhantomData,
|
| 195 | }
|
| 196 | }
|
| 197 | }
|