Removed unwraps in synch.rs

This commit is contained in:
Samy Solhi 2023-03-22 16:20:53 +01:00
parent ca9f458a7e
commit 21a0da8f24

View File

@ -5,7 +5,6 @@ use crate::simulator::machine::Machine;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use super::system::System;
use super::thread_manager::ThreadManager; use super::thread_manager::ThreadManager;
/// Structure of a Semaphore used for synchronisation /// Structure of a Semaphore used for synchronisation
@ -69,8 +68,9 @@ impl Semaphore {
pub fn v(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager){ pub fn v(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager){
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
self.counter += 1; self.counter += 1;
if self.waiting_queue.peek() != None { match self.waiting_queue.pop() {
thread_manager.ready_to_run(self.waiting_queue.pop().unwrap()); Some(thread) => thread_manager.ready_to_run(thread),
None => ()
} }
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
} }
@ -149,20 +149,23 @@ impl Lock {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
match thread_manager.get_g_current_thread() { match thread_manager.get_g_current_thread() {
Some(thread) => { Some(_) => {
if self.held_by_current_thread(thread_manager) { if self.held_by_current_thread(thread_manager) {
if self.waiting_queue.peek() != None { match self.waiting_queue.pop() {
self.owner = Some(self.waiting_queue.pop().unwrap()); Some(thread) => {
self.owner = Some(thread);
match &self.owner { match &self.owner {
Some(x) => thread_manager.ready_to_run(Rc::clone(&x)), Some(x) => thread_manager.ready_to_run(Rc::clone(&x)),
None => () None => ()
} }
} else { },
None => {
self.free = true; self.free = true;
self.owner = None; self.owner = None;
} }
} }
} }
}
None => () None => ()
} }
@ -229,8 +232,9 @@ impl Condition {
pub fn signal(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) { pub fn signal(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
if self.waiting_queue.peek() != None { match self.waiting_queue.pop() {
thread_manager.ready_to_run(self.waiting_queue.pop().unwrap()); Some(thread) => thread_manager.ready_to_run(thread),
None => ()
} }
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);
@ -246,8 +250,9 @@ impl Condition {
pub fn broadcast(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) { pub fn broadcast(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
let old_status = machine.interrupt.set_status(InterruptOff); let old_status = machine.interrupt.set_status(InterruptOff);
while self.waiting_queue.peek() != None { match self.waiting_queue.pop() {
thread_manager.ready_to_run(self.waiting_queue.pop().unwrap()); Some(thread) => thread_manager.ready_to_run(thread),
None => ()
} }
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);