2023-03-08 15:45:35 +01:00
|
|
|
use crate::utility::list::List;
|
|
|
|
use crate::kernel::thread::Thread;
|
|
|
|
use crate::simulator::interrupt::InterruptStatus::InterruptOff;
|
|
|
|
use crate::simulator::machine::Machine;
|
2023-03-09 14:00:42 +01:00
|
|
|
use std::cell::RefCell;
|
2023-03-08 16:39:00 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
|
|
|
|
use super::scheduler::Scheduler;
|
2023-03-09 14:00:42 +01:00
|
|
|
use super::thread_manager::ThreadManager;
|
2023-03-08 15:45:35 +01:00
|
|
|
|
2023-03-13 23:38:45 +01:00
|
|
|
/// Structure of a Semaphore used for synchronisation
|
2023-03-13 23:45:09 +01:00
|
|
|
pub struct Semaphore {
|
2023-03-08 15:45:35 +01:00
|
|
|
|
2023-03-14 14:45:19 +01:00
|
|
|
/// Counter of simultanous Semaphore
|
2023-03-08 15:45:35 +01:00
|
|
|
counter:i32,
|
2023-03-14 14:45:19 +01:00
|
|
|
/// QUeue of Semaphore waiting to be exucated
|
2023-03-09 14:00:42 +01:00
|
|
|
waiting_queue:List<Rc<RefCell<Thread>>>,
|
2023-03-14 14:45:19 +01:00
|
|
|
/// Thread manager which managing threads
|
|
|
|
thread_manager: Rc<RefCell<ThreadManager>>
|
2023-03-08 15:45:35 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:45:19 +01:00
|
|
|
impl Semaphore {
|
2023-03-08 15:45:35 +01:00
|
|
|
|
2023-03-14 16:44:10 +01:00
|
|
|
/// Initializes a semaphore, so that it can be used for synchronization.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - *counter* initial value of counter
|
|
|
|
/// - *thread_manager* Thread manager which managing threads
|
2023-03-14 16:34:44 +01:00
|
|
|
pub fn new(counter: i32, thread_manager: Rc<RefCell<ThreadManager>>) -> Semaphore{
|
|
|
|
Semaphore { counter, waiting_queue: List::new(), thread_manager}
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:38:45 +01:00
|
|
|
/// Decrement the value, and wait if it becomes < 0. Checking the
|
|
|
|
/// value and decrementing must be done atomically, so we
|
|
|
|
/// need to disable interrupts before checking the value.
|
|
|
|
///
|
|
|
|
/// Note that thread_manager::thread_sleep assumes that interrupts are disabled
|
|
|
|
/// when it is called.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - *current_thread* the current thread
|
|
|
|
/// - *machine* the machine where the threads are executed
|
2023-03-09 14:00:42 +01:00
|
|
|
pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine){
|
2023-03-08 15:45:35 +01:00
|
|
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
2023-03-08 16:39:00 +01:00
|
|
|
self.counter -= 1;
|
2023-03-08 15:45:35 +01:00
|
|
|
if self.counter < 0 {
|
|
|
|
self.waiting_queue.push(Rc::clone(¤t_thread));
|
2023-03-09 14:00:42 +01:00
|
|
|
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
2023-03-08 15:45:35 +01:00
|
|
|
}
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:16:40 +01:00
|
|
|
/// Increment semaphore value, waking up a waiting thread if any.
|
|
|
|
/// As with P(), this operation must be atomic, so we need to disable
|
|
|
|
/// interrupts.
|
2023-03-13 23:38:45 +01:00
|
|
|
///
|
|
|
|
/// scheduler::ready_to_run() assumes that interrupts
|
2023-03-14 15:16:40 +01:00
|
|
|
/// are disabled when it is called.
|
2023-03-13 23:38:45 +01:00
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - **machine** the machine where the threads are executed
|
|
|
|
/// - **scheduler** the scheduler which determine which thread to execute
|
2023-03-08 16:39:00 +01:00
|
|
|
pub fn v(&mut self, machine: &mut Machine, scheduler: &mut Scheduler){
|
2023-03-08 15:45:35 +01:00
|
|
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
2023-03-08 16:39:00 +01:00
|
|
|
self.counter -= 1;
|
|
|
|
if self.waiting_queue.peek() != None {
|
|
|
|
scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
2023-03-08 15:45:35 +01:00
|
|
|
}
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:38:45 +01:00
|
|
|
/// Lock used for synchronisation, can be interpreted has a Semaphore with a
|
|
|
|
/// counter of 1
|
|
|
|
/// It's used for critical parts
|
2023-03-13 23:45:09 +01:00
|
|
|
pub struct Lock{
|
2023-03-08 15:45:35 +01:00
|
|
|
|
2023-03-14 14:45:19 +01:00
|
|
|
/// Thread owning the lock
|
2023-03-14 16:34:44 +01:00
|
|
|
owner: Option<Rc<RefCell<Thread>>>,
|
2023-03-14 14:45:19 +01:00
|
|
|
/// The queue of threads waiting for execution
|
2023-03-09 14:00:42 +01:00
|
|
|
waiting_queue:List<Rc<RefCell<Thread>>>,
|
2023-03-14 14:45:19 +01:00
|
|
|
/// Thread manager which managing threads
|
2023-03-13 23:45:09 +01:00
|
|
|
thread_manager: Rc<RefCell<ThreadManager>>,
|
2023-03-14 14:45:19 +01:00
|
|
|
/// A boolean definig if the lock is free or not
|
2023-03-08 16:39:00 +01:00
|
|
|
free: bool
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-14 00:09:45 +01:00
|
|
|
impl Lock {
|
2023-03-13 23:38:45 +01:00
|
|
|
|
2023-03-14 16:34:44 +01:00
|
|
|
/// Initialize a Lock, so that it can be used for synchronization.
|
|
|
|
/// The lock is initialy free
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - **thread_manager** Thread manager which managing threads
|
|
|
|
pub fn new(thread_manager: Rc<RefCell<ThreadManager>>) -> Lock {
|
|
|
|
Lock { owner: None, waiting_queue: List::new(), thread_manager, free: true }
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:38:45 +01:00
|
|
|
/// Wait until the lock become free. Checking the
|
|
|
|
/// state of the lock (free or busy) and modify it must be done
|
|
|
|
/// atomically, so we need to disable interrupts before checking
|
|
|
|
/// the value of free.
|
|
|
|
///
|
|
|
|
/// Note that thread_manager::thread_seep assumes that interrupts are disabled
|
|
|
|
/// when it is called.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - **current_thread** the current thread
|
|
|
|
/// - **machine** the machine where the threads are executed
|
2023-03-14 16:34:44 +01:00
|
|
|
pub fn acquire(&mut self, current_thread: Option<Rc<RefCell<Thread>>>, machine: &mut Machine) {
|
2023-03-08 16:39:00 +01:00
|
|
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
|
|
|
|
|
|
|
if self.free {
|
|
|
|
self.free = false;
|
|
|
|
self.owner = current_thread;
|
|
|
|
} else {
|
2023-03-14 16:34:44 +01:00
|
|
|
match current_thread {
|
|
|
|
Some(x) => {
|
|
|
|
self.waiting_queue.push(Rc::clone(&x));
|
|
|
|
self.thread_manager.borrow_mut().thread_sleep(x)
|
|
|
|
},
|
|
|
|
None => ()
|
|
|
|
}
|
2023-03-08 16:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:38:45 +01:00
|
|
|
/// Wake up a waiter if necessary, or release it if no thread is waiting.
|
|
|
|
/// We check that the lock is held by the g_current_thread.
|
|
|
|
/// As with Acquire, this operation must be atomic, so we need to disable
|
|
|
|
/// interrupts. scheduler::ready_to_run() assumes that threads
|
|
|
|
/// are disabled when it is called.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - **machine** the machine where the code is executed
|
|
|
|
/// - **scheduler** the scheduler which determine which thread to execute
|
2023-03-09 14:00:42 +01:00
|
|
|
pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<RefCell<Thread>>) {
|
2023-03-08 16:39:00 +01:00
|
|
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
|
|
|
|
2023-03-13 23:45:09 +01:00
|
|
|
if self.held_by_current_thread(current_thread) {
|
2023-03-08 16:39:00 +01:00
|
|
|
if self.waiting_queue.peek() != None {
|
2023-03-14 16:34:44 +01:00
|
|
|
self.owner = Some(self.waiting_queue.pop().unwrap());
|
|
|
|
match &self.owner {
|
|
|
|
Some(x) => scheduler.ready_to_run(Rc::clone(&x)),
|
|
|
|
None => ()
|
|
|
|
}
|
2023-03-08 16:39:00 +01:00
|
|
|
} else {
|
|
|
|
self.free = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:45:09 +01:00
|
|
|
pub fn held_by_current_thread(&mut self, current_thread: Rc<RefCell<Thread>>) -> bool {
|
2023-03-14 16:44:10 +01:00
|
|
|
match &self.owner {
|
|
|
|
Some(x) => Rc::ptr_eq(&x, ¤t_thread),
|
|
|
|
None => false
|
|
|
|
}
|
2023-03-08 16:39:00 +01:00
|
|
|
}
|
2023-03-08 15:45:35 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:38:45 +01:00
|
|
|
/// Structure of a condition used for synchronisation
|
2023-03-13 23:45:09 +01:00
|
|
|
pub struct Condition{
|
2023-03-08 15:45:35 +01:00
|
|
|
|
2023-03-14 14:45:19 +01:00
|
|
|
/// The queue of threads waiting for execution
|
2023-03-09 14:00:42 +01:00
|
|
|
waiting_queue:List<Rc<RefCell<Thread>>>,
|
2023-03-14 14:45:19 +01:00
|
|
|
/// Thread manager which managing threads
|
2023-03-13 23:45:09 +01:00
|
|
|
thread_manager: Rc<RefCell<ThreadManager>>,
|
2023-03-08 16:39:00 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-14 00:09:45 +01:00
|
|
|
impl Condition {
|
2023-03-13 23:38:45 +01:00
|
|
|
|
2023-03-14 16:34:44 +01:00
|
|
|
/// Initializes a Condition, so that it can be used for synchronization.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - *thread_manager* Thread manager which managing threads
|
|
|
|
pub fn new(thread_manager: Rc<RefCell<ThreadManager>>) -> Condition {
|
|
|
|
Condition{ waiting_queue: List::new(), thread_manager }
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:38:45 +01:00
|
|
|
/// Block the calling thread (put it in the wait queue).
|
|
|
|
/// This operation must be atomic, so we need to disable interrupts.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - **current_thread** the current thread
|
|
|
|
/// - **machine** the machine where threads are executed
|
|
|
|
pub fn wait(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine) {
|
2023-03-08 16:39:00 +01:00
|
|
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
|
|
|
|
|
|
|
self.waiting_queue.push(Rc::clone(¤t_thread));
|
2023-03-09 14:00:42 +01:00
|
|
|
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
2023-03-08 16:39:00 +01:00
|
|
|
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:38:45 +01:00
|
|
|
/// Wake up the first thread of the wait queue (if any).
|
|
|
|
/// This operation must be atomic, so we need to disable interrupts.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - **machine** the machine where the code is executed
|
|
|
|
/// - **scheduler** the scheduler which determine which thread to execute
|
2023-03-08 16:39:00 +01:00
|
|
|
pub fn signal(&mut self, machine: &mut Machine, scheduler: &mut Scheduler) {
|
|
|
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
|
|
|
|
|
|
|
if self.waiting_queue.peek() != None {
|
|
|
|
scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:38:45 +01:00
|
|
|
/// Wake up all threads waiting in the waitqueue of the condition
|
|
|
|
/// This operation must be atomic, so we need to disable interrupts.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - **machine** the machine where the code is executed
|
|
|
|
/// - **scheduler** the scheduler which determine which thread to execute
|
2023-03-08 16:39:00 +01:00
|
|
|
pub fn broadcast(&mut self, machine: &mut Machine, scheduler: &mut Scheduler) {
|
|
|
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
|
|
|
|
|
|
|
while self.waiting_queue.peek() != None {
|
|
|
|
scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
|
|
|
}
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:45:35 +01:00
|
|
|
}
|