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 /// The state required by the lock.
29 type State; typedef
31 /// The state required to be kept between lock and unlock.
34 /// Initialises the lock.
41 ptr: *mut Self::State, in init() argument
46 /// Acquires the lock, making the caller its owner.
52 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState; in lock() method
54 /// Releases the lock, giving up its ownership.
58 /// It must only be called by the current owner of the lock.
59 unsafe fn unlock(ptr: *mut Self::State, guard_state: &Self::GuardState); in unlock() argument
61 /// Reacquires the lock, making the caller its owner.
65 /// Callers must ensure that `guard_state` comes from a previous call to [`Backend::lock`] (or
67 unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) { in relock() argument
68 // SAFETY: The safety requirements ensure that the lock is initialised. in relock()
69 *guard_state = unsafe { Self::lock(ptr) }; in relock()
75 /// Exposes one of the kernel locking primitives. Which one is exposed depends on the lock
78 pub struct Lock<T: ?Sized, B: Backend> { struct
79 /// The kernel lock object.
81 state: Opaque<B::State>, field
83 /// Some locks are known to be self-referential (e.g., mutexes), while others are architecture
85 /// some architecture uses self-references now or in the future.
89 /// The data protected by the lock.
93 // SAFETY: `Lock` can be transferred across thread boundaries iff the data it protects can. argument
94 unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {} implementation
96 // SAFETY: `Lock` serialises the interior mutability it provides, so it is `Sync` as long as the
98 unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {} implementation
100 impl<T, B: Backend> Lock<T, B> { implementation
101 /// Constructs a new lock initialiser.
102 pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> { in new()
108 state <- Opaque::ffi_init(|slot| unsafe { in new()
115 impl<T: ?Sized, B: Backend> Lock<T, B> { impl
116 /// Acquires the lock and gives the caller access to the data protected by it.
117 pub fn lock(&self) -> Guard<'_, T, B> { in lock() method
120 let state = unsafe { B::lock(self.state.get()) }; in lock() localVariable
121 // SAFETY: The lock was just acquired. in lock()
122 unsafe { Guard::new(self, state) } in lock()
126 /// A lock guard.
130 /// protected by the lock.
131 #[must_use = "the lock unlocks immediately when the guard is unused"]
133 pub(crate) lock: &'a Lock<T, B>, field
134 pub(crate) state: B::GuardState, field
138 // SAFETY: `Guard` is sync when the data protected by the lock is also sync.
143 // SAFETY: The caller owns the lock, so it is safe to unlock it. in do_unlocked()
144 unsafe { B::unlock(self.lock.state.get(), &self.state) }; in do_unlocked()
146 // SAFETY: The lock was just unlocked above and is being relocked now. in do_unlocked()
148 ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &mut self.state) }); in do_unlocked()
157 fn deref(&self) -> &Self::Target { in deref()
158 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. in deref()
159 unsafe { &*self.lock.data.get() } in deref()
164 fn deref_mut(&mut self) -> &mut Self::Target { in deref_mut()
165 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. in deref_mut()
166 unsafe { &mut *self.lock.data.get() } in deref_mut()
172 // SAFETY: The caller owns the lock, so it is safe to unlock it. in drop()
173 unsafe { B::unlock(self.lock.state.get(), &self.state) }; in drop()
178 /// Constructs a new immutable lock guard.
182 /// The caller must ensure that it owns the lock.
183 pub(crate) unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self { in new()
185 lock, in new()
186 state, in new()