Fix global var

This commit is contained in:
Quentin Legot 2023-03-01 11:10:15 +01:00 committed by François Autin
parent e3e217c57f
commit cf1c7aad5a
No known key found for this signature in database
GPG Key ID: 343F5D382E1DD77C
6 changed files with 49 additions and 19 deletions

9
Cargo.lock generated
View File

@ -5,3 +5,12 @@ version = 3
[[package]] [[package]]
name = "burritos" name = "burritos"
version = "0.1.0" 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"

View File

@ -3,6 +3,5 @@ name = "burritos"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
lazy_static = "1.4.0"

View File

@ -1,4 +1,5 @@
#[derive(PartialEq)]
pub struct Process { pub struct Process {
} }

View File

@ -1,7 +1,6 @@
use crate::utility::list::List; use crate::utility::list::List;
use crate::kernel::thread::Thread; use crate::kernel::thread::Thread;
use crate::utility::system::{g_current_thread, g_thread_to_be_destroyed}; use crate::utility::system::{g_current_thread, g_thread_to_be_destroyed};
use std::rc::Rc;
struct Scheduler<> { struct Scheduler<> {
@ -51,22 +50,33 @@ impl Scheduler {
/// ///
/// **next_thread** thread to dispatch to the CPU /// **next_thread** thread to dispatch to the CPU
pub fn switch_to(&self, next_thread: Thread) { 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(); match g_thread_to_be_destroyed.write() {
old_thread.save_simulator_state(); Ok(mut thread_to_be_destroyed) => {
if thread_to_be_destroyed.is_some() {
if(old_thread != g_current_thread) { drop(thread_to_be_destroyed.take());
next_thread.restore_processor_state(); }
next_thread.restore_simulator_state(); },
} Err(err) => {
panic!("RwLock is poisonned: {}", err);
if(g_thread_to_be_destroyed.is_some()) { }
drop(g_thread_to_be_destroyed.take()); }
},
Err(err) => {
panic!("RwLock is poisonned: {}", err);
}
} }
} }
} }

View File

@ -2,16 +2,19 @@ use super::process::Process;
use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS}, utility::system::ObjectType}; use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS}, utility::system::ObjectType};
#[derive(PartialEq)]
struct SimulatorContext { struct SimulatorContext {
// todo // todo
} }
#[derive(PartialEq)]
struct ThreadContext { struct ThreadContext {
pub int_registers: [i64; NUM_INT_REGS], pub int_registers: [i64; NUM_INT_REGS],
pub float_registers: [i64; NUM_FP_REGS], pub float_registers: [i64; NUM_FP_REGS],
pc: i64, pc: i64,
} }
#[derive(PartialEq)]
pub struct Thread { pub struct Thread {
name: String, name: String,
process: Option<Process>, process: Option<Process>,

View File

@ -1,9 +1,17 @@
use std::sync::{Mutex, RwLock};
use lazy_static::lazy_static;
use crate::kernel::thread::Thread; use crate::kernel::thread::Thread;
extern crate lazy_static;
lazy_static! {
pub static ref g_current_thread: RwLock<Option<Thread>> = RwLock::new(Option::None);
pub static ref g_thread_to_be_destroyed: RwLock<Option<Thread>> = RwLock::new(Option::None);
}
pub static g_current_thread: Box<Option<Thread>> = Box::new(Option::None); #[derive(PartialEq)]
pub static g_thread_to_be_destroyed: Box<Option<Thread>> = Box::new(Option::None);
pub enum ObjectType { pub enum ObjectType {
SEMAPHORE_TYPE, SEMAPHORE_TYPE,
LOCK_TYPE, LOCK_TYPE,