From f81d1dd2ba44b25fab002811da05e1218ed4a04e Mon Sep 17 00:00:00 2001 From: pjht Date: Sun, 9 Jun 2024 18:44:12 -0500 Subject: [PATCH] Use a queue instaed of a stack to schedule processes --- src/tasking.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/tasking.rs b/src/tasking.rs index 572fa16..ef4f2b9 100644 --- a/src/tasking.rs +++ b/src/tasking.rs @@ -1,7 +1,7 @@ use crate::{ dbg, gdt, println, qemu_exit, virtual_memory::{ASpaceMutex, AddressSpace, PagingError, KERNEL_SPACE} }; -use alloc::{boxed::Box, vec::Vec}; +use alloc::{boxed::Box, collections::VecDeque, vec::Vec}; use core::{arch::asm, ptr::addr_of}; use crossbeam_queue::SegQueue; use slab::Slab; @@ -98,7 +98,7 @@ unsafe impl Send for Process {} pub static TASKING: Lazy> = Lazy::new(|| { Mutex::new(Tasking { processes: Slab::new(), - ready_to_run: Vec::new(), + ready_to_run: VecDeque::new(), current_process: None, freeable_kstacks: Vec::new(), }) @@ -107,7 +107,7 @@ pub static TASKING: Lazy> = Lazy::new(|| { #[derive(Debug)] pub struct Tasking { processes: Slab, - ready_to_run: Vec, + ready_to_run: VecDeque, current_process: Option, freeable_kstacks: Vec>, } @@ -136,7 +136,7 @@ impl Tasking { message_queue: SegQueue::new(), sleeping: None, }); - self.ready_to_run.push(pid); + self.ready_to_run.push_back(pid); Ok(pid) } @@ -146,7 +146,7 @@ impl Tasking { Some(x) => x, None => return, }; - if let Some(next_process_pid) = self.ready_to_run.pop() { + if let Some(next_process_pid) = self.ready_to_run.pop_front() { let current_address_space = self.processes[next_process_pid] .address_space .take() @@ -156,7 +156,7 @@ impl Tasking { let next_process = &self.processes[next_process_pid]; gdt::set_tss_stack(next_process.kernel_esp_top); if self.processes[current_process].sleeping.is_none() { - self.ready_to_run.push(current_process); + self.ready_to_run.push_back(current_process); } let kernel_esp = next_process.kernel_esp; let previous_process = self.current_process.replace(next_process_pid).unwrap(); @@ -171,7 +171,7 @@ impl Tasking { } pub fn exit(&mut self) -> ! { - if let Some(next_process_pid) = self.ready_to_run.pop() { + if let Some(next_process_pid) = self.ready_to_run.pop_front() { if let Some(current_process) = self.current_process { self.freeable_kstacks.push(self.processes.remove(current_process).kernel_stack); } @@ -224,7 +224,7 @@ impl Tasking { pub fn wake(&mut self, pid: usize) { if self.processes[pid].sleeping.is_some() { self.processes[pid].sleeping = None; - self.ready_to_run.push(pid); + self.ready_to_run.push_back(pid); } } }