use of system in parameters in synch
This commit is contained in:
parent
e5242aab0c
commit
c8df1e5053
@ -7,6 +7,7 @@ use std::rc::Rc;
|
|||||||
|
|
||||||
|
|
||||||
use super::scheduler::Scheduler;
|
use super::scheduler::Scheduler;
|
||||||
|
use super::system::System;
|
||||||
use super::thread_manager::ThreadManager;
|
use super::thread_manager::ThreadManager;
|
||||||
|
|
||||||
/// Structure of a Semaphore used for synchronisation
|
/// Structure of a Semaphore used for synchronisation
|
||||||
@ -42,14 +43,14 @@ impl Semaphore {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - *current_thread* the current thread
|
/// - *current_thread* the current thread
|
||||||
/// - *machine* the machine where the threads are executed
|
/// - *machine* the machine where the threads are executed
|
||||||
pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine){
|
pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, system: Rc<RefCell<System>>) {
|
||||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
||||||
self.counter -= 1;
|
self.counter -= 1;
|
||||||
if self.counter < 0 {
|
if self.counter < 0 {
|
||||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||||
self.thread_manager.borrow_mut().thread_sleep(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.
|
/// Increment semaphore value, waking up a waiting thread if any.
|
||||||
@ -62,13 +63,13 @@ impl Semaphore {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **machine** the machine where the threads are executed
|
/// - **machine** the machine where the threads are executed
|
||||||
/// - **scheduler** the scheduler which determine which thread to execute
|
/// - **scheduler** the scheduler which determine which thread to execute
|
||||||
pub fn v(&mut self, machine: &mut Machine, scheduler: &mut Scheduler){
|
pub fn v(&mut self, system: Rc<RefCell<System>>){
|
||||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
||||||
self.counter += 1;
|
self.counter += 1;
|
||||||
if self.waiting_queue.peek() != None {
|
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
|
/// ### Parameters
|
||||||
/// - **current_thread** the current thread
|
/// - **current_thread** the current thread
|
||||||
/// - **machine** the machine where the threads are executed
|
/// - **machine** the machine where the threads are executed
|
||||||
pub fn acquire(&mut self, current_thread: Option<Rc<RefCell<Thread>>>, machine: &mut Machine) {
|
pub fn acquire(&mut self, current_thread: Option<Rc<RefCell<Thread>>>, system: Rc<RefCell<System>>) {
|
||||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
||||||
|
|
||||||
if self.free {
|
if self.free {
|
||||||
self.free = false;
|
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.
|
/// Wake up a waiter if necessary, or release it if no thread is waiting.
|
||||||
@ -138,12 +139,15 @@ impl Lock {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **machine** the machine where the code is executed
|
/// - **machine** the machine where the code is executed
|
||||||
/// - **scheduler** the scheduler which determine which thread to execute
|
/// - **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>>) {
|
pub fn release(&mut self, system: Rc<RefCell<System>>, current_thread: Rc<RefCell<Thread>>) {
|
||||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
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.held_by_current_thread(current_thread) {
|
||||||
if self.waiting_queue.peek() != None {
|
if self.waiting_queue.peek() != None {
|
||||||
self.owner = Some(self.waiting_queue.pop().unwrap());
|
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 {
|
match &self.owner {
|
||||||
Some(x) => scheduler.ready_to_run(Rc::clone(&x)),
|
Some(x) => scheduler.ready_to_run(Rc::clone(&x)),
|
||||||
None => ()
|
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 {
|
pub fn held_by_current_thread(&mut self, current_thread: Rc<RefCell<Thread>>) -> bool {
|
||||||
@ -235,120 +239,132 @@ impl Condition {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_semaphore_single() {
|
mod test {
|
||||||
// Init
|
use std::{rc::Rc, cell::RefCell};
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
use crate::{kernel::{thread::Thread, synch::{Semaphore, Lock}}, init_system, simulator::machine::Machine};
|
||||||
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());
|
|
||||||
|
|
||||||
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread3)));
|
#[test]
|
||||||
semaphore.p(thread3, &mut machine);
|
fn test_semaphore_single() {
|
||||||
assert_eq!(semaphore.counter, -1);
|
// Init
|
||||||
assert!(semaphore.waiting_queue.iter().count() == 1);
|
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
|
#[test]
|
||||||
semaphore.v(&mut machine, &mut scheduler);
|
#[ignore]
|
||||||
assert_eq!(semaphore.counter, 0);
|
fn test_semaphore_multiple() {
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
// 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);
|
let mut borrow_tm = tm.borrow_mut();
|
||||||
assert_eq!(semaphore.counter, 1);
|
let scheduler = &mut borrow_tm.g_scheduler;
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
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);
|
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread3)));
|
||||||
assert_eq!(semaphore.counter, 2);
|
semaphore.p(thread3, Rc::clone(&system));
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
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]
|
#[test]
|
||||||
fn test_lock_simple() {
|
#[ignore]
|
||||||
let thread_manager = Rc::new(RefCell::new(ThreadManager::new()));
|
fn test_lock_simple() {
|
||||||
let mut machine = Machine::init_machine();
|
let system = init_system!();
|
||||||
let mut scheduler = Scheduler::new();
|
let sys = system.borrow_mut();
|
||||||
let thread = Rc::new(RefCell::new(Thread::new("test_lock")));
|
let tm = sys.get_thread_manager();
|
||||||
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread)));
|
let thread = Rc::new(RefCell::new(Thread::new("test_lock")));
|
||||||
let mut lock = Lock::new(Rc::clone(&thread_manager));
|
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread)));
|
||||||
|
let mut lock = Lock::new(Rc::clone(&tm));
|
||||||
|
|
||||||
assert!(lock.free);
|
assert!(lock.free);
|
||||||
lock.acquire(Some(Rc::clone(&thread)), &mut machine);
|
lock.acquire(Some(Rc::clone(&thread)), Rc::clone(&system));
|
||||||
assert!(lock.held_by_current_thread(Rc::clone(&thread)));
|
assert!(lock.held_by_current_thread(Rc::clone(&thread)));
|
||||||
|
|
||||||
assert!(!lock.free);
|
assert!(!lock.free);
|
||||||
lock.release(&mut machine, &mut scheduler, Rc::clone(&thread));
|
lock.release(Rc::clone(&system), Rc::clone(&thread));
|
||||||
assert!(!lock.held_by_current_thread(thread));
|
assert!(!lock.held_by_current_thread(thread));
|
||||||
assert!(lock.free);
|
assert!(lock.free);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lock_multiple() {
|
#[ignore]
|
||||||
let thread_manager = Rc::new(RefCell::new(ThreadManager::new()));
|
fn test_lock_multiple() {
|
||||||
let mut machine = Machine::init_machine();
|
let system = init_system!();
|
||||||
let mut scheduler = Scheduler::new();
|
let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1")));
|
||||||
let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1")));
|
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2")));
|
||||||
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2")));
|
let thread3 = Rc::new(RefCell::new(Thread::new("test_lock3")));
|
||||||
let thread3 = Rc::new(RefCell::new(Thread::new("test_lock3")));
|
|
||||||
|
|
||||||
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
|
let tm = system.borrow_mut().get_thread_manager();
|
||||||
let mut lock = Lock::new(Rc::clone(&thread_manager));
|
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
|
||||||
|
let mut lock = Lock::new(Rc::clone(&tm));
|
||||||
|
|
||||||
assert!(lock.free);
|
assert!(lock.free);
|
||||||
lock.acquire(Some(Rc::clone(&thread1)), &mut machine);
|
lock.acquire(Some(Rc::clone(&thread1)), Rc::clone(&system));
|
||||||
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
|
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
|
||||||
assert!(!lock.free);
|
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.acquire(Some(Rc::clone(&thread2)), &mut machine);
|
lock.acquire(Some(Rc::clone(&thread2)), Rc::clone(&system));
|
||||||
thread_manager.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
|
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
|
||||||
|
|
||||||
|
|
||||||
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
|
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
|
||||||
assert!(lock.waiting_queue.iter().count() == 1);
|
assert!(lock.waiting_queue.iter().count() == 1);
|
||||||
assert!(!lock.free);
|
assert!(!lock.free);
|
||||||
|
|
||||||
lock.release(&mut machine, &mut scheduler, Rc::clone(&thread1));
|
lock.release(Rc::clone(&system), Rc::clone(&thread1));
|
||||||
assert!(!lock.held_by_current_thread(thread1));
|
assert!(!lock.held_by_current_thread(thread1));
|
||||||
assert!(lock.held_by_current_thread(Rc::clone(&thread2)));
|
assert!(lock.held_by_current_thread(Rc::clone(&thread2)));
|
||||||
assert!(!lock.free);
|
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));
|
lock.release(Rc::clone(&system), Rc::clone(&thread2));
|
||||||
assert!(!lock.held_by_current_thread(thread2));
|
assert!(!lock.held_by_current_thread(thread2));
|
||||||
assert!(lock.free);
|
assert!(lock.free);
|
||||||
|
}
|
||||||
}
|
}
|
@ -12,7 +12,10 @@ macro_rules! init_system {
|
|||||||
init_system!(m)
|
init_system!(m)
|
||||||
}};
|
}};
|
||||||
($a:expr) => {{
|
($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
|
&self.g_machine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_thread_manager(&self) -> Rc<RefCell<ThreadManager>> {
|
||||||
|
Rc::clone(&self.thread_manager)
|
||||||
|
}
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
/// Assign a machine to the system
|
/// Assign a machine to the system
|
||||||
|
@ -87,7 +87,7 @@ impl Drop for Thread {
|
|||||||
|
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.object_type = ObjectType::InvalidType;
|
self.object_type = ObjectType::InvalidType;
|
||||||
todo!();
|
// todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -94,6 +94,7 @@ impl ThreadManager {
|
|||||||
|
|
||||||
let mut next_thread = self.g_scheduler.find_next_to_run();
|
let mut next_thread = self.g_scheduler.find_next_to_run();
|
||||||
while next_thread.is_none() {
|
while next_thread.is_none() {
|
||||||
|
eprintln!("Nobody to run => idle");
|
||||||
machine.interrupt.idle();
|
machine.interrupt.idle();
|
||||||
next_thread = self.g_scheduler.find_next_to_run();
|
next_thread = self.g_scheduler.find_next_to_run();
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ impl Interrupt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn idle(&self) {
|
pub fn idle(&self) {
|
||||||
todo!();
|
// todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user