Skip to content

Commit 9f89617

Browse files
committed
samples: philosophers: Convert to array of stacks
Now that kboj_define supports arrays of thread stacks, change the expanded definitions into array definitions. The demo now properly supports changing `NUM_PHIL` to use a different number of philosophers. Signed-off-by: David Brown <[email protected]>
1 parent 94a7332 commit 9f89617

File tree

1 file changed

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

1 file changed

+4
-25
lines changed

samples/philosophers/src/lib.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ extern crate alloc;
1313
#[allow(unused_imports)]
1414
use alloc::boxed::Box;
1515
use alloc::vec::Vec;
16-
use zephyr::sys::thread::Thread;
1716
use zephyr::time::{Duration, sleep, Tick};
1817
use zephyr::{
1918
printkln,
@@ -63,19 +62,10 @@ extern "C" fn rust_main() {
6362
let syncers = get_syncer();
6463

6564
printkln!("Pre fork");
66-
// At this time, the arrays of threads are not supported, so manually unroll the loop for now.
67-
// If NUM_PHIL is changed, this loop and the declarations at the end will have to be updated.
68-
let threads: [Thread; NUM_PHIL] = [
69-
PHIL_THREAD_1.init_once(PHIL_STACK_1.init_once(()).unwrap()).unwrap(),
70-
PHIL_THREAD_2.init_once(PHIL_STACK_2.init_once(()).unwrap()).unwrap(),
71-
PHIL_THREAD_3.init_once(PHIL_STACK_3.init_once(()).unwrap()).unwrap(),
72-
PHIL_THREAD_4.init_once(PHIL_STACK_4.init_once(()).unwrap()).unwrap(),
73-
PHIL_THREAD_5.init_once(PHIL_STACK_5.init_once(()).unwrap()).unwrap(),
74-
PHIL_THREAD_6.init_once(PHIL_STACK_6.init_once(()).unwrap()).unwrap(),
75-
];
7665

7766
for (i, syncer) in (0..NUM_PHIL).zip(syncers.into_iter()) {
78-
threads[i].spawn(move || {
67+
let thread = PHIL_THREADS[i].init_once(PHIL_STACKS[i].init_once(()).unwrap()).unwrap();
68+
thread.spawn(move || {
7969
phil_thread(i, syncer);
8070
});
8171
}
@@ -139,17 +129,6 @@ fn get_random_delay(id: usize, period: usize) -> Duration {
139129
}
140130

141131
kobj_define! {
142-
static PHIL_THREAD_1: StaticThread;
143-
static PHIL_THREAD_2: StaticThread;
144-
static PHIL_THREAD_3: StaticThread;
145-
static PHIL_THREAD_4: StaticThread;
146-
static PHIL_THREAD_5: StaticThread;
147-
static PHIL_THREAD_6: StaticThread;
148-
149-
static PHIL_STACK_1: ThreadStack<PHIL_STACK_SIZE>;
150-
static PHIL_STACK_2: ThreadStack<PHIL_STACK_SIZE>;
151-
static PHIL_STACK_3: ThreadStack<PHIL_STACK_SIZE>;
152-
static PHIL_STACK_4: ThreadStack<PHIL_STACK_SIZE>;
153-
static PHIL_STACK_5: ThreadStack<PHIL_STACK_SIZE>;
154-
static PHIL_STACK_6: ThreadStack<PHIL_STACK_SIZE>;
132+
static PHIL_THREADS: [StaticThread; NUM_PHIL];
133+
static PHIL_STACKS: [ThreadStack<PHIL_STACK_SIZE>; NUM_PHIL];
155134
}

0 commit comments

Comments
 (0)