From cf1c7aad5ad79b52154f504e5df49e16b88242c0 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 1 Mar 2023 11:10:15 +0100 Subject: [PATCH] Fix global var --- Cargo.lock | 9 +++++++++ Cargo.toml | 3 +-- src/kernel/process.rs | 1 + src/kernel/scheduler.rs | 38 ++++++++++++++++++++++++-------------- src/kernel/thread.rs | 3 +++ src/utility/system.rs | 14 +++++++++++--- 6 files changed, 49 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1e08526..5b90585 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,12 @@ version = 3 [[package]] name = "burritos" version = "0.1.0" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" diff --git a/Cargo.toml b/Cargo.toml index 466166c..fe2c644 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,5 @@ name = "burritos" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] +lazy_static = "1.4.0" diff --git a/src/kernel/process.rs b/src/kernel/process.rs index fe973f4..5ae7efc 100644 --- a/src/kernel/process.rs +++ b/src/kernel/process.rs @@ -1,4 +1,5 @@ +#[derive(PartialEq)] pub struct Process { } \ No newline at end of file diff --git a/src/kernel/scheduler.rs b/src/kernel/scheduler.rs index 1c70460..207c5e6 100644 --- a/src/kernel/scheduler.rs +++ b/src/kernel/scheduler.rs @@ -1,7 +1,6 @@ use crate::utility::list::List; use crate::kernel::thread::Thread; use crate::utility::system::{g_current_thread, g_thread_to_be_destroyed}; -use std::rc::Rc; struct Scheduler<> { @@ -51,22 +50,33 @@ impl Scheduler { /// /// **next_thread** thread to dispatch to the CPU pub fn switch_to(&self, next_thread: Thread) { - let old_thread = Box::clone(&g_current_thread).unwrap(); + match g_current_thread.write() { + Ok(mut current_thread) => { + let old_thread = current_thread.as_mut().unwrap(); - g_current_thread.check_overflow(); + old_thread.save_processor_state(); + old_thread.save_simulator_state(); - g_current_thread = Box::new(Option::Some(next_thread)); + if old_thread != &next_thread { + next_thread.restore_processor_state(); + next_thread.restore_simulator_state(); + current_thread.replace(next_thread); + } - old_thread.save_processor_state(); - old_thread.save_simulator_state(); - - if(old_thread != g_current_thread) { - next_thread.restore_processor_state(); - next_thread.restore_simulator_state(); - } - - if(g_thread_to_be_destroyed.is_some()) { - drop(g_thread_to_be_destroyed.take()); + match g_thread_to_be_destroyed.write() { + Ok(mut thread_to_be_destroyed) => { + if thread_to_be_destroyed.is_some() { + drop(thread_to_be_destroyed.take()); + } + }, + Err(err) => { + panic!("RwLock is poisonned: {}", err); + } + } + }, + Err(err) => { + panic!("RwLock is poisonned: {}", err); + } } } } \ No newline at end of file diff --git a/src/kernel/thread.rs b/src/kernel/thread.rs index 3b6cd83..9b0354d 100644 --- a/src/kernel/thread.rs +++ b/src/kernel/thread.rs @@ -2,16 +2,19 @@ use super::process::Process; use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS}, utility::system::ObjectType}; +#[derive(PartialEq)] struct SimulatorContext { // todo } +#[derive(PartialEq)] struct ThreadContext { pub int_registers: [i64; NUM_INT_REGS], pub float_registers: [i64; NUM_FP_REGS], pc: i64, } +#[derive(PartialEq)] pub struct Thread { name: String, process: Option, diff --git a/src/utility/system.rs b/src/utility/system.rs index 642dd11..f5b8b50 100644 --- a/src/utility/system.rs +++ b/src/utility/system.rs @@ -1,9 +1,17 @@ +use std::sync::{Mutex, RwLock}; + +use lazy_static::lazy_static; + use crate::kernel::thread::Thread; +extern crate lazy_static; + +lazy_static! { + pub static ref g_current_thread: RwLock> = RwLock::new(Option::None); + pub static ref g_thread_to_be_destroyed: RwLock> = RwLock::new(Option::None); +} -pub static g_current_thread: Box> = Box::new(Option::None); -pub static g_thread_to_be_destroyed: Box> = Box::new(Option::None); - +#[derive(PartialEq)] pub enum ObjectType { SEMAPHORE_TYPE, LOCK_TYPE,