add condvar in kernel and app
This commit is contained in:
parent
a3ec647496
commit
b3bce3bef2
8 changed files with 243 additions and 34 deletions
39
os/src/sync/condvar.rs
Normal file
39
os/src/sync/condvar.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use alloc::{sync::Arc, collections::VecDeque};
|
||||
use crate::task::{add_task, TaskControlBlock, current_task, block_current_and_run_next};
|
||||
use crate::sync::{Mutex, UPSafeCell};
|
||||
|
||||
pub struct Condvar {
|
||||
pub inner: UPSafeCell<CondvarInner>,
|
||||
}
|
||||
|
||||
pub struct CondvarInner {
|
||||
pub wait_queue: VecDeque<Arc<TaskControlBlock>>,
|
||||
}
|
||||
|
||||
impl Condvar {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: unsafe { UPSafeCell::new(
|
||||
CondvarInner {
|
||||
wait_queue: VecDeque::new(),
|
||||
}
|
||||
)},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn signal(&self) {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
if let Some(task) = inner.wait_queue.pop_front() {
|
||||
add_task(task);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wait(&self, mutex:Arc<dyn Mutex>) {
|
||||
mutex.unlock();
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
inner.wait_queue.push_back(current_task().unwrap());
|
||||
drop(inner);
|
||||
block_current_and_run_next();
|
||||
mutex.lock();
|
||||
}
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
mod up;
|
||||
mod mutex;
|
||||
mod semaphore;
|
||||
mod condvar;
|
||||
|
||||
pub use up::UPSafeCell;
|
||||
pub use mutex::{Mutex, MutexSpin, MutexBlocking};
|
||||
pub use semaphore::Semaphore;
|
||||
pub use condvar::Condvar;
|
Loading…
Add table
Add a link
Reference in a new issue