|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! A kernel mutex. |
| 4 | +//! |
| 5 | +//! This module allows Rust code to use the kernel's [`struct mutex`]. |
| 6 | +
|
| 7 | +use super::{Guard, Lock, NeedsLockClass}; |
| 8 | +use crate::{bindings, CStr}; |
| 9 | +use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin}; |
| 10 | + |
| 11 | +/// Safely initialises a [`Mutex`] with the given name, generating a new lock class. |
| 12 | +#[macro_export] |
| 13 | +macro_rules! mutex_init { |
| 14 | + ($mutex:expr, $name:literal) => { |
| 15 | + $crate::init_with_lockdep!($mutex, $name) |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +/// Exposes the kernel's [`struct mutex`]. When multiple threads attempt to lock the same mutex, |
| 20 | +/// only one at a time is allowed to progress, the others will block (sleep) until the mutex is |
| 21 | +/// unlocked, at which point another thread will be allowed to wake up and make progress. |
| 22 | +/// |
| 23 | +/// A [`Mutex`] must first be initialised with a call to [`Mutex::init`] before it can be used. The |
| 24 | +/// [`mutex_init`] macro is provided to automatically assign a new lock class to a mutex instance. |
| 25 | +/// |
| 26 | +/// Since it may block, [`Mutex`] needs to be used with care in atomic contexts. |
| 27 | +/// |
| 28 | +/// [`struct mutex`]: ../../../include/linux/mutex.h |
| 29 | +pub struct Mutex<T: ?Sized> { |
| 30 | + /// The kernel `struct mutex` object. |
| 31 | + mutex: UnsafeCell<bindings::mutex>, |
| 32 | + |
| 33 | + /// A mutex needs to be pinned because it contains a [`struct list_head`] that is |
| 34 | + /// self-referential, so it cannot be safely moved once it is initialised. |
| 35 | + _pin: PhantomPinned, |
| 36 | + |
| 37 | + /// The data protected by the mutex. |
| 38 | + data: UnsafeCell<T>, |
| 39 | +} |
| 40 | + |
| 41 | +// SAFETY: `Mutex` can be transferred across thread boundaries iff the data it protects can. |
| 42 | +unsafe impl<T: ?Sized + Send> Send for Mutex<T> {} |
| 43 | + |
| 44 | +// SAFETY: `Mutex` serialises the interior mutability it provides, so it is `Sync` as long as the |
| 45 | +// data it protects is `Send`. |
| 46 | +unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {} |
| 47 | + |
| 48 | +impl<T> Mutex<T> { |
| 49 | + /// Constructs a new mutex. |
| 50 | + /// |
| 51 | + /// # Safety |
| 52 | + /// |
| 53 | + /// The caller must call [`Mutex::init`] before using the mutex. |
| 54 | + pub unsafe fn new(t: T) -> Self { |
| 55 | + Self { |
| 56 | + mutex: UnsafeCell::new(bindings::mutex::default()), |
| 57 | + data: UnsafeCell::new(t), |
| 58 | + _pin: PhantomPinned, |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<T: ?Sized> Mutex<T> { |
| 64 | + /// Locks the mutex and gives the caller access to the data protected by it. Only one thread at |
| 65 | + /// a time is allowed to access the protected data. |
| 66 | + pub fn lock(&self) -> Guard<Self> { |
| 67 | + self.lock_noguard(); |
| 68 | + // SAFETY: The mutex was just acquired. |
| 69 | + unsafe { Guard::new(self) } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl<T: ?Sized> NeedsLockClass for Mutex<T> { |
| 74 | + unsafe fn init(self: Pin<&Self>, name: CStr<'static>, key: *mut bindings::lock_class_key) { |
| 75 | + bindings::__mutex_init(self.mutex.get(), name.as_ptr() as _, key); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<T: ?Sized> Lock for Mutex<T> { |
| 80 | + type Inner = T; |
| 81 | + |
| 82 | + #[cfg(not(CONFIG_DEBUG_LOCK_ALLOC))] |
| 83 | + fn lock_noguard(&self) { |
| 84 | + // SAFETY: `mutex` points to valid memory. |
| 85 | + unsafe { bindings::mutex_lock(self.mutex.get()) }; |
| 86 | + } |
| 87 | + |
| 88 | + #[cfg(CONFIG_DEBUG_LOCK_ALLOC)] |
| 89 | + fn lock_noguard(&self) { |
| 90 | + // SAFETY: `mutex` points to valid memory. |
| 91 | + unsafe { bindings::mutex_lock_nested(self.mutex.get(), 0) }; |
| 92 | + } |
| 93 | + |
| 94 | + unsafe fn unlock(&self) { |
| 95 | + bindings::mutex_unlock(self.mutex.get()); |
| 96 | + } |
| 97 | + |
| 98 | + unsafe fn locked_data(&self) -> &UnsafeCell<T> { |
| 99 | + &self.data |
| 100 | + } |
| 101 | +} |
0 commit comments