diff --git a/src/lib.rs b/src/lib.rs index eaa2938..dfbc7c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,7 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) -> ! { unsafe{ arch::interrupt::enable(); } // thread::test::unpack(); - sync::philosopher::philosopher(); + sync::test::philosopher(); // 直接进入用户态暂不可用:内核代码用户不可访问 // unsafe{ diff --git a/src/process/processor.rs b/src/process/processor.rs index ba3aac7..9810df5 100644 --- a/src/process/processor.rs +++ b/src/process/processor.rs @@ -30,7 +30,7 @@ impl Processor { kernel_page_table: None, next: None, // NOTE: max_time_slice <= 5 to ensure 'priority' test pass - scheduler: RRScheduler::new(100), + scheduler: RRScheduler::new(5), } } diff --git a/src/sync/mod.rs b/src/sync/mod.rs new file mode 100644 index 0000000..0b880ac --- /dev/null +++ b/src/sync/mod.rs @@ -0,0 +1,5 @@ +mod mutex; +mod semaphore; +pub mod test; + +pub use self::mutex::*; \ No newline at end of file diff --git a/src/sync.rs b/src/sync/mutex.rs similarity index 79% rename from src/sync.rs rename to src/sync/mutex.rs index dc6d400..5660b3c 100644 --- a/src/sync.rs +++ b/src/sync/mutex.rs @@ -30,7 +30,6 @@ use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering}; use core::cell::UnsafeCell; use core::ops::{Deref, DerefMut}; use core::fmt; -use arch::interrupt; pub type SpinLock = Mutex; pub type SpinNoIrqLock = Mutex; @@ -223,6 +222,8 @@ impl MutexSupport for Spin { fn after_unlock(&self) {} } +use arch::interrupt; + /// Spin & no-interrupt lock #[derive(Debug)] pub struct SpinNoIrq; @@ -273,86 +274,4 @@ impl MutexSupport for Thread { t.unpark(); } } -} - - -pub mod philosopher { - //! Dining philosophers problem - //! - //! The code is borrowed from [RustDoc - Dining Philosophers](https://doc.rust-lang.org/1.6.0/book/dining-philosophers.html) - - use thread; - use core::time::Duration; - use alloc::{arc::Arc, Vec}; - use super::ThreadLock as Mutex; - - struct Philosopher { - name: &'static str, - left: usize, - right: usize, - } - - impl Philosopher { - fn new(name: &'static str, left: usize, right: usize) -> Philosopher { - Philosopher { - name, - left, - right, - } - } - - fn eat(&self, table: &Table) { - let _left = table.forks[self.left].lock(); - let _right = table.forks[self.right].lock(); - - println!("{} is eating.", self.name); - thread::sleep(Duration::from_secs(1)); - } - - fn think(&self) { - println!("{} is thinking.", self.name); - thread::sleep(Duration::from_secs(1)); - } - } - - struct Table { - forks: Vec>, - } - - pub fn philosopher() { - let table = Arc::new(Table { - forks: vec![ - Mutex::new(()), - Mutex::new(()), - Mutex::new(()), - Mutex::new(()), - Mutex::new(()), - ] - }); - - let philosophers = vec![ - Philosopher::new("1", 0, 1), - Philosopher::new("2", 1, 2), - Philosopher::new("3", 2, 3), - Philosopher::new("4", 3, 4), - Philosopher::new("5", 0, 4), - ]; - - let handles: Vec<_> = philosophers.into_iter().map(|p| { - let table = table.clone(); - - thread::spawn(move || { - for i in 0..5 { - p.think(); - p.eat(&table); - println!("{} iter {} end.", p.name, i); - } - }) - }).collect(); - - for h in handles { - h.join().unwrap(); - } - println!("philosophers dining end"); - } } \ No newline at end of file diff --git a/src/sync/semaphore.rs b/src/sync/semaphore.rs new file mode 100644 index 0000000..6ee6be7 --- /dev/null +++ b/src/sync/semaphore.rs @@ -0,0 +1,2 @@ +use super::mutex; + diff --git a/src/sync/test.rs b/src/sync/test.rs new file mode 100644 index 0000000..61a0036 --- /dev/null +++ b/src/sync/test.rs @@ -0,0 +1,78 @@ +//! Dining philosophers problem +//! +//! The code is borrowed from [RustDoc - Dining Philosophers](https://doc.rust-lang.org/1.6.0/book/dining-philosophers.html) + +use thread; +use core::time::Duration; +use alloc::{arc::Arc, Vec}; +use sync::ThreadLock as Mutex; + +struct Philosopher { + name: &'static str, + left: usize, + right: usize, +} + +impl Philosopher { + fn new(name: &'static str, left: usize, right: usize) -> Philosopher { + Philosopher { + name, + left, + right, + } + } + + fn eat(&self, table: &Table) { + let _left = table.forks[self.left].lock(); + let _right = table.forks[self.right].lock(); + + println!("{} is eating.", self.name); + thread::sleep(Duration::from_secs(1)); + } + + fn think(&self) { + println!("{} is thinking.", self.name); + thread::sleep(Duration::from_secs(1)); + } +} + +struct Table { + forks: Vec>, +} + +pub fn philosopher() { + let table = Arc::new(Table { + forks: vec![ + Mutex::new(()), + Mutex::new(()), + Mutex::new(()), + Mutex::new(()), + Mutex::new(()), + ] + }); + + let philosophers = vec![ + Philosopher::new("1", 0, 1), + Philosopher::new("2", 1, 2), + Philosopher::new("3", 2, 3), + Philosopher::new("4", 3, 4), + Philosopher::new("5", 0, 4), + ]; + + let handles: Vec<_> = philosophers.into_iter().map(|p| { + let table = table.clone(); + + thread::spawn(move || { + for i in 0..5 { + p.think(); + p.eat(&table); + println!("{} iter {} end.", p.name, i); + } + }) + }).collect(); + + for h in handles { + h.join().unwrap(); + } + println!("philosophers dining end"); +} \ No newline at end of file