Skip to content

Commit ea6cf3f

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 f09a2bc commit ea6cf3f

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::{init::PinInit, pin_init, str::CStr, try_pin_init, types::Opaque, types::ScopeGuard};
1010
use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned};
1111
use macros::pin_data;
@@ -114,7 +114,40 @@ unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
114114

115115
impl<T, B: Backend> Lock<T, B> {
116116
/// Constructs a new lock initialiser.
117-
pub fn new(t: T, name: &'static CStr, key: LockClassKey) -> impl PinInit<Self> {
117+
#[track_caller]
118+
pub fn new(t: T) -> impl PinInit<Self> {
119+
let (key, name) = caller_lock_class();
120+
Self::new_with_key(t, name, key)
121+
}
122+
123+
/// Constructs a new lock initialiser taking an initialiser/
124+
pub fn pin_init<E>(t: impl PinInit<T, E>) -> impl PinInit<Self, E>
125+
where
126+
E: core::convert::From<core::convert::Infallible>,
127+
{
128+
let (key, name) = caller_lock_class();
129+
Self::pin_init_with_key(t, name, key)
130+
}
131+
132+
/// Constructs a new lock initialiser.
133+
#[allow(clippy::new_ret_no_self)]
134+
#[track_caller]
135+
pub fn new_named(t: T, name: &'static CStr) -> impl PinInit<Self> {
136+
let (key, _) = caller_lock_class();
137+
Self::new_with_key(t, name, key)
138+
}
139+
140+
/// Constructs a new lock initialiser taking an initialiser/
141+
pub fn pin_init_named<E>(t: impl PinInit<T, E>, name: &'static CStr) -> impl PinInit<Self, E>
142+
where
143+
E: core::convert::From<core::convert::Infallible>,
144+
{
145+
let (key, _) = caller_lock_class();
146+
Self::pin_init_with_key(t, name, key)
147+
}
148+
149+
/// Constructs a new lock initialiser given a particular name and lock class key.
150+
pub fn new_with_key(t: T, name: &'static CStr, key: LockClassKey) -> impl PinInit<Self> {
118151
pin_init!(Self {
119152
data: UnsafeCell::new(t),
120153
_pin: PhantomPinned,
@@ -126,8 +159,9 @@ impl<T, B: Backend> Lock<T, B> {
126159
})
127160
}
128161

129-
/// Constructs a new lock initialiser taking an initialiser.
130-
pub fn pin_init<E>(
162+
/// Constructs a new lock initialiser taking an initialiser given a particular
163+
/// name and lock class key.
164+
pub fn pin_init_with_key<E>(
131165
t: impl PinInit<T, E>,
132166
name: &'static CStr,
133167
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)