From fe519555cc7d7147f98017389f0886b0183e3df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Wed, 19 Apr 2023 23:33:37 +0200 Subject: [PATCH] Removed thread_to_be_destroyed field and associated methods This used to exist in NachOS' source code as memory management is manual in C++. Here, simply dropping the Thread from the thread list allows the borrow checker to automatically destroy the thread. --- src/kernel/thread_manager.rs | 55 ++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index da4518e..eb69172 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -1,11 +1,31 @@ -use std::{rc::Rc, cell::{RefCell, Ref}, fmt::format}; +use std::{ + rc::Rc, + cell::{ + RefCell, + Ref + } +}; -use crate::{utility::{list::List, objaddr::ObjAddr}, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS, Machine}, interrupt::InterruptStatus, error::{MachineOk, MachineError}}}; +use crate::{ + utility::{ + list::List, + objaddr::ObjAddr + }, + simulator::{ + machine::{ + NUM_INT_REGS, + NUM_FP_REGS, + Machine + }, + interrupt::InterruptStatus, + error::{ + MachineOk, + MachineError + } + } +}; use super::{thread::Thread, process::Process}; - -pub const SIMULATORSTACKSIZE: usize = 32 * 1024; - /// # Thread manager /// /// An instance of this struct is responsible for managing threads on behalf of the system @@ -13,8 +33,6 @@ pub const SIMULATORSTACKSIZE: usize = 32 * 1024; pub struct ThreadManager { /// Current running thread pub g_current_thread: Option>>, - /// The thread to be destroyed next - pub g_thread_to_be_destroyed: Option>>, /// The list of alive threads pub g_alive: List>>, /// Thread in ready state waiting to become active @@ -28,8 +46,7 @@ impl ThreadManager { /// Thread manager constructor pub fn new(debug: bool) -> Self { Self { - g_current_thread: Option::None, - g_thread_to_be_destroyed: Option::None, + g_current_thread: Option::None, g_alive: List::default(), ready_list: List::default(), obj_addrs: ObjAddr::init(), @@ -85,11 +102,6 @@ impl ThreadManager { // next_thread.restore_simulator_state(); self.set_g_current_thread(Some(next_thread)); } - - if let Some(th) = self.get_g_thread_to_be_destroyed() { - drop(th); - self.g_thread_to_be_destroyed = None - } } /// Start a thread, attaching it to a process @@ -147,8 +159,7 @@ impl ThreadManager { /// Finish the execution of the thread and prepare its deallocation pub fn thread_finish(&mut self, machine: &mut Machine, thread: Rc>) { let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); - self.set_g_thread_to_be_destroyed(Option::Some(Rc::clone(&thread))); - self.g_alive.remove(Rc::clone(&thread)); + assert_eq!(self.g_alive.remove(Rc::clone(&thread)), true); self.debug(format!("Sleeping thread {}", thread.borrow().get_name())); // g_objets_addrs->removeObject(self.thread) // a ajouté plus tard self.thread_sleep(machine, Rc::clone(&thread)); @@ -296,13 +307,6 @@ impl ThreadManager { &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) -> &Option>> { - &self.g_thread_to_be_destroyed - } - /// List of alive threads pub fn get_g_alive(&mut self) -> &mut List>> { &mut self.g_alive @@ -313,11 +317,6 @@ impl ThreadManager { self.g_current_thread = thread } - /// Set thread to be destroyed next - pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option>>) { - self.g_thread_to_be_destroyed = thread - } - pub fn get_obj_addrs(&mut self) -> &mut ObjAddr { &mut self.obj_addrs }