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:
parent
8b13cc6ef6
commit
fe519555cc
@ -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<Rc<RefCell<Thread>>>,
|
||||
/// The thread to be destroyed next
|
||||
pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>,
|
||||
/// The list of alive threads
|
||||
pub g_alive: List<Rc<RefCell<Thread>>>,
|
||||
/// 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<RefCell<Thread>>) {
|
||||
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<Rc<RefCell<Thread>>> {
|
||||
&self.g_thread_to_be_destroyed
|
||||
}
|
||||
|
||||
/// List of alive threads
|
||||
pub fn get_g_alive(&mut self) -> &mut List<Rc<RefCell<Thread>>> {
|
||||
&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<Rc<RefCell<Thread>>>) {
|
||||
self.g_thread_to_be_destroyed = thread
|
||||
}
|
||||
|
||||
pub fn get_obj_addrs(&mut self) -> &mut ObjAddr {
|
||||
&mut self.obj_addrs
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user