From 39e26e61bb085af7ccda42e6dedc25def2af6dc1 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Mon, 13 Mar 2023 20:55:46 +0100 Subject: [PATCH] Add thread_sleep --- src/kernel/thread_manager.rs | 31 +++++++++++++++++++++++-------- src/simulator/interrupt.rs | 6 +++++- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index ac65bf1..1788a0b 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -1,6 +1,6 @@ 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}; @@ -8,8 +8,8 @@ pub const SIMULATORSTACKSIZE: usize = 32 * 1024; #[derive(PartialEq)] pub struct ThreadManager<'a> { - pub g_current_thread: Option, - pub g_thread_to_be_destroyed: Option, + pub g_current_thread: Option>>, + pub g_thread_to_be_destroyed: Option>>, pub g_alive: List>>, pub g_scheduler: Scheduler, pub system: Cell>> @@ -30,6 +30,7 @@ impl<'a> ThreadManager<'a> { /// Start a thread, attaching it to a process pub fn start_thread(&mut self, thread: Rc>, owner: Process, func_pc: i64, argument: i64) -> Result<(), ErrorCode> { let mut thread_m = thread.borrow_mut(); + assert_eq!(thread_m.process, Option::None); thread_m.process = Option::Some(owner); let ptr = 0; // todo addrspace 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 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 = self.g_scheduler(); @@ -68,7 +70,20 @@ impl<'a> ThreadManager<'a> { /// Put the thread to sleep and relinquish the processor pub fn thread_sleep(&mut self, thread: Rc>) { - 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 @@ -104,14 +119,14 @@ impl<'a> ThreadManager<'a> { } /// Currently running thread - pub fn get_g_current_thread(&mut self) -> &mut Option { + pub fn get_g_current_thread(&mut self) -> &mut Option>> { &mut self.g_current_thread } /// Thread to be destroyed by [...] /// /// TODO: Finish the comment with the relevant value - pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option { + pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option>> { &mut self.g_thread_to_be_destroyed } @@ -126,12 +141,12 @@ impl<'a> ThreadManager<'a> { } /// Set currently running thread - pub fn set_g_current_thread(&mut self, thread: Option) { + pub fn set_g_current_thread(&mut self, thread: Option>>) { self.g_current_thread = thread } /// Set thread to be destroyed next - pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option) { + pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option>>) { self.g_thread_to_be_destroyed = thread } diff --git a/src/simulator/interrupt.rs b/src/simulator/interrupt.rs index e63e0af..2dc1c94 100644 --- a/src/simulator/interrupt.rs +++ b/src/simulator/interrupt.rs @@ -29,9 +29,13 @@ impl Interrupt { self.level } + pub fn idle(&self) { + todo!(); + } + } -#[derive(PartialEq, Clone, Copy)] +#[derive(PartialEq, Clone, Copy, Debug)] pub enum InterruptStatus { InterruptOff, InterruptOn