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::system::System;
use super::thread_manager::ThreadManager;
/// Structure of a Semaphore used for synchronisation
@ -42,14 +43,14 @@ impl Semaphore {
/// ### Parameters
/// - *current_thread* the current thread
/// - *machine* the machine where the threads are executed
pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine){
let old_status = machine.interrupt.set_status(InterruptOff);
pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, system: Rc<RefCell<System>>) {
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
self.counter -= 1;
if self.counter < 0 {
self.waiting_queue.push(Rc::clone(&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.
@ -62,13 +63,13 @@ impl Semaphore {
/// ### Parameters
/// - **machine** the machine where the threads are executed
/// - **scheduler** the scheduler which determine which thread to execute
pub fn v(&mut self, machine: &mut Machine, scheduler: &mut Scheduler){
let old_status = machine.interrupt.set_status(InterruptOff);
pub fn v(&mut self, system: Rc<RefCell<System>>){
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
self.counter += 1;
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
/// - **current_thread** the current thread
/// - **machine** the machine where the threads are executed
pub fn acquire(&mut self, current_thread: Option<Rc<RefCell<Thread>>>, machine: &mut Machine) {
let old_status = machine.interrupt.set_status(InterruptOff);
pub fn acquire(&mut self, current_thread: Option<Rc<RefCell<Thread>>>, system: Rc<RefCell<System>>) {
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
if self.free {
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.
@ -138,12 +139,15 @@ impl Lock {
/// ### Parameters
/// - **machine** the machine where the code is executed
/// - **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>>) {
let old_status = machine.interrupt.set_status(InterruptOff);
pub fn release(&mut self, system: Rc<RefCell<System>>, current_thread: Rc<RefCell<Thread>>) {
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.waiting_queue.peek() != None {
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 {
Some(x) => scheduler.ready_to_run(Rc::clone(&x)),
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 {
@ -235,120 +239,132 @@ impl Condition {
}
#[test]
fn test_semaphore_single() {
// Init
let thread_manager = Rc::new(RefCell::new(ThreadManager::new()));
let mut semaphore = Semaphore::new(1, thread_manager);
let mut machine = Machine::init_machine();
let mut scheduler = Scheduler::new();
let thread = Rc::new(RefCell::new(Thread::new("test_semaphore")));
// P
semaphore.p(thread, &mut machine);
assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
// V
semaphore.v(&mut machine, &mut scheduler);
assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty());
}
#[cfg(test)]
mod test {
use std::{rc::Rc, cell::RefCell};
#[test]
fn test_semaphore_multiple() {
// Init
let thread_manager = Rc::new(RefCell::new(ThreadManager::new()));
let mut semaphore = Semaphore::new(2, Rc::clone(&thread_manager));
let mut machine = Machine::init_machine();
let mut scheduler = Scheduler::new();
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")));
// P
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
semaphore.p(thread1, &mut machine);
assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty());
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
semaphore.p(thread2, &mut machine);
assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
use crate::{kernel::{thread::Thread, synch::{Semaphore, Lock}}, init_system, simulator::machine::Machine};
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread3)));
semaphore.p(thread3, &mut machine);
assert_eq!(semaphore.counter, -1);
assert!(semaphore.waiting_queue.iter().count() == 1);
#[test]
fn test_semaphore_single() {
// Init
let system = init_system!();
let mut semaphore = Semaphore::new(1, Rc::clone(&system.borrow_mut().get_thread_manager()));
let thread = Rc::new(RefCell::new(Thread::new("test_semaphore")));
// P
semaphore.p(thread, Rc::clone(&system));
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());
}
// V
semaphore.v(&mut machine, &mut scheduler);
assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty());
#[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")));
semaphore.v(&mut machine, &mut scheduler);
assert_eq!(semaphore.counter, 1);
assert!(semaphore.waiting_queue.is_empty());
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());
semaphore.v(&mut machine, &mut scheduler);
assert_eq!(semaphore.counter, 2);
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());
}
#[test]
fn test_lock_simple() {
let thread_manager = Rc::new(RefCell::new(ThreadManager::new()));
let mut machine = Machine::init_machine();
let mut scheduler = Scheduler::new();
let thread = Rc::new(RefCell::new(Thread::new("test_lock")));
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread)));
let mut lock = Lock::new(Rc::clone(&thread_manager));
#[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));
assert!(lock.free);
lock.acquire(Some(Rc::clone(&thread)), &mut machine);
assert!(lock.held_by_current_thread(Rc::clone(&thread)));
assert!(lock.free);
lock.acquire(Some(Rc::clone(&thread)), Rc::clone(&system));
assert!(lock.held_by_current_thread(Rc::clone(&thread)));
assert!(!lock.free);
lock.release(&mut machine, &mut scheduler, Rc::clone(&thread));
assert!(!lock.held_by_current_thread(thread));
assert!(lock.free);
}
assert!(!lock.free);
lock.release(Rc::clone(&system), Rc::clone(&thread));
assert!(!lock.held_by_current_thread(thread));
assert!(lock.free);
}
#[test]
fn test_lock_multiple() {
let thread_manager = Rc::new(RefCell::new(ThreadManager::new()));
let mut machine = Machine::init_machine();
let mut scheduler = Scheduler::new();
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")));
#[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")));
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
let mut lock = Lock::new(Rc::clone(&thread_manager));
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.free);
lock.acquire(Some(Rc::clone(&thread1)), &mut machine);
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
assert!(!lock.free);
assert!(lock.free);
lock.acquire(Some(Rc::clone(&thread1)), Rc::clone(&system));
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
assert!(!lock.free);
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
lock.acquire(Some(Rc::clone(&thread2)), &mut machine);
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
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)));
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
assert!(lock.waiting_queue.iter().count() == 1);
assert!(!lock.free);
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
assert!(lock.waiting_queue.iter().count() == 1);
assert!(!lock.free);
lock.release(&mut machine, &mut scheduler, Rc::clone(&thread1));
assert!(!lock.held_by_current_thread(thread1));
assert!(lock.held_by_current_thread(Rc::clone(&thread2)));
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);
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
lock.release(&mut machine, &mut scheduler, Rc::clone(&thread2));
assert!(!lock.held_by_current_thread(thread2));
assert!(lock.free);
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)
}};
($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
}
pub fn get_thread_manager(&self) -> Rc<RefCell<ThreadManager>> {
Rc::clone(&self.thread_manager)
}
// Setters
/// Assign a machine to the system

View File

@ -87,7 +87,7 @@ impl Drop for Thread {
fn drop(&mut self) {
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();
while next_thread.is_none() {
eprintln!("Nobody to run => idle");
machine.interrupt.idle();
next_thread = self.g_scheduler.find_next_to_run();
}

View File

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