Skip to content

Commit ad4d9b3

Browse files
committed
samples: philosophers: Migrate to new task declaration
Move away from the `kobj_define` task declaration to use the new `#[zephyr::thread]` to define these. This allows for a more natural declaration where the thread just looks like an attribute added to a regular function declaration. This also eliminates the static Mutex, as the Mutex now has a constructor that avoids allocation (it is still put in an Arc, though). Signed-off-by: David Brown <[email protected]>
1 parent 406b628 commit ad4d9b3

File tree

1 file changed

+4
-18
lines changed
  • samples/philosophers/src

1 file changed

+4
-18
lines changed

samples/philosophers/src/lib.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use alloc::boxed::Box;
1414
use alloc::vec::Vec;
1515
use zephyr::time::{sleep, Duration, Tick};
1616
use zephyr::{
17-
kobj_define, printkln,
17+
printkln,
1818
sync::{Arc, Mutex},
1919
sys::uptime_get,
2020
};
@@ -68,23 +68,15 @@ extern "C" fn rust_main() {
6868
printkln!("Hello world from Rust on {}", zephyr::kconfig::CONFIG_BOARD);
6969
printkln!("Time tick: {}", zephyr::time::SYS_FREQUENCY);
7070

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

7673
let syncers = get_syncer();
7774

7875
printkln!("Pre fork");
7976

8077
for (i, syncer) in (0..NUM_PHIL).zip(syncers.into_iter()) {
8178
let child_stat = stats.clone();
82-
let thread = PHIL_THREADS[i]
83-
.init_once(PHIL_STACKS[i].init_once(()).unwrap())
84-
.unwrap();
85-
thread.spawn(move || {
86-
phil_thread(i, syncer, child_stat);
87-
});
79+
phil_thread(i, syncer, child_stat).start();
8880
}
8981

9082
let delay = Duration::secs_at_least(10);
@@ -133,6 +125,7 @@ fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
133125
get_channel_syncer()
134126
}
135127

128+
#[zephyr::thread(stack_size = PHIL_STACK_SIZE, pool_size = NUM_PHIL)]
136129
fn phil_thread(n: usize, syncer: Arc<dyn ForkSync>, stats: Arc<Mutex<Stats>>) {
137130
printkln!("Child {} started: {:?}", n, syncer);
138131

@@ -210,10 +203,3 @@ impl Stats {
210203
);
211204
}
212205
}
213-
214-
kobj_define! {
215-
static PHIL_THREADS: [StaticThread; NUM_PHIL];
216-
static PHIL_STACKS: [ThreadStack<PHIL_STACK_SIZE>; NUM_PHIL];
217-
218-
static STAT_MUTEX: StaticMutex;
219-
}

0 commit comments

Comments
 (0)