2023-03-13 21:47:06 +01:00
|
|
|
use std::{rc::Rc, cell::{RefCell, RefMut, Ref}};
|
2023-03-08 21:10:51 +01:00
|
|
|
|
2023-03-13 20:55:46 +01:00
|
|
|
use crate::{utility::list::List, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS}, interrupt::InterruptStatus}};
|
2023-03-15 17:57:53 +01:00
|
|
|
use crate::simulator::machine::Machine;
|
2023-03-08 21:10:51 +01:00
|
|
|
|
|
|
|
use super::{scheduler::Scheduler, thread::Thread, system::System, mgerror::ErrorCode, process::Process};
|
|
|
|
|
2023-03-09 12:08:33 +01:00
|
|
|
pub const SIMULATORSTACKSIZE: usize = 32 * 1024;
|
2023-03-08 21:10:51 +01:00
|
|
|
|
2023-03-14 15:16:40 +01:00
|
|
|
/// # Thread manager
|
|
|
|
///
|
|
|
|
/// An instance of this struct is responsible for managing threads on behalf of the system
|
2023-03-08 21:10:51 +01:00
|
|
|
#[derive(PartialEq)]
|
2023-03-13 21:47:06 +01:00
|
|
|
pub struct ThreadManager {
|
2023-03-14 15:16:40 +01:00
|
|
|
/// Current running thread
|
2023-03-13 20:55:46 +01:00
|
|
|
pub g_current_thread: Option<Rc<RefCell<Thread>>>,
|
2023-03-14 15:16:40 +01:00
|
|
|
/// The thread to be destroyed next
|
2023-03-13 20:55:46 +01:00
|
|
|
pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>,
|
2023-03-14 15:16:40 +01:00
|
|
|
/// The list of alive threads
|
2023-03-09 14:00:42 +01:00
|
|
|
pub g_alive: List<Rc<RefCell<Thread>>>,
|
2023-03-14 15:16:40 +01:00
|
|
|
/// The thread scheduler
|
2023-03-08 21:10:51 +01:00
|
|
|
pub g_scheduler: Scheduler,
|
2023-03-14 15:16:40 +01:00
|
|
|
/// The system owning the thread manager
|
2023-03-13 21:47:06 +01:00
|
|
|
pub system: Option<Rc<RefCell<System>>>
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 21:47:06 +01:00
|
|
|
impl ThreadManager {
|
2023-03-08 21:10:51 +01:00
|
|
|
|
2023-03-14 15:16:40 +01:00
|
|
|
/// Thread manager constructor
|
2023-03-08 21:10:51 +01:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
g_current_thread: Option::None,
|
|
|
|
g_thread_to_be_destroyed: Option::None,
|
|
|
|
g_alive: List::new(),
|
|
|
|
g_scheduler: Scheduler::new(),
|
2023-03-13 21:47:06 +01:00
|
|
|
system: Option::None
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start a thread, attaching it to a process
|
2023-03-09 14:00:42 +01:00
|
|
|
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();
|
2023-03-13 20:55:46 +01:00
|
|
|
assert_eq!(thread_m.process, Option::None);
|
2023-03-09 14:00:42 +01:00
|
|
|
thread_m.process = Option::Some(owner);
|
2023-03-08 21:10:51 +01:00
|
|
|
let ptr = 0; // todo addrspace
|
2023-03-09 14:00:42 +01:00
|
|
|
thread_m.init_thread_context(func_pc, ptr, argument);
|
2023-03-08 21:10:51 +01:00
|
|
|
let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray
|
2023-03-09 14:00:42 +01:00
|
|
|
thread_m.init_simulator_context(base_stack_addr);
|
|
|
|
thread_m.process.as_mut().unwrap().num_thread += 1;
|
|
|
|
self.get_g_alive().push(Rc::clone(&thread));
|
2023-03-13 21:47:06 +01:00
|
|
|
self.g_scheduler.ready_to_run(Rc::clone(&thread));
|
2023-03-08 21:10:51 +01:00
|
|
|
Result::Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for another thread to finish its execution
|
2023-03-15 17:57:53 +01:00
|
|
|
pub fn thread_join(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, id_thread: Rc<RefCell<Thread>>) {
|
2023-03-08 21:10:51 +01:00
|
|
|
while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
|
2023-03-15 17:57:53 +01:00
|
|
|
self.thread_yield(machine, thread_manager, Rc::clone(&id_thread));
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Relinquish the CPU if any other thread is runnable.
|
|
|
|
///
|
|
|
|
/// Cannot use yield as a function name -> reserved name in rust
|
2023-03-15 17:57:53 +01:00
|
|
|
pub fn thread_yield(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, thread: Rc<RefCell<Thread>>) {
|
|
|
|
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
2023-03-11 14:48:56 +01:00
|
|
|
|
2023-03-15 17:57:53 +01:00
|
|
|
assert_eq!(Some(Rc::clone(&thread)), self.g_current_thread);
|
2023-03-15 15:20:20 +01:00
|
|
|
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);
|
2023-03-15 17:57:53 +01:00
|
|
|
scheduler.switch_to(machine, thread_manager, next_thread);
|
2023-03-11 14:48:56 +01:00
|
|
|
}
|
2023-03-15 15:20:20 +01:00
|
|
|
machine.interrupt.set_status(old_status);
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
|
2023-03-09 12:08:33 +01:00
|
|
|
/// Put the thread to sleep and relinquish the processor
|
2023-03-15 17:57:53 +01:00
|
|
|
pub fn thread_sleep(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
|
|
|
assert_eq!(Some(Rc::clone(&thread)), self.g_current_thread);
|
2023-03-15 15:20:20 +01:00
|
|
|
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();
|
2023-03-13 20:55:46 +01:00
|
|
|
}
|
2023-03-15 17:57:53 +01:00
|
|
|
self.g_scheduler.switch_to(machine, self, Rc::clone(&next_thread.unwrap()));
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finish the execution of the thread and prepare its deallocation
|
2023-03-15 17:57:53 +01:00
|
|
|
pub fn thread_finish(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager, thread: Rc<RefCell<Thread>>) {
|
2023-03-15 15:20:20 +01:00
|
|
|
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
|
2023-03-15 17:57:53 +01:00
|
|
|
self.thread_sleep(machine, Rc::clone(&thread));
|
2023-03-15 15:20:20 +01:00
|
|
|
machine.interrupt.set_status(old_status);
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 17:57:53 +01:00
|
|
|
pub fn thread_save_processor_state(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
2023-03-15 15:20:20 +01:00
|
|
|
let mut t: RefMut<_> = thread.borrow_mut();
|
|
|
|
for i in 0..NUM_INT_REGS {
|
2023-03-15 17:57:53 +01:00
|
|
|
t.thread_context.int_registers[i] = machine.read_int_register(i);
|
2023-03-15 15:20:20 +01:00
|
|
|
}
|
|
|
|
for i in 0..NUM_FP_REGS {
|
2023-03-15 17:57:53 +01:00
|
|
|
t.thread_context.float_registers[i] = machine.read_fp_register(i);
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 17:57:53 +01:00
|
|
|
pub fn thread_restore_processor_state(&self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
2023-03-15 15:20:20 +01:00
|
|
|
|
|
|
|
let t: Ref<_> = thread.borrow();
|
|
|
|
for i in 0..NUM_INT_REGS {
|
|
|
|
machine.write_int_register(i, t.thread_context.int_registers[i]);
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 21:10:51 +01:00
|
|
|
/// Currently running thread
|
2023-03-13 20:55:46 +01:00
|
|
|
pub fn get_g_current_thread(&mut self) -> &mut Option<Rc<RefCell<Thread>>> {
|
2023-03-08 21:10:51 +01:00
|
|
|
&mut self.g_current_thread
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Thread to be destroyed by [...]
|
|
|
|
///
|
|
|
|
/// TODO: Finish the comment with the relevant value
|
2023-03-13 20:55:46 +01:00
|
|
|
pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option<Rc<RefCell<Thread>>> {
|
2023-03-08 21:10:51 +01:00
|
|
|
&mut self.g_thread_to_be_destroyed
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List of alive threads
|
2023-03-09 14:00:42 +01:00
|
|
|
pub fn get_g_alive(&mut self) -> &mut List<Rc<RefCell<Thread>>> {
|
2023-03-08 21:10:51 +01:00
|
|
|
&mut self.g_alive
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set currently running thread
|
2023-03-13 20:55:46 +01:00
|
|
|
pub fn set_g_current_thread(&mut self, thread: Option<Rc<RefCell<Thread>>>) {
|
2023-03-08 21:10:51 +01:00
|
|
|
self.g_current_thread = thread
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set thread to be destroyed next
|
2023-03-13 20:55:46 +01:00
|
|
|
pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option<Rc<RefCell<Thread>>>) {
|
2023-03-08 21:10:51 +01:00
|
|
|
self.g_thread_to_be_destroyed = thread
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|