decrease some dependencies

This commit is contained in:
Quentin Legot 2023-03-15 17:57:53 +01:00
parent 6dd0cbcc87
commit fa64d4314d
4 changed files with 38 additions and 39 deletions

View File

@ -3,6 +3,7 @@ use std::rc::Rc;
use crate::utility::list::List; use crate::utility::list::List;
use crate::kernel::thread::Thread; use crate::kernel::thread::Thread;
use crate::simulator::machine::Machine;
use super::system::System; use super::system::System;
use super::thread_manager::ThreadManager; use super::thread_manager::ThreadManager;
@ -53,15 +54,15 @@ impl Scheduler {
/// ## Parameter /// ## Parameter
/// ///
/// **next_thread** thread to dispatch to the CPU /// **next_thread** thread to dispatch to the CPU
pub fn switch_to(&mut self, system: &mut System, next_thread: Rc<RefCell<Thread>>) { pub fn switch_to(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, next_thread: Rc<RefCell<Thread>>) {
let old_thread = system.get_thread_manager().get_g_current_thread().as_ref().unwrap(); let old_thread = thread_manager.get_g_current_thread().as_ref().unwrap();
system.get_thread_manager().thread_save_processor_state(system, Rc::clone(&old_thread)); thread_manager.thread_save_processor_state(machine, Rc::clone(&old_thread));
// old_thread.save_simulator_state(); // old_thread.save_simulator_state();
if old_thread != &next_thread { if old_thread != &next_thread {
system.get_thread_manager().thread_restore_processor_state(system, Rc::clone(&next_thread)); thread_manager.thread_restore_processor_state(machine, Rc::clone(&next_thread));
// next_thread.restore_simulator_state(); // next_thread.restore_simulator_state();
system.get_thread_manager().set_g_current_thread(Option::Some(next_thread)); thread_manager.set_g_current_thread(Some(next_thread));
} }
} }

View File

@ -41,19 +41,19 @@ impl Semaphore {
/// ### Parameters TODO Refaire /// ### Parameters TODO Refaire
/// - *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, system: &mut System) { pub fn p(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
let old_status = system.get_machine().interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
self.counter -= 1; self.counter -= 1;
if self.counter < 0 { if self.counter < 0 {
match system.get_thread_manager().get_g_current_thread() { match thread_manager.get_g_current_thread() {
Some(thread) => { Some(thread) => {
self.waiting_queue.push(Rc::clone(thread)); self.waiting_queue.push(Rc::clone(thread));
system.get_thread_manager().thread_sleep(system, Rc::clone(thread)); thread_manager.thread_sleep(machine, Rc::clone(thread));
}, },
None => unreachable!("Current thread should not be None") None => unreachable!("Current thread should not be None")
} }
} }
system.get_machine().interrupt.set_status(old_status); machine.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.
@ -112,30 +112,31 @@ 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, system: &mut System) { pub fn acquire(&mut self, thread_manager: &mut ThreadManager, machine: &mut Machine) {
let old_status = system.get_machine().interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
if self.free { if self.free {
self.free = false; self.free = false;
self.owner = Option::Some(match system.get_thread_manager().get_g_current_thread() { self.owner = Option::Some(match thread_manager.get_g_current_thread() {
Some(th) => { Some(th) => {
Rc::clone(th) Rc::clone(th)
}, },
None => unreachable!() None => unreachable!()
}); });
} else { } else {
let t = system.get_thread_manager().get_g_current_thread(); let t = thread_manager.get_g_current_thread();
match t { match t {
Some(x) => { Some(x) => {
let x = Rc::clone(x); let x = Rc::clone(x);
self.waiting_queue.push(Rc::clone(&x)); self.waiting_queue.push(Rc::clone(&x));
system.thread_sleep(Rc::clone(&x));
thread_manager.thread_sleep(machine, Rc::clone(&x));
}, },
None => unreachable!("Current thread should not be None") None => unreachable!("Current thread should not be None")
} }
} }
system.get_machine().interrupt.set_status(old_status); machine.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.
@ -266,10 +267,10 @@ mod test {
fn test_semaphore_single() { fn test_semaphore_single() {
// Init // Init
let system = init_system!(); let system = init_system!();
let mut semaphore = Semaphore::new(1, Rc::clone(&system.borrow_mut().get_thread_manager())); let mut semaphore = Semaphore::new(1);
let thread = Rc::new(RefCell::new(Thread::new("test_semaphore"))); let thread = Rc::new(RefCell::new(Thread::new("test_semaphore")));
// P // P
semaphore.p(thread, Rc::clone(&system)); semaphore.p(system, thread);
assert_eq!(semaphore.counter, 0); assert_eq!(semaphore.counter, 0);
assert!(semaphore.waiting_queue.is_empty()); assert!(semaphore.waiting_queue.is_empty());
// V // V

View File

@ -49,7 +49,8 @@ impl System {
/// Sets a thread asleep /// Sets a thread asleep
/// ///
pub fn thread_sleep(&mut self, thread: Rc<RefCell<Thread>>) { pub fn thread_sleep(&mut self, thread: Rc<RefCell<Thread>>) {
self.thread_manager.thread_sleep(self, thread); let machine = self.get_machine();
self.thread_manager.thread_sleep(machine, thread);
} }
// GETTERS // GETTERS

View File

@ -1,6 +1,7 @@
use std::{rc::Rc, cell::{RefCell, RefMut, Ref}}; use std::{rc::Rc, cell::{RefCell, RefMut, Ref}};
use crate::{utility::list::List, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS}, interrupt::InterruptStatus}}; use crate::{utility::list::List, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS}, interrupt::InterruptStatus}};
use crate::simulator::machine::Machine;
use super::{scheduler::Scheduler, thread::Thread, system::System, mgerror::ErrorCode, process::Process}; use super::{scheduler::Scheduler, thread::Thread, system::System, mgerror::ErrorCode, process::Process};
@ -52,33 +53,31 @@ impl ThreadManager {
} }
/// Wait for another thread to finish its execution /// Wait for another thread to finish its execution
pub fn thread_join(&mut self, system: &mut System, id_thread: Rc<RefCell<Thread>>) { pub fn thread_join(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, id_thread: Rc<RefCell<Thread>>) {
while self.get_g_alive().contains(&Rc::clone(&id_thread)) { while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
self.thread_yield(system, Rc::clone(&id_thread)); self.thread_yield(machine, thread_manager, Rc::clone(&id_thread));
} }
} }
/// Relinquish the CPU if any other thread is runnable. /// Relinquish the CPU if any other thread is runnable.
/// ///
/// Cannot use yield as a function name -> reserved name in rust /// Cannot use yield as a function name -> reserved name in rust
pub fn thread_yield(&mut self, system: &mut System, thread: Rc<RefCell<Thread>>) { pub fn thread_yield(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, thread: Rc<RefCell<Thread>>) {
let mut machine = system.get_machine(); let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
let old_status = machine.interrupt.set_status(crate::simulator::interrupt::InterruptStatus::InterruptOff);
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread); assert_eq!(Some(Rc::clone(&thread)), self.g_current_thread);
let next_thread = self.g_scheduler.find_next_to_run(); let next_thread = self.g_scheduler.find_next_to_run();
if let Some(next_thread) = next_thread { if let Some(next_thread) = next_thread {
let scheduler = &mut self.g_scheduler; let scheduler = &mut self.g_scheduler;
scheduler.ready_to_run(thread); scheduler.ready_to_run(thread);
scheduler.switch_to(system, next_thread); scheduler.switch_to(machine, thread_manager, next_thread);
} }
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }
/// Put the thread to sleep and relinquish the processor /// Put the thread to sleep and relinquish the processor
pub fn thread_sleep(&mut self, system: &mut System, thread: Rc<RefCell<Thread>>) { pub fn thread_sleep(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread); assert_eq!(Some(Rc::clone(&thread)), self.g_current_thread);
let machine = system.get_machine();
assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff); assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
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() {
@ -86,36 +85,33 @@ impl ThreadManager {
machine.interrupt.idle(); machine.interrupt.idle();
next_thread = self.g_scheduler.find_next_to_run(); next_thread = self.g_scheduler.find_next_to_run();
} }
self.g_scheduler.switch_to(system, Rc::clone(&next_thread.unwrap())); self.g_scheduler.switch_to(machine, self, Rc::clone(&next_thread.unwrap()));
} }
/// Finish the execution of the thread and prepare its deallocation /// Finish the execution of the thread and prepare its deallocation
pub fn thread_finish(&mut self, system: &mut System, thread: Rc<RefCell<Thread>>) { pub fn thread_finish(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, thread: Rc<RefCell<Thread>>) {
let mut machine = system.get_machine();
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread)); self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread));
self.g_alive.remove(Rc::clone(&thread)); self.g_alive.remove(Rc::clone(&thread));
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard // g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
self.thread_sleep(system, Rc::clone(&thread)); self.thread_sleep(machine, Rc::clone(&thread));
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }
pub fn thread_save_processor_state(&mut self, system: &mut System, thread: Rc<RefCell<Thread>>) { pub fn thread_save_processor_state(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
let mut t: RefMut<_> = thread.borrow_mut(); let mut t: RefMut<_> = thread.borrow_mut();
for i in 0..NUM_INT_REGS { for i in 0..NUM_INT_REGS {
t.thread_context.int_registers[i] = system.get_machine().read_int_register(i); t.thread_context.int_registers[i] = machine.read_int_register(i);
} }
for i in 0..NUM_FP_REGS { for i in 0..NUM_FP_REGS {
t.thread_context.float_registers[i] = system.get_machine().read_fp_register(i); t.thread_context.float_registers[i] = machine.read_fp_register(i);
} }
} }
pub fn thread_restore_processor_state(&self, system: &mut System, thread: Rc<RefCell<Thread>>) { pub fn thread_restore_processor_state(&self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
let t: Ref<_> = thread.borrow(); let t: Ref<_> = thread.borrow();
for i in 0..NUM_INT_REGS { for i in 0..NUM_INT_REGS {
let machine = system.get_machine();
let mut machine = machine;
machine.write_int_register(i, t.thread_context.int_registers[i]); machine.write_int_register(i, t.thread_context.int_registers[i]);
} }
} }