Add thread_sleep

This commit is contained in:
Quentin Legot 2023-03-13 20:55:46 +01:00
parent c0765270d7
commit 39e26e61bb
2 changed files with 28 additions and 9 deletions

View File

@ -1,6 +1,6 @@
use std::{rc::Rc, cell::{Cell, RefCell, RefMut, Ref}}; use std::{rc::Rc, cell::{Cell, RefCell, RefMut, Ref}};
use crate::{utility::list::List, simulator::machine::{NUM_INT_REGS, NUM_FP_REGS}}; use crate::{utility::list::List, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS}, interrupt::InterruptStatus}};
use super::{scheduler::Scheduler, thread::Thread, system::System, mgerror::ErrorCode, process::Process}; use super::{scheduler::Scheduler, thread::Thread, system::System, mgerror::ErrorCode, process::Process};
@ -8,8 +8,8 @@ pub const SIMULATORSTACKSIZE: usize = 32 * 1024;
#[derive(PartialEq)] #[derive(PartialEq)]
pub struct ThreadManager<'a> { pub struct ThreadManager<'a> {
pub g_current_thread: Option<Thread>, pub g_current_thread: Option<Rc<RefCell<Thread>>>,
pub g_thread_to_be_destroyed: Option<Thread>, pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>,
pub g_alive: List<Rc<RefCell<Thread>>>, pub g_alive: List<Rc<RefCell<Thread>>>,
pub g_scheduler: Scheduler, pub g_scheduler: Scheduler,
pub system: Cell<Option<&'a System<'a>>> pub system: Cell<Option<&'a System<'a>>>
@ -30,6 +30,7 @@ impl<'a> ThreadManager<'a> {
/// Start a thread, attaching it to a process /// 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> { 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(); let mut thread_m = thread.borrow_mut();
assert_eq!(thread_m.process, Option::None);
thread_m.process = Option::Some(owner); thread_m.process = Option::Some(owner);
let ptr = 0; // todo addrspace let ptr = 0; // todo addrspace
thread_m.init_thread_context(func_pc, ptr, argument); thread_m.init_thread_context(func_pc, ptr, argument);
@ -56,6 +57,7 @@ impl<'a> ThreadManager<'a> {
let mut machine = system.get_g_machine().borrow_mut(); let mut machine = system.get_g_machine().borrow_mut();
let old_status = machine.interrupt.set_status(crate::simulator::interrupt::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);
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 = self.g_scheduler(); let scheduler = self.g_scheduler();
@ -68,7 +70,20 @@ impl<'a> ThreadManager<'a> {
/// Put the thread to sleep and relinquish the processor /// 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, thread: Rc<RefCell<Thread>>) {
todo!();
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
if let Some(system) = self.system.get() {
let mut machine = system.get_g_machine().borrow_mut();
assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
let mut next_thread = self.g_scheduler().find_next_to_run();
while next_thread.is_none() {
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 /// Finish the execution of the thread and prepare its deallocation
@ -104,14 +119,14 @@ impl<'a> ThreadManager<'a> {
} }
/// Currently running thread /// Currently running thread
pub fn get_g_current_thread(&mut self) -> &mut Option<Thread> { pub fn get_g_current_thread(&mut self) -> &mut Option<Rc<RefCell<Thread>>> {
&mut self.g_current_thread &mut self.g_current_thread
} }
/// Thread to be destroyed by [...] /// Thread to be destroyed by [...]
/// ///
/// TODO: Finish the comment with the relevant value /// TODO: Finish the comment with the relevant value
pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option<Thread> { pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option<Rc<RefCell<Thread>>> {
&mut self.g_thread_to_be_destroyed &mut self.g_thread_to_be_destroyed
} }
@ -126,12 +141,12 @@ impl<'a> ThreadManager<'a> {
} }
/// Set currently running thread /// Set currently running thread
pub fn set_g_current_thread(&mut self, thread: Option<Thread>) { pub fn set_g_current_thread(&mut self, thread: Option<Rc<RefCell<Thread>>>) {
self.g_current_thread = thread self.g_current_thread = thread
} }
/// Set thread to be destroyed next /// Set thread to be destroyed next
pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option<Thread>) { pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option<Rc<RefCell<Thread>>>) {
self.g_thread_to_be_destroyed = thread self.g_thread_to_be_destroyed = thread
} }

View File

@ -29,9 +29,13 @@ impl Interrupt {
self.level self.level
} }
pub fn idle(&self) {
todo!();
}
} }
#[derive(PartialEq, Clone, Copy)] #[derive(PartialEq, Clone, Copy, Debug)]
pub enum InterruptStatus { pub enum InterruptStatus {
InterruptOff, InterruptOff,
InterruptOn InterruptOn