Skip to content

Commit 0a991e4

Browse files
committed
samples: philosphers: Run cargo fmt
Format the source according to the default rules. This provides a baseline for future changes to require well-formatted source. Signed-off-by: David Brown <[email protected]>
1 parent ece1ddd commit 0a991e4

File tree

6 files changed

+70
-80
lines changed

6 files changed

+70
-80
lines changed

samples/philosophers/src/channel.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
99
extern crate alloc;
1010

11-
use alloc::vec::Vec;
1211
use alloc::boxed::Box;
12+
use alloc::vec::Vec;
1313

1414
use zephyr::sync::channel::{self, Receiver, Sender};
15-
use zephyr::{
16-
kobj_define,
17-
sync::Arc,
18-
};
15+
use zephyr::{kobj_define, sync::Arc};
1916

20-
use crate::{NUM_PHIL, ForkSync};
17+
use crate::{ForkSync, NUM_PHIL};
2118

2219
/// An implementation of ForkSync that uses a server commnicated with channels to perform the
2320
/// synchronization.
@@ -97,10 +94,7 @@ impl ChannelFork {
9794
}
9895

9996
impl ChannelSync {
100-
pub fn new(
101-
command: Sender<Command>,
102-
reply: (Sender<()>, Receiver<()>)) -> ChannelSync
103-
{
97+
pub fn new(command: Sender<Command>, reply: (Sender<()>, Receiver<()>)) -> ChannelSync {
10498
ChannelSync {
10599
command,
106100
reply_send: reply.0,
@@ -118,23 +112,20 @@ pub fn get_channel_syncer() -> Vec<Arc<dyn ForkSync>> {
118112
if cfg!(CONFIG_USE_BOUNDED_CHANNELS) {
119113
// Use only one message, so that send will block, to ensure that works.
120114
(cq_send, cq_recv) = channel::bounded(1);
121-
reply_queues = [(); NUM_PHIL].each_ref().map(|()| {
122-
channel::bounded(1)
123-
});
115+
reply_queues = [(); NUM_PHIL].each_ref().map(|()| channel::bounded(1));
124116
} else {
125117
(cq_send, cq_recv) = channel::unbounded();
126-
reply_queues = [(); NUM_PHIL].each_ref().map(|()| {
127-
channel::unbounded()
128-
});
118+
reply_queues = [(); NUM_PHIL].each_ref().map(|()| channel::unbounded());
129119
}
130120

131121
let syncer = reply_queues.into_iter().map(|rqueue| {
132-
let item = Box::new(ChannelSync::new(cq_send.clone(), rqueue))
133-
as Box<dyn ForkSync>;
122+
let item = Box::new(ChannelSync::new(cq_send.clone(), rqueue)) as Box<dyn ForkSync>;
134123
Arc::from(item)
135124
});
136125

137-
let channel_child = CHANNEL_THREAD.init_once(CHANNEL_STACK.init_once(()).unwrap()).unwrap();
126+
let channel_child = CHANNEL_THREAD
127+
.init_once(CHANNEL_STACK.init_once(()).unwrap())
128+
.unwrap();
138129
channel_child.spawn(move || {
139130
channel_thread(cq_recv);
140131
});
@@ -162,7 +153,9 @@ fn channel_thread(cq_recv: Receiver<Command>) {
162153

163154
impl ForkSync for ChannelSync {
164155
fn take(&self, index: usize) {
165-
self.command.send(Command::Acquire(index, self.reply_send.clone())).unwrap();
156+
self.command
157+
.send(Command::Acquire(index, self.reply_send.clone()))
158+
.unwrap();
166159
// When the reply comes, we know we have the resource.
167160
self.reply_recv.recv().unwrap();
168161
}

samples/philosophers/src/condsync.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
//! This implementation of the Fork synchronizer uses a single data object, protected by a
77
//! `sync::Mutex`, and coordinated by a `sync::Condvar`.
88
9-
use crate::{
10-
ForkSync,
11-
NUM_PHIL,
12-
};
13-
use zephyr::sync::Mutex;
9+
use crate::{ForkSync, NUM_PHIL};
1410
use zephyr::sync::Condvar;
11+
use zephyr::sync::Mutex;
1512
// use zephyr::time::Forever;
1613

1714
#[derive(Debug)]
@@ -24,7 +21,7 @@ pub struct CondSync {
2421

2522
impl CondSync {
2623
#[allow(dead_code)]
27-
pub fn new() -> CondSync {
24+
pub fn new() -> CondSync {
2825
CondSync {
2926
lock: Mutex::new([false; NUM_PHIL]),
3027
cond: Condvar::new(),

samples/philosophers/src/dynsemsync.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
88
extern crate alloc;
99

10-
use alloc::vec::Vec;
1110
use alloc::boxed::Box;
11+
use alloc::vec::Vec;
1212

13-
use zephyr::{
14-
sync::Arc, sys::sync::Semaphore, time::Forever
15-
};
13+
use zephyr::{sync::Arc, sys::sync::Semaphore, time::Forever};
1614

1715
use crate::{ForkSync, NUM_PHIL};
1816

@@ -35,17 +33,19 @@ impl ForkSync for SemSync {
3533

3634
#[allow(dead_code)]
3735
pub fn dyn_semaphore_sync() -> Vec<Arc<dyn ForkSync>> {
38-
let forks = [(); NUM_PHIL].each_ref().map(|()| {
39-
Arc::new(Semaphore::new(1, 1).unwrap())
40-
});
41-
42-
let syncers = (0..NUM_PHIL).map(|_| {
43-
let syncer = SemSync {
44-
forks: forks.clone(),
45-
};
46-
let item = Box::new(syncer) as Box<dyn ForkSync>;
47-
Arc::from(item)
48-
}).collect();
36+
let forks = [(); NUM_PHIL]
37+
.each_ref()
38+
.map(|()| Arc::new(Semaphore::new(1, 1).unwrap()));
39+
40+
let syncers = (0..NUM_PHIL)
41+
.map(|_| {
42+
let syncer = SemSync {
43+
forks: forks.clone(),
44+
};
45+
let item = Box::new(syncer) as Box<dyn ForkSync>;
46+
Arc::from(item)
47+
})
48+
.collect();
4949

5050
syncers
5151
}

samples/philosophers/src/lib.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
#![no_std]
5-
65
// Cargo tries to detect configs that have typos in them. Unfortunately, the Zephyr Kconfig system
76
// uses a large number of Kconfigs and there is no easy way to know which ones might conceivably be
87
// valid. This prevents a warning about each cfg that is used.
@@ -13,31 +12,30 @@ extern crate alloc;
1312
#[allow(unused_imports)]
1413
use alloc::boxed::Box;
1514
use alloc::vec::Vec;
16-
use zephyr::time::{Duration, sleep, Tick};
15+
use zephyr::time::{sleep, Duration, Tick};
1716
use zephyr::{
18-
printkln,
19-
kobj_define,
20-
sys::uptime_get,
17+
kobj_define, printkln,
2118
sync::{Arc, Mutex},
19+
sys::uptime_get,
2220
};
2321

2422
// These are optional, based on Kconfig, so allow them to be unused.
2523
#[allow(unused_imports)]
26-
use crate::condsync::CondSync;
24+
use crate::channel::get_channel_syncer;
2725
#[allow(unused_imports)]
28-
use crate::sysmutex::SysMutexSync;
26+
use crate::condsync::CondSync;
2927
#[allow(unused_imports)]
30-
use crate::channel::get_channel_syncer;
28+
use crate::dynsemsync::dyn_semaphore_sync;
3129
#[allow(unused_imports)]
3230
use crate::semsync::semaphore_sync;
3331
#[allow(unused_imports)]
34-
use crate::dynsemsync::dyn_semaphore_sync;
32+
use crate::sysmutex::SysMutexSync;
3533

3634
mod channel;
3735
mod condsync;
3836
mod dynsemsync;
39-
mod sysmutex;
4037
mod semsync;
38+
mod sysmutex;
4139

4240
/// How many philosophers. There will be the same number of forks.
4341
const NUM_PHIL: usize = 6;
@@ -67,19 +65,23 @@ trait ForkSync: core::fmt::Debug + Sync + Send {
6765

6866
#[no_mangle]
6967
extern "C" fn rust_main() {
70-
printkln!("Hello world from Rust on {}",
71-
zephyr::kconfig::CONFIG_BOARD);
68+
printkln!("Hello world from Rust on {}", zephyr::kconfig::CONFIG_BOARD);
7269
printkln!("Time tick: {}", zephyr::time::SYS_FREQUENCY);
7370

74-
let stats = Arc::new(Mutex::new_from(Stats::default(), STAT_MUTEX.init_once(()).unwrap()));
71+
let stats = Arc::new(Mutex::new_from(
72+
Stats::default(),
73+
STAT_MUTEX.init_once(()).unwrap(),
74+
));
7575

7676
let syncers = get_syncer();
7777

7878
printkln!("Pre fork");
7979

8080
for (i, syncer) in (0..NUM_PHIL).zip(syncers.into_iter()) {
8181
let child_stat = stats.clone();
82-
let thread = PHIL_THREADS[i].init_once(PHIL_STACKS[i].init_once(()).unwrap()).unwrap();
82+
let thread = PHIL_THREADS[i]
83+
.init_once(PHIL_STACKS[i].init_once(()).unwrap())
84+
.unwrap();
8385
thread.spawn(move || {
8486
phil_thread(i, syncer, child_stat);
8587
});
@@ -105,8 +107,7 @@ fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
105107

106108
#[cfg(CONFIG_SYNC_SYS_MUTEX)]
107109
fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
108-
let syncer = Box::new(SysMutexSync::new())
109-
as Box<dyn ForkSync>;
110+
let syncer = Box::new(SysMutexSync::new()) as Box<dyn ForkSync>;
110111
let syncer: Arc<dyn ForkSync> = Arc::from(syncer);
111112
let mut result = Vec::new();
112113
for _ in 0..NUM_PHIL {
@@ -118,8 +119,7 @@ fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
118119
#[cfg(CONFIG_SYNC_CONDVAR)]
119120
fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
120121
// Condvar version
121-
let syncer = Box::new(CondSync::new())
122-
as Box<dyn ForkSync>;
122+
let syncer = Box::new(CondSync::new()) as Box<dyn ForkSync>;
123123
let syncer: Arc<dyn ForkSync> = Arc::from(syncer);
124124
let mut result = Vec::new();
125125
for _ in 0..NUM_PHIL {
@@ -141,7 +141,7 @@ fn phil_thread(n: usize, syncer: Arc<dyn ForkSync>, stats: Arc<Mutex<Stats>>) {
141141
// Per Dijkstra, the last phyilosopher needs to reverse forks, or we deadlock.
142142
(0, n)
143143
} else {
144-
(n, n+1)
144+
(n, n + 1)
145145
};
146146

147147
loop {
@@ -202,7 +202,12 @@ impl Stats {
202202
}
203203

204204
fn show(&self) {
205-
printkln!("c:{:?}, e:{:?}, t:{:?}", self.count, self.eating, self.thinking);
205+
printkln!(
206+
"c:{:?}, e:{:?}, t:{:?}",
207+
self.count,
208+
self.eating,
209+
self.thinking
210+
);
206211
}
207212
}
208213

samples/philosophers/src/semsync.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
88
extern crate alloc;
99

10-
use alloc::vec::Vec;
1110
use alloc::boxed::Box;
11+
use alloc::vec::Vec;
1212

13-
use zephyr::{
14-
kobj_define, sync::Arc, sys::sync::Semaphore, time::Forever
15-
};
13+
use zephyr::{kobj_define, sync::Arc, sys::sync::Semaphore, time::Forever};
1614

1715
use crate::{ForkSync, NUM_PHIL};
1816

@@ -40,13 +38,15 @@ pub fn semaphore_sync() -> Vec<Arc<dyn ForkSync>> {
4038
Arc::new(m.init_once((1, 1)).unwrap())
4139
});
4240

43-
let syncers = (0..NUM_PHIL).map(|_| {
44-
let syncer = SemSync {
45-
forks: forks.clone(),
46-
};
47-
let item = Box::new(syncer) as Box<dyn ForkSync>;
48-
Arc::from(item)
49-
}).collect();
41+
let syncers = (0..NUM_PHIL)
42+
.map(|_| {
43+
let syncer = SemSync {
44+
forks: forks.clone(),
45+
};
46+
let item = Box::new(syncer) as Box<dyn ForkSync>;
47+
Arc::from(item)
48+
})
49+
.collect();
5050

5151
syncers
5252
}

samples/philosophers/src/sysmutex.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
//! This is a simple implementation of the Fork synchronizer that uses underlying Zephyr `k_mutex`
77
//! wrapped in `sys::Mutex`. The ForkSync semantics map simply to these.
88
9-
use crate::{
10-
ForkSync,
11-
NUM_PHIL,
12-
};
9+
use crate::{ForkSync, NUM_PHIL};
1310
use zephyr::sys::sync::Mutex;
1411
use zephyr::time::Forever;
1512

@@ -25,10 +22,8 @@ pub struct SysMutexSync {
2522

2623
impl SysMutexSync {
2724
#[allow(dead_code)]
28-
pub fn new() -> SysMutexSync {
29-
let locks = [(); NUM_PHIL].each_ref().map(|()| {
30-
Mutex::new().unwrap()
31-
});
25+
pub fn new() -> SysMutexSync {
26+
let locks = [(); NUM_PHIL].each_ref().map(|()| Mutex::new().unwrap());
3227
SysMutexSync { locks }
3328
}
3429
}

0 commit comments

Comments
 (0)