From 32c1237c2699532af2ea07141fdece99eedd3ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rativel?= Date: Wed, 19 Apr 2023 15:52:31 +0200 Subject: [PATCH] Tests lock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: RĂ©mi Rativel --- src/kernel/thread_manager.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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