diff --git a/src/kernel/scheduler.rs b/src/kernel/scheduler.rs index 7071fbb..01ad40f 100644 --- a/src/kernel/scheduler.rs +++ b/src/kernel/scheduler.rs @@ -3,12 +3,12 @@ use std::rc::Rc; use crate::utility::list::List; use crate::kernel::thread::Thread; +use super::system::System; use super::thread_manager::ThreadManager; #[derive(PartialEq)] pub struct Scheduler { ready_list: List>>, - pub thread_manager: Option>> } impl Scheduler { @@ -19,7 +19,6 @@ impl Scheduler { pub fn new() -> Self { Self { ready_list: List::new(), - thread_manager: Option::None } } @@ -54,21 +53,16 @@ impl Scheduler { /// ## Parameter /// /// **next_thread** thread to dispatch to the CPU - pub fn switch_to(&mut self, next_thread: Rc>) { - if let Some(tm) = &self.thread_manager { - let rc = Rc::clone(&tm); - if let Some(old_thread) = tm.borrow_mut().get_g_current_thread() { - rc.borrow_mut().thread_save_processor_state(Rc::clone(&old_thread)); - // old_thread.save_simulator_state(); + pub fn switch_to(&mut self, system: &mut System, next_thread: Rc>) { + let old_thread = system.get_thread_manager().get_g_current_thread().as_ref().unwrap(); + system.get_thread_manager().thread_save_processor_state(system, Rc::clone(&old_thread)); + // old_thread.save_simulator_state(); - if old_thread != &next_thread { - rc.borrow_mut().thread_restore_processor_state(Rc::clone(&next_thread)); - // next_thread.restore_simulator_state(); - rc.borrow_mut().set_g_current_thread(Option::Some(next_thread)); - } - } - } else { - panic!("thread manager shouldn't be none"); + if old_thread != &next_thread { + system.get_thread_manager().thread_restore_processor_state(system, Rc::clone(&next_thread)); + // next_thread.restore_simulator_state(); + system.get_thread_manager().set_g_current_thread(Option::Some(next_thread)); } + } } \ No newline at end of file diff --git a/src/kernel/synch.rs b/src/kernel/synch.rs index f5741f7..89df690 100644 --- a/src/kernel/synch.rs +++ b/src/kernel/synch.rs @@ -17,8 +17,6 @@ pub struct Semaphore { counter:i32, /// QUeue of Semaphore waiting to be exucated waiting_queue:List>>, - /// Thread manager which managing threads - thread_manager: Rc> } @@ -29,8 +27,8 @@ impl Semaphore { /// ### Parameters /// - *counter* initial value of counter /// - *thread_manager* Thread manager which managing threads - pub fn new(counter: i32, thread_manager: Rc>) -> Semaphore{ - Semaphore { counter, waiting_queue: List::new(), thread_manager} + pub fn new(counter: i32) -> Semaphore{ + Semaphore { counter, waiting_queue: List::new()} } /// Decrement the value, and wait if it becomes < 0. Checking the @@ -87,8 +85,6 @@ pub struct Lock{ owner: Option>>, /// The queue of threads waiting for execution waiting_queue:List>>, - /// Thread manager which managing threads - thread_manager: Rc>, /// A boolean definig if the lock is free or not free: bool @@ -101,8 +97,8 @@ impl Lock { /// /// ### Parameters /// - **thread_manager** Thread manager which managing threads - pub fn new(thread_manager: Rc>) -> Lock { - Lock { owner: None, waiting_queue: List::new(), thread_manager, free: true } + pub fn new() -> Lock { + Lock { owner: None, waiting_queue: List::new(), free: true } } /// Wait until the lock become free. Checking the @@ -192,8 +188,6 @@ pub struct Condition{ /// The queue of threads waiting for execution waiting_queue:List>>, - /// Thread manager which managing threads - thread_manager: Rc>, } @@ -203,8 +197,8 @@ impl Condition { /// /// ### Parameters /// - *thread_manager* Thread manager which managing threads - pub fn new(thread_manager: Rc>) -> Condition { - Condition{ waiting_queue: List::new(), thread_manager } + pub fn new() -> Condition { + Condition{ waiting_queue: List::new()} } /// Block the calling thread (put it in the wait queue). @@ -213,13 +207,18 @@ impl Condition { /// ### Parameters /// - **current_thread** the current thread /// - **machine** the machine where threads are executed - pub fn wait(&mut self, current_thread: Rc>, machine: &mut Machine, system: &mut System) { - let old_status = machine.interrupt.set_status(InterruptOff); + pub fn wait(&mut self, system: &mut System) { + let old_status = system.get_machine().interrupt.set_status(InterruptOff); - self.waiting_queue.push(Rc::clone(¤t_thread)); - self.thread_manager.borrow_mut().thread_sleep(system, current_thread); + 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!() + } - machine.interrupt.set_status(old_status); + system.get_machine().interrupt.set_status(old_status); } /// Wake up the first thread of the wait queue (if any). @@ -228,14 +227,14 @@ impl Condition { /// ### Parameters /// - **machine** the machine where the code is executed /// - **scheduler** the scheduler which determine which thread to execute - pub fn signal(&mut self, machine: &mut Machine, scheduler: &mut Scheduler) { - let old_status = machine.interrupt.set_status(InterruptOff); + pub fn signal(&mut self, system: &mut System) { + let old_status = system.get_machine().interrupt.set_status(InterruptOff); if self.waiting_queue.peek() != None { - scheduler.ready_to_run(self.waiting_queue.pop().unwrap()); + system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap()); } - machine.interrupt.set_status(old_status); + system.get_machine().interrupt.set_status(old_status); } @@ -245,13 +244,13 @@ impl Condition { /// ### Parameters /// - **machine** the machine where the code is executed /// - **scheduler** the scheduler which determine which thread to execute - pub fn broadcast(&mut self, machine: &mut Machine, scheduler: &mut Scheduler) { - let old_status = machine.interrupt.set_status(InterruptOff); + pub fn broadcast(&mut self, system: &mut System) { + let old_status = system.get_machine().interrupt.set_status(InterruptOff); while self.waiting_queue.peek() != None { - scheduler.ready_to_run(self.waiting_queue.pop().unwrap()); + system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap()); } - machine.interrupt.set_status(old_status); + system.get_machine().interrupt.set_status(old_status); } diff --git a/src/kernel/system.rs b/src/kernel/system.rs index 7383911..6104dc3 100644 --- a/src/kernel/system.rs +++ b/src/kernel/system.rs @@ -49,7 +49,7 @@ impl System { /// Sets a thread asleep /// pub fn thread_sleep(&mut self, thread: Rc>) { - &self.thread_manager.thread_sleep(self, thread); + self.thread_manager.thread_sleep(self, thread); } // GETTERS diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index 2d5cc06..5820906 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -70,7 +70,7 @@ impl ThreadManager { if let Some(next_thread) = next_thread { let scheduler = &mut self.g_scheduler; scheduler.ready_to_run(thread); - scheduler.switch_to(next_thread); + scheduler.switch_to(system, next_thread); } machine.interrupt.set_status(old_status); } @@ -86,7 +86,7 @@ impl ThreadManager { machine.interrupt.idle(); next_thread = self.g_scheduler.find_next_to_run(); } - self.g_scheduler.switch_to(Rc::clone(&next_thread.unwrap())); + self.g_scheduler.switch_to(system, Rc::clone(&next_thread.unwrap())); } /// Finish the execution of the thread and prepare its deallocation