Lines Matching full:lock
3 //! Generic kernel lock and guard.
5 //! It contains a generic Rust lock and guard that allow for different backends (e.g., mutexes,
22 /// The "backend" of a lock.
24 /// It is the actual implementation of the lock, without the need to repeat patterns used in all
29 /// - Implementers must ensure that only one thread/CPU may access the protected data once the lock
30 /// is owned, that is, between calls to [`lock`] and [`unlock`].
32 /// lock operation.
34 /// [`lock`]: Backend::lock
38 /// The state required by the lock.
41 /// The state required to be kept between [`lock`] and [`unlock`].
43 /// [`lock`]: Backend::lock
47 /// Initialises the lock.
59 /// Acquires the lock, making the caller its owner.
65 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState;
67 /// Tries to acquire the lock.
74 /// Releases the lock, giving up its ownership.
78 /// It must only be called by the current owner of the lock.
81 /// Reacquires the lock, making the caller its owner.
85 /// Callers must ensure that `guard_state` comes from a previous call to [`Backend::lock`] (or
88 // SAFETY: The safety requirements ensure that the lock is initialised.
89 *guard_state = unsafe { Self::lock(ptr) };
92 /// Asserts that the lock is held using lockdep.
102 /// Exposes one of the kernel locking primitives. Which one is exposed depends on the lock
106 pub struct Lock<T: ?Sized, B: Backend> {
107 /// The kernel lock object.
117 /// The data protected by the lock.
122 // SAFETY: `Lock` can be transferred across thread boundaries iff the data it protects can.
123 unsafe impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B> {}
125 // SAFETY: `Lock` serialises the interior mutability it provides, so it is `Sync` as long as the
127 unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
129 impl<T, B: Backend> Lock<T, B> {
130 /// Constructs a new lock initialiser.
148 impl<B: Backend> Lock<(), B> {
149 /// Constructs a [`Lock`] from a raw pointer.
151 /// This can be useful for interacting with a lock which was initialised outside of Rust.
163 // - Since the lock data type is `()` which is a ZST, `state` is the only non-ZST member of
171 impl<T: ?Sized, B: Backend> Lock<T, B> {
172 /// Acquires the lock and gives the caller access to the data protected by it.
174 pub fn lock(&self) -> Guard<'_, T, B> {
177 let state = unsafe { B::lock(self.state.get()) };
178 // SAFETY: The lock was just acquired.
182 /// Tries to acquire the lock.
184 /// Returns a guard that can be used to access the data protected by the lock if successful.
186 #[must_use = "if unused, the lock will be immediately unlocked"]
195 /// A lock guard.
199 /// protected by the lock.
200 #[must_use = "the lock unlocks immediately when the guard is unused"]
202 pub(crate) lock: &'a Lock<T, B>,
207 // SAFETY: `Guard` is sync when the data protected by the lock is also sync.
211 /// Returns the lock that this guard originates from.
216 /// lock is held.
219 /// # use kernel::{new_spinlock, sync::lock::{Backend, Guard, Lock}};
222 /// fn assert_held<T, B: Backend>(guard: &Guard<'_, T, B>, lock: &Lock<T, B>) {
223 /// // Address-equal means the same lock.
224 /// assert!(core::ptr::eq(guard.lock_ref(), lock));
227 /// // Creates a new lock on the stack.
232 /// let g = l.lock();
237 pub fn lock_ref(&self) -> &'a Lock<T, B> {
238 self.lock
242 // SAFETY: The caller owns the lock, so it is safe to unlock it.
243 unsafe { B::unlock(self.lock.state.get(), &self.state) };
246 // SAFETY: The lock was just unlocked above and is being relocked now.
247 unsafe { B::relock(self.lock.state.get(), &mut self.state) });
268 /// let mut data: MutexGuard<'_, Data> = mutex.lock();
273 // SAFETY: `self.lock.data` is structurally pinned.
274 unsafe { Pin::new_unchecked(&mut *self.lock.data.get()) }
283 // SAFETY: The caller owns the lock, so it is safe to deref the protected data.
284 unsafe { &*self.lock.data.get() }
294 // SAFETY: The caller owns the lock, so it is safe to deref the protected data.
295 unsafe { &mut *self.lock.data.get() }
302 // SAFETY: The caller owns the lock, so it is safe to unlock it.
303 unsafe { B::unlock(self.lock.state.get(), &self.state) };
308 /// Constructs a new immutable lock guard.
312 /// The caller must ensure that it owns the lock.
314 pub unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self {
315 // SAFETY: The caller can only hold the lock if `Backend::init` has already been called.
316 unsafe { B::assert_is_held(lock.state.get()) };
319 lock,