update condvar-related testcases.

This commit is contained in:
Yifan Wu 2023-02-01 20:37:17 +08:00
parent e31bf71ea6
commit bf5b5f7d07
7 changed files with 71 additions and 7 deletions

View File

@ -62,7 +62,7 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
SYSCALL_SEMAPHORE_CREATE => sys_semaphore_create(args[0]), SYSCALL_SEMAPHORE_CREATE => sys_semaphore_create(args[0]),
SYSCALL_SEMAPHORE_UP => sys_semaphore_up(args[0]), SYSCALL_SEMAPHORE_UP => sys_semaphore_up(args[0]),
SYSCALL_SEMAPHORE_DOWN => sys_semaphore_down(args[0]), SYSCALL_SEMAPHORE_DOWN => sys_semaphore_down(args[0]),
SYSCALL_CONDVAR_CREATE => sys_condvar_create(args[0]), SYSCALL_CONDVAR_CREATE => sys_condvar_create(),
SYSCALL_CONDVAR_SIGNAL => sys_condvar_signal(args[0]), SYSCALL_CONDVAR_SIGNAL => sys_condvar_signal(args[0]),
SYSCALL_CONDVAR_WAIT => sys_condvar_wait(args[0], args[1]), SYSCALL_CONDVAR_WAIT => sys_condvar_wait(args[0], args[1]),
_ => panic!("Unsupported syscall_id: {}", syscall_id), _ => panic!("Unsupported syscall_id: {}", syscall_id),

View File

@ -93,7 +93,7 @@ pub fn sys_semaphore_down(sem_id: usize) -> isize {
0 0
} }
pub fn sys_condvar_create(_arg: usize) -> isize { pub fn sys_condvar_create() -> isize {
let process = current_process(); let process = current_process();
let mut process_inner = process.inner_exclusive_access(); let mut process_inner = process.inner_exclusive_access();
let id = if let Some(id) = process_inner let id = if let Some(id) = process_inner

View File

@ -28,7 +28,7 @@ impl Barrier {
} }
pub fn block(&self) { pub fn block(&self) {
mutex_lock(self.mutex_id); mutex_lock(self.mutex_id);
let mut count = self.count.get(); let count = self.count.get();
// SAFETY: Here, the accesses of the count is in the // SAFETY: Here, the accesses of the count is in the
// critical section protected by the mutex. // critical section protected by the mutex.
unsafe { *count = *count + 1; } unsafe { *count = *count + 1; }

View File

@ -35,8 +35,8 @@ unsafe fn second() -> ! {
println!("Second: A is {}", A); println!("Second: A is {}", A);
condvar_wait(CONDVAR_ID, MUTEX_ID); condvar_wait(CONDVAR_ID, MUTEX_ID);
} }
mutex_unlock(MUTEX_ID);
println!("A is {}, Second can work now", A); println!("A is {}, Second can work now", A);
mutex_unlock(MUTEX_ID);
exit(0) exit(0)
} }

View File

@ -0,0 +1,64 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
extern crate alloc;
use alloc::vec;
use user_lib::exit;
use user_lib::{
semaphore_create, semaphore_down, semaphore_up, mutex_blocking_create, mutex_lock, mutex_unlock,
};
use user_lib::{sleep, thread_create, waittid};
static mut A: usize = 0;
const SEM_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;
semaphore_up(SEM_ID);
mutex_unlock(MUTEX_ID);
exit(0)
}
unsafe fn second() -> ! {
println!("Second want to continue,but need to wait A=1");
loop {
mutex_lock(MUTEX_ID);
if A == 0 {
println!("Second: A is {}", A);
mutex_unlock(MUTEX_ID);
semaphore_down(SEM_ID);
} else {
mutex_unlock(MUTEX_ID);
break;
}
}
println!("A is {}, Second can work now", A);
exit(0)
}
#[no_mangle]
pub fn main() -> i32 {
// create semaphore & mutex
assert_eq!(semaphore_create(0) as usize, SEM_ID);
assert_eq!(mutex_blocking_create() as usize, MUTEX_ID);
// create threads
let threads = vec![
thread_create(first as usize, 0),
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
}

View File

@ -189,7 +189,7 @@ pub fn semaphore_down(sem_id: usize) {
sys_semaphore_down(sem_id); sys_semaphore_down(sem_id);
} }
pub fn condvar_create() -> isize { pub fn condvar_create() -> isize {
sys_condvar_create(0) sys_condvar_create()
} }
pub fn condvar_signal(condvar_id: usize) { pub fn condvar_signal(condvar_id: usize) {
sys_condvar_signal(condvar_id); sys_condvar_signal(condvar_id);

View File

@ -145,8 +145,8 @@ pub fn sys_semaphore_down(sem_id: usize) -> isize {
syscall(SYSCALL_SEMAPHORE_DOWN, [sem_id, 0, 0]) syscall(SYSCALL_SEMAPHORE_DOWN, [sem_id, 0, 0])
} }
pub fn sys_condvar_create(_arg: usize) -> isize { pub fn sys_condvar_create() -> isize {
syscall(SYSCALL_CONDVAR_CREATE, [_arg, 0, 0]) syscall(SYSCALL_CONDVAR_CREATE, [0, 0, 0])
} }
pub fn sys_condvar_signal(condvar_id: usize) -> isize { pub fn sys_condvar_signal(condvar_id: usize) -> isize {