From 4177d0327c3eacdc606f0b22f99f208fd48cfff3 Mon Sep 17 00:00:00 2001 From: kong <45937622+kkkkkong@users.noreply.github.com> Date: Mon, 20 Feb 2023 17:03:37 +0800 Subject: [PATCH] =?UTF-8?q?RTQueue=E6=94=B9=E7=94=A8=E5=8F=8C=E5=90=91?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=AD=98=E5=82=A8(#174)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * RTQueue改用双向链表存储 --- kernel/src/sched/rt.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/kernel/src/sched/rt.rs b/kernel/src/sched/rt.rs index 4f4873f4..14917393 100644 --- a/kernel/src/sched/rt.rs +++ b/kernel/src/sched/rt.rs @@ -1,6 +1,6 @@ use core::{ptr::null_mut, sync::atomic::compiler_fence}; -use alloc::{boxed::Box, vec::Vec}; +use alloc::{boxed::Box, vec::Vec, collections::LinkedList}; use crate::{ arch::asm::current::current_pcb, @@ -37,14 +37,14 @@ pub unsafe fn sched_rt_init() { struct RTQueue { /// 队列的锁 lock: RawSpinlock, - /// 进程的队列 - queue: Vec<&'static mut process_control_block>, + /// 存储进程的双向队列 + queue: LinkedList<&'static mut process_control_block>, } impl RTQueue { pub fn new() -> RTQueue { RTQueue { - queue: Vec::new(), + queue: LinkedList::new(), lock: RawSpinlock::INIT, } } @@ -57,17 +57,17 @@ impl RTQueue { self.lock.unlock(); return; } - self.queue.push(pcb); + self.queue.push_back(pcb); self.lock.unlock(); } - /// @brief 将pcb从调度队列中弹出,若队列为空,则返回None + /// @brief 将pcb从调度队列头部取出,若队列为空,则返回None pub fn dequeue(&mut self) -> Option<&'static mut process_control_block> { let res: Option<&'static mut process_control_block>; self.lock.lock(); if self.queue.len() > 0 { // 队列不为空,返回下一个要执行的pcb - res = Some(self.queue.pop().unwrap()); + res = Some(self.queue.pop_front().unwrap()); } else { // 如果队列为空,则返回None res = None; @@ -75,6 +75,18 @@ impl RTQueue { self.lock.unlock(); return res; } + pub fn enqueue_front(&mut self, pcb: &'static mut process_control_block) { + self.lock.lock(); + + // 如果进程是IDLE进程,那么就不加入队列 + if pcb.pid == 0 { + self.lock.unlock(); + return; + } + self.queue.push_front(pcb); + self.lock.unlock(); + } + } /// @brief RT调度器类 @@ -154,9 +166,9 @@ impl Scheduler for SchedulerRT { return Some(proc); } } - // curr优先级更大,说明一定是实时进程,将所选进程入队列 + // curr优先级更大,说明一定是实时进程,将所选进程入队列,此时需要入队首 else { - sched_enqueue(proc, false); + self.cpu_queue[proc.cpu_id as usize].enqueue_front(proc); } } return None;