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,
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`.
26 /// lock operation.
28 /// The state required by the lock.
31 /// The state required to be kept between lock and unlock.
34 /// Initialises the lock.
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.
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
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.
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> { impl
101 /// Constructs a new lock initialiser.
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()
121 // SAFETY: The lock was just acquired. 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
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()
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()
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()