This commit is contained in:
Rémi Rativel 2023-03-13 23:45:09 +01:00
parent dc6c454250
commit 65ac9c6f06

View File

@ -10,15 +10,15 @@ use super::scheduler::Scheduler;
use super::thread_manager::ThreadManager; use super::thread_manager::ThreadManager;
/// Structure of a Semaphore used for synchronisation /// Structure of a Semaphore used for synchronisation
pub struct Semaphore<'t> { pub struct Semaphore {
counter:i32, counter:i32,
waiting_queue:List<Rc<RefCell<Thread>>>, waiting_queue:List<Rc<RefCell<Thread>>>,
thread_manager: Rc<RefCell<ThreadManager<'t>>> // On s'assure que le tm vit plus longtemps que les semaphore avec le lifetime thread_manager: Rc<RefCell<ThreadManager>> // On s'assure que le tm vit plus longtemps que les semaphore avec le lifetime
} }
impl<'t> Semaphore<'_> { impl<'t> Semaphore {
/// Decrement the value, and wait if it becomes < 0. Checking the /// Decrement the value, and wait if it becomes < 0. Checking the
/// value and decrementing must be done atomically, so we /// value and decrementing must be done atomically, so we
@ -63,16 +63,16 @@ impl<'t> Semaphore<'_> {
/// Lock used for synchronisation, can be interpreted has a Semaphore with a /// Lock used for synchronisation, can be interpreted has a Semaphore with a
/// counter of 1 /// counter of 1
/// It's used for critical parts /// It's used for critical parts
pub struct Lock<'t>{ pub struct Lock{
owner: Rc<RefCell<Thread>>, owner: Rc<RefCell<Thread>>,
waiting_queue:List<Rc<RefCell<Thread>>>, waiting_queue:List<Rc<RefCell<Thread>>>,
thread_manager: Rc<RefCell<ThreadManager<'t>>>, thread_manager: Rc<RefCell<ThreadManager>>,
free: bool free: bool
} }
impl<'t> Lock<'_> { impl<'t> Lock {
/// Wait until the lock become free. Checking the /// Wait until the lock become free. Checking the
/// state of the lock (free or busy) and modify it must be done /// state of the lock (free or busy) and modify it must be done
@ -111,7 +111,7 @@ impl<'t> Lock<'_> {
pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<RefCell<Thread>>) { pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<RefCell<Thread>>) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
if self.is_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 = self.waiting_queue.pop().unwrap(); self.owner = self.waiting_queue.pop().unwrap();
scheduler.ready_to_run(Rc::clone(&self.owner)); scheduler.ready_to_run(Rc::clone(&self.owner));
@ -123,20 +123,20 @@ impl<'t> Lock<'_> {
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }
pub fn is_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 {
Rc::ptr_eq(&self.owner, &current_thread) Rc::ptr_eq(&self.owner, &current_thread)
} }
} }
/// Structure of a condition used for synchronisation /// Structure of a condition used for synchronisation
pub struct Condition<'t>{ pub struct Condition{
waiting_queue:List<Rc<RefCell<Thread>>>, waiting_queue:List<Rc<RefCell<Thread>>>,
thread_manager: Rc<RefCell<ThreadManager<'t>>>, thread_manager: Rc<RefCell<ThreadManager>>,
} }
impl<'t> Condition<'_> { impl<'t> Condition {
/// Block the calling thread (put it in the wait queue). /// Block the calling thread (put it in the wait queue).
/// This operation must be atomic, so we need to disable interrupts. /// This operation must be atomic, so we need to disable interrupts.