105 lines
2.5 KiB
Rust
105 lines
2.5 KiB
Rust
use std::rc::Rc;
|
|
use crate::{
|
|
kernel::{
|
|
thread::Thread,
|
|
scheduler::Scheduler
|
|
},
|
|
utility::list::List,
|
|
simulator::machine::Machine
|
|
};
|
|
|
|
/// # 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
|
|
}
|
|
|
|
impl System {
|
|
|
|
/// 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
|
|
}
|
|
}
|
|
|
|
// 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<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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
}
|
|
|
|
#[derive(PartialEq)]
|
|
pub enum ObjectType {
|
|
SemaphoreType,
|
|
LockType,
|
|
ConditionType,
|
|
FileType,
|
|
ThreadType,
|
|
InvalidType
|
|
} |