Skip to content

Commit 1da768b

Browse files
committed
rust: remove Arc from rust samples.
Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 6dc937a commit 1da768b

File tree

2 files changed

+51
-45
lines changed

2 files changed

+51
-45
lines changed

samples/rust/rust_miscdev.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![no_std]
66
#![feature(allocator_api, global_asm)]
77

8-
use alloc::{boxed::Box, sync::Arc};
8+
use alloc::boxed::Box;
99
use core::pin::Pin;
1010
use kernel::prelude::*;
1111
use kernel::{
@@ -14,7 +14,7 @@ use kernel::{
1414
file_operations::{FileOpener, FileOperations},
1515
io_buffer::{IoBufferReader, IoBufferWriter},
1616
miscdev,
17-
sync::{CondVar, Mutex},
17+
sync::{CondVar, Mutex, Ref},
1818
Error,
1919
};
2020

@@ -38,38 +38,41 @@ struct SharedState {
3838
}
3939

4040
impl SharedState {
41-
fn try_new() -> Result<Pin<Arc<Self>>> {
42-
let state = Arc::try_pin(Self {
43-
// SAFETY: `condvar_init!` is called below.
44-
state_changed: unsafe { CondVar::new() },
45-
// SAFETY: `mutex_init!` is called below.
46-
inner: unsafe { Mutex::new(SharedStateInner { token_count: 0 }) },
47-
})?;
48-
// SAFETY: `state_changed` is pinned behind `Pin<Arc>`.
49-
let state_changed = unsafe { Pin::new_unchecked(&state.state_changed) };
50-
kernel::condvar_init!(state_changed, "SharedState::state_changed");
51-
// SAFETY: `inner` is pinned behind `Pin<Arc>`.
52-
let inner = unsafe { Pin::new_unchecked(&state.inner) };
53-
kernel::mutex_init!(inner, "SharedState::inner");
54-
Ok(state)
41+
fn try_new() -> Result<Pin<Ref<Self>>> {
42+
Ok(Ref::pinned(Ref::try_new_and_init(
43+
Self {
44+
// SAFETY: `condvar_init!` is called below.
45+
state_changed: unsafe { CondVar::new() },
46+
// SAFETY: `mutex_init!` is called below.
47+
inner: unsafe { Mutex::new(SharedStateInner { token_count: 0 }) },
48+
},
49+
|state| {
50+
// SAFETY: `state_changed` is pinned when `state` is.
51+
let state_changed = unsafe { Pin::new_unchecked(&state.state_changed) };
52+
kernel::condvar_init!(state_changed, "SharedState::state_changed");
53+
// SAFETY: `inner` is pinned when `state` is.
54+
let inner = unsafe { Pin::new_unchecked(&state.inner) };
55+
kernel::mutex_init!(inner, "SharedState::inner");
56+
},
57+
)?))
5558
}
5659
}
5760

5861
struct Token;
5962

60-
impl FileOpener<Pin<Arc<SharedState>>> for Token {
61-
fn open(shared: &Pin<Arc<SharedState>>) -> Result<Self::Wrapper> {
63+
impl FileOpener<Pin<Ref<SharedState>>> for Token {
64+
fn open(shared: &Pin<Ref<SharedState>>) -> Result<Self::Wrapper> {
6265
Ok(shared.clone())
6366
}
6467
}
6568

6669
impl FileOperations for Token {
67-
type Wrapper = Pin<Arc<SharedState>>;
70+
type Wrapper = Pin<Ref<SharedState>>;
6871

6972
kernel::declare_file_operations!(read, write);
7073

7174
fn read<T: IoBufferWriter>(
72-
shared: &SharedState,
75+
shared: &Ref<SharedState>,
7376
_: &File,
7477
data: &mut T,
7578
offset: u64,
@@ -102,10 +105,10 @@ impl FileOperations for Token {
102105
}
103106

104107
fn write<T: IoBufferReader>(
105-
shared: &SharedState,
108+
shared: &Ref<SharedState>,
106109
_: &File,
107110
data: &mut T,
108-
_offs: u64,
111+
_offset: u64,
109112
) -> Result<usize> {
110113
{
111114
let mut inner = shared.inner.lock();
@@ -128,7 +131,7 @@ impl FileOperations for Token {
128131
}
129132

130133
struct RustMiscdev {
131-
_dev: Pin<Box<miscdev::Registration<Pin<Arc<SharedState>>>>>,
134+
_dev: Pin<Box<miscdev::Registration<Pin<Ref<SharedState>>>>>,
132135
}
133136

134137
impl KernelModule for RustMiscdev {

samples/rust/rust_semaphore.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#![no_std]
1717
#![feature(allocator_api, global_asm)]
1818

19-
use alloc::{boxed::Box, sync::Arc};
19+
use alloc::boxed::Box;
2020
use core::{
2121
pin::Pin,
2222
sync::atomic::{AtomicU64, Ordering},
@@ -29,7 +29,7 @@ use kernel::{
2929
miscdev::Registration,
3030
mutex_init,
3131
prelude::*,
32-
sync::{CondVar, Mutex},
32+
sync::{CondVar, Mutex, Ref},
3333
user_ptr::{UserSlicePtrReader, UserSlicePtrWriter},
3434
Error,
3535
};
@@ -54,7 +54,7 @@ struct Semaphore {
5454

5555
struct FileState {
5656
read_count: AtomicU64,
57-
shared: Arc<Semaphore>,
57+
shared: Ref<Semaphore>,
5858
}
5959

6060
impl FileState {
@@ -70,8 +70,8 @@ impl FileState {
7070
}
7171
}
7272

73-
impl FileOpener<Arc<Semaphore>> for FileState {
74-
fn open(shared: &Arc<Semaphore>) -> Result<Box<Self>> {
73+
impl FileOpener<Ref<Semaphore>> for FileState {
74+
fn open(shared: &Ref<Semaphore>) -> Result<Box<Self>> {
7575
Ok(Box::try_new(Self {
7676
read_count: AtomicU64::new(0),
7777
shared: shared.clone(),
@@ -111,31 +111,34 @@ impl FileOperations for FileState {
111111
}
112112

113113
struct RustSemaphore {
114-
_dev: Pin<Box<Registration<Arc<Semaphore>>>>,
114+
_dev: Pin<Box<Registration<Ref<Semaphore>>>>,
115115
}
116116

117117
impl KernelModule for RustSemaphore {
118118
fn init() -> Result<Self> {
119119
pr_info!("Rust semaphore sample (init)\n");
120120

121-
let sema = Arc::try_new(Semaphore {
122-
// SAFETY: `condvar_init!` is called below.
123-
changed: unsafe { CondVar::new() },
124-
125-
// SAFETY: `mutex_init!` is called below.
126-
inner: unsafe {
127-
Mutex::new(SemaphoreInner {
128-
count: 0,
129-
max_seen: 0,
130-
})
121+
let sema = Ref::try_new_and_init(
122+
Semaphore {
123+
// SAFETY: `condvar_init!` is called below.
124+
changed: unsafe { CondVar::new() },
125+
126+
// SAFETY: `mutex_init!` is called below.
127+
inner: unsafe {
128+
Mutex::new(SemaphoreInner {
129+
count: 0,
130+
max_seen: 0,
131+
})
132+
},
131133
},
132-
})?;
133-
134-
// SAFETY: `changed` is pinned behind `Arc`.
135-
condvar_init!(Pin::new_unchecked(&sema.changed), "Semaphore::changed");
134+
|sema| {
135+
// SAFETY: `changed` is pinned when `sema` is.
136+
condvar_init!(Pin::new_unchecked(&sema.changed), "Semaphore::changed");
136137

137-
// SAFETY: `inner` is pinned behind `Arc`.
138-
mutex_init!(Pin::new_unchecked(&sema.inner), "Semaphore::inner");
138+
// SAFETY: `inner` is pinned when `sema` is.
139+
mutex_init!(Pin::new_unchecked(&sema.inner), "Semaphore::inner");
140+
},
141+
)?;
139142

140143
Ok(Self {
141144
_dev: Registration::new_pinned::<FileState>(c_str!("rust_semaphore"), None, sema)?,

0 commit comments

Comments
 (0)