Fix thread delay sleeping

This commit is contained in:
pjht 2024-11-28 21:52:52 -06:00
parent f81452f431
commit ab0c22ecc9
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E

View File

@ -2,11 +2,9 @@ use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use crate::{ use crate::{
interrupts::{self, EoiGuard}, interrupts::{self, EoiGuard},
println, println, TASKING,
tasking::SleepReason,
TASKING,
}; };
use alloc::collections::vec_deque::VecDeque; use alloc::vec::Vec;
use spin::Mutex; use spin::Mutex;
use x86_64::instructions::port::{Port, PortWriteOnly}; use x86_64::instructions::port::{Port, PortWriteOnly};
@ -17,7 +15,7 @@ const MAX_FREQ: u32 = 1_193_180;
pub static NUM_INTERRUPTS: AtomicU64 = AtomicU64::new(0); pub static NUM_INTERRUPTS: AtomicU64 = AtomicU64::new(0);
pub static THREAD_SLEEP_LIST: Mutex<VecDeque<(u64, usize, usize)>> = Mutex::new(VecDeque::new()); pub static THREAD_SLEEP_LIST: Mutex<Vec<(u64, usize, usize)>> = Mutex::new(Vec::new());
pub static CHECK_SLEEP_LIST: AtomicBool = AtomicBool::new(false); pub static CHECK_SLEEP_LIST: AtomicBool = AtomicBool::new(false);
pub fn init(mut freq: u32) { pub fn init(mut freq: u32) {
@ -61,8 +59,8 @@ fn handler(_irq: u8, eoi_guard: EoiGuard) {
let ticks = NUM_INTERRUPTS.fetch_add(1, Ordering::Relaxed) + 1; let ticks = NUM_INTERRUPTS.fetch_add(1, Ordering::Relaxed) + 1;
if CHECK_SLEEP_LIST.load(Ordering::Relaxed) { if CHECK_SLEEP_LIST.load(Ordering::Relaxed) {
let mut list = THREAD_SLEEP_LIST.lock(); let mut list = THREAD_SLEEP_LIST.lock();
while list.front().is_some_and(|x| x.0 <= ticks) { while list.last().is_some_and(|x| x.0 <= ticks) {
let entry = list.pop_front().unwrap(); let entry = list.pop().unwrap();
TASKING.force_wake(entry.1, entry.2).unwrap(); TASKING.force_wake(entry.1, entry.2).unwrap();
} }
} }
@ -74,9 +72,11 @@ fn handler(_irq: u8, eoi_guard: EoiGuard) {
pub fn thread_sleeping(pid: usize, tid: usize, duration: u64) { pub fn thread_sleeping(pid: usize, tid: usize, duration: u64) {
let end = NUM_INTERRUPTS.load(Ordering::Relaxed) + duration; let end = NUM_INTERRUPTS.load(Ordering::Relaxed) + duration;
THREAD_SLEEP_LIST.lock().push_back((end, pid, tid)); let mut list = THREAD_SLEEP_LIST.lock();
list.push((end, pid, tid));
list.sort_by(|x, y| (y.0).cmp(&x.0));
} }
pub fn cancel_sleep(pid: usize, tid: usize) { pub fn cancel_sleep(pid: usize, tid: usize) {
THREAD_SLEEP_LIST.lock().retain(|x| x.1 != pid && x.2 != tid); THREAD_SLEEP_LIST.lock().retain(|x| x.1 != pid || x.2 != tid);
} }