diff --git a/os/src/syscall/mod.rs b/os/src/syscall/mod.rs index fa4a4cf1..ca3fc526 100644 --- a/os/src/syscall/mod.rs +++ b/os/src/syscall/mod.rs @@ -62,7 +62,7 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize { SYSCALL_SEMAPHORE_CREATE => sys_semaphore_create(args[0]), SYSCALL_SEMAPHORE_UP => sys_semaphore_up(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_WAIT => sys_condvar_wait(args[0], args[1]), _ => panic!("Unsupported syscall_id: {}", syscall_id), diff --git a/os/src/syscall/sync.rs b/os/src/syscall/sync.rs index dd0e856a..eb67583a 100644 --- a/os/src/syscall/sync.rs +++ b/os/src/syscall/sync.rs @@ -93,7 +93,7 @@ pub fn sys_semaphore_down(sem_id: usize) -> isize { 0 } -pub fn sys_condvar_create(_arg: usize) -> isize { +pub fn sys_condvar_create() -> isize { let process = current_process(); let mut process_inner = process.inner_exclusive_access(); let id = if let Some(id) = process_inner diff --git a/user/src/bin/barrier_condvar.rs b/user/src/bin/barrier_condvar.rs index db0a80ba..ca0953b3 100644 --- a/user/src/bin/barrier_condvar.rs +++ b/user/src/bin/barrier_condvar.rs @@ -28,7 +28,7 @@ impl Barrier { } pub fn block(&self) { 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 // critical section protected by the mutex. unsafe { *count = *count + 1; } diff --git a/user/src/bin/test_condvar.rs b/user/src/bin/condsync_condvar.rs similarity index 100% rename from user/src/bin/test_condvar.rs rename to user/src/bin/condsync_condvar.rs index 2db9d8a4..b83aeda2 100644 --- a/user/src/bin/test_condvar.rs +++ b/user/src/bin/condsync_condvar.rs @@ -35,8 +35,8 @@ unsafe fn second() -> ! { println!("Second: A is {}", A); condvar_wait(CONDVAR_ID, MUTEX_ID); } - mutex_unlock(MUTEX_ID); println!("A is {}, Second can work now", A); + mutex_unlock(MUTEX_ID); exit(0) } diff --git a/user/src/bin/condsync_sem.rs b/user/src/bin/condsync_sem.rs new file mode 100644 index 00000000..d7b875a7 --- /dev/null +++ b/user/src/bin/condsync_sem.rs @@ -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 +} diff --git a/user/src/lib.rs b/user/src/lib.rs index 3677a65d..7e5482ae 100644 --- a/user/src/lib.rs +++ b/user/src/lib.rs @@ -189,7 +189,7 @@ pub fn semaphore_down(sem_id: usize) { sys_semaphore_down(sem_id); } pub fn condvar_create() -> isize { - sys_condvar_create(0) + sys_condvar_create() } pub fn condvar_signal(condvar_id: usize) { sys_condvar_signal(condvar_id); diff --git a/user/src/syscall.rs b/user/src/syscall.rs index 4cd714ef..eecdf096 100644 --- a/user/src/syscall.rs +++ b/user/src/syscall.rs @@ -145,8 +145,8 @@ pub fn sys_semaphore_down(sem_id: usize) -> isize { syscall(SYSCALL_SEMAPHORE_DOWN, [sem_id, 0, 0]) } -pub fn sys_condvar_create(_arg: usize) -> isize { - syscall(SYSCALL_CONDVAR_CREATE, [_arg, 0, 0]) +pub fn sys_condvar_create() -> isize { + syscall(SYSCALL_CONDVAR_CREATE, [0, 0, 0]) } pub fn sys_condvar_signal(condvar_id: usize) -> isize {