burritos/src/kernel/system.rs

102 lines
2.3 KiB
Rust
Raw Normal View History

//! # System module
//!
//! Module containing structs and methods pertaining to the state of the operating system
use std::{cell::RefCell, rc::Rc};
2023-03-08 21:10:51 +01:00
use crate::simulator::machine::Machine;
use super::thread_manager::ThreadManager;
2023-03-01 11:10:15 +01:00
2023-03-15 10:10:53 +01:00
/// This macro properly initializes the system
#[macro_export]
macro_rules! init_system {
() => {{
let m = Machine::init_machine();
init_system!(m)
}};
($a:expr) => {{
2023-03-15 11:09:34 +01:00
let sys = std::rc::Rc::new(std::cell::RefCell::new(crate::System::new($a)));
crate::System::freeze(std::rc::Rc::clone(&sys));
sys
2023-03-15 10:10:53 +01:00
}};
}
2023-03-08 15:16:10 +01:00
/// # 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
2023-03-08 15:48:33 +01:00
#[derive(PartialEq)]
pub struct System {
g_machine: RefCell<Machine>,
thread_manager: Rc<RefCell<ThreadManager>>
2023-03-08 15:16:10 +01:00
}
2023-03-01 11:10:15 +01:00
impl System {
2023-03-01 15:45:49 +01:00
2023-03-08 15:34:13 +01:00
/// System constructor
pub fn new(machine: Machine) -> System {
2023-03-08 15:34:13 +01:00
Self {
g_machine: RefCell::new(machine),
thread_manager: Rc::new(RefCell::new(ThreadManager::new()))
2023-03-08 15:34:13 +01:00
}
2023-03-08 21:10:51 +01:00
}
/// use thread_manager setter to send it system instance
pub fn freeze(this: Rc<RefCell<System>>) {
let copy = Rc::clone(&this);
let tm = &this.borrow_mut().thread_manager;
tm.borrow_mut().system = Option::Some(copy);
ThreadManager::freeze(tm);
2023-03-08 21:10:51 +01:00
}
2023-03-08 15:34:13 +01:00
2023-03-08 15:16:10 +01:00
// GETTERS
2023-03-01 10:11:19 +01:00
2023-03-08 15:16:10 +01:00
/// Returns the Machine
///
/// Useful to access RAM, devices, ...
pub fn get_g_machine(&self) -> &RefCell<Machine> {
&self.g_machine
2023-03-08 15:16:10 +01:00
}
2023-03-15 11:09:34 +01:00
pub fn get_thread_manager(&self) -> Rc<RefCell<ThreadManager>> {
Rc::clone(&self.thread_manager)
}
2023-03-08 15:16:10 +01:00
// Setters
/// Assign a machine to the system
pub fn set_g_machine(&mut self, machine: RefCell<Machine>) {
2023-03-08 15:16:10 +01:00
self.g_machine = machine
}
}
2023-02-28 14:43:40 +01:00
#[derive(PartialEq, Debug)]
2023-02-28 14:43:40 +01:00
pub enum ObjectType {
SemaphoreType,
LockType,
ConditionType,
FileType,
ThreadType,
InvalidType
}
#[cfg(test)]
mod tests {
use crate::{System, Machine};
#[test]
fn test_init_system() {
init_system!();
}
2023-02-28 14:43:40 +01:00
}