Fix global var

This commit is contained in:
Quentin Legot 2023-03-01 11:10:15 +01:00
parent 6f98224da1
commit 4c8062905c
6 changed files with 49 additions and 19 deletions

9
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

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

View File

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

View File

@ -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<Process>,

View File

@ -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<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);
pub static g_thread_to_be_destroyed: Box<Option<Thread>> = Box::new(Option::None);
#[derive(PartialEq)]
pub enum ObjectType {
SEMAPHORE_TYPE,
LOCK_TYPE,