From 134e2bd2cc8a40cfb8c2a15695aaf8f920ab660b Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 12 Apr 2023 14:01:39 +0200 Subject: [PATCH] Fix a lot of errors --- src/kernel/exception.rs | 27 +++++++++++++++------------ src/kernel/system.rs | 10 +++++----- src/utility/objaddr.rs | 8 ++++---- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/kernel/exception.rs b/src/kernel/exception.rs index 748b3bf..4a58876 100644 --- a/src/kernel/exception.rs +++ b/src/kernel/exception.rs @@ -198,34 +198,37 @@ fn syscall(machine: &mut Machine, system: &mut System) -> Result Result<(), Error> { +fn sc_p(machine: &mut Machine, system: &mut System) -> Result { let id_sema = machine.int_reg.get_reg(10); - match system.get_obj_addrs().search_semaphore(id_sema) { - Some(sema) => { sema.p(machine, system.get_thread_manager()); Ok(()) } - None => Err(format!("Coudn't find semaphore {}", id_sema)) + match system.get_obj_addrs().search_semaphore(id_sema as i32) { + Some(sema) => { sema.p(machine, system.get_thread_manager()); Ok(MachineOk::Ok) } + None => Err(format!("Coudn't find semaphore {}", id_sema))? } } -fn sc_v(machine: &mut Machine, system: &mut System) -> Result<(), Error> { +fn sc_v(machine: &mut Machine, system: &mut System) -> Result { let id_sema = machine.int_reg.get_reg(10); - match system.get_obj_addrs().search_semaphore(id_sema) { - Some(sema) => { sema.v(machine, system.get_thread_manager()); Ok(()) }, - None => Err(format!("Couldn't find semaphore {}", id_sema)) + match system.get_obj_addrs().search_semaphore(id_sema as i32) { + Some(sema) => { + sema.v(machine, system.get_thread_manager()); + Ok(MachineOk::Ok) + }, + None => Err(format!("Couldn't find semaphore {}", id_sema))? } } -fn sc_sem_create(machine: &mut Machine) -> Result<(), Error> { +fn sc_sem_create(machine: &mut Machine) -> Result { let addr_name = machine.read_int_register(10) as usize; let initial_count = machine.read_int_register(11); let size = get_length_param(addr_name as usize, machine); let _name = get_string_param(addr_name, size, machine); match initial_count < 0 { - true => Err(format!("Initial_count < 0")), - false => Ok(()) + true => Err(format!("Initial_count < 0"))?, + false => Ok(MachineOk::Ok) } } -fn sc_new_thread(machine: &mut Machine, system: &mut System) -> Result<(), Error> { +fn sc_new_thread(machine: &mut Machine, system: &mut System) -> Result { // Get the address of the string for the name of the thread let name_addr = machine.read_int_register(10) as usize; // Get the pointer of the function to be executed in the new thread diff --git a/src/kernel/system.rs b/src/kernel/system.rs index cdae1b0..6e30fa2 100644 --- a/src/kernel/system.rs +++ b/src/kernel/system.rs @@ -15,12 +15,12 @@ use crate::utility::objaddr::ObjAddr; /// - The list of active threads /// - The thread to be destroyed next /// - The scheduler which acts upon these threads -pub struct System<'a> { +pub struct System { thread_manager: ThreadManager, - obj_addrs: ObjAddr<'a> + obj_addrs: ObjAddr } -impl<'a> System<'a> { +impl System { // GETTERS @@ -28,11 +28,11 @@ impl<'a> System<'a> { &mut self.thread_manager } - pub fn get_obj_addrs(&mut self) -> &'a mut ObjAddr { &mut self.obj_addrs } + pub fn get_obj_addrs(&mut self) -> &mut ObjAddr { &mut self.obj_addrs } } -impl<'a> Default for System<'a> { +impl Default for System { /// System constructor fn default() -> Self { Self { thread_manager: ThreadManager::new(), obj_addrs: ObjAddr::init() } diff --git a/src/utility/objaddr.rs b/src/utility/objaddr.rs index d6e37de..c898f63 100644 --- a/src/utility/objaddr.rs +++ b/src/utility/objaddr.rs @@ -48,20 +48,20 @@ impl ObjAddr { /// Searches for a semaphore of id **id** in self pub fn search_semaphore(&self, id: i32) -> Option<&mut Semaphore> { - self.semaphores.get(&id) + self.semaphores.get_mut(&id) } /// Searches for a lock of id **id** in self pub fn search_lock(&self, id:i32) -> Option<&mut Lock> { - self.locks.get(&id) + self.locks.get_mut(&id) } /// Removes the object of id **id** from self if it exists - pub fn remove_semaphore(&mut self, id: i32) { + pub fn remove_semaphore(&mut self, id: i32) -> Option { self.semaphores.remove(&id) } - pub fn remove_semaphore(&mut self, id:i32) { + pub fn remove_lock(&mut self, id:i32) -> Option { self.locks.remove(&id) }