Skip to content

Commit

Permalink
Merge pull request torvalds#289 from obviyus/change-pin
Browse files Browse the repository at this point in the history
rust: make SharedState::try_new() return a pinned Arc
  • Loading branch information
alex authored May 23, 2021
2 parents 45d79fc + 84da4b2 commit 1a73789
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions samples/rust/rust_miscdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ struct SharedState {
}

impl SharedState {
fn try_new() -> Result<Arc<Self>> {
let state = Arc::try_new(Self {
// SAFETY: `condvar_init!` is called below.
state_changed: unsafe { CondVar::new() },
// SAFETY: `mutex_init!` is called below.
inner: unsafe { Mutex::new(SharedStateInner { token_count: 0 }) },
})?;
fn try_new() -> Result<Pin<Arc<Self>>> {
// SAFETY: `state` is pinning `Arc`, which implements `Unpin`.
let state = unsafe {
Pin::new_unchecked(Arc::try_new(Self {
// SAFETY: `condvar_init!` is called below.
state_changed: CondVar::new(),
// SAFETY: `mutex_init!` is called below.
inner: Mutex::new(SharedStateInner { token_count: 0 }),
})?)
};
// SAFETY: `state_changed` is pinned behind `Arc`.
let state_changed = unsafe { Pin::new_unchecked(&state.state_changed) };
kernel::condvar_init!(state_changed, "SharedState::state_changed");
Expand All @@ -56,11 +59,11 @@ impl SharedState {
}

struct Token {
shared: Arc<SharedState>,
shared: Pin<Arc<SharedState>>,
}

impl FileOpener<Arc<SharedState>> for Token {
fn open(shared: &Arc<SharedState>) -> Result<Self::Wrapper> {
impl FileOpener<Pin<Arc<SharedState>>> for Token {
fn open(shared: &Pin<Arc<SharedState>>) -> Result<Self::Wrapper> {
Ok(Box::try_new(Self {
shared: shared.clone(),
})?)
Expand Down Expand Up @@ -122,7 +125,7 @@ impl FileOperations for Token {
}

struct RustMiscdev {
_dev: Pin<Box<miscdev::Registration<Arc<SharedState>>>>,
_dev: Pin<Box<miscdev::Registration<Pin<Arc<SharedState>>>>>,
}

impl KernelModule for RustMiscdev {
Expand Down

0 comments on commit 1a73789

Please sign in to comment.