Tests lock

Signed-off-by: Rémi Rativel <remi.rativel@etudiant.univ-rennes1.fr>
This commit is contained in:
Rémi Rativel 2023-04-19 15:52:31 +02:00
parent 35b2949243
commit 32c1237c26

View File

@ -301,7 +301,10 @@ impl ThreadManager {
Some(thread) => { Some(thread) => {
lock.owner = Some(thread); lock.owner = Some(thread);
match &lock.owner { 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 => () None => ()
} }
}, },
@ -369,6 +372,7 @@ mod test {
use std::{rc::Rc, cell::RefCell}; 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::{simulator::{machine::Machine, loader}, kernel::{system::System, thread::Thread, process::Process, thread_manager::ThreadManager, synch::Semaphore}};
use crate::kernel::synch::Lock;
#[test] #[test]
fn test_thread_context() { fn test_thread_context() {
@ -398,6 +402,31 @@ mod test {
machine.run(system); 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] #[test]
fn test_semaphore_single() { fn test_semaphore_single() {
// Init // Init