From 6565beef4cedc5b52fe33718cfb4132188000ac6 Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Sat, 15 Jun 2024 19:59:52 +0800 Subject: [PATCH] feat: scheduler: implement schedule --- kernel/src/scheduler.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/kernel/src/scheduler.rs b/kernel/src/scheduler.rs index 97b342f..13c9b9c 100644 --- a/kernel/src/scheduler.rs +++ b/kernel/src/scheduler.rs @@ -1,6 +1,6 @@ use crate::objects::*; use core::ptr::NonNull; -use log::error; +use log::{error, trace}; use spin::lazy::Lazy; use utils::linked_list::Link; @@ -40,19 +40,18 @@ impl Scheduler { pub fn schedule(&self) { while let Some(next) = self.head.next_mut() { - // TODO: also need to check whether it is schedulable - if next.timetick() > 0 { - // Available to run, activate it + if next.timetick() > 0 && next.schedulable() { + trace!("[Scheduler] Switch to {}, tick: {}", next.tid(), next.timetick()); next.activate(); } else if next.timetick() == 0 { - // No time left, refill time tick and move to the end of the queue next.set_timetick(TIME_SLICE); } // put to the end of the queue - // todo: only move blocked and time expired threads to the end - next.link.detach(); - self.head.prepend(next); + if next.timetick() == 0 || !next.schedulable() { + next.link.detach(); + self.head.prepend(next); + } } error!("[Scheduler] No thread to schedule! Where is IDLE thread?");