burritos/src/kernel/synch.rs

388 lines
14 KiB
Rust
Raw Normal View History

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;
use std::cell::RefCell;
2023-03-08 16:39:00 +01:00
use std::rc::Rc;
use super::scheduler::Scheduler;
2023-03-15 11:09:34 +01:00
use super::system::System;
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
}
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::new()}
}
/// 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) => {
self.waiting_queue.push(Rc::clone(thread));
2023-03-15 17:57:53 +01:00
thread_manager.thread_sleep(machine, Rc::clone(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-15 16:28:29 +01:00
pub fn v(&mut self, system: &mut System){
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
2023-03-14 20:28:57 +01:00
self.counter += 1;
2023-03-08 16:39:00 +01:00
if self.waiting_queue.peek() != None {
2023-03-15 16:28:29 +01:00
system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
2023-03-08 15:45:35 +01:00
}
2023-03-15 16:28:29 +01:00
system.get_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
}
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::new(), 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
2023-03-15 17:57:53 +01:00
pub fn acquire(&mut self, thread_manager: &mut ThreadManager, machine: &mut Machine) {
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)
},
None => unreachable!()
});
2023-03-08 16:39:00 +01:00
} else {
2023-03-15 17:57:53 +01:00
let t = thread_manager.get_g_current_thread();
2023-03-15 16:28:29 +01:00
match t {
Some(x) => {
2023-03-15 16:28:29 +01:00
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-15 16:28:29 +01:00
pub fn release(&mut self, system: &mut System) {
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
2023-03-15 11:09:34 +01:00
2023-03-15 16:28:29 +01:00
match system.get_thread_manager().get_g_current_thread() {
Some(thread) => {
if self.held_by_current_thread(system) {
if self.waiting_queue.peek() != None {
self.owner = Some(self.waiting_queue.pop().unwrap());
match &self.owner {
Some(x) => system.get_thread_manager().g_scheduler.ready_to_run(Rc::clone(&x)),
None => ()
}
} else {
self.free = true;
self.owner = None;
}
}
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-15 16:28:29 +01:00
system.get_machine().interrupt.set_status(old_status);
2023-03-08 16:39:00 +01:00
}
2023-03-15 16:28:29 +01:00
pub fn held_by_current_thread(&mut self, system: &mut System) -> bool {
2023-03-14 16:44:10 +01:00
match &self.owner {
2023-03-15 16:28:29 +01:00
Some(x) =>
match system.get_thread_manager().get_g_current_thread() {
Some(thread) => Rc::ptr_eq(&x, thread),
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
}
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::new()}
}
/// 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
2023-03-15 16:51:57 +01:00
pub fn wait(&mut self, system: &mut System) {
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
2023-03-08 16:39:00 +01:00
2023-03-15 16:51:57 +01:00
match system.get_thread_manager().get_g_current_thread() {
Some(thread) => {
self.waiting_queue.push(Rc::clone(thread));
system.thread_sleep(Rc::clone(thread));
},
None => unreachable!()
}
2023-03-08 16:39:00 +01:00
2023-03-15 16:51:57 +01:00
system.get_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-15 16:51:57 +01:00
pub fn signal(&mut self, system: &mut System) {
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
2023-03-08 16:39:00 +01:00
if self.waiting_queue.peek() != None {
2023-03-15 16:51:57 +01:00
system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
2023-03-08 16:39:00 +01:00
}
2023-03-15 16:51:57 +01:00
system.get_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-15 16:51:57 +01:00
pub fn broadcast(&mut self, system: &mut System) {
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
2023-03-08 16:39:00 +01:00
while self.waiting_queue.peek() != None {
2023-03-15 16:51:57 +01:00
system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
2023-03-08 16:39:00 +01:00
}
2023-03-15 16:51:57 +01:00
system.get_machine().interrupt.set_status(old_status);
2023-03-08 16:39:00 +01:00
}
2023-03-14 20:28:57 +01:00
}
2023-03-15 11:09:34 +01:00
#[cfg(test)]
mod test {
use std::{rc::Rc, cell::RefCell};
use crate::{kernel::{thread::Thread, synch::{Semaphore, Lock}}, init_system, simulator::machine::Machine};
#[test]
fn test_semaphore_single() {
// Init
let system = init_system!();
2023-03-15 17:57:53 +01:00
let mut semaphore = Semaphore::new(1);
2023-03-15 11:09:34 +01:00
let thread = Rc::new(RefCell::new(Thread::new("test_semaphore")));
// P
2023-03-15 17:57:53 +01:00
semaphore.p(system, thread);
2023-03-15 11:09:34 +01:00
assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
// V
semaphore.v(Rc::clone(&system));
assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty());
}
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
#[test]
#[ignore]
fn test_semaphore_multiple() {
// Init
let system = init_system!();
let tm = system.borrow_mut().get_thread_manager();
let mut semaphore = Semaphore::new(2, Rc::clone(&tm));
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 borrow_tm.g_scheduler;
scheduler.ready_to_run(Rc::clone(&thread1));
scheduler.ready_to_run(Rc::clone(&thread2));
scheduler.ready_to_run(Rc::clone(&thread3));
// P
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread1)));
semaphore.p(thread1, Rc::clone(&system));
assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty());
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread2)));
semaphore.p(thread2, Rc::clone(&system));
assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread3)));
semaphore.p(thread3, Rc::clone(&system));
assert_eq!(semaphore.counter, -1);
assert!(semaphore.waiting_queue.iter().count() == 1);
// V
semaphore.v(Rc::clone(&system));
assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
semaphore.v(Rc::clone(&system));
assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty());
semaphore.v(Rc::clone(&system));
assert_eq!(semaphore.counter, 2);
assert!(semaphore.waiting_queue.is_empty());
}
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
#[test]
#[ignore]
fn test_lock_simple() {
let system = init_system!();
let sys = system.borrow_mut();
let tm = sys.get_thread_manager();
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));
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
assert!(lock.free);
lock.acquire(Some(Rc::clone(&thread)), Rc::clone(&system));
assert!(lock.held_by_current_thread(Rc::clone(&thread)));
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
assert!(!lock.free);
lock.release(Rc::clone(&system), Rc::clone(&thread));
assert!(!lock.held_by_current_thread(thread));
assert!(lock.free);
}
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
#[test]
#[ignore]
fn test_lock_multiple() {
let system = init_system!();
let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1")));
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2")));
let thread3 = Rc::new(RefCell::new(Thread::new("test_lock3")));
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
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));
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
assert!(lock.free);
lock.acquire(Some(Rc::clone(&thread1)), Rc::clone(&system));
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
assert!(!lock.free);
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
lock.acquire(Some(Rc::clone(&thread2)), Rc::clone(&system));
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
assert!(lock.waiting_queue.iter().count() == 1);
assert!(!lock.free);
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
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);
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
2023-03-14 20:28:57 +01:00
2023-03-15 11:09:34 +01:00
lock.release(Rc::clone(&system), Rc::clone(&thread2));
assert!(!lock.held_by_current_thread(thread2));
assert!(lock.free);
}
2023-03-08 15:45:35 +01:00
}