From e7637128807b932b5012bceadc8b1d34bc892970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Wed, 8 Mar 2023 15:16:10 +0100 Subject: [PATCH] New system structure --- Cargo.lock | 7 ---- Cargo.toml | 1 - src/kernel/system.rs | 94 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 81 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9898199..e3bcbe3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,16 +6,9 @@ version = 3 name = "burritos" version = "0.1.0" dependencies = [ - "lazy_static", "libc", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - [[package]] name = "libc" version = "0.2.139" diff --git a/Cargo.toml b/Cargo.toml index d9e3b00..1d66884 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] -lazy_static = "1.4.0" libc = { version = "0.2.139", features = ["extra_traits"] } diff --git a/src/kernel/system.rs b/src/kernel/system.rs index 1fd2de9..b0f54c8 100644 --- a/src/kernel/system.rs +++ b/src/kernel/system.rs @@ -1,19 +1,87 @@ -use std::{sync::{RwLock, Arc}}; +use std::rc::Rc; +use crate::{ + kernel::{ + thread::Thread, + scheduler::Scheduler + }, + utility::list::List, + simulator::machine::Machine +}; -use lazy_static::lazy_static; - -use crate::{kernel::{thread::Thread, scheduler::Scheduler}, utility::list::List, simulator::machine::Machine}; - -extern crate lazy_static; - -lazy_static! { - pub static ref G_MACHINE: RwLock = RwLock::new(Machine::_init_machine()); - 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 ref G_ALIVE: RwLock>> = RwLock::new(List::new()); - pub static ref G_SCHEDULER: RwLock = RwLock::new(Scheduler::new()); +/// # System +/// +/// This structure represents the state of the threads running on the operating system. +/// It contains references to the following: +/// +/// - The simulated machine +/// - The current running thread +/// - The list of active threads +/// - The thread to be destroyed next +/// - The scheduler which acts upon these threads +pub struct System { + g_machine: Machine, + g_current_thread: Option, + g_thread_to_be_destroyed: Option, + g_alive: List>, + g_scheduler: Scheduler } +impl System { + + // GETTERS + + /// Returns the Machine + /// + /// Useful to access RAM, devices, ... + pub fn get_g_machine(&mut self) -> &mut Machine { + &mut self.g_machine + } + + /// Currently running thread + pub fn get_g_current_thread(&mut self) -> &mut Option { + &mut self.g_current_thread + } + + /// Thread to be destroyed by [...] + /// + /// TODO: Finish the comment with the relevant value + pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option { + &mut self.g_thread_to_be_destroyed + } + + /// List of alive threads + pub fn get_g_alive(&mut self) -> &mut List> { + &mut self.g_alive + } + + /// Current scheduler + pub fn g_scheduler(&mut self) -> &mut Scheduler { + &mut self.g_scheduler + } + + // Setters + + /// Assign a machine to the system + pub fn set_g_machine(&mut self, machine: Machine) { + self.g_machine = machine + } + + /// Set currently running thread + pub fn set_g_current_thread(&mut self, thread: Option) { + self.g_current_thread = thread + } + + /// Set thread to be destroyed next + pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option) { + self.g_thread_to_be_destroyed = thread + } + + /// Set Scheduler which will manage the threads + pub fn set_g_scheduler(&mut self, scheduler: Scheduler) { + self.g_scheduler = scheduler + } + +} #[derive(PartialEq)] pub enum ObjectType {