2023-03-15 11:01:25 +01:00
|
|
|
//! # System module
|
|
|
|
//!
|
|
|
|
//! Module containing structs and methods pertaining to the state of the operating system
|
|
|
|
|
2023-03-13 21:47:06 +01:00
|
|
|
use std::{cell::RefCell, rc::Rc};
|
2023-03-09 12:08:33 +01:00
|
|
|
|
2023-03-08 21:10:51 +01:00
|
|
|
use crate::simulator::machine::Machine;
|
|
|
|
|
2023-03-15 16:28:29 +01:00
|
|
|
use super::{thread_manager::ThreadManager, thread::Thread};
|
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 15:20:20 +01:00
|
|
|
$crate::System::new($a)
|
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)]
|
2023-03-13 21:47:06 +01:00
|
|
|
pub struct System {
|
2023-03-15 14:56:05 +01:00
|
|
|
machine: Machine,
|
2023-03-15 15:20:20 +01:00
|
|
|
thread_manager: ThreadManager
|
2023-03-08 15:16:10 +01:00
|
|
|
}
|
2023-03-01 11:10:15 +01:00
|
|
|
|
2023-03-13 21:47:06 +01:00
|
|
|
impl System {
|
2023-03-01 15:45:49 +01:00
|
|
|
|
2023-03-08 15:34:13 +01:00
|
|
|
/// System constructor
|
2023-03-13 21:47:06 +01:00
|
|
|
pub fn new(machine: Machine) -> System {
|
2023-03-08 15:34:13 +01:00
|
|
|
Self {
|
2023-03-15 14:56:05 +01:00
|
|
|
machine,
|
2023-03-15 15:20:20 +01:00
|
|
|
thread_manager: ThreadManager::new()
|
2023-03-08 15:34:13 +01:00
|
|
|
}
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:28:29 +01:00
|
|
|
/// Sets a thread asleep
|
|
|
|
///
|
|
|
|
pub fn thread_sleep(&mut self, thread: Rc<RefCell<Thread>>) {
|
2023-03-15 17:57:53 +01:00
|
|
|
let machine = self.get_machine();
|
|
|
|
self.thread_manager.thread_sleep(machine, thread);
|
2023-03-15 16:28:29 +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, ...
|
2023-03-15 16:28:29 +01:00
|
|
|
pub fn get_machine(&mut self) -> &mut Machine {
|
|
|
|
&mut self.machine
|
2023-03-08 15:16:10 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:28:29 +01:00
|
|
|
pub fn get_thread_manager(&mut self) -> &mut ThreadManager {
|
|
|
|
&mut self.thread_manager
|
2023-03-15 11:09:34 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:16:10 +01:00
|
|
|
// Setters
|
|
|
|
|
2023-03-15 15:20:20 +01:00
|
|
|
/// Assign a machine to the system
|
2023-03-15 14:56:05 +01:00
|
|
|
pub fn set_machine(&mut self, machine: Machine) {
|
|
|
|
self.machine = machine
|
2023-03-08 15:16:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-02-28 14:43:40 +01:00
|
|
|
|
2023-03-09 13:24:04 +01:00
|
|
|
#[derive(PartialEq, Debug)]
|
2023-02-28 14:43:40 +01:00
|
|
|
pub enum ObjectType {
|
2023-03-01 11:16:21 +01:00
|
|
|
SemaphoreType,
|
|
|
|
LockType,
|
|
|
|
ConditionType,
|
|
|
|
FileType,
|
|
|
|
ThreadType,
|
|
|
|
InvalidType
|
2023-03-13 15:09:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use crate::{System, Machine};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_init_system() {
|
|
|
|
init_system!();
|
|
|
|
}
|
|
|
|
|
2023-02-28 14:43:40 +01:00
|
|
|
}
|