use of system in parameters in synch

This commit is contained in:
Quentin Legot 2023-03-15 11:09:34 +01:00
parent e5242aab0c
commit c8df1e5053
5 changed files with 138 additions and 114 deletions

View File

@ -7,6 +7,7 @@ use std::rc::Rc;
use super::scheduler::Scheduler; use super::scheduler::Scheduler;
use super::system::System;
use super::thread_manager::ThreadManager; use super::thread_manager::ThreadManager;
/// Structure of a Semaphore used for synchronisation /// Structure of a Semaphore used for synchronisation
@ -42,14 +43,14 @@ impl Semaphore {
/// ### Parameters /// ### Parameters
/// - *current_thread* the current thread /// - *current_thread* the current thread
/// - *machine* the machine where the threads are executed /// - *machine* the machine where the threads are executed
pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine){ pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, system: Rc<RefCell<System>>) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
self.counter -= 1; self.counter -= 1;
if self.counter < 0 { if self.counter < 0 {
self.waiting_queue.push(Rc::clone(&current_thread)); self.waiting_queue.push(Rc::clone(&current_thread));
self.thread_manager.borrow_mut().thread_sleep(current_thread); self.thread_manager.borrow_mut().thread_sleep(current_thread);
} }
machine.interrupt.set_status(old_status); system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
} }
/// Increment semaphore value, waking up a waiting thread if any. /// Increment semaphore value, waking up a waiting thread if any.
@ -62,13 +63,13 @@ impl Semaphore {
/// ### Parameters /// ### Parameters
/// - **machine** the machine where the threads are executed /// - **machine** the machine where the threads are executed
/// - **scheduler** the scheduler which determine which thread to execute /// - **scheduler** the scheduler which determine which thread to execute
pub fn v(&mut self, machine: &mut Machine, scheduler: &mut Scheduler){ pub fn v(&mut self, system: Rc<RefCell<System>>){
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
self.counter += 1; self.counter += 1;
if self.waiting_queue.peek() != None { if self.waiting_queue.peek() != None {
scheduler.ready_to_run(self.waiting_queue.pop().unwrap()); system.borrow_mut().get_thread_manager().borrow_mut().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
} }
machine.interrupt.set_status(old_status); system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
} }
} }
@ -110,8 +111,8 @@ impl Lock {
/// ### Parameters /// ### Parameters
/// - **current_thread** the current thread /// - **current_thread** the current thread
/// - **machine** the machine where the threads are executed /// - **machine** the machine where the threads are executed
pub fn acquire(&mut self, current_thread: Option<Rc<RefCell<Thread>>>, machine: &mut Machine) { pub fn acquire(&mut self, current_thread: Option<Rc<RefCell<Thread>>>, system: Rc<RefCell<System>>) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
if self.free { if self.free {
self.free = false; self.free = false;
@ -126,7 +127,7 @@ impl Lock {
} }
} }
machine.interrupt.set_status(old_status); system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
} }
/// Wake up a waiter if necessary, or release it if no thread is waiting. /// Wake up a waiter if necessary, or release it if no thread is waiting.
@ -138,12 +139,15 @@ impl Lock {
/// ### Parameters /// ### Parameters
/// - **machine** the machine where the code is executed /// - **machine** the machine where the code is executed
/// - **scheduler** the scheduler which determine which thread to execute /// - **scheduler** the scheduler which determine which thread to execute
pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<RefCell<Thread>>) { pub fn release(&mut self, system: Rc<RefCell<System>>, current_thread: Rc<RefCell<Thread>>) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
if self.held_by_current_thread(current_thread) { if self.held_by_current_thread(current_thread) {
if self.waiting_queue.peek() != None { if self.waiting_queue.peek() != None {
self.owner = Some(self.waiting_queue.pop().unwrap()); self.owner = Some(self.waiting_queue.pop().unwrap());
let sys = system.borrow_mut();
let tm = sys.get_thread_manager();
let scheduler = &mut tm.borrow_mut().g_scheduler;
match &self.owner { match &self.owner {
Some(x) => scheduler.ready_to_run(Rc::clone(&x)), Some(x) => scheduler.ready_to_run(Rc::clone(&x)),
None => () None => ()
@ -154,7 +158,7 @@ impl Lock {
} }
} }
machine.interrupt.set_status(old_status); system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
} }
pub fn held_by_current_thread(&mut self, current_thread: Rc<RefCell<Thread>>) -> bool { pub fn held_by_current_thread(&mut self, current_thread: Rc<RefCell<Thread>>) -> bool {
@ -235,120 +239,132 @@ impl Condition {
} }
#[test] #[cfg(test)]
fn test_semaphore_single() { mod test {
// Init use std::{rc::Rc, cell::RefCell};
let thread_manager = Rc::new(RefCell::new(ThreadManager::new()));
let mut semaphore = Semaphore::new(1, thread_manager); use crate::{kernel::{thread::Thread, synch::{Semaphore, Lock}}, init_system, simulator::machine::Machine};
let mut machine = Machine::init_machine();
let mut scheduler = Scheduler::new(); #[test]
let thread = Rc::new(RefCell::new(Thread::new("test_semaphore"))); fn test_semaphore_single() {
// P // Init
semaphore.p(thread, &mut machine); let system = init_system!();
assert_eq!(semaphore.counter, 0); let mut semaphore = Semaphore::new(1, Rc::clone(&system.borrow_mut().get_thread_manager()));
assert!(semaphore.waiting_queue.is_empty()); let thread = Rc::new(RefCell::new(Thread::new("test_semaphore")));
// V // P
semaphore.v(&mut machine, &mut scheduler); semaphore.p(thread, Rc::clone(&system));
assert_eq!(semaphore.counter, 1); assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty()); assert!(semaphore.waiting_queue.is_empty());
} // V
semaphore.v(Rc::clone(&system));
#[test] assert_eq!(semaphore.counter, 1);
fn test_semaphore_multiple() { assert!(semaphore.waiting_queue.is_empty());
// Init }
let thread_manager = Rc::new(RefCell::new(ThreadManager::new()));
let mut semaphore = Semaphore::new(2, Rc::clone(&thread_manager)); #[test]
let mut machine = Machine::init_machine(); #[ignore]
let mut scheduler = Scheduler::new(); fn test_semaphore_multiple() {
let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1"))); // Init
let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2"))); let system = init_system!();
let thread3 = Rc::new(RefCell::new(Thread::new("test_semaphore_3"))); let tm = system.borrow_mut().get_thread_manager();
// P let mut semaphore = Semaphore::new(2, Rc::clone(&tm));
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1))); let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1")));
semaphore.p(thread1, &mut machine); let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2")));
assert_eq!(semaphore.counter, 1); let thread3 = Rc::new(RefCell::new(Thread::new("test_semaphore_3")));
assert!(semaphore.waiting_queue.is_empty());
let mut borrow_tm = tm.borrow_mut();
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2))); let scheduler = &mut borrow_tm.g_scheduler;
semaphore.p(thread2, &mut machine); scheduler.ready_to_run(Rc::clone(&thread1));
assert_eq!(semaphore.counter, 0); scheduler.ready_to_run(Rc::clone(&thread2));
assert!(semaphore.waiting_queue.is_empty()); scheduler.ready_to_run(Rc::clone(&thread3));
// P
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread3))); borrow_tm.set_g_current_thread(Some(Rc::clone(&thread1)));
semaphore.p(thread3, &mut machine); semaphore.p(thread1, Rc::clone(&system));
assert_eq!(semaphore.counter, -1); assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.iter().count() == 1); assert!(semaphore.waiting_queue.is_empty());
// V borrow_tm.set_g_current_thread(Some(Rc::clone(&thread2)));
semaphore.v(&mut machine, &mut scheduler); semaphore.p(thread2, Rc::clone(&system));
assert_eq!(semaphore.counter, 0); assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty()); assert!(semaphore.waiting_queue.is_empty());
semaphore.v(&mut machine, &mut scheduler); borrow_tm.set_g_current_thread(Some(Rc::clone(&thread3)));
assert_eq!(semaphore.counter, 1); semaphore.p(thread3, Rc::clone(&system));
assert!(semaphore.waiting_queue.is_empty()); assert_eq!(semaphore.counter, -1);
assert!(semaphore.waiting_queue.iter().count() == 1);
semaphore.v(&mut machine, &mut scheduler);
assert_eq!(semaphore.counter, 2); // V
assert!(semaphore.waiting_queue.is_empty()); semaphore.v(Rc::clone(&system));
} assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
semaphore.v(Rc::clone(&system));
#[test] assert_eq!(semaphore.counter, 1);
fn test_lock_simple() { assert!(semaphore.waiting_queue.is_empty());
let thread_manager = Rc::new(RefCell::new(ThreadManager::new()));
let mut machine = Machine::init_machine(); semaphore.v(Rc::clone(&system));
let mut scheduler = Scheduler::new(); assert_eq!(semaphore.counter, 2);
let thread = Rc::new(RefCell::new(Thread::new("test_lock"))); assert!(semaphore.waiting_queue.is_empty());
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread))); }
let mut lock = Lock::new(Rc::clone(&thread_manager));
assert!(lock.free);
lock.acquire(Some(Rc::clone(&thread)), &mut machine); #[test]
assert!(lock.held_by_current_thread(Rc::clone(&thread))); #[ignore]
fn test_lock_simple() {
assert!(!lock.free); let system = init_system!();
lock.release(&mut machine, &mut scheduler, Rc::clone(&thread)); let sys = system.borrow_mut();
assert!(!lock.held_by_current_thread(thread)); let tm = sys.get_thread_manager();
assert!(lock.free); let thread = Rc::new(RefCell::new(Thread::new("test_lock")));
} tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread)));
let mut lock = Lock::new(Rc::clone(&tm));
#[test]
fn test_lock_multiple() { assert!(lock.free);
let thread_manager = Rc::new(RefCell::new(ThreadManager::new())); lock.acquire(Some(Rc::clone(&thread)), Rc::clone(&system));
let mut machine = Machine::init_machine(); assert!(lock.held_by_current_thread(Rc::clone(&thread)));
let mut scheduler = Scheduler::new();
let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1"))); assert!(!lock.free);
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2"))); lock.release(Rc::clone(&system), Rc::clone(&thread));
let thread3 = Rc::new(RefCell::new(Thread::new("test_lock3"))); assert!(!lock.held_by_current_thread(thread));
assert!(lock.free);
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1))); }
let mut lock = Lock::new(Rc::clone(&thread_manager));
#[test]
assert!(lock.free); #[ignore]
lock.acquire(Some(Rc::clone(&thread1)), &mut machine); fn test_lock_multiple() {
assert!(lock.held_by_current_thread(Rc::clone(&thread1))); let system = init_system!();
assert!(!lock.free); let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1")));
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2")));
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2))); let thread3 = Rc::new(RefCell::new(Thread::new("test_lock3")));
lock.acquire(Some(Rc::clone(&thread2)), &mut machine);
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1))); let tm = system.borrow_mut().get_thread_manager();
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
let mut lock = Lock::new(Rc::clone(&tm));
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
assert!(lock.waiting_queue.iter().count() == 1); assert!(lock.free);
assert!(!lock.free); lock.acquire(Some(Rc::clone(&thread1)), Rc::clone(&system));
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
lock.release(&mut machine, &mut scheduler, Rc::clone(&thread1)); assert!(!lock.free);
assert!(!lock.held_by_current_thread(thread1));
assert!(lock.held_by_current_thread(Rc::clone(&thread2))); tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
assert!(!lock.free); lock.acquire(Some(Rc::clone(&thread2)), Rc::clone(&system));
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
lock.release(&mut machine, &mut scheduler, Rc::clone(&thread2)); assert!(lock.waiting_queue.iter().count() == 1);
assert!(!lock.held_by_current_thread(thread2)); assert!(!lock.free);
assert!(lock.free);
lock.release(Rc::clone(&system), Rc::clone(&thread1));
assert!(!lock.held_by_current_thread(thread1));
assert!(lock.held_by_current_thread(Rc::clone(&thread2)));
assert!(!lock.free);
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
lock.release(Rc::clone(&system), Rc::clone(&thread2));
assert!(!lock.held_by_current_thread(thread2));
assert!(lock.free);
}
} }

View File

@ -12,7 +12,10 @@ macro_rules! init_system {
init_system!(m) init_system!(m)
}}; }};
($a:expr) => {{ ($a:expr) => {{
System::new($a) let sys = std::rc::Rc::new(std::cell::RefCell::new(crate::System::new($a)));
crate::System::freeze(std::rc::Rc::clone(&sys));
sys
}}; }};
} }
@ -59,6 +62,10 @@ impl System {
&self.g_machine &self.g_machine
} }
pub fn get_thread_manager(&self) -> Rc<RefCell<ThreadManager>> {
Rc::clone(&self.thread_manager)
}
// Setters // Setters
/// Assign a machine to the system /// Assign a machine to the system

View File

@ -87,7 +87,7 @@ impl Drop for Thread {
fn drop(&mut self) { fn drop(&mut self) {
self.object_type = ObjectType::InvalidType; self.object_type = ObjectType::InvalidType;
todo!(); // todo!();
} }
} }

View File

@ -94,6 +94,7 @@ impl ThreadManager {
let mut next_thread = self.g_scheduler.find_next_to_run(); let mut next_thread = self.g_scheduler.find_next_to_run();
while next_thread.is_none() { while next_thread.is_none() {
eprintln!("Nobody to run => idle");
machine.interrupt.idle(); machine.interrupt.idle();
next_thread = self.g_scheduler.find_next_to_run(); next_thread = self.g_scheduler.find_next_to_run();
} }

View File

@ -30,7 +30,7 @@ impl Interrupt {
} }
pub fn idle(&self) { pub fn idle(&self) {
todo!(); // todo!();
} }
} }