1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-23 00:16:17 +04:00

Split sync mod

This commit is contained in:
WangRunji 2018-06-01 15:07:12 +08:00
parent beffc302f0
commit 5891613b22
6 changed files with 89 additions and 85 deletions

View File

@ -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{

View File

@ -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),
}
}

5
src/sync/mod.rs Normal file
View File

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

View File

@ -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<T> = Mutex<T, Spin>;
pub type SpinNoIrqLock<T> = Mutex<T, SpinNoIrq>;
@ -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<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");
}
}

2
src/sync/semaphore.rs Normal file
View File

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

78
src/sync/test.rs Normal file
View File

@ -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");
}