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,
16 /// The "backend" of a lock.
18 /// It is the actual implementation of the lock, without the need to repeat patterns used in all
23 /// - Implementers must ensure that only one thread/CPU may access the protected data once the lock
24 /// is owned, that is, between calls to [`lock`] and [`unlock`].
25 /// - Implementers must also ensure that [`relock`] uses the same locking method as the original
26 /// lock operation.
28 /// [`lock`]: Backend::lock
32 /// The state required by the lock.
33 type State; typedef
35 /// The state required to be kept between [`lock`] and [`unlock`].
37 /// [`lock`]: Backend::lock
41 /// Initialises the lock.
48 ptr: *mut Self::State, in init() argument
53 /// Acquires the lock, making the caller its owner.
59 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState; in lock() method
61 /// Releases the lock, giving up its ownership.
65 /// It must only be called by the current owner of the lock.
66 unsafe fn unlock(ptr: *mut Self::State, guard_state: &Self::GuardState); in unlock() argument
68 /// Reacquires the lock, making the caller its owner.
72 /// Callers must ensure that `guard_state` comes from a previous call to [`Backend::lock`] (or
74 unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) { in relock() argument
75 // SAFETY: The safety requirements ensure that the lock is initialised. in relock()
76 *guard_state = unsafe { Self::lock(ptr) }; in relock()
82 /// Exposes one of the kernel locking primitives. Which one is exposed depends on the lock
85 pub struct Lock<T: ?Sized, B: Backend> { struct
86 /// The kernel lock object.
88 state: Opaque<B::State>, field
90 /// Some locks are known to be self-referential (e.g., mutexes), while others are architecture
92 /// some architecture uses self-references now or in the future.
96 /// The data protected by the lock.
100 // SAFETY: `Lock` can be transferred across thread boundaries iff the data it protects can. argument
101 unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {} implementation
103 // SAFETY: `Lock` serialises the interior mutability it provides, so it is `Sync` as long as the
105 unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {} implementation
107 impl<T, B: Backend> Lock<T, B> { implementation
108 /// Constructs a new lock initialiser.
109 pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> { in new()
115 state <- Opaque::ffi_init(|slot| unsafe { in new()
122 impl<T: ?Sized, B: Backend> Lock<T, B> { implementation
123 /// Acquires the lock and gives the caller access to the data protected by it.
124 pub fn lock(&self) -> Guard<'_, T, B> { in lock() method
127 let state = unsafe { B::lock(self.state.get()) }; in lock() localVariable
128 // SAFETY: The lock was just acquired. in lock()
129 unsafe { Guard::new(self, state) } in lock()
133 /// A lock guard.
137 /// protected by the lock.
138 #[must_use = "the lock unlocks immediately when the guard is unused"]
140 pub(crate) lock: &'a Lock<T, B>, field
141 pub(crate) state: B::GuardState, field
145 // SAFETY: `Guard` is sync when the data protected by the lock is also sync.
149 pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U { in do_unlocked()
150 // SAFETY: The caller owns the lock, so it is safe to unlock it. in do_unlocked()
151 unsafe { B::unlock(self.lock.state.get(), &self.state) }; in do_unlocked()
153 // SAFETY: The lock was just unlocked above and is being relocked now. in do_unlocked()
155 ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &mut self.state) }); in do_unlocked()
164 fn deref(&self) -> &Self::Target { in deref()
165 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. in deref()
166 unsafe { &*self.lock.data.get() } in deref()
171 fn deref_mut(&mut self) -> &mut Self::Target { in deref_mut()
172 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. in deref_mut()
173 unsafe { &mut *self.lock.data.get() } in deref_mut()
179 // SAFETY: The caller owns the lock, so it is safe to unlock it. in drop()
180 unsafe { B::unlock(self.lock.state.get(), &self.state) }; in drop()
185 /// Constructs a new immutable lock guard.
189 /// The caller must ensure that it owns the lock.
190 pub(crate) unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self { in new()
192 lock, in new()
193 state, in new()