Skip to content

Commit 2497a71

Browse files
danielalmeida-collaboraPeter Zijlstra
authored andcommitted
rust: lock: Pin the inner data
In preparation to support Lock<T> where T is pinned, the first thing that needs to be done is to structurally pin the 'data' member. This switches the 't' parameter in Lock<T>::new() to take in an impl PinInit<T> instead of a plain T. This in turn uses the blanket implementation "impl PinInit<T> for T". Subsequent patches will touch on Guard<T>. Suggested-by: Benno Lossin <lossin@kernel.org> Suggested-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: #1181
1 parent da123f0 commit 2497a71

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

rust/kernel/sync/lock.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
types::{NotThreadSafe, Opaque, ScopeGuard},
1212
};
1313
use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin};
14-
use pin_init::{pin_data, pin_init, PinInit};
14+
use pin_init::{pin_data, pin_init, PinInit, Wrapper};
1515

1616
pub mod mutex;
1717
pub mod spinlock;
@@ -115,6 +115,7 @@ pub struct Lock<T: ?Sized, B: Backend> {
115115
_pin: PhantomPinned,
116116

117117
/// The data protected by the lock.
118+
#[pin]
118119
pub(crate) data: UnsafeCell<T>,
119120
}
120121

@@ -127,9 +128,13 @@ unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
127128

128129
impl<T, B: Backend> Lock<T, B> {
129130
/// Constructs a new lock initialiser.
130-
pub fn new(t: T, name: &'static CStr, key: Pin<&'static LockClassKey>) -> impl PinInit<Self> {
131+
pub fn new(
132+
t: impl PinInit<T>,
133+
name: &'static CStr,
134+
key: Pin<&'static LockClassKey>,
135+
) -> impl PinInit<Self> {
131136
pin_init!(Self {
132-
data: UnsafeCell::new(t),
137+
data <- UnsafeCell::pin_init(t),
133138
_pin: PhantomPinned,
134139
// SAFETY: `slot` is valid while the closure is called and both `name` and `key` have
135140
// static lifetimes so they live indefinitely.

0 commit comments

Comments
 (0)