Skip to content

Commit f0f772c

Browse files
hoshinolinajannau
authored andcommitted
rust: kernel: lock: Add Lock::pin_init()
This allows initializing a lock using pin_init!(), instead of requiring the inner data to be passed through the stack. Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent 39c84e7 commit f0f772c

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

rust/kernel/sync/lock.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! spinlocks, raw spinlocks) to be provided with minimal effort.
77
88
use super::LockClassKey;
9-
use crate::{init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard};
9+
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;
1212

@@ -101,6 +101,7 @@ pub struct Lock<T: ?Sized, B: Backend> {
101101
_pin: PhantomPinned,
102102

103103
/// The data protected by the lock.
104+
#[pin]
104105
pub(crate) data: UnsafeCell<T>,
105106
}
106107

@@ -124,6 +125,31 @@ impl<T, B: Backend> Lock<T, B> {
124125
}),
125126
})
126127
}
128+
129+
/// Constructs a new lock initialiser taking an initialiser.
130+
pub fn pin_init<E>(
131+
t: impl PinInit<T, E>,
132+
name: &'static CStr,
133+
key: &'static LockClassKey,
134+
) -> impl PinInit<Self, E>
135+
where
136+
E: core::convert::From<core::convert::Infallible>,
137+
{
138+
try_pin_init!(Self {
139+
// SAFETY: We are just forwarding the initialization across a
140+
// cast away from UnsafeCell, so the pin_init_from_closure and
141+
// __pinned_init() requirements are in sync.
142+
data <- unsafe { crate::init::pin_init_from_closure(move |slot: *mut UnsafeCell<T>| {
143+
t.__pinned_init(slot as *mut T)
144+
})},
145+
_pin: PhantomPinned,
146+
// SAFETY: `slot` is valid while the closure is called and both `name` and `key` have
147+
// static lifetimes so they live indefinitely.
148+
state <- Opaque::ffi_init(|slot| unsafe {
149+
B::init(slot, name.as_char_ptr(), key.as_ptr())
150+
}),
151+
}? E)
152+
}
127153
}
128154

129155
impl<T: ?Sized, B: Backend> Lock<T, B> {

rust/kernel/sync/lock/mutex.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ macro_rules! new_mutex {
1717
}
1818
pub use new_mutex;
1919

20+
/// Creates a [`Mutex`] initialiser with the given name and a newly-created lock class,
21+
/// given an initialiser for the inner type.
22+
///
23+
/// It uses the name if one is given, otherwise it generates one based on the file name and line
24+
/// number.
25+
#[macro_export]
26+
macro_rules! new_mutex_pinned {
27+
($inner:expr $(, $name:literal)? $(,)?) => {
28+
$crate::sync::Mutex::pin_init(
29+
$inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
30+
};
31+
}
32+
2033
/// A mutual exclusion primitive.
2134
///
2235
/// Exposes the kernel's [`struct mutex`]. When multiple threads attempt to lock the same mutex,

0 commit comments

Comments
 (0)