Add MutexSpin and several syscalls.

This commit is contained in:
Yifan Wu 2021-10-07 12:40:47 -07:00
parent d1e55d85d8
commit 8974a29245
8 changed files with 159 additions and 2 deletions

40
os/src/sync/mutex.rs Normal file
View file

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