Changed all reference to thread with an RefCell to enforce mutability

This commit is contained in:
Quentin Legot 2023-03-09 14:00:42 +01:00
parent f586c56a0b
commit e1ba3f6078
6 changed files with 62 additions and 60 deletions

View File

@ -1,31 +1,31 @@
/// Error enum, use it with Result<YourSucessStruct, **ErrorCode**> /// Error enum, use it with Result<YourSucessStruct, **ErrorCode**>
pub enum ErrorCode { pub enum ErrorCode {
INC_ERROR, IncError,
OPENFILE_ERROR, OpenfileError,
EXEC_FILE_FORMAT_ERROR, ExecFileFormatError,
OUT_OF_MEMORY, OutOfMemory,
OUT_OF_DISK, OutOfDisk,
ALREADY_IN_DIRECTORY, AlreadyInDirectory,
INEXIST_FILE_ERROR, InexistFileError,
INEXIST_DIRECTORY_ERROR, InexistDirectoryError,
NOSPACE_IN_DIRECTORY, NospaceInDirectory,
NOT_A_FILE, NotAFile,
NOT_A_DIRECTORY, NotADirectory,
DIRECTORY_NOT_EMPTY, DirectoryNotEmpty,
INVALID_COUNTER, InvalidCounter,
/* Invalid typeId fields: */ /* Invalid typeId fields: */
INVALID_SEMAPHORE_ID, InvalidSemaphoreId,
INVALID_LOCK_ID, InvalidLockId,
INVALID_CONDITION_ID, InvalidConditionId,
INVALID_FILE_ID, InvalidFileId,
INVALID_THREAD_ID, InvalidThreadId,
/* Other messages */ /* Other messages */
WRONG_FILE_ENDIANESS, WrongFileEndianess,
NO_ACIA, NoAcia,
NUMMSGERROR /* Must always be last */ NUMMSGERROR /* Must always be last */
} }

View File

@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use crate::utility::list::List; use crate::utility::list::List;
@ -7,7 +8,7 @@ use super::system::System;
#[derive(PartialEq)] #[derive(PartialEq)]
pub struct Scheduler { pub struct Scheduler {
ready_list: List<Rc<Thread>> ready_list: List<Rc<RefCell<Thread>>>
} }
impl Scheduler { impl Scheduler {
@ -28,7 +29,7 @@ impl Scheduler {
/// ## Pamameter /// ## Pamameter
/// ///
/// **thread** is the thread to be put on the read list /// **thread** is the thread to be put on the read list
pub fn ready_to_run(&mut self, thread: Rc<Thread>) { pub fn ready_to_run(&mut self, thread: Rc<RefCell<Thread>>) {
self.ready_list.push(thread); self.ready_list.push(thread);
} }
@ -38,7 +39,7 @@ impl Scheduler {
/// Thread is removed from the ready list. /// Thread is removed from the ready list.
/// ///
/// **return** Thread thread to be scheduled /// **return** Thread thread to be scheduled
pub fn find_next_to_run(&mut self) -> Option<Rc<Thread>> { pub fn find_next_to_run(&mut self) -> Option<Rc<RefCell<Thread>>> {
self.ready_list.pop() self.ready_list.pop()
} }

View File

@ -2,26 +2,29 @@ use crate::utility::list::List;
use crate::kernel::thread::Thread; use crate::kernel::thread::Thread;
use crate::simulator::interrupt::InterruptStatus::InterruptOff; use crate::simulator::interrupt::InterruptStatus::InterruptOff;
use crate::simulator::machine::Machine; use crate::simulator::machine::Machine;
use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use super::scheduler::Scheduler; use super::scheduler::Scheduler;
use super::thread_manager::ThreadManager;
pub struct Semaphore{ pub struct Semaphore<'t> {
counter:i32, counter:i32,
waiting_queue:List<Rc<Thread>> waiting_queue:List<Rc<RefCell<Thread>>>,
thread_manager: Rc<RefCell<ThreadManager<'t>>> // On s'assure que le tm vit plus longtemps que les semaphore avec le lifetime
} }
impl Semaphore{ impl<'t> Semaphore<'_> {
pub fn p(&mut self, current_thread: Rc<Thread>, machine: &mut Machine){ pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine){
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
self.counter -= 1; self.counter -= 1;
if self.counter < 0 { if self.counter < 0 {
self.waiting_queue.push(Rc::clone(&current_thread)); self.waiting_queue.push(Rc::clone(&current_thread));
current_thread.sleep(); self.thread_manager.borrow_mut().thread_sleep(current_thread);
} }
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }
@ -36,16 +39,17 @@ impl Semaphore{
} }
} }
pub struct Lock{ pub struct Lock<'t>{
owner: Rc<Thread>, owner: Rc<RefCell<Thread>>,
waiting_queue:List<Rc<Thread>>, waiting_queue:List<Rc<RefCell<Thread>>>,
thread_manager: Rc<RefCell<ThreadManager<'t>>>,
free: bool free: bool
} }
impl Lock { impl<'t> Lock<'_> {
pub fn acquire(&mut self, machine: &mut Machine, current_thread: Rc<Thread>) { pub fn acquire(&mut self, machine: &mut Machine, current_thread: Rc<RefCell<Thread>>) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
if self.free { if self.free {
@ -53,13 +57,13 @@ impl Lock {
self.owner = current_thread; self.owner = current_thread;
} else { } else {
self.waiting_queue.push(Rc::clone(&current_thread)); self.waiting_queue.push(Rc::clone(&current_thread));
current_thread.sleep(); self.thread_manager.borrow_mut().thread_sleep(current_thread);
} }
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }
pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<Thread>) { pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<RefCell<Thread>>) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
if self.is_held_by_current_thread(current_thread) { if self.is_held_by_current_thread(current_thread) {
@ -74,24 +78,25 @@ impl Lock {
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }
pub fn is_held_by_current_thread(&mut self, current_thread: Rc<Thread>) -> bool { pub fn is_held_by_current_thread(&mut self, current_thread: Rc<RefCell<Thread>>) -> bool {
Rc::ptr_eq(&self.owner, &current_thread) Rc::ptr_eq(&self.owner, &current_thread)
} }
} }
pub struct Condition{ pub struct Condition<'t>{
waiting_queue:List<Rc<Thread>> waiting_queue:List<Rc<RefCell<Thread>>>,
thread_manager: Rc<RefCell<ThreadManager<'t>>>,
} }
impl Condition { impl<'t> Condition<'_> {
pub fn wait(&mut self, machine: &mut Machine, current_thread: Rc<Thread>) { pub fn wait(&mut self, machine: &mut Machine, current_thread: Rc<RefCell<Thread>>) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
self.waiting_queue.push(Rc::clone(&current_thread)); self.waiting_queue.push(Rc::clone(&current_thread));
current_thread.sleep(); self.thread_manager.borrow_mut().thread_sleep(current_thread);
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }

View File

@ -69,10 +69,6 @@ impl Thread {
// } // }
} }
pub fn sleep(&self) {
unreachable!("Has been moved to thread manager");
}
pub fn save_simulator_state(&self) { pub fn save_simulator_state(&self) {
todo!(); todo!();
} }

View File

@ -10,7 +10,7 @@ pub const SIMULATORSTACKSIZE: usize = 32 * 1024;
pub struct ThreadManager<'a> { pub struct ThreadManager<'a> {
pub g_current_thread: Option<Thread>, pub g_current_thread: Option<Thread>,
pub g_thread_to_be_destroyed: Option<Thread>, pub g_thread_to_be_destroyed: Option<Thread>,
pub g_alive: List<Rc<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>>>
} }
@ -28,21 +28,21 @@ 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, mut thread: 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> {
thread.process = Option::Some(owner); let mut thread_m = thread.borrow_mut();
thread_m.process = Option::Some(owner);
let ptr = 0; // todo addrspace let ptr = 0; // todo addrspace
thread.init_thread_context(func_pc, ptr, argument); thread_m.init_thread_context(func_pc, ptr, argument);
let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray
thread.init_simulator_context(base_stack_addr); thread_m.init_simulator_context(base_stack_addr);
thread.process.as_mut().unwrap().num_thread += 1; thread_m.process.as_mut().unwrap().num_thread += 1;
let thread_m = Rc::new(thread); self.get_g_alive().push(Rc::clone(&thread));
self.get_g_alive().push(Rc::clone(&thread_m)); self.g_scheduler().ready_to_run(Rc::clone(&thread));
self.g_scheduler().ready_to_run(Rc::clone(&thread_m));
Result::Ok(()) Result::Ok(())
} }
/// Wait for another thread to finish its execution /// Wait for another thread to finish its execution
pub fn thread_join(&mut self, id_thread: Rc<Thread>) { pub fn thread_join(&mut self, id_thread: Rc<RefCell<Thread>>) {
while self.get_g_alive().contains(&Rc::clone(&id_thread)) { while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
self.thread_yield(Rc::clone(&id_thread)); self.thread_yield(Rc::clone(&id_thread));
} }
@ -51,17 +51,17 @@ impl<'a> ThreadManager<'a> {
/// Relinquish the CPU if any other thread is runnable. /// Relinquish the CPU if any other thread is runnable.
/// ///
/// Cannot use yield as a function name -> reserved name in rust /// Cannot use yield as a function name -> reserved name in rust
pub fn thread_yield(&mut self, thread: Rc<Thread>) { pub fn thread_yield(&mut self, thread: Rc<RefCell<Thread>>) {
todo!(); todo!();
} }
/// 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<Thread>) { pub fn thread_sleep(&mut self, thread: Rc<RefCell<Thread>>) {
todo!(); todo!();
} }
/// Finish the execution of the thread and prepare its deallocation /// Finish the execution of the thread and prepare its deallocation
pub fn thread_finish(&self, thread: Rc<Thread>) { pub fn thread_finish(&self, thread: Rc<RefCell<Thread>>) {
todo!(); todo!();
} }
@ -105,7 +105,7 @@ impl<'a> ThreadManager<'a> {
} }
/// List of alive threads /// List of alive threads
pub fn get_g_alive(&mut self) -> &mut List<Rc<Thread>> { pub fn get_g_alive(&mut self) -> &mut List<Rc<RefCell<Thread>>> {
&mut self.g_alive &mut self.g_alive
} }

View File

@ -59,7 +59,7 @@ impl UContextT {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
stackBottom: Vec::default() stack_bottom: Vec::default()
} }
} }