Lines Matching +full:lock +full:- +full:state

1 // SPDX-License-Identifier: GPL-2.0
3 //! Generic kernel lock and guard.
5 //! It contains a generic Rust lock and guard that allow for different backends (e.g., mutexes,
24 /// The "backend" of a lock.
26 /// It is the actual implementation of the lock, without the need to repeat patterns used in all
31 /// - Implementers must ensure that only one thread/CPU may access the protected data once the lock
32 /// is owned, that is, between calls to [`lock`] and [`unlock`].
33 /// - Implementers must also ensure that [`relock`] uses the same locking method as the original
34 /// lock operation.
36 /// [`lock`]: Backend::lock
40 /// The state required by the lock.
41 type State; typedef
43 /// The state required to be kept between [`lock`] and [`unlock`].
45 /// [`lock`]: Backend::lock
49 /// Initialises the lock.
56 ptr: *mut Self::State, in init() argument
61 /// Acquires the lock, making the caller its owner.
67 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState; in lock() method
69 /// Tries to acquire the lock.
74 unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState>; in try_lock()
76 /// Releases the lock, giving up its ownership.
80 /// It must only be called by the current owner of the lock.
81 unsafe fn unlock(ptr: *mut Self::State, guard_state: &Self::GuardState); in unlock() argument
83 /// Reacquires the lock, making the caller its owner.
87 /// Callers must ensure that `guard_state` comes from a previous call to [`Backend::lock`] (or
89 unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) { in relock() argument
90 // SAFETY: The safety requirements ensure that the lock is initialised. in relock()
91 *guard_state = unsafe { Self::lock(ptr) }; in relock()
94 /// Asserts that the lock is held using lockdep.
99 unsafe fn assert_is_held(ptr: *mut Self::State); in assert_is_held() argument
104 /// Exposes one of the kernel locking primitives. Which one is exposed depends on the lock
108 pub struct Lock<T: ?Sized, B: Backend> { struct
109 /// The kernel lock object.
111 state: Opaque<B::State>, field
113 /// Some locks are known to be self-referential (e.g., mutexes), while others are architecture
115 /// some architecture uses self-references now or in the future.
119 /// The data protected by the lock.
123 // SAFETY: `Lock` can be transferred across thread boundaries iff the data it protects can. argument
124 unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {} implementation
126 // SAFETY: `Lock` serialises the interior mutability it provides, so it is `Sync` as long as the
128 unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {} implementation
130 impl<T, B: Backend> Lock<T, B> { implementation
131 /// Constructs a new lock initialiser.
132 pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> { in new()
138 state <- Opaque::ffi_init(|slot| unsafe { in new()
145 impl<B: Backend> Lock<(), B> { impl
146 /// Constructs a [`Lock`] from a raw pointer.
148 /// This can be useful for interacting with a lock which was initialised outside of Rust.
152 /// The caller promises that `ptr` points to a valid initialised instance of [`State`] during
155 /// [`State`]: Backend::State
156 pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self { in from_raw()
158 // - By the safety contract `ptr` must point to a valid initialised instance of `B::State` in from_raw()
159 // - Since the lock data type is `()` which is a ZST, `state` is the only non-ZST member of in from_raw()
161 // - Combined with `#[repr(C)]`, this guarantees `Self` has an equivalent data layout to in from_raw()
162 // `B::State`. in from_raw()
167 impl<T: ?Sized, B: Backend> Lock<T, B> { impl
168 /// Acquires the lock and gives the caller access to the data protected by it.
169 pub fn lock(&self) -> Guard<'_, T, B> { in lock() method
172 let state = unsafe { B::lock(self.state.get()) }; in lock() localVariable
173 // SAFETY: The lock was just acquired. in lock()
174 unsafe { Guard::new(self, state) } in lock()
177 /// Tries to acquire the lock.
179 /// Returns a guard that can be used to access the data protected by the lock if successful.
180 pub fn try_lock(&self) -> Option<Guard<'_, T, B>> { in try_lock()
183 unsafe { B::try_lock(self.state.get()).map(|state| Guard::new(self, state)) } in try_lock()
187 /// A lock guard.
191 /// protected by the lock.
192 #[must_use = "the lock unlocks immediately when the guard is unused"]
194 pub(crate) lock: &'a Lock<T, B>, field
195 pub(crate) state: B::GuardState, field
199 // SAFETY: `Guard` is sync when the data protected by the lock is also sync.
203 pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U { in do_unlocked()
204 // SAFETY: The caller owns the lock, so it is safe to unlock it. in do_unlocked()
205 unsafe { B::unlock(self.lock.state.get(), &self.state) }; in do_unlocked()
208 // SAFETY: The lock was just unlocked above and is being relocked now. in do_unlocked()
209 unsafe { B::relock(self.lock.state.get(), &mut self.state) }); in do_unlocked()
218 fn deref(&self) -> &Self::Target { in deref()
219 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. in deref()
220 unsafe { &*self.lock.data.get() } in deref()
225 fn deref_mut(&mut self) -> &mut Self::Target { in deref_mut()
226 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. in deref_mut()
227 unsafe { &mut *self.lock.data.get() } in deref_mut()
233 // SAFETY: The caller owns the lock, so it is safe to unlock it. in drop()
234 unsafe { B::unlock(self.lock.state.get(), &self.state) }; in drop()
239 /// Constructs a new immutable lock guard.
243 /// The caller must ensure that it owns the lock.
244 pub unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self { in new()
245 // SAFETY: The caller can only hold the lock if `Backend::init` has already been called. in new()
246 unsafe { B::assert_is_held(lock.state.get()) }; in new()
249 lock, in new()
250 state, in new()