|
| 1 | +// Copyright (c) 2023 Linaro LTD |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#![no_std] |
| 5 | + |
| 6 | +extern crate alloc; |
| 7 | + |
| 8 | +use alloc::boxed::Box; |
| 9 | +use alloc::vec::Vec; |
| 10 | +use zephyr::time::{Duration, sleep, Tick}; |
| 11 | +use zephyr::{ |
| 12 | + printkln, |
| 13 | + kobj_define, |
| 14 | + sys::uptime_get, |
| 15 | + sync::Arc, |
| 16 | +}; |
| 17 | + |
| 18 | +use crate::sysmutex::SysMutexSync; |
| 19 | + |
| 20 | +mod sysmutex; |
| 21 | + |
| 22 | +/// How many philosophers. There will be the same number of forks. |
| 23 | +const NUM_PHIL: usize = 6; |
| 24 | + |
| 25 | +/// How much stack should each philosopher thread get. Worst case I've seen is riscv64, with 3336 |
| 26 | +/// bytes, when printing messages. Make a bit larger to work. |
| 27 | +const PHIL_STACK_SIZE: usize = 4096; |
| 28 | + |
| 29 | +// The dining philosophers problem is a simple example of cooperation between multiple threads. |
| 30 | +// This implementation use one of several different underlying mechanism to support this cooperation. |
| 31 | + |
| 32 | +// This example uses dynamic dispatch to allow multiple implementations. The intent is to be able |
| 33 | +// to periodically shut down all of the philosphers and start them up with a differernt sync |
| 34 | +// mechanism. This isn't implemented yet. |
| 35 | + |
| 36 | +/// The philosophers use a fork synchronization mechanism. Essentially, this is 6 locks, and will be |
| 37 | +/// implemented in a few different ways to demonstrate/test different mechanmism in Rust. All of |
| 38 | +/// them implement The ForkSync trait which provides this mechanism. |
| 39 | +trait ForkSync: core::fmt::Debug + Sync + Send { |
| 40 | + /// Take the given fork. The are indexed the same as the philosopher index number. This will |
| 41 | + /// block until the fork is released. |
| 42 | + fn take(&self, index: usize); |
| 43 | + |
| 44 | + /// Release the given fork. Index is the same as take. |
| 45 | + fn release(&self, index: usize); |
| 46 | +} |
| 47 | + |
| 48 | +#[no_mangle] |
| 49 | +extern "C" fn rust_main() { |
| 50 | + printkln!("Hello world from Rust on {}", |
| 51 | + zephyr::kconfig::CONFIG_BOARD); |
| 52 | + printkln!("Time tick: {}", zephyr::time::SYS_FREQUENCY); |
| 53 | + |
| 54 | + let syncers = get_syncer(); |
| 55 | + |
| 56 | + printkln!("Pre fork"); |
| 57 | + for (i, syncer) in (0..PHIL_THREAD.len()).zip(syncers.into_iter()) { |
| 58 | + /* |
| 59 | + let child_syncer = syncer.clone(); |
| 60 | + */ |
| 61 | + // The Rust borrow checker doesn't seem quite smart enough to realize that we are moving |
| 62 | + // these out individually. Best would be to rewrite this to iterate over the queue, but for |
| 63 | + // now, the clone isn't terribly costly. |
| 64 | + /* |
| 65 | + let child_syncer = ChannelSync::new(cq_send.clone(), reply_queues[i].clone()); |
| 66 | + let child_syncer = Arc::new(child_syncer); |
| 67 | + */ |
| 68 | + // let child_stat = stats.clone(); |
| 69 | + let thread = PHIL_THREAD[i].spawn(PHIL_STACK[i].token(), move || { |
| 70 | + phil_thread(i, syncer /*, child_stat*/); |
| 71 | + }); |
| 72 | + thread.start(); |
| 73 | + } |
| 74 | + |
| 75 | + let delay = Duration::secs_at_least(10); |
| 76 | + loop { |
| 77 | + // Periodically, printout the stats. |
| 78 | + zephyr::time::sleep(delay); |
| 79 | + // stats.lock().unwrap().show(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +fn get_syncer() -> Vec<Arc<dyn ForkSync>> { |
| 84 | + // Simple mutex version. |
| 85 | + let syncer = Box::new(SysMutexSync::new()) |
| 86 | + as Box<dyn ForkSync>; |
| 87 | + let syncer: Arc<dyn ForkSync> = Arc::from(syncer); |
| 88 | + let mut result = Vec::new(); |
| 89 | + for _ in 0..NUM_PHIL { |
| 90 | + result.push(syncer.clone()); |
| 91 | + } |
| 92 | + result |
| 93 | +} |
| 94 | + |
| 95 | +fn phil_thread(n: usize, syncer: Arc<dyn ForkSync> /*, stats: Arc<Mutex<Stats>>*/) { |
| 96 | + printkln!("Child {} started: {:?}", n, syncer); |
| 97 | + |
| 98 | + // Determine our two forks. |
| 99 | + let forks = if n == NUM_PHIL - 1 { |
| 100 | + // Per Dijkstra, the last phyilosopher needs to reverse forks, or we deadlock. |
| 101 | + (0, n) |
| 102 | + } else { |
| 103 | + (n, n+1) |
| 104 | + }; |
| 105 | + |
| 106 | + loop { |
| 107 | + { |
| 108 | + // printkln!("Child {} hungry", n); |
| 109 | + // printkln!("Child {} take left fork", n); |
| 110 | + syncer.take(forks.0); |
| 111 | + // printkln!("Child {} take right fork", n); |
| 112 | + syncer.take(forks.1); |
| 113 | + |
| 114 | + let delay = get_random_delay(n, 25); |
| 115 | + printkln!("Child {} eating ({} ms)", n, delay); |
| 116 | + sleep(delay); |
| 117 | + // stats.lock().unwrap().record_eat(n, delay); |
| 118 | + |
| 119 | + // Release the forks. |
| 120 | + // printkln!("Child {} giving up forks", n); |
| 121 | + syncer.release(forks.1); |
| 122 | + syncer.release(forks.0); |
| 123 | + |
| 124 | + let delay = get_random_delay(n, 25); |
| 125 | + printkln!("Child {} thinking ({} ms)", n, delay); |
| 126 | + sleep(delay); |
| 127 | + // stats.lock().unwrap().record_think(n, delay); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/// Get a random delay, based on the ID of this user, and the current uptime. |
| 133 | +fn get_random_delay(id: usize, period: usize) -> Duration { |
| 134 | + let tick = (uptime_get() & (usize::MAX as i64)) as usize; |
| 135 | + let delay = (tick / 100 * (id + 1)) & 0x1f; |
| 136 | + |
| 137 | + // Use one greater to be sure to never get a delay of zerp. |
| 138 | + Duration::millis_at_least(((delay + 1) * period) as Tick) |
| 139 | +} |
| 140 | + |
| 141 | +kobj_define! { |
| 142 | + static PHIL_THREAD: [StaticThread; NUM_PHIL]; |
| 143 | + static PHIL_STACK: [ThreadStack<PHIL_STACK_SIZE>; NUM_PHIL]; |
| 144 | +} |
0 commit comments