Skip to content

Commit 47a4d8a

Browse files
hoshinolinajannau
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 <lina@asahilina.net>
1 parent a5299e9 commit 47a4d8a

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

rust/kernel/sync/lock.rs

Lines changed: 39 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
init::PinInit,
1111
pin_init,
@@ -131,7 +131,41 @@ unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
131131

132132
impl<T, B: Backend> Lock<T, B> {
133133
/// Constructs a new lock initialiser.
134-
pub fn new(t: T, name: &'static CStr, key: LockClassKey) -> impl PinInit<Self> {
134+
#[track_caller]
135+
pub fn new(t: T) -> impl PinInit<Self> {
136+
let (key, name) = caller_lock_class();
137+
Self::new_with_key(t, name, key)
138+
}
139+
140+
/// Constructs a new lock initialiser taking an initialiser/
141+
#[track_caller]
142+
pub fn pin_init<E>(t: impl PinInit<T, E>) -> impl PinInit<Self, E>
143+
where
144+
E: core::convert::From<core::convert::Infallible>,
145+
{
146+
let (key, name) = caller_lock_class();
147+
Self::pin_init_with_key(t, name, key)
148+
}
149+
150+
/// Constructs a new lock initialiser.
151+
#[track_caller]
152+
pub fn new_named(t: T, name: &'static CStr) -> impl PinInit<Self> {
153+
let (key, _) = caller_lock_class();
154+
Self::new_with_key(t, name, key)
155+
}
156+
157+
/// Constructs a new lock initialiser taking an initialiser/
158+
#[track_caller]
159+
pub fn pin_init_named<E>(t: impl PinInit<T, E>, name: &'static CStr) -> impl PinInit<Self, E>
160+
where
161+
E: core::convert::From<core::convert::Infallible>,
162+
{
163+
let (key, _) = caller_lock_class();
164+
Self::pin_init_with_key(t, name, key)
165+
}
166+
167+
/// Constructs a new lock initialiser given a particular name and lock class key.
168+
pub fn new_with_key(t: T, name: &'static CStr, key: LockClassKey) -> impl PinInit<Self> {
135169
pin_init!(Self {
136170
data: UnsafeCell::new(t),
137171
_pin: PhantomPinned,
@@ -143,8 +177,9 @@ impl<T, B: Backend> Lock<T, B> {
143177
})
144178
}
145179

146-
/// Constructs a new lock initialiser taking an initialiser.
147-
pub fn pin_init<E>(
180+
/// Constructs a new lock initialiser taking an initialiser given a particular
181+
/// name and lock class key.
182+
pub fn pin_init_with_key<E>(
148183
t: impl PinInit<T, E>,
149184
name: &'static CStr,
150185
key: LockClassKey,

rust/kernel/sync/lock/mutex.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#[macro_export]
1212
macro_rules! new_mutex {
1313
($inner:expr $(, $name:literal)? $(,)?) => {
14-
$crate::sync::Mutex::new(
14+
$crate::sync::Mutex::new_with_key(
1515
$inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
1616
};
1717
}
@@ -25,7 +25,7 @@ pub use new_mutex;
2525
#[macro_export]
2626
macro_rules! new_mutex_pinned {
2727
($inner:expr $(, $name:literal)? $(,)?) => {
28-
$crate::sync::Mutex::pin_init(
28+
$crate::sync::Mutex::pin_init_with_key(
2929
$inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
3030
};
3131
}

rust/kernel/sync/lock/spinlock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#[macro_export]
1212
macro_rules! new_spinlock {
1313
($inner:expr $(, $name:literal)? $(,)?) => {
14-
$crate::sync::SpinLock::new(
14+
$crate::sync::SpinLock::new_with_class(
1515
$inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
1616
};
1717
}

0 commit comments

Comments
 (0)