Moved to reference passing system

This commit is contained in:
François Autin
2023-03-15 15:20:20 +01:00
parent abb97d17d5
commit 1906ec836c
4 changed files with 64 additions and 109 deletions

View File

@ -36,11 +36,6 @@ impl ThreadManager {
}
}
pub fn freeze(this: &Rc<RefCell<ThreadManager>>) {
let copy = Rc::clone(this);
this.borrow_mut().g_scheduler.thread_manager = Option::Some(copy);
}
/// Start a thread, attaching it to a process
pub fn start_thread(&mut self, thread: Rc<RefCell<Thread>>, owner: Process, func_pc: i64, argument: i64) -> Result<(), ErrorCode> {
let mut thread_m = thread.borrow_mut();
@ -57,93 +52,71 @@ impl ThreadManager {
}
/// Wait for another thread to finish its execution
pub fn thread_join(&mut self, id_thread: Rc<RefCell<Thread>>) {
pub fn thread_join(&mut self, system: &mut System, id_thread: Rc<RefCell<Thread>>) {
while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
self.thread_yield(Rc::clone(&id_thread));
self.thread_yield(system, Rc::clone(&id_thread));
}
}
/// Relinquish the CPU if any other thread is runnable.
///
/// Cannot use yield as a function name -> reserved name in rust
pub fn thread_yield(&mut self, thread: Rc<RefCell<Thread>>) {
if let Some(system) = &self.system {
let sys = system.borrow_mut();
let mut machine = sys.get_machine();
let old_status = machine.interrupt.set_status(crate::simulator::interrupt::InterruptStatus::InterruptOff);
pub fn thread_yield(&mut self, system: &mut System, thread: Rc<RefCell<Thread>>) {
let mut machine = system.get_machine();
let old_status = machine.interrupt.set_status(crate::simulator::interrupt::InterruptStatus::InterruptOff);
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
let next_thread = self.g_scheduler.find_next_to_run();
if let Some(next_thread) = next_thread {
let scheduler = &mut self.g_scheduler;
scheduler.ready_to_run(thread);
scheduler.switch_to(next_thread);
}
machine.interrupt.set_status(old_status);
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
let next_thread = self.g_scheduler.find_next_to_run();
if let Some(next_thread) = next_thread {
let scheduler = &mut self.g_scheduler;
scheduler.ready_to_run(thread);
scheduler.switch_to(system, next_thread);
}
machine.interrupt.set_status(old_status);
}
/// Put the thread to sleep and relinquish the processor
pub fn thread_sleep(&mut self, thread: Rc<RefCell<Thread>>) {
pub fn thread_sleep(&mut self, system: &mut System, thread: Rc<RefCell<Thread>>) {
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
if let Some(system) = &self.system {
let sys = system.borrow_mut();
let machine = sys.get_machine();
assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
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();
}
self.g_scheduler.switch_to(Rc::clone(&next_thread.unwrap()));
let machine = system.get_machine();
assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
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();
}
self.g_scheduler.switch_to(system, Rc::clone(&next_thread.unwrap()));
}
/// Finish the execution of the thread and prepare its deallocation
pub fn thread_finish(&mut self, thread: Rc<RefCell<Thread>>) {
if let Some(system) = &self.system {
let sys = Rc::clone(system);
let sys = sys.borrow_mut();
let mut machine = sys.get_machine();
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread));
self.g_alive.remove(Rc::clone(&thread));
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
self.thread_sleep(Rc::clone(&thread));
machine.interrupt.set_status(old_status);
pub fn thread_finish(&mut self, system: &mut System, thread: Rc<RefCell<Thread>>) {
let mut machine = system.get_machine();
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread));
self.g_alive.remove(Rc::clone(&thread));
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
self.thread_sleep(system, Rc::clone(&thread));
machine.interrupt.set_status(old_status);
}
pub fn thread_save_processor_state(&mut self, system: &mut System, thread: Rc<RefCell<Thread>>) {
let mut t: RefMut<_> = thread.borrow_mut();
for i in 0..NUM_INT_REGS {
t.thread_context.int_registers[i] = system.get_machine().read_int_register(i);
}
for i in 0..NUM_FP_REGS {
t.thread_context.float_registers[i] = system.get_machine().read_fp_register(i);
}
}
pub fn thread_save_processor_state(&mut self, thread: Rc<RefCell<Thread>>) {
if let Some(system) = &self.system {
let mut t: RefMut<_> = thread.borrow_mut();
let system = system;
for i in 0..NUM_INT_REGS {
t.thread_context.int_registers[i] = system.get_machine().read_int_register(i);
}
for i in 0..NUM_FP_REGS {
t.thread_context.float_registers[i] = system.get_machine().read_fp_register(i);
}
} else {
unreachable!("System is None")
}
}
pub fn thread_restore_processor_state(&self, system: &mut System, thread: Rc<RefCell<Thread>>) {
pub fn thread_restore_processor_state(&self, thread: Rc<RefCell<Thread>>) {
if let Some(system) = &self.system {
let system = system.borrow_mut();
let t: Ref<_> = thread.borrow();
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]);
}
} else {
unreachable!("System is None")
let t: Ref<_> = thread.borrow();
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]);
}
}