parent
1c0bbf4404
commit
713e78ea91
@ -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;
|
@ -0,0 +1,56 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use user_lib::{condvar_create, condvar_signal, condvar_wait, mutex_blocking_create, mutex_lock, mutex_unlock};
|
||||
use user_lib::{thread_create, waittid, sleep};
|
||||
use user_lib::exit;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
static mut A: usize = 0;
|
||||
|
||||
const CONDVAR_ID: usize = 0;
|
||||
const MUTEX_ID: usize = 0;
|
||||
|
||||
unsafe fn first() -> ! {
|
||||
sleep(10);
|
||||
println!("First work, Change A --> 1 and wakeup Second");
|
||||
mutex_lock(MUTEX_ID);
|
||||
A=1;
|
||||
condvar_signal(CONDVAR_ID);
|
||||
mutex_unlock(MUTEX_ID);
|
||||
exit(0)
|
||||
}
|
||||
|
||||
unsafe fn second() -> ! {
|
||||
println!("Second want to continue,but need to wait A=1");
|
||||
mutex_lock(MUTEX_ID);
|
||||
while A==0 {
|
||||
println!("Second: A is {}", A);
|
||||
condvar_wait(CONDVAR_ID, MUTEX_ID);
|
||||
}
|
||||
mutex_unlock(MUTEX_ID);
|
||||
println!("A is {}, Second can work now", A);
|
||||
exit(0)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
// create condvar & mutex
|
||||
assert_eq!(condvar_create() as usize, CONDVAR_ID);
|
||||
assert_eq!(mutex_blocking_create() as usize, MUTEX_ID);
|
||||
// create threads
|
||||
let mut threads = Vec::new();
|
||||
threads.push(thread_create(first as usize, 0));
|
||||
threads.push(thread_create(second as usize, 0));
|
||||
// wait for all threads to complete
|
||||
for thread in threads.iter() {
|
||||
waittid(*thread as usize);
|
||||
}
|
||||
println!("test_condvar passed!");
|
||||
0
|
||||
}
|
Loading…
Reference in new issue