From e1ba3f6078dbf3887a2d7da46830eb73c8ac80a5 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 9 Mar 2023 14:00:42 +0100 Subject: [PATCH] Changed all reference to thread with an RefCell to enforce mutability --- src/kernel/mgerror.rs | 40 +++++++++++++++++------------------ src/kernel/scheduler.rs | 7 +++--- src/kernel/synch.rs | 41 ++++++++++++++++++++---------------- src/kernel/thread.rs | 4 ---- src/kernel/thread_manager.rs | 28 ++++++++++++------------ src/kernel/ucontext.rs | 2 +- 6 files changed, 62 insertions(+), 60 deletions(-) diff --git a/src/kernel/mgerror.rs b/src/kernel/mgerror.rs index 453a9bd..54e0e58 100644 --- a/src/kernel/mgerror.rs +++ b/src/kernel/mgerror.rs @@ -1,31 +1,31 @@ /// Error enum, use it with Result pub enum ErrorCode { - INC_ERROR, - OPENFILE_ERROR, - EXEC_FILE_FORMAT_ERROR, - OUT_OF_MEMORY, + IncError, + OpenfileError, + ExecFileFormatError, + OutOfMemory, - OUT_OF_DISK, - ALREADY_IN_DIRECTORY, - INEXIST_FILE_ERROR, - INEXIST_DIRECTORY_ERROR, - NOSPACE_IN_DIRECTORY, - NOT_A_FILE, - NOT_A_DIRECTORY, - DIRECTORY_NOT_EMPTY, - INVALID_COUNTER, + OutOfDisk, + AlreadyInDirectory, + InexistFileError, + InexistDirectoryError, + NospaceInDirectory, + NotAFile, + NotADirectory, + DirectoryNotEmpty, + InvalidCounter, /* Invalid typeId fields: */ - INVALID_SEMAPHORE_ID, - INVALID_LOCK_ID, - INVALID_CONDITION_ID, - INVALID_FILE_ID, - INVALID_THREAD_ID, + InvalidSemaphoreId, + InvalidLockId, + InvalidConditionId, + InvalidFileId, + InvalidThreadId, /* Other messages */ - WRONG_FILE_ENDIANESS, - NO_ACIA, + WrongFileEndianess, + NoAcia, NUMMSGERROR /* Must always be last */ } \ No newline at end of file diff --git a/src/kernel/scheduler.rs b/src/kernel/scheduler.rs index 0398799..c0c6cb3 100644 --- a/src/kernel/scheduler.rs +++ b/src/kernel/scheduler.rs @@ -1,3 +1,4 @@ +use std::cell::RefCell; use std::rc::Rc; use crate::utility::list::List; @@ -7,7 +8,7 @@ use super::system::System; #[derive(PartialEq)] pub struct Scheduler { - ready_list: List> + ready_list: List>> } impl Scheduler { @@ -28,7 +29,7 @@ impl Scheduler { /// ## Pamameter /// /// **thread** is the thread to be put on the read list - pub fn ready_to_run(&mut self, thread: Rc) { + pub fn ready_to_run(&mut self, thread: Rc>) { self.ready_list.push(thread); } @@ -38,7 +39,7 @@ impl Scheduler { /// Thread is removed from the ready list. /// /// **return** Thread thread to be scheduled - pub fn find_next_to_run(&mut self) -> Option> { + pub fn find_next_to_run(&mut self) -> Option>> { self.ready_list.pop() } diff --git a/src/kernel/synch.rs b/src/kernel/synch.rs index 98a1f76..77fc56c 100644 --- a/src/kernel/synch.rs +++ b/src/kernel/synch.rs @@ -2,26 +2,29 @@ use crate::utility::list::List; use crate::kernel::thread::Thread; use crate::simulator::interrupt::InterruptStatus::InterruptOff; use crate::simulator::machine::Machine; +use std::cell::RefCell; use std::rc::Rc; use super::scheduler::Scheduler; +use super::thread_manager::ThreadManager; -pub struct Semaphore{ +pub struct Semaphore<'t> { counter:i32, - waiting_queue:List> + waiting_queue:List>>, + thread_manager: Rc>> // 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, machine: &mut Machine){ + pub fn p(&mut self, current_thread: Rc>, machine: &mut Machine){ let old_status = machine.interrupt.set_status(InterruptOff); self.counter -= 1; if self.counter < 0 { self.waiting_queue.push(Rc::clone(¤t_thread)); - current_thread.sleep(); + self.thread_manager.borrow_mut().thread_sleep(current_thread); } machine.interrupt.set_status(old_status); } @@ -36,16 +39,17 @@ impl Semaphore{ } } -pub struct Lock{ +pub struct Lock<'t>{ - owner: Rc, - waiting_queue:List>, + owner: Rc>, + waiting_queue:List>>, + thread_manager: Rc>>, free: bool } -impl Lock { - pub fn acquire(&mut self, machine: &mut Machine, current_thread: Rc) { +impl<'t> Lock<'_> { + pub fn acquire(&mut self, machine: &mut Machine, current_thread: Rc>) { let old_status = machine.interrupt.set_status(InterruptOff); if self.free { @@ -53,13 +57,13 @@ impl Lock { self.owner = current_thread; } else { self.waiting_queue.push(Rc::clone(¤t_thread)); - current_thread.sleep(); + self.thread_manager.borrow_mut().thread_sleep(current_thread); } machine.interrupt.set_status(old_status); } - pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc) { + pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc>) { let old_status = machine.interrupt.set_status(InterruptOff); if self.is_held_by_current_thread(current_thread) { @@ -74,24 +78,25 @@ impl Lock { machine.interrupt.set_status(old_status); } - pub fn is_held_by_current_thread(&mut self, current_thread: Rc) -> bool { + pub fn is_held_by_current_thread(&mut self, current_thread: Rc>) -> bool { Rc::ptr_eq(&self.owner, ¤t_thread) } } -pub struct Condition{ +pub struct Condition<'t>{ - waiting_queue:List> + waiting_queue:List>>, + thread_manager: Rc>>, } -impl Condition { +impl<'t> Condition<'_> { - pub fn wait(&mut self, machine: &mut Machine, current_thread: Rc) { + pub fn wait(&mut self, machine: &mut Machine, current_thread: Rc>) { let old_status = machine.interrupt.set_status(InterruptOff); self.waiting_queue.push(Rc::clone(¤t_thread)); - current_thread.sleep(); + self.thread_manager.borrow_mut().thread_sleep(current_thread); machine.interrupt.set_status(old_status); } diff --git a/src/kernel/thread.rs b/src/kernel/thread.rs index 298df68..7e185a0 100644 --- a/src/kernel/thread.rs +++ b/src/kernel/thread.rs @@ -69,10 +69,6 @@ impl Thread { // } } - pub fn sleep(&self) { - unreachable!("Has been moved to thread manager"); - } - pub fn save_simulator_state(&self) { todo!(); } diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index 39a7f81..2d32e55 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -10,7 +10,7 @@ pub const SIMULATORSTACKSIZE: usize = 32 * 1024; pub struct ThreadManager<'a> { pub g_current_thread: Option, pub g_thread_to_be_destroyed: Option, - pub g_alive: List>, + pub g_alive: List>>, pub g_scheduler: Scheduler, pub system: Cell>> } @@ -28,21 +28,21 @@ impl<'a> ThreadManager<'a> { } /// 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> { - thread.process = Option::Some(owner); + pub fn start_thread(&mut self, thread: Rc>, owner: Process, func_pc: i64, argument: i64) -> Result<(), ErrorCode> { + let mut thread_m = thread.borrow_mut(); + thread_m.process = Option::Some(owner); 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 - thread.init_simulator_context(base_stack_addr); - thread.process.as_mut().unwrap().num_thread += 1; - let thread_m = Rc::new(thread); - self.get_g_alive().push(Rc::clone(&thread_m)); - self.g_scheduler().ready_to_run(Rc::clone(&thread_m)); + thread_m.init_simulator_context(base_stack_addr); + thread_m.process.as_mut().unwrap().num_thread += 1; + self.get_g_alive().push(Rc::clone(&thread)); + self.g_scheduler().ready_to_run(Rc::clone(&thread)); Result::Ok(()) } /// Wait for another thread to finish its execution - pub fn thread_join(&mut self, id_thread: Rc) { + pub fn thread_join(&mut self, id_thread: Rc>) { while self.get_g_alive().contains(&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. /// /// Cannot use yield as a function name -> reserved name in rust - pub fn thread_yield(&mut self, thread: Rc) { + pub fn thread_yield(&mut self, thread: Rc>) { todo!(); } /// Put the thread to sleep and relinquish the processor - pub fn thread_sleep(&mut self, thread: Rc) { + pub fn thread_sleep(&mut self, thread: Rc>) { todo!(); } /// Finish the execution of the thread and prepare its deallocation - pub fn thread_finish(&self, thread: Rc) { + pub fn thread_finish(&self, thread: Rc>) { todo!(); } @@ -105,7 +105,7 @@ impl<'a> ThreadManager<'a> { } /// List of alive threads - pub fn get_g_alive(&mut self) -> &mut List> { + pub fn get_g_alive(&mut self) -> &mut List>> { &mut self.g_alive } diff --git a/src/kernel/ucontext.rs b/src/kernel/ucontext.rs index 1ffee6f..c50c223 100644 --- a/src/kernel/ucontext.rs +++ b/src/kernel/ucontext.rs @@ -59,7 +59,7 @@ impl UContextT { pub fn new() -> Self { Self { - stackBottom: Vec::default() + stack_bottom: Vec::default() } }