parent
43c6b7cf01
commit
b0fad5aca3
@ -1,3 +1,5 @@
|
||||
mod up;
|
||||
mod mutex;
|
||||
|
||||
pub use up::UPSafeCell;
|
||||
pub use up::UPSafeCell;
|
||||
pub use mutex::{Mutex, MutexSpin};
|
||||
|
@ -0,0 +1,40 @@
|
||||
use super::UPSafeCell;
|
||||
use crate::task::suspend_current_and_run_next;
|
||||
|
||||
pub trait Mutex: Sync + Send {
|
||||
fn lock(&self);
|
||||
fn unlock(&self);
|
||||
}
|
||||
|
||||
pub struct MutexSpin {
|
||||
locked: UPSafeCell<bool>,
|
||||
}
|
||||
|
||||
impl MutexSpin {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
locked: unsafe { UPSafeCell::new(false) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mutex for MutexSpin {
|
||||
fn lock(&self) {
|
||||
loop {
|
||||
let mut locked = self.locked.exclusive_access();
|
||||
if *locked {
|
||||
drop(locked);
|
||||
suspend_current_and_run_next();
|
||||
continue;
|
||||
} else {
|
||||
*locked = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn unlock(&self) {
|
||||
let mut locked = self.locked.exclusive_access();
|
||||
*locked = false;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
use crate::task::current_process;
|
||||
use crate::sync::MutexSpin;
|
||||
use alloc::sync::Arc;
|
||||
|
||||
pub fn sys_mutex_create() -> isize {
|
||||
let process = current_process();
|
||||
let mut process_inner = process.inner_exclusive_access();
|
||||
if let Some(id) = process_inner
|
||||
.mutex_list
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, item)| item.is_none())
|
||||
.map(|(id, _)| id) {
|
||||
process_inner.mutex_list[id] = Some(Arc::new(MutexSpin::new()));
|
||||
id as isize
|
||||
} else {
|
||||
process_inner.mutex_list.push(Some(Arc::new(MutexSpin::new())));
|
||||
process_inner.mutex_list.len() as isize - 1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sys_mutex_lock(mutex_id: usize) -> isize {
|
||||
let process = current_process();
|
||||
let process_inner = process.inner_exclusive_access();
|
||||
let mutex = Arc::clone(process_inner.mutex_list[mutex_id].as_ref().unwrap());
|
||||
drop(process_inner);
|
||||
drop(process);
|
||||
mutex.lock();
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_mutex_unlock(mutex_id: usize) -> isize {
|
||||
let process = current_process();
|
||||
let process_inner = process.inner_exclusive_access();
|
||||
let mutex = Arc::clone(process_inner.mutex_list[mutex_id].as_ref().unwrap());
|
||||
drop(process_inner);
|
||||
drop(process);
|
||||
mutex.unlock();
|
||||
0
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use user_lib::{exit, thread_create, waittid, get_time};
|
||||
use user_lib::{mutex_create, mutex_lock, mutex_unlock};
|
||||
use alloc::vec::Vec;
|
||||
|
||||
static mut A: usize = 0;
|
||||
const PER_THREAD: usize = 10000;
|
||||
const THREAD_COUNT: usize = 8;
|
||||
|
||||
unsafe fn f() -> ! {
|
||||
let mut t = 2usize;
|
||||
for _ in 0..PER_THREAD {
|
||||
mutex_lock(0);
|
||||
let a = &mut A as *mut usize;
|
||||
let cur = a.read_volatile();
|
||||
for _ in 0..500 { t = t * t % 10007; }
|
||||
a.write_volatile(cur + 1);
|
||||
mutex_unlock(0);
|
||||
}
|
||||
exit(t as i32)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let start = get_time();
|
||||
assert_eq!(mutex_create(), 0);
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
time_cost.push(waittid(*tid));
|
||||
}
|
||||
println!("time cost is {}ms", get_time() - start);
|
||||
assert_eq!(unsafe { A }, PER_THREAD * THREAD_COUNT);
|
||||
0
|
||||
}
|
Loading…
Reference in new issue