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