Fix a lot of errors

This commit is contained in:
Quentin Legot 2023-04-12 14:01:39 +02:00
parent e8629b1ebf
commit 134e2bd2cc
3 changed files with 24 additions and 21 deletions

View File

@ -198,34 +198,37 @@ fn syscall(machine: &mut Machine, system: &mut System) -> Result<MachineOk, Mach
}
}
fn sc_p(machine: &mut Machine, system: &mut System) -> Result<(), Error> {
fn sc_p(machine: &mut Machine, system: &mut System) -> Result<MachineOk, MachineError> {
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<MachineOk, MachineError> {
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<MachineOk, MachineError> {
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<MachineOk, MachineError> {
// 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

View File

@ -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() }

View File

@ -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<Semaphore> {
self.semaphores.remove(&id)
}
pub fn remove_semaphore(&mut self, id:i32) {
pub fn remove_lock(&mut self, id:i32) -> Option<Lock> {
self.locks.remove(&id)
}