Use a queue instaed of a stack to schedule processes

This commit is contained in:
pjht 2024-06-09 18:44:12 -05:00
parent 1376aa5f44
commit f81d1dd2ba
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
dbg, gdt, println, qemu_exit, virtual_memory::{ASpaceMutex, AddressSpace, PagingError, KERNEL_SPACE} 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 core::{arch::asm, ptr::addr_of};
use crossbeam_queue::SegQueue; use crossbeam_queue::SegQueue;
use slab::Slab; use slab::Slab;
@ -98,7 +98,7 @@ unsafe impl Send for Process {}
pub static TASKING: Lazy<Mutex<Tasking>> = Lazy::new(|| { pub static TASKING: Lazy<Mutex<Tasking>> = Lazy::new(|| {
Mutex::new(Tasking { Mutex::new(Tasking {
processes: Slab::new(), processes: Slab::new(),
ready_to_run: Vec::new(), ready_to_run: VecDeque::new(),
current_process: None, current_process: None,
freeable_kstacks: Vec::new(), freeable_kstacks: Vec::new(),
}) })
@ -107,7 +107,7 @@ pub static TASKING: Lazy<Mutex<Tasking>> = Lazy::new(|| {
#[derive(Debug)] #[derive(Debug)]
pub struct Tasking { pub struct Tasking {
processes: Slab<Process>, processes: Slab<Process>,
ready_to_run: Vec<usize>, ready_to_run: VecDeque<usize>,
current_process: Option<usize>, current_process: Option<usize>,
freeable_kstacks: Vec<Box<[usize], &'static ASpaceMutex>>, freeable_kstacks: Vec<Box<[usize], &'static ASpaceMutex>>,
} }
@ -136,7 +136,7 @@ impl Tasking {
message_queue: SegQueue::new(), message_queue: SegQueue::new(),
sleeping: None, sleeping: None,
}); });
self.ready_to_run.push(pid); self.ready_to_run.push_back(pid);
Ok(pid) Ok(pid)
} }
@ -146,7 +146,7 @@ impl Tasking {
Some(x) => x, Some(x) => x,
None => return, 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] let current_address_space = self.processes[next_process_pid]
.address_space .address_space
.take() .take()
@ -156,7 +156,7 @@ impl Tasking {
let next_process = &self.processes[next_process_pid]; let next_process = &self.processes[next_process_pid];
gdt::set_tss_stack(next_process.kernel_esp_top); gdt::set_tss_stack(next_process.kernel_esp_top);
if self.processes[current_process].sleeping.is_none() { 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 kernel_esp = next_process.kernel_esp;
let previous_process = self.current_process.replace(next_process_pid).unwrap(); let previous_process = self.current_process.replace(next_process_pid).unwrap();
@ -171,7 +171,7 @@ impl Tasking {
} }
pub fn exit(&mut self) -> ! { 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 { if let Some(current_process) = self.current_process {
self.freeable_kstacks.push(self.processes.remove(current_process).kernel_stack); self.freeable_kstacks.push(self.processes.remove(current_process).kernel_stack);
} }
@ -224,7 +224,7 @@ impl Tasking {
pub fn wake(&mut self, pid: usize) { pub fn wake(&mut self, pid: usize) {
if self.processes[pid].sleeping.is_some() { if self.processes[pid].sleeping.is_some() {
self.processes[pid].sleeping = None; self.processes[pid].sleeping = None;
self.ready_to_run.push(pid); self.ready_to_run.push_back(pid);
} }
} }
} }