diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index 0f98417..2150a0b 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -301,7 +301,10 @@ impl ThreadManager { Some(thread) => { lock.owner = Some(thread); match &lock.owner { - Some(x) => self.ready_to_run(Rc::clone(x)), + Some(x) => { + self.ready_to_run(Rc::clone(x)); + lock.free = true; + }, None => () } }, @@ -369,6 +372,7 @@ mod test { use std::{rc::Rc, cell::RefCell}; use crate::{simulator::{machine::Machine, loader}, kernel::{system::System, thread::Thread, process::Process, thread_manager::ThreadManager, synch::Semaphore}}; + use crate::kernel::synch::Lock; #[test] fn test_thread_context() { @@ -398,6 +402,31 @@ mod test { machine.run(system); } + #[test] + fn test_lock(){ + let mut machine = Machine::new(true); + let mut thread_manager = ThreadManager::new(true); + let lock = Lock::new(); + let lock_id = thread_manager.get_obj_addrs().add_lock(lock); + let thread = Rc::new(RefCell::new(Thread::new("test_lock"))); + thread_manager.ready_to_run(Rc::clone(&thread)); + thread_manager.set_g_current_thread(Some(thread)); + + thread_manager.lock_acquire(lock_id, &mut machine).expect("lock acquire return an error: "); + { + let lock = thread_manager.get_obj_addrs().search_lock(lock_id).unwrap(); + assert!(!lock.free); + assert!(lock.waiting_queue.is_empty()); + } + + thread_manager.lock_release(lock_id, &mut machine).expect("lock release return an error: "); + { + let lock = thread_manager.get_obj_addrs().search_lock(lock_id).unwrap(); + assert!(!lock.free); + assert!(lock.waiting_queue.is_empty()); + } + } + #[test] fn test_semaphore_single() { // Init