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 /// [`lock`]: Backend::lock
32 /// The state required by the lock.
35 /// The state required to be kept between [`lock`] and [`unlock`].
37 /// [`lock`]: Backend::lock
41 /// Initialises the lock.
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.
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
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.
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.
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()
128 // SAFETY: The lock was just acquired. 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
145 // SAFETY: `Guard` is sync when the data protected by the lock is also sync.
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()
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()
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()