From 3b9a3ab18a22c5a6b9fdd9ae495ebdb9d279a55d Mon Sep 17 00:00:00 2001 From: Able Date: Tue, 1 Feb 2022 15:27:40 -0600 Subject: [PATCH] scheduler rewrite --- ableos/src/arch/x86_64/interrupts.rs | 1 + ableos/src/kmain.rs | 10 ---- ableos/src/scheduler/mod.rs | 12 +++- ableos/src/scheduler/new_sched.rs | 85 ++++++++++++++++++++++++++++ ableos/src/scheduler/proc.rs | 2 +- ableos/src/utils.rs | 8 +++ 6 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 ableos/src/scheduler/new_sched.rs diff --git a/ableos/src/arch/x86_64/interrupts.rs b/ableos/src/arch/x86_64/interrupts.rs index a55c8c3..e2d7af8 100644 --- a/ableos/src/arch/x86_64/interrupts.rs +++ b/ableos/src/arch/x86_64/interrupts.rs @@ -119,6 +119,7 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac print!("{:?}", KeyCode::from(key)) } }, + // Register a keyboard interrupt handler for the given scancode. } } } diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 0cd840c..c793262 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -26,14 +26,6 @@ use { log::*, }; -#[no_mangle] -#[allow(unconditional_recursion)] -pub extern "C" fn stack_overflow() -> u8 { - stack_overflow(); - // meme number - 69 // NOTE: Any specific reason for this number aside from memes? -} - lazy_static! { pub static ref TICK: AtomicU64 = AtomicU64::new(0); pub static ref BOOT_CONF: BootConfig = boot_conf::BootConfig::new(); @@ -83,8 +75,6 @@ pub fn kernel_main() -> ! { } } - crate::tests::screen_writer_test(); - use crate::wasm::WasmProgram; let ret = WasmProgram::new_from_bytes(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]); trace!("Binary Valid: {:?}", ret.validate_header()); diff --git a/ableos/src/scheduler/mod.rs b/ableos/src/scheduler/mod.rs index 06f8413..776f6b6 100644 --- a/ableos/src/scheduler/mod.rs +++ b/ableos/src/scheduler/mod.rs @@ -1,8 +1,17 @@ #![warn(missing_docs)] -use alloc::{vec, vec::Vec}; + +//! The standard ableOS scheduler named +//! +//! # Notes +//! The scheduler is also responsible for choosing the priority of a process. +//! The scheduler is responsible for choosing which process to execute next. + +use alloc::vec::Vec; pub mod capabilities; pub mod proc; + +mod new_sched; use proc::{Process, PID}; #[cfg(test)] @@ -13,7 +22,6 @@ use crate::file::PathRep; use self::capabilities::Capabilities; lazy_static::lazy_static!( - /// The standard implementation for ableOS pub static ref SCHEDULER: spin::Mutex = spin::Mutex::new(Scheduler::new()); diff --git a/ableos/src/scheduler/new_sched.rs b/ableos/src/scheduler/new_sched.rs new file mode 100644 index 0000000..bb5bf9c --- /dev/null +++ b/ableos/src/scheduler/new_sched.rs @@ -0,0 +1,85 @@ +use alloc::vec::Vec; + +use crate::{ + capabilities::Capabilities, + proc::{Process, PID}, + Priority, +}; +/// Add additional wake conditions to the list +pub enum WakeCondition { + /// Wake when the process has been blocked for a certain amount of time + TimerInterrupt(u64), + SocketRead(PID), + SocketWrite(PID), + SocketOpen(PID), + SocketClose(PID), + // HardwareEvent, +} + +// NOTE: Define what is a sleeping process in the context of the ableOS kernel. +// Blocked processes are processes that are waiting for a certain event to occur. +pub struct BlockedProcess { + pub pid: PID, + pub wake_time: u64, +} + +pub struct Scheduler { + pub free_pid: PID, + pub process_exec_time: u64, + pub execution_queue: Vec, + pub sleeping_queue: Vec, + // pub blocked_queue: Vec, + // / All timed processes sorted by wake time + // pub timed_blocked_queue: Vec, +} +impl Scheduler { + /// Create a new scheduler + pub fn new() -> Self { + Self { + free_pid: PID(0), + process_exec_time: 0, + execution_queue: Vec::new(), + sleeping_queue: Vec::new(), + } + } + + /// Change the current process to the next process in the list + pub fn next_process(&mut self) { + self.process_exec_time = 0; + let previous_task = self.execution_queue[0].clone(); + self.execution_queue.remove(0); + self.execution_queue.push(previous_task); + } + + /// Creates a new process + /// # Arguments + /// * `priority` - The priority of the process + /// # Example + /// + /// ``` + /// let mut scheduler = scheduler(); + /// let mut process = scheduler.new_process(Priority::Medium); + /// ``` + /// + /// TODO: Add a priority queue + /// TODO: Add a process queue + pub fn new_process(&mut self, priority: Priority) -> Process { + let process = Process { + id: self.free_pid, + priority, + capabilities: Capabilities::empty(), + }; + self.free_pid.0 += 1; + process + } + + pub fn sleep_process(&mut self, process: &mut Process) { + let sleeping_process = BlockedProcess { + pid: process.id, + wake_time: 0, + }; + + self.sleeping_queue.push(process.clone()); + self.execution_queue.remove(0); + } +} diff --git a/ableos/src/scheduler/proc.rs b/ableos/src/scheduler/proc.rs index 6b265e9..53e282f 100644 --- a/ableos/src/scheduler/proc.rs +++ b/ableos/src/scheduler/proc.rs @@ -3,7 +3,7 @@ use super::{capabilities::Capabilities, FileAccessTypes, Priority}; /// Process Identification -#[derive(Clone, PartialEq, Debug)] +#[derive(Clone, Copy, PartialEq, Debug)] pub struct PID(pub usize); diff --git a/ableos/src/utils.rs b/ableos/src/utils.rs index 91088e7..5ec7612 100644 --- a/ableos/src/utils.rs +++ b/ableos/src/utils.rs @@ -16,3 +16,11 @@ mod tests { assert_eq!(type_of(&1), "i32"); } } + +#[no_mangle] +#[allow(unconditional_recursion)] +pub extern "C" fn stack_overflow() -> u8 { + stack_overflow(); + // meme number + 69 // NOTE: Any specific reason for this number aside from memes? +}