xref: /linux/rust/kernel/sync/lock/mutex.rs (revision e283ee239220908118d66eea46dd8bb6175767b2)
16d20d629SWedson Almeida Filho // SPDX-License-Identifier: GPL-2.0
26d20d629SWedson Almeida Filho 
36d20d629SWedson Almeida Filho //! A kernel mutex.
46d20d629SWedson Almeida Filho //!
56d20d629SWedson Almeida Filho //! This module allows Rust code to use the kernel's `struct mutex`.
66d20d629SWedson Almeida Filho 
76d20d629SWedson Almeida Filho use crate::bindings;
86d20d629SWedson Almeida Filho 
96d20d629SWedson Almeida Filho /// Creates a [`Mutex`] initialiser with the given name and a newly-created lock class.
106d20d629SWedson Almeida Filho ///
116d20d629SWedson Almeida Filho /// It uses the name if one is given, otherwise it generates one based on the file name and line
126d20d629SWedson Almeida Filho /// number.
136d20d629SWedson Almeida Filho #[macro_export]
146d20d629SWedson Almeida Filho macro_rules! new_mutex {
156d20d629SWedson Almeida Filho     ($inner:expr $(, $name:literal)? $(,)?) => {
166d20d629SWedson Almeida Filho         $crate::sync::Mutex::new(
176d20d629SWedson Almeida Filho             $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
186d20d629SWedson Almeida Filho     };
196d20d629SWedson Almeida Filho }
20*e283ee23SAlice Ryhl pub use new_mutex;
216d20d629SWedson Almeida Filho 
226d20d629SWedson Almeida Filho /// A mutual exclusion primitive.
236d20d629SWedson Almeida Filho ///
246d20d629SWedson Almeida Filho /// Exposes the kernel's [`struct mutex`]. When multiple threads attempt to lock the same mutex,
256d20d629SWedson Almeida Filho /// only one at a time is allowed to progress, the others will block (sleep) until the mutex is
266d20d629SWedson Almeida Filho /// unlocked, at which point another thread will be allowed to wake up and make progress.
276d20d629SWedson Almeida Filho ///
286d20d629SWedson Almeida Filho /// Since it may block, [`Mutex`] needs to be used with care in atomic contexts.
296d20d629SWedson Almeida Filho ///
306d20d629SWedson Almeida Filho /// Instances of [`Mutex`] need a lock class and to be pinned. The recommended way to create such
316d20d629SWedson Almeida Filho /// instances is with the [`pin_init`](crate::pin_init) and [`new_mutex`] macros.
326d20d629SWedson Almeida Filho ///
336d20d629SWedson Almeida Filho /// # Examples
346d20d629SWedson Almeida Filho ///
356d20d629SWedson Almeida Filho /// The following example shows how to declare, allocate and initialise a struct (`Example`) that
366d20d629SWedson Almeida Filho /// contains an inner struct (`Inner`) that is protected by a mutex.
376d20d629SWedson Almeida Filho ///
386d20d629SWedson Almeida Filho /// ```
39*e283ee23SAlice Ryhl /// use kernel::sync::{new_mutex, Mutex};
406d20d629SWedson Almeida Filho ///
416d20d629SWedson Almeida Filho /// struct Inner {
426d20d629SWedson Almeida Filho ///     a: u32,
436d20d629SWedson Almeida Filho ///     b: u32,
446d20d629SWedson Almeida Filho /// }
456d20d629SWedson Almeida Filho ///
466d20d629SWedson Almeida Filho /// #[pin_data]
476d20d629SWedson Almeida Filho /// struct Example {
486d20d629SWedson Almeida Filho ///     c: u32,
496d20d629SWedson Almeida Filho ///     #[pin]
506d20d629SWedson Almeida Filho ///     d: Mutex<Inner>,
516d20d629SWedson Almeida Filho /// }
526d20d629SWedson Almeida Filho ///
536d20d629SWedson Almeida Filho /// impl Example {
546d20d629SWedson Almeida Filho ///     fn new() -> impl PinInit<Self> {
556d20d629SWedson Almeida Filho ///         pin_init!(Self {
566d20d629SWedson Almeida Filho ///             c: 10,
576d20d629SWedson Almeida Filho ///             d <- new_mutex!(Inner { a: 20, b: 30 }),
586d20d629SWedson Almeida Filho ///         })
596d20d629SWedson Almeida Filho ///     }
606d20d629SWedson Almeida Filho /// }
616d20d629SWedson Almeida Filho ///
626d20d629SWedson Almeida Filho /// // Allocate a boxed `Example`.
636d20d629SWedson Almeida Filho /// let e = Box::pin_init(Example::new())?;
646d20d629SWedson Almeida Filho /// assert_eq!(e.c, 10);
656d20d629SWedson Almeida Filho /// assert_eq!(e.d.lock().a, 20);
666d20d629SWedson Almeida Filho /// assert_eq!(e.d.lock().b, 30);
67bfa7dff0SMiguel Ojeda /// # Ok::<(), Error>(())
686d20d629SWedson Almeida Filho /// ```
696d20d629SWedson Almeida Filho ///
706d20d629SWedson Almeida Filho /// The following example shows how to use interior mutability to modify the contents of a struct
716d20d629SWedson Almeida Filho /// protected by a mutex despite only having a shared reference:
726d20d629SWedson Almeida Filho ///
736d20d629SWedson Almeida Filho /// ```
746d20d629SWedson Almeida Filho /// use kernel::sync::Mutex;
756d20d629SWedson Almeida Filho ///
766d20d629SWedson Almeida Filho /// struct Example {
776d20d629SWedson Almeida Filho ///     a: u32,
786d20d629SWedson Almeida Filho ///     b: u32,
796d20d629SWedson Almeida Filho /// }
806d20d629SWedson Almeida Filho ///
816d20d629SWedson Almeida Filho /// fn example(m: &Mutex<Example>) {
826d20d629SWedson Almeida Filho ///     let mut guard = m.lock();
836d20d629SWedson Almeida Filho ///     guard.a += 10;
846d20d629SWedson Almeida Filho ///     guard.b += 20;
856d20d629SWedson Almeida Filho /// }
866d20d629SWedson Almeida Filho /// ```
876d20d629SWedson Almeida Filho ///
88bc2e7d5cSMiguel Ojeda /// [`struct mutex`]: srctree/include/linux/mutex.h
896d20d629SWedson Almeida Filho pub type Mutex<T> = super::Lock<T, MutexBackend>;
906d20d629SWedson Almeida Filho 
916d20d629SWedson Almeida Filho /// A kernel `struct mutex` lock backend.
926d20d629SWedson Almeida Filho pub struct MutexBackend;
936d20d629SWedson Almeida Filho 
946d20d629SWedson Almeida Filho // SAFETY: The underlying kernel `struct mutex` object ensures mutual exclusion.
956d20d629SWedson Almeida Filho unsafe impl super::Backend for MutexBackend {
966d20d629SWedson Almeida Filho     type State = bindings::mutex;
976d20d629SWedson Almeida Filho     type GuardState = ();
986d20d629SWedson Almeida Filho 
996d20d629SWedson Almeida Filho     unsafe fn init(
1006d20d629SWedson Almeida Filho         ptr: *mut Self::State,
1016d20d629SWedson Almeida Filho         name: *const core::ffi::c_char,
1026d20d629SWedson Almeida Filho         key: *mut bindings::lock_class_key,
1036d20d629SWedson Almeida Filho     ) {
1046d20d629SWedson Almeida Filho         // SAFETY: The safety requirements ensure that `ptr` is valid for writes, and `name` and
1056d20d629SWedson Almeida Filho         // `key` are valid for read indefinitely.
1066d20d629SWedson Almeida Filho         unsafe { bindings::__mutex_init(ptr, name, key) }
1076d20d629SWedson Almeida Filho     }
1086d20d629SWedson Almeida Filho 
1096d20d629SWedson Almeida Filho     unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
1106d20d629SWedson Almeida Filho         // SAFETY: The safety requirements of this function ensure that `ptr` points to valid
1116d20d629SWedson Almeida Filho         // memory, and that it has been initialised before.
1126d20d629SWedson Almeida Filho         unsafe { bindings::mutex_lock(ptr) };
1136d20d629SWedson Almeida Filho     }
1146d20d629SWedson Almeida Filho 
1156d20d629SWedson Almeida Filho     unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
1166d20d629SWedson Almeida Filho         // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
1176d20d629SWedson Almeida Filho         // caller is the owner of the mutex.
1186d20d629SWedson Almeida Filho         unsafe { bindings::mutex_unlock(ptr) };
1196d20d629SWedson Almeida Filho     }
1206d20d629SWedson Almeida Filho }
121