Skip to content

Commit 1929fb5

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 fdff163 commit 1929fb5

File tree

1 file changed

+3
-12
lines changed
  • samples/philosophers/src

1 file changed

+3
-12
lines changed

samples/philosophers/src/lib.rs

+3-12
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
};
@@ -75,12 +75,7 @@ extern "C" fn rust_main() {
7575
printkln!("Pre fork");
7676

7777
for (i, syncer) in (0..NUM_PHIL).zip(syncers.into_iter()) {
78-
let thread = PHIL_THREADS[i]
79-
.init_once(PHIL_STACKS[i].init_once(()).unwrap())
80-
.unwrap();
81-
thread.spawn(move || {
82-
phil_thread(i, syncer, stats);
83-
});
78+
phil_thread(i, syncer, stats).start();
8479
}
8580

8681
let delay = Duration::secs_at_least(10);
@@ -129,6 +124,7 @@ fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
129124
get_channel_syncer()
130125
}
131126

127+
#[zephyr::thread(stack_size = PHIL_STACK_SIZE, pool_size = NUM_PHIL)]
132128
fn phil_thread(n: usize, syncer: Arc<dyn ForkSync>, stats: &'static Mutex<Stats>) {
133129
printkln!("Child {} started: {:?}", n, syncer);
134130

@@ -219,8 +215,3 @@ impl Stats {
219215
}
220216

221217
static STAT_MUTEX: Mutex<Stats> = Mutex::new(Stats::new());
222-
223-
kobj_define! {
224-
static PHIL_THREADS: [StaticThread; NUM_PHIL];
225-
static PHIL_STACKS: [ThreadStack<PHIL_STACK_SIZE>; NUM_PHIL];
226-
}

0 commit comments

Comments
 (0)