1
0
Fork 0
forked from AbleOS/ableos
ableos-idl/ableos/src/kernel_state.rs

56 lines
1.3 KiB
Rust
Raw Normal View History

use hashbrown::HashMap;
use spin::Lazy;
2022-01-13 08:54:33 -06:00
use crate::{
filesystem::StorageDevice,
handle::{Handle, HandleResource},
};
pub static KERNEL_STATE: Lazy<spin::Mutex<KernelInternalState>> =
Lazy::new(|| spin::Mutex::new(KernelInternalState::new()));
2022-01-13 08:54:33 -06:00
pub struct KernelInternalState {
2022-02-19 07:17:44 -06:00
pub hostname: String,
storage_devices: HashMap<Handle, Box<dyn StorageDevice>>,
2022-01-13 08:54:33 -06:00
should_shutdown: bool,
}
impl KernelInternalState {
pub fn new() -> Self {
Self {
should_shutdown: false,
storage_devices: HashMap::new(),
2022-01-27 01:37:12 -06:00
hostname: "".to_string(),
2022-01-13 08:54:33 -06:00
}
}
2022-01-27 01:37:12 -06:00
pub fn set_hostname(&mut self, hostname: String) {
self.hostname = hostname;
}
pub fn add_storage_device(&mut self, device: impl StorageDevice + Send + 'static) {
self.storage_devices
.insert(Handle::new(HandleResource::StorageDevice), Box::new(device));
}
pub fn get_storage_device(&self, handle: Handle) -> Option<&dyn StorageDevice> {
self.storage_devices.get(&handle).map(|d| &**d)
}
2022-01-13 08:54:33 -06:00
pub fn shutdown(&mut self) {
self.should_shutdown = true;
}
2022-01-13 08:54:33 -06:00
pub fn update_state(&mut self) {
if self.should_shutdown {
crate::arch::shutdown();
}
}
}
impl Default for KernelInternalState {
fn default() -> Self {
Self::new()
}
}