Add MutexSpin and several syscalls.
This commit is contained in:
parent
d1e55d85d8
commit
8974a29245
8 changed files with 159 additions and 2 deletions
|
@ -1,3 +1,5 @@
|
|||
mod up;
|
||||
mod mutex;
|
||||
|
||||
pub use up::UPSafeCell;
|
||||
pub use up::UPSafeCell;
|
||||
pub use mutex::{Mutex, MutexSpin};
|
||||
|
|
40
os/src/sync/mutex.rs
Normal file
40
os/src/sync/mutex.rs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue