BurritOS/src/kernel/system.rs

105 lines
2.5 KiB
Rust
Raw Normal View History

2023-03-08 15:16:10 +01:00
use std::rc::Rc;
use crate::{
kernel::{
thread::Thread,
scheduler::Scheduler
},
utility::list::List,
simulator::machine::Machine
};
2023-03-01 11:10:15 +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
pub struct System {
g_machine: Machine,
g_current_thread: Option<Thread>,
g_thread_to_be_destroyed: Option<Thread>,
g_alive: List<Rc<Thread>>,
g_scheduler: Scheduler
}
2023-03-01 11:10:15 +01:00
2023-03-08 15:16:10 +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, scheduler: Scheduler) -> Self {
Self {
g_machine: machine,
g_current_thread: None,
g_thread_to_be_destroyed: None,
g_alive: List::new(),
g_scheduler: scheduler
}
}
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(&mut self) -> &mut Machine {
&mut self.g_machine
}
/// Currently running thread
pub fn get_g_current_thread(&mut self) -> &mut Option<Thread> {
&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<Thread> {
&mut self.g_thread_to_be_destroyed
}
/// List of alive threads
pub fn get_g_alive(&mut self) -> &mut List<Rc<Thread>> {
&mut self.g_alive
}
/// Current scheduler
pub fn g_scheduler(&mut self) -> &mut Scheduler {
&mut self.g_scheduler
}
2023-03-01 10:11:19 +01:00
2023-03-08 15:16:10 +01:00
// 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<Thread>) {
self.g_current_thread = thread
}
/// Set thread to be destroyed next
pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option<Thread>) {
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
}
}
2023-02-28 14:43:40 +01:00
2023-03-01 11:10:15 +01:00
#[derive(PartialEq)]
2023-02-28 14:43:40 +01:00
pub enum ObjectType {
SemaphoreType,
LockType,
ConditionType,
FileType,
ThreadType,
InvalidType
2023-02-28 14:43:40 +01:00
}