1 // SPDX-License-Identifier: GPL-2.0
5 //! This module allows Rust code to use the kernel's `spinlock_t`.
9 /// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class.
11 /// It uses the name if one is given, otherwise it generates one based on the file name and line
14 macro_rules! new_spinlock {
15 ($inner:expr $(, $name:literal)? $(,)?) => {
16 $crate::sync::SpinLock::new(
17 $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
23 /// Exposes the kernel's [`spinlock_t`]. When multiple CPUs attempt to lock the same spinlock, only
24 /// one at a time is allowed to progress, the others will block (spinning) until the spinlock is
25 /// unlocked, at which point another CPU will be allowed to make progress.
27 /// Instances of [`SpinLock`] need a lock class and to be pinned. The recommended way to create such
28 /// instances is with the [`pin_init`](crate::pin_init) and [`new_spinlock`] macros.
32 /// The following example shows how to declare, allocate and initialise a struct (`Example`) that
33 /// contains an inner struct (`Inner`) that is protected by a spinlock.
36 /// use kernel::{init::InPlaceInit, init::PinInit, new_spinlock, pin_init, sync::SpinLock};
47 /// d: SpinLock<Inner>,
51 /// fn new() -> impl PinInit<Self> {
54 /// d <- new_spinlock!(Inner { a: 20, b: 30 }),
59 /// // Allocate a boxed `Example`.
60 /// let e = Box::pin_init(Example::new())?;
61 /// assert_eq!(e.c, 10);
62 /// assert_eq!(e.d.lock().a, 20);
63 /// assert_eq!(e.d.lock().b, 30);
64 /// # Ok::<(), Error>(())
67 /// The following example shows how to use interior mutability to modify the contents of a struct
68 /// protected by a spinlock despite only having a shared reference:
71 /// use kernel::sync::SpinLock;
78 /// fn example(m: &SpinLock<Example>) {
79 /// let mut guard = m.lock();
85 /// [`spinlock_t`]: srctree/include/linux/spinlock.h
86 pub type SpinLock<T> = super::Lock<T, SpinLockBackend>;
88 /// A kernel `spinlock_t` lock backend.
89 pub struct SpinLockBackend;
91 // SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion. `relock` uses the
92 // default implementation that always calls the same locking method.
93 unsafe impl super::Backend for SpinLockBackend {
94 type State = bindings::spinlock_t;
98 ptr: *mut Self::State,
99 name: *const core::ffi::c_char,
100 key: *mut bindings::lock_class_key,
102 // SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and
103 // `key` are valid for read indefinitely.
104 unsafe { bindings::__spin_lock_init(ptr, name, key) }
107 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
108 // SAFETY: The safety requirements of this function ensure that `ptr` points to valid
109 // memory, and that it has been initialised before.
110 unsafe { bindings::spin_lock(ptr) }
113 unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
114 // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
115 // caller is the owner of the mutex.
116 unsafe { bindings::spin_unlock(ptr) }