From 597ffa753a7091c624427b9da0ea44b3e7c54fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rativel?= Date: Thu, 20 Apr 2023 15:20:28 +0200 Subject: [PATCH] test lock for multiple threads 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 | 49 +++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index 962b3f4..2de0449 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -443,7 +443,7 @@ mod test { } #[test] - fn test_lock(){ + fn test_lock_single(){ let mut machine = Machine::new(true, get_debug_configuration()); let mut thread_manager = ThreadManager::new(true); let lock = Lock::new(); @@ -470,6 +470,53 @@ mod test { } } + #[test] + fn test_lock_multiple() { + let mut machine = Machine::new(true, get_debug_configuration()); + let mut thread_manager = ThreadManager::new(true); + let lock = Lock::new(); + let lock_id = thread_manager.get_obj_addrs().add_lock(lock); + let thread_1 = Rc::new(RefCell::new(Thread::new("test_lock_1"))); + let thread_2 = Rc::new(RefCell::new(Thread::new("test_lock_2"))); + let thread_test_1 = thread_1.clone(); + let thread_test_2 = thread_2.clone(); + thread_manager.ready_to_run(Rc::clone(&thread_1)); + thread_manager.ready_to_run(Rc::clone(&thread_2)); + thread_manager.set_g_current_thread(Some(thread_1)); + + thread_manager.lock_acquire(lock_id, &mut machine).expect("lock acquire return an error at first iteration: "); + { + let lock = thread_manager.get_obj_addrs().search_lock(lock_id).unwrap(); + assert_eq!(lock.owner,Some(thread_test_1.clone())); + assert!(!lock.free); + assert!(lock.waiting_queue.is_empty()); + } + + thread_manager.set_g_current_thread(Some(thread_2)); + thread_manager.lock_acquire(lock_id, &mut machine).expect("lock acquire return an error at second iteration: "); + { + let lock = thread_manager.get_obj_addrs().search_lock(lock_id).unwrap(); + assert_eq!(lock.owner,Some(thread_test_1)); + assert!(!lock.free); + assert_eq!(lock.waiting_queue.iter().count(),1); + } + thread_manager.lock_release(lock_id, &mut machine).expect("lock release return an error at first iteration: "); + { + let lock = thread_manager.get_obj_addrs().search_lock(lock_id).unwrap(); + assert_eq!(lock.owner, Some(thread_test_2)); + assert!(!lock.free); + assert!(lock.waiting_queue.is_empty()); + } + thread_manager.lock_release(lock_id, &mut machine).expect("lock release return an error at second iteration: "); + { + let lock = thread_manager.get_obj_addrs().search_lock(lock_id).unwrap(); + assert_eq!(lock.owner, None); + assert!(lock.free); + assert!(lock.waiting_queue.is_empty()); + } + } + + #[test] fn test_semaphore_single() { // Init