burritos/src/kernel/synch.rs

401 lines
14 KiB
Rust
Raw Normal View History

2023-03-08 15:45:35 +01:00
use crate::utility::list::List;
use crate::utility::objaddr::*;
2023-03-08 15:45:35 +01:00
use crate::kernel::thread::Thread;
use crate::simulator::interrupt::InterruptStatus::InterruptOff;
use crate::simulator::machine::Machine;
use std::cell::RefCell;
2023-03-08 16:39:00 +01:00
use std::rc::Rc;
use super::thread_manager::ThreadManager;
2023-03-08 15:45:35 +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
waiting_queue:List<Rc<RefCell<Thread>>>,
2023-03-08 15:45:35 +01:00
}
impl SynchObj for Semaphore {}
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-15 16:51:57 +01:00
pub fn new(counter: i32) -> Semaphore{
Semaphore { counter, waiting_queue: List::default() }
}
/// 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.
///
2023-03-15 16:28:29 +01:00
/// ### Parameters TODO Refaire
/// - *current_thread* the current thread
/// - *machine* the machine where the threads are executed
2023-03-15 17:57:53 +01:00
pub fn p(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
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 {
2023-03-15 17:57:53 +01:00
match thread_manager.get_g_current_thread() {
2023-03-15 16:28:29 +01:00
Some(thread) => {
let rc1_thread = Rc::clone(thread);
let rc2_thread = Rc::clone(thread);
self.waiting_queue.push(rc1_thread);
thread_manager.thread_sleep(machine, rc2_thread);
2023-03-15 16:28:29 +01:00
},
None => unreachable!("Current thread should not be None")
}
2023-03-08 15:45:35 +01:00
}
2023-03-15 17:57:53 +01:00
machine.interrupt.set_status(old_status);
2023-03-08 15:45:35 +01:00
}
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.
///
/// scheduler::ready_to_run() assumes that interrupts
2023-03-14 15:16:40 +01:00
/// are disabled when it is called.
///
/// ### Parameters
/// - **machine** the machine where the threads are executed
/// - **scheduler** the scheduler which determine which thread to execute
2023-03-22 14:59:28 +01:00
pub fn v(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager){
let old_status = machine.interrupt.set_status(InterruptOff);
2023-03-14 20:28:57 +01:00
self.counter += 1;
2023-03-22 16:20:53 +01:00
match self.waiting_queue.pop() {
Some(thread) => thread_manager.ready_to_run(thread),
None => ()
2023-03-08 15:45:35 +01:00
}
2023-03-22 14:59:28 +01:00
machine.interrupt.set_status(old_status);
2023-03-08 15:45:35 +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
owner: Option<Rc<RefCell<Thread>>>,
2023-03-14 14:45:19 +01:00
/// The queue of threads waiting for execution
waiting_queue:List<Rc<RefCell<Thread>>>,
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
}
impl SynchObj for Lock {}
2023-03-14 00:09:45 +01:00
impl Lock {
/// Initialize a Lock, so that it can be used for synchronization.
/// The lock is initialy free
///
/// ### Parameters
/// - **thread_manager** Thread manager which managing threads
2023-03-15 16:51:57 +01:00
pub fn new() -> Lock {
Lock { owner: None, waiting_queue: List::default(), free: true }
}
/// 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
pub fn acquire(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
2023-03-15 17:57:53 +01:00
let old_status = machine.interrupt.set_status(InterruptOff);
2023-03-08 16:39:00 +01:00
if self.free {
self.free = false;
2023-03-15 17:57:53 +01:00
self.owner = Option::Some(match thread_manager.get_g_current_thread() {
2023-03-15 16:28:29 +01:00
Some(th) => {
Rc::clone(&th)
2023-03-15 16:28:29 +01:00
},
None => unreachable!()
});
2023-03-08 16:39:00 +01:00
} else {
match thread_manager.get_g_current_thread() {
Some(x) => {
let x = Rc::clone(&x);
self.waiting_queue.push(Rc::clone(&x));
2023-03-15 17:57:53 +01:00
thread_manager.thread_sleep(machine, Rc::clone(&x));
},
2023-03-15 16:28:29 +01:00
None => unreachable!("Current thread should not be None")
}
2023-03-08 16:39:00 +01:00
}
2023-03-15 17:57:53 +01:00
machine.interrupt.set_status(old_status);
2023-03-08 16:39:00 +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-22 14:59:28 +01:00
pub fn release(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
let old_status = machine.interrupt.set_status(InterruptOff);
2023-03-15 11:09:34 +01:00
2023-03-22 14:59:28 +01:00
match thread_manager.get_g_current_thread() {
2023-03-22 16:21:15 +01:00
Some(_) => {
2023-03-22 14:59:28 +01:00
if self.held_by_current_thread(thread_manager) {
2023-03-22 16:20:53 +01:00
match self.waiting_queue.pop() {
Some(thread) => {
self.owner = Some(thread);
match &self.owner {
Some(x) => thread_manager.ready_to_run(Rc::clone(&x)),
None => ()
}
},
None => {
self.free = true;
self.owner = None;
2023-03-15 16:28:29 +01:00
}
}
}
2023-03-08 16:39:00 +01:00
}
2023-03-15 16:28:29 +01:00
None => ()
2023-03-08 16:39:00 +01:00
}
2023-03-22 14:59:28 +01:00
machine.interrupt.set_status(old_status);
2023-03-08 16:39:00 +01:00
}
2023-03-22 16:03:36 +01:00
/// True if the current thread holds this lock.
/// Useful for checking in Release, and in Condition operations below.
2023-03-22 14:59:28 +01:00
pub fn held_by_current_thread(&mut self, thread_manager: &mut ThreadManager) -> bool {
2023-03-14 16:44:10 +01:00
match &self.owner {
2023-03-15 16:28:29 +01:00
Some(x) =>
2023-03-22 14:59:28 +01:00
match thread_manager.get_g_current_thread() {
2023-03-22 16:21:15 +01:00
Some(thread) => Rc::ptr_eq(x, thread),
2023-03-15 16:28:29 +01:00
None => false
}
2023-03-14 16:44:10 +01:00
None => false
}
2023-03-08 16:39:00 +01:00
}
2023-03-08 15:45:35 +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
waiting_queue:List<Rc<RefCell<Thread>>>,
2023-03-08 16:39:00 +01:00
}
impl SynchObj for Condition {}
2023-03-14 00:09:45 +01:00
impl Condition {
/// Initializes a Condition, so that it can be used for synchronization.
///
/// ### Parameters
/// - *thread_manager* Thread manager which managing threads
2023-03-15 16:51:57 +01:00
pub fn new() -> Condition {
Condition{ waiting_queue: List::default()}
}
/// 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, machine: &mut Machine, thread_manager: &mut ThreadManager) {
let old_status = machine.interrupt.set_status(InterruptOff);
match thread_manager.get_g_current_thread() {
2023-03-15 16:51:57 +01:00
Some(thread) => {
let rc1 = Rc::clone(thread);
let rc2 = Rc::clone(thread);
self.waiting_queue.push(rc1);
thread_manager.thread_sleep(machine, rc2);
2023-03-15 16:51:57 +01:00
},
None => unreachable!()
}
2023-03-08 16:39:00 +01:00
machine.interrupt.set_status(old_status);
2023-03-08 16:39:00 +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-22 15:48:29 +01:00
pub fn signal(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
let old_status = machine.interrupt.set_status(InterruptOff);
2023-03-08 16:39:00 +01:00
2023-03-22 16:20:53 +01:00
match self.waiting_queue.pop() {
Some(thread) => thread_manager.ready_to_run(thread),
None => ()
2023-03-08 16:39:00 +01:00
}
2023-03-22 15:48:29 +01:00
machine.interrupt.set_status(old_status);
2023-03-08 16:39:00 +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-22 15:48:29 +01:00
pub fn broadcast(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
let old_status = machine.interrupt.set_status(InterruptOff);
2023-03-08 16:39:00 +01:00
2023-03-22 16:20:53 +01:00
match self.waiting_queue.pop() {
Some(thread) => thread_manager.ready_to_run(thread),
None => ()
2023-03-08 16:39:00 +01:00
}
2023-03-22 15:48:29 +01:00
machine.interrupt.set_status(old_status);
2023-03-08 16:39:00 +01:00
}
2023-03-14 20:28:57 +01:00
}
2023-03-22 14:59:28 +01:00
#[cfg(test)]
mod test {
use std::{rc::Rc, cell::RefCell};
2023-03-22 15:48:29 +01:00
use crate::{kernel::{thread::Thread, synch::{Semaphore, Lock}, thread_manager::ThreadManager}, simulator::machine::Machine};
2023-03-22 14:59:28 +01:00
#[test]
fn test_semaphore_single() {
// Init
let mut machine = Machine::init_machine();
let mut thread_manager = ThreadManager::new();
let mut semaphore = Semaphore::new(1);
let thread = Rc::new(RefCell::new(Thread::new("test_semaphore")));
thread_manager.ready_to_run(Rc::clone(&thread));
thread_manager.set_g_current_thread(Some(thread));
// P
semaphore.p(&mut machine, &mut thread_manager);
assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
// V
semaphore.v(&mut machine, &mut thread_manager);
assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty());
}
#[test]
fn test_semaphore_multiple() {
// Init
let mut tm = ThreadManager::new();
let mut machine = Machine::init_machine();
let mut semaphore = Semaphore::new(2);
let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1")));
let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2")));
let thread3 = Rc::new(RefCell::new(Thread::new("test_semaphore_3")));
// let mut borrow_tm = tm.borrow_mut();
// let scheduler = &mut tm.g_scheduler;
tm.ready_to_run(Rc::clone(&thread1));
tm.ready_to_run(Rc::clone(&thread2));
tm.ready_to_run(Rc::clone(&thread3));
// P
tm.set_g_current_thread(Some(Rc::clone(&thread1)));
semaphore.p(&mut machine, &mut tm);
assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty());
2023-03-15 11:09:34 +01:00
2023-03-22 14:59:28 +01:00
tm.set_g_current_thread(Some(Rc::clone(&thread2)));
semaphore.p(&mut machine, &mut tm);
assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
tm.set_g_current_thread(Some(Rc::clone(&thread3)));
semaphore.p(&mut machine, &mut tm);
assert_eq!(semaphore.counter, -1);
assert!(semaphore.waiting_queue.iter().count() == 1);
// V
semaphore.v(&mut machine, &mut tm);
assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
semaphore.v(&mut machine, &mut tm);
assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty());
semaphore.v(&mut machine, &mut tm);
assert_eq!(semaphore.counter, 2);
assert!(semaphore.waiting_queue.is_empty());
}
2023-03-14 20:28:57 +01:00
2023-03-22 14:59:28 +01:00
#[test]
fn test_lock_simple() {
let mut machine = Machine::init_machine();
let mut tm = ThreadManager::new();
let thread = Rc::new(RefCell::new(Thread::new("test_lock")));
tm.ready_to_run(Rc::clone(&thread));
tm.set_g_current_thread(Some(Rc::clone(&thread)));
let mut lock = Lock::new();
assert!(lock.free);
lock.acquire(&mut machine, &mut tm);
assert!(lock.held_by_current_thread(&mut tm));
assert!(!lock.free);
lock.release(&mut machine, &mut tm);
assert!(!lock.held_by_current_thread(&mut tm));
assert!(lock.free);
}
#[test]
fn test_lock_multiple() {
let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1")));
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2")));
let mut machine = Machine::init_machine();
let mut tm = ThreadManager::new();
2023-03-15 11:09:34 +01:00
2023-03-22 14:59:28 +01:00
tm.ready_to_run(Rc::clone(&thread1));
tm.ready_to_run(Rc::clone(&thread2));
tm.set_g_current_thread(Some(Rc::clone(&thread1)));
let mut lock = Lock::new();
2023-03-14 20:28:57 +01:00
2023-03-22 14:59:28 +01:00
assert!(lock.free);
lock.acquire(&mut machine, &mut tm);
assert!(lock.held_by_current_thread(&mut tm));
assert!(!lock.free);
2023-03-14 20:28:57 +01:00
2023-03-22 14:59:28 +01:00
tm.set_g_current_thread(Some(Rc::clone(&thread2)));
lock.acquire(&mut machine, &mut tm);
tm.set_g_current_thread(Some(Rc::clone(&thread1)));
assert!(lock.held_by_current_thread(&mut tm));
assert!(lock.waiting_queue.iter().count() == 1);
assert!(!lock.free);
2023-03-14 20:28:57 +01:00
2023-03-22 14:59:28 +01:00
lock.release(&mut machine, &mut tm);
assert!(!lock.held_by_current_thread(&mut tm));
tm.set_g_current_thread(Some(Rc::clone(&thread2)));
assert!(lock.held_by_current_thread(&mut tm));
assert!(!lock.free);
2023-03-14 20:28:57 +01:00
2023-03-22 14:59:28 +01:00
lock.release(&mut machine, &mut tm);
assert!(!lock.held_by_current_thread(&mut tm));
assert!(lock.free);
}
}