Skip to content

Commit 5900705

Browse files
hoshinolinamarcan
authored andcommitted
rust: sync: Classless Lock::new() and pin_init()
Use the new automagic lock class code to remove the lock class and name parameters from Lock::new() and Lock::pin_init(). The old functions are renamed to new_with_class() and pin_init_with_class() respectively. Signed-off-by: Asahi Lina <[email protected]>
1 parent f74738f commit 5900705

3 files changed

Lines changed: 41 additions & 7 deletions

File tree

rust/kernel/sync/lock.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! It contains a generic Rust lock and guard that allow for different backends (e.g., mutexes,
66
//! spinlocks, raw spinlocks) to be provided with minimal effort.
77
8-
use super::LockClassKey;
8+
use super::{lockdep::caller_lock_class, LockClassKey};
99
use crate::{
1010
bindings, init::PinInit, pin_init, str::CStr, try_pin_init, types::Opaque, types::ScopeGuard,
1111
};
@@ -103,7 +103,40 @@ unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
103103
impl<T, B: Backend> Lock<T, B> {
104104
/// Constructs a new lock initialiser.
105105
#[allow(clippy::new_ret_no_self)]
106-
pub fn new(t: T, name: &'static CStr, key: LockClassKey) -> impl PinInit<Self> {
106+
#[track_caller]
107+
pub fn new(t: T) -> impl PinInit<Self> {
108+
let (key, name) = caller_lock_class();
109+
Self::new_with_key(t, name, key)
110+
}
111+
112+
/// Constructs a new lock initialiser taking an initialiser/
113+
pub fn pin_init<E>(t: impl PinInit<T, E>) -> impl PinInit<Self, E>
114+
where
115+
E: core::convert::From<core::convert::Infallible>,
116+
{
117+
let (key, name) = caller_lock_class();
118+
Self::pin_init_with_key(t, name, key)
119+
}
120+
121+
/// Constructs a new lock initialiser.
122+
#[allow(clippy::new_ret_no_self)]
123+
#[track_caller]
124+
pub fn new_named(t: T, name: &'static CStr) -> impl PinInit<Self> {
125+
let (key, _) = caller_lock_class();
126+
Self::new_with_key(t, name, key)
127+
}
128+
129+
/// Constructs a new lock initialiser taking an initialiser/
130+
pub fn pin_init_named<E>(t: impl PinInit<T, E>, name: &'static CStr) -> impl PinInit<Self, E>
131+
where
132+
E: core::convert::From<core::convert::Infallible>,
133+
{
134+
let (key, _) = caller_lock_class();
135+
Self::pin_init_with_key(t, name, key)
136+
}
137+
138+
/// Constructs a new lock initialiser given a particular name and lock class key.
139+
pub fn new_with_key(t: T, name: &'static CStr, key: LockClassKey) -> impl PinInit<Self> {
107140
pin_init!(Self {
108141
data: UnsafeCell::new(t),
109142
_pin: PhantomPinned,
@@ -115,8 +148,9 @@ impl<T, B: Backend> Lock<T, B> {
115148
})
116149
}
117150

118-
/// Constructs a new lock initialiser taking an initialiser.
119-
pub fn pin_init<E>(
151+
/// Constructs a new lock initialiser taking an initialiser given a particular
152+
/// name and lock class key.
153+
pub fn pin_init_with_key<E>(
120154
t: impl PinInit<T, E>,
121155
name: &'static CStr,
122156
key: LockClassKey,

rust/kernel/sync/lock/mutex.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::bindings;
1313
#[macro_export]
1414
macro_rules! new_mutex {
1515
($inner:expr $(, $name:literal)? $(,)?) => {
16-
$crate::sync::Mutex::new(
16+
$crate::sync::Mutex::new_with_key(
1717
$inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
1818
};
1919
}
@@ -26,7 +26,7 @@ macro_rules! new_mutex {
2626
#[macro_export]
2727
macro_rules! new_mutex_pinned {
2828
($inner:expr $(, $name:literal)? $(,)?) => {
29-
$crate::sync::Mutex::pin_init(
29+
$crate::sync::Mutex::pin_init_with_key(
3030
$inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
3131
};
3232
}

rust/kernel/sync/lock/spinlock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::bindings;
1313
#[macro_export]
1414
macro_rules! new_spinlock {
1515
($inner:expr $(, $name:literal)? $(,)?) => {
16-
$crate::sync::SpinLock::new(
16+
$crate::sync::SpinLock::new_with_class(
1717
$inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
1818
};
1919
}

0 commit comments

Comments
 (0)