Split sync mod

master
WangRunji 7 years ago
parent beffc302f0
commit 5891613b22

@ -91,7 +91,7 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) -> ! {
unsafe{ arch::interrupt::enable(); } unsafe{ arch::interrupt::enable(); }
// thread::test::unpack(); // thread::test::unpack();
sync::philosopher::philosopher(); sync::test::philosopher();
// 直接进入用户态暂不可用:内核代码用户不可访问 // 直接进入用户态暂不可用:内核代码用户不可访问
// unsafe{ // unsafe{

@ -30,7 +30,7 @@ impl Processor {
kernel_page_table: None, kernel_page_table: None,
next: None, next: None,
// NOTE: max_time_slice <= 5 to ensure 'priority' test pass // NOTE: max_time_slice <= 5 to ensure 'priority' test pass
scheduler: RRScheduler::new(100), scheduler: RRScheduler::new(5),
} }
} }

@ -0,0 +1,5 @@
mod mutex;
mod semaphore;
pub mod test;
pub use self::mutex::*;

@ -30,7 +30,6 @@ use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use core::cell::UnsafeCell; use core::cell::UnsafeCell;
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use core::fmt; use core::fmt;
use arch::interrupt;
pub type SpinLock<T> = Mutex<T, Spin>; pub type SpinLock<T> = Mutex<T, Spin>;
pub type SpinNoIrqLock<T> = Mutex<T, SpinNoIrq>; pub type SpinNoIrqLock<T> = Mutex<T, SpinNoIrq>;
@ -223,6 +222,8 @@ impl MutexSupport for Spin {
fn after_unlock(&self) {} fn after_unlock(&self) {}
} }
use arch::interrupt;
/// Spin & no-interrupt lock /// Spin & no-interrupt lock
#[derive(Debug)] #[derive(Debug)]
pub struct SpinNoIrq; pub struct SpinNoIrq;
@ -274,85 +275,3 @@ impl MutexSupport for Thread {
} }
} }
} }
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<Mutex<()>>,
}
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");
}
}

@ -0,0 +1,2 @@
use super::mutex;

@ -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<Mutex<()>>,
}
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");
}
Loading…
Cancel
Save