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.
This commit is contained in:
François Autin 2023-04-19 23:33:37 +02:00
parent 8b13cc6ef6
commit fe519555cc
No known key found for this signature in database
GPG Key ID: 343F5D382E1DD77C

View File

@ -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}; use super::{thread::Thread, process::Process};
pub const SIMULATORSTACKSIZE: usize = 32 * 1024;
/// # Thread manager /// # Thread manager
/// ///
/// An instance of this struct is responsible for managing threads on behalf of the system /// 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 { pub struct ThreadManager {
/// Current running thread /// Current running thread
pub g_current_thread: Option<Rc<RefCell<Thread>>>, pub g_current_thread: Option<Rc<RefCell<Thread>>>,
/// The thread to be destroyed next
pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>,
/// The list of alive threads /// The list of alive threads
pub g_alive: List<Rc<RefCell<Thread>>>, pub g_alive: List<Rc<RefCell<Thread>>>,
/// Thread in ready state waiting to become active /// Thread in ready state waiting to become active
@ -29,7 +47,6 @@ impl ThreadManager {
pub fn new(debug: bool) -> Self { pub fn new(debug: bool) -> Self {
Self { Self {
g_current_thread: Option::None, g_current_thread: Option::None,
g_thread_to_be_destroyed: Option::None,
g_alive: List::default(), g_alive: List::default(),
ready_list: List::default(), ready_list: List::default(),
obj_addrs: ObjAddr::init(), obj_addrs: ObjAddr::init(),
@ -85,11 +102,6 @@ impl ThreadManager {
// next_thread.restore_simulator_state(); // next_thread.restore_simulator_state();
self.set_g_current_thread(Some(next_thread)); 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 /// Start a thread, attaching it to a process
@ -147,8 +159,7 @@ impl ThreadManager {
/// Finish the execution of the thread and prepare its deallocation /// Finish the execution of the thread and prepare its deallocation
pub fn thread_finish(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) { pub fn thread_finish(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
self.set_g_thread_to_be_destroyed(Option::Some(Rc::clone(&thread))); assert_eq!(self.g_alive.remove(Rc::clone(&thread)), true);
self.g_alive.remove(Rc::clone(&thread));
self.debug(format!("Sleeping thread {}", thread.borrow().get_name())); self.debug(format!("Sleeping thread {}", thread.borrow().get_name()));
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard // g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
self.thread_sleep(machine, Rc::clone(&thread)); self.thread_sleep(machine, Rc::clone(&thread));
@ -296,13 +307,6 @@ impl ThreadManager {
&self.g_current_thread &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<Rc<RefCell<Thread>>> {
&self.g_thread_to_be_destroyed
}
/// List of alive threads /// List of alive threads
pub fn get_g_alive(&mut self) -> &mut List<Rc<RefCell<Thread>>> { pub fn get_g_alive(&mut self) -> &mut List<Rc<RefCell<Thread>>> {
&mut self.g_alive &mut self.g_alive
@ -313,11 +317,6 @@ impl ThreadManager {
self.g_current_thread = thread self.g_current_thread = thread
} }
/// Set thread to be destroyed next
pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option<Rc<RefCell<Thread>>>) {
self.g_thread_to_be_destroyed = thread
}
pub fn get_obj_addrs(&mut self) -> &mut ObjAddr { pub fn get_obj_addrs(&mut self) -> &mut ObjAddr {
&mut self.obj_addrs &mut self.obj_addrs
} }