From 5a02273cdce3f25de6f6448b450076c592ea7d4e Mon Sep 17 00:00:00 2001 From: ChiChen <2531693734@qq.com> Date: Thu, 24 Aug 2023 15:41:46 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E8=B0=83=E6=95=B4pcb=E7=9A=84sched=5Finfo?= =?UTF-8?q?=E5=92=8Crwlock=EF=BC=8C=E4=BB=A5=E9=81=BF=E5=85=8D=E8=B0=83?= =?UTF-8?q?=E5=BA=A6=E5=99=A8=E6=AD=BB=E9=94=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/libs/rwlock.rs | 24 +++++++++- kernel/src/process/idle.rs | 3 +- kernel/src/process/mod.rs | 90 ++++++++++++++++++++++++++++++------- kernel/src/sched/core.rs | 23 +++++----- kernel/src/sched/syscall.rs | 2 +- 5 files changed, 109 insertions(+), 33 deletions(-) diff --git a/kernel/src/libs/rwlock.rs b/kernel/src/libs/rwlock.rs index e5335cefb..08274a3b5 100644 --- a/kernel/src/libs/rwlock.rs +++ b/kernel/src/libs/rwlock.rs @@ -7,7 +7,12 @@ use core::{ sync::atomic::{AtomicU32, Ordering}, }; -use crate::{process::ProcessManager, syscall::SystemError}; +use crate::{ + arch::{asm::irqflags::local_irq_save, CurrentIrqArch}, + exception::{InterruptArch, IrqFlagsGuard}, + process::ProcessManager, + syscall::SystemError, +}; ///RwLock读写锁 @@ -213,6 +218,23 @@ impl RwLock { } } + #[allow(dead_code)] + #[inline] + + + /// @brief 获取WRITER守卫并关中断 + pub fn write_irqsave(&self) -> (RwLockWriteGuard, IrqFlagsGuard) { + loop { + match self.try_write() { + Some(guard) => { + let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() }; + return (guard, irq_guard); + } + None => spin_loop(), + } + } + } + #[allow(dead_code)] #[inline] /// @brief 尝试获得UPGRADER守卫 diff --git a/kernel/src/process/idle.rs b/kernel/src/process/idle.rs index bcaac5402..b5a37c2ad 100644 --- a/kernel/src/process/idle.rs +++ b/kernel/src/process/idle.rs @@ -54,8 +54,7 @@ impl ProcessManager { }; assert!(idle_pcb.sched_info().on_cpu().is_none()); - idle_pcb.sched_info_mut().set_on_cpu(Some(i as u32)); - + idle_pcb.sched_info().set_on_cpu(Some(i as u32)); v.push(idle_pcb); } diff --git a/kernel/src/process/mod.rs b/kernel/src/process/mod.rs index 9d04430f4..2f7aa0f89 100644 --- a/kernel/src/process/mod.rs +++ b/kernel/src/process/mod.rs @@ -14,7 +14,7 @@ use alloc::{ use hashbrown::HashMap; use crate::{ - arch::{asm::current::current_pcb, process::ArchPCBInfo}, + arch::{process::ArchPCBInfo, cpu}, filesystem::vfs::{file::FileDescriptorVec, FileType}, include::bindings::bindings::CLONE_SIGNAL, kdebug, @@ -34,9 +34,12 @@ use crate::{ init::initial_kernel_thread, kthread::{KernelThreadClosure, KernelThreadCreateInfo, KernelThreadMechanism}, }, - sched::{core::CPU_EXECUTING, SchedPolicy, SchedPriority}, + sched::{ + core::{sched_enqueue, CPU_EXECUTING}, + SchedPolicy, SchedPriority, + }, smp::kick_cpu, - syscall::SystemError, + syscall::SystemError, exception::IrqFlagsGuard, }; use self::kthread::WorkerPrivate; @@ -395,8 +398,24 @@ impl ProcessControlBlock { return self.sched_info.read(); } - pub fn sched_info_mut(&self) -> RwLockWriteGuard { - return self.sched_info.write(); + /// @brief 获取对sched_info的写锁(关中断) + /// + /// # Example + /// + /// ``` + /// + /// + /// // 获取写锁和 irq_guard + /// let (sched_info,irq_guard) = pcb.sched_info_mut; + /// // do something + /// sched_info.set_state(state); + /// // 销毁读锁和guard,销毁guard时,会恢复之前的中断状态 + /// drop(sched_info) + /// drop(irq_guard); + /// + /// ``` + pub fn sched_info_mut(&self) -> (RwLockWriteGuard,IrqFlagsGuard) { + return self.sched_info.write_irqsave(); } pub fn worker_private(&self) -> SpinLockGuard> { @@ -527,10 +546,10 @@ impl ProcessBasicInfo { #[derive(Debug)] pub struct ProcessSchedulerInfo { /// 当前进程所在的cpu - on_cpu: Option, + on_cpu: AtomicIsize, /// 如果当前进程等待被迁移到另一个cpu核心上(也就是flags中的PF_NEED_MIGRATE被置位), /// 该字段存储要被迁移到的目标处理器核心号 - migrate_to: Option, + migrate_to: AtomicIsize, /// 当前进程的状态 state: ProcessState, @@ -546,9 +565,13 @@ pub struct ProcessSchedulerInfo { impl ProcessSchedulerInfo { pub fn new(on_cpu: Option) -> RwLock { + let cpu_id = match on_cpu{ + Some(cpu_id)=> cpu_id as isize, + None=>-1, + }; return RwLock::new(Self { - on_cpu, - migrate_to: None, + on_cpu: AtomicIsize::new(cpu_id), + migrate_to: AtomicIsize::new(-1), state: ProcessState::Blocked(false), sched_policy: SchedPolicy::CFS, virtual_runtime: AtomicIsize::new(0), @@ -558,26 +581,39 @@ impl ProcessSchedulerInfo { } pub fn on_cpu(&self) -> Option { - return self.on_cpu; + let on_cpu = self.on_cpu.load(Ordering::SeqCst); + if on_cpu== -1{ + return None; + }else{return Some(on_cpu as u32)} } - pub fn set_on_cpu(&mut self, on_cpu: Option) { - self.on_cpu = on_cpu; + pub fn set_on_cpu(&self, on_cpu: Option) { + if let Some(cpu_id) = on_cpu{ + self.on_cpu.store(cpu_id as isize , Ordering::SeqCst); + } + else{ + self.on_cpu.store(-1, Ordering::SeqCst); + } } pub fn migrate_to(&self) -> Option { - return self.migrate_to; + let migrate_to = self.migrate_to.load(Ordering::SeqCst); + if migrate_to==-1{ + return None; + }else{return Some(migrate_to as u32)} } - pub fn set_migrate_to(&mut self, migrate_to: Option) { - self.migrate_to = migrate_to; + pub fn set_migrate_to(&self, migrate_to: Option) { + if let Some(data) = migrate_to{ + self.migrate_to.store(data as isize, Ordering::SeqCst); + }else{self.migrate_to.store(-1, Ordering::SeqCst)} } pub fn state(&self) -> ProcessState { return self.state; } - fn set_state(&mut self, state: ProcessState) { + pub fn set_state(&mut self, state: ProcessState) { self.state = state; } @@ -711,3 +747,25 @@ impl Drop for KernelStack { pub fn process_init() { ProcessManager::init(); } + +pub fn process_wakeup(pcb: Arc) { + // c版本代码 + // BUG_ON(pcb == NULL); + // if (pcb == NULL) + // return -EINVAL; + // // 如果pcb正在调度队列中,则不重复加入调度队列 + // if (pcb->state & PROC_RUNNING) + // return 0; + + // pcb->state |= PROC_RUNNING; + // sched_enqueue_old(pcb, true); + // return 0; + + if pcb.sched_info().state() != ProcessState::Runnable { + let (mut sched_info,irq_guard) = pcb.sched_info_mut(); + sched_info.set_state(ProcessState::Runnable); + drop(sched_info); + drop(irq_guard); + sched_enqueue(pcb, true); + } +} diff --git a/kernel/src/sched/core.rs b/kernel/src/sched/core.rs index ebb2696ab..8aac10683 100644 --- a/kernel/src/sched/core.rs +++ b/kernel/src/sched/core.rs @@ -3,15 +3,13 @@ use core::sync::atomic::compiler_fence; use alloc::{sync::Arc, vec::Vec}; use crate::{ - arch::{asm::current::current_pcb, cpu::current_cpu_id}, + arch:: cpu::current_cpu_id, include::bindings::bindings::smp_get_total_cpu, - include::bindings::bindings::{ - process_control_block, MAX_CPU_NUM, PF_NEED_MIGRATE, SCHED_FIFO, SCHED_NORMAL, SCHED_RR, - }, + include::bindings::bindings::process_control_block, + kinfo, mm::percpu::PerCpu, - process::{AtomicPid, Pid, ProcessControlBlock, ProcessFlags, ProcessManager}, - syscall::SystemError, + process::{AtomicPid, Pid, ProcessControlBlock, ProcessFlags, ProcessManager, ProcessState}, }; use super::rt::{sched_rt_init, SchedulerRT, __get_rt_scheduler}; @@ -67,7 +65,7 @@ pub fn loads_balance(pcb: Arc) { && !pcb.flags().contains(ProcessFlags::NEED_MIGRATE)) { pcb.flags().insert(ProcessFlags::NEED_MIGRATE); - pcb.sched_info_mut().set_migrate_to(Some(min_loads_cpu_id)); + pcb.sched_info().set_migrate_to(Some(min_loads_cpu_id)); // kdebug!("set migrating, pcb:{:?}", pcb); } } @@ -119,11 +117,10 @@ pub extern "C" fn sched_enqueue_old(pcb: &'static mut process_control_block, mut /// @param reset_time 是否重置虚拟运行时间 pub fn sched_enqueue(pcb: Arc, mut reset_time: bool) { compiler_fence(core::sync::atomic::Ordering::SeqCst); - - // 调度器不处理running位为0的进程,pcb重构后处理? - // if pcb.state & (PROC_RUNNING as u64) == 0 { - // return; - // } + if pcb.sched_info().state() != ProcessState::Runnable + { + return; + } let cfs_scheduler = __get_cfs_scheduler(); let rt_scheduler = __get_rt_scheduler(); @@ -135,7 +132,7 @@ pub fn sched_enqueue(pcb: Arc, mut reset_time: bool) { if pcb.flags().contains(ProcessFlags::NEED_MIGRATE) { // kdebug!("migrating pcb:{:?}", pcb); pcb.flags().remove(ProcessFlags::NEED_MIGRATE); - pcb.sched_info_mut() + pcb.sched_info() .set_on_cpu(pcb.sched_info().migrate_to()); reset_time = true; } diff --git a/kernel/src/sched/syscall.rs b/kernel/src/sched/syscall.rs index 36285f670..c19be8d04 100644 --- a/kernel/src/sched/syscall.rs +++ b/kernel/src/sched/syscall.rs @@ -1,5 +1,5 @@ use crate::{ - arch::{asm::current::current_pcb, context::switch_process, CurrentIrqArch}, + arch::CurrentIrqArch, exception::InterruptArch, process::ProcessManager, syscall::{Syscall, SystemError}, From dd0bf6e96d8f27c017876f0c1a6d7f565ddc3c4e Mon Sep 17 00:00:00 2001 From: ChiChen <2531693734@qq.com> Date: Thu, 24 Aug 2023 18:04:18 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E5=9C=A8=20Writ?= =?UTF-8?q?erGuard=20=E4=B8=AD=E7=BB=B4=E6=8A=A4=20Irq=5Fguard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/libs/rwlock.rs | 21 ++++++----- kernel/src/process/mod.rs | 76 +++++++++++++++++---------------------- kernel/src/sched/core.rs | 11 +++--- 3 files changed, 50 insertions(+), 58 deletions(-) diff --git a/kernel/src/libs/rwlock.rs b/kernel/src/libs/rwlock.rs index 08274a3b5..0086f7b25 100644 --- a/kernel/src/libs/rwlock.rs +++ b/kernel/src/libs/rwlock.rs @@ -8,7 +8,7 @@ use core::{ }; use crate::{ - arch::{asm::irqflags::local_irq_save, CurrentIrqArch}, + arch::CurrentIrqArch, exception::{InterruptArch, IrqFlagsGuard}, process::ProcessManager, syscall::SystemError, @@ -61,6 +61,7 @@ pub struct RwLockUpgradableGuard<'a, T: 'a> { pub struct RwLockWriteGuard<'a, T: 'a> { data: *mut T, inner: &'a RwLock, + irq_guard: Option, } unsafe impl Send for RwLock {} @@ -200,6 +201,7 @@ impl RwLock { return Some(RwLockWriteGuard { data: unsafe { &mut *self.data.get() }, inner: self, + irq_guard: None, }); } else { return None; @@ -220,15 +222,14 @@ impl RwLock { #[allow(dead_code)] #[inline] - - /// @brief 获取WRITER守卫并关中断 - pub fn write_irqsave(&self) -> (RwLockWriteGuard, IrqFlagsGuard) { + pub fn write_irqsave(&self) -> RwLockWriteGuard { + let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() }; loop { match self.try_write() { - Some(guard) => { - let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() }; - return (guard, irq_guard); + Some(mut guard) => { + guard.irq_guard = Some(irq_guard); + return guard; } None => spin_loop(), } @@ -348,6 +349,7 @@ impl<'rwlock, T> RwLockUpgradableGuard<'rwlock, T> { Ok(RwLockWriteGuard { data: unsafe { &mut *inner.data.get() }, inner, + irq_guard: None, }) } else { Err(self) @@ -518,7 +520,10 @@ impl<'rwlock, T> Drop for RwLockWriteGuard<'rwlock, T> { self.inner .lock .fetch_and(!(WRITER | UPGRADED), Ordering::Release); - + if let Some(_) = self.irq_guard { + // 自动 drop 包含的 irq_guard,恢复中断 + self.irq_guard = None; + } ProcessManager::current_pcb().preempt_enable(); } } diff --git a/kernel/src/process/mod.rs b/kernel/src/process/mod.rs index 2f7aa0f89..4c16561a8 100644 --- a/kernel/src/process/mod.rs +++ b/kernel/src/process/mod.rs @@ -2,8 +2,7 @@ use core::{ hash::{Hash, Hasher}, intrinsics::unlikely, mem::ManuallyDrop, - ptr::null_mut, - sync::atomic::{compiler_fence, AtomicBool, AtomicIsize, AtomicUsize, Ordering}, + sync::atomic::{compiler_fence, AtomicBool, AtomicI32, AtomicIsize, AtomicUsize, Ordering}, }; use alloc::{ @@ -14,7 +13,7 @@ use alloc::{ use hashbrown::HashMap; use crate::{ - arch::{process::ArchPCBInfo, cpu}, + arch::{cpu, process::ArchPCBInfo}, filesystem::vfs::{file::FileDescriptorVec, FileType}, include::bindings::bindings::CLONE_SIGNAL, kdebug, @@ -39,7 +38,7 @@ use crate::{ SchedPolicy, SchedPriority, }, smp::kick_cpu, - syscall::SystemError, exception::IrqFlagsGuard, + syscall::SystemError, }; use self::kthread::WorkerPrivate; @@ -398,23 +397,11 @@ impl ProcessControlBlock { return self.sched_info.read(); } - /// @brief 获取对sched_info的写锁(关中断) - /// - /// # Example - /// - /// ``` - /// - /// - /// // 获取写锁和 irq_guard - /// let (sched_info,irq_guard) = pcb.sched_info_mut; - /// // do something - /// sched_info.set_state(state); - /// // 销毁读锁和guard,销毁guard时,会恢复之前的中断状态 - /// drop(sched_info) - /// drop(irq_guard); - /// - /// ``` - pub fn sched_info_mut(&self) -> (RwLockWriteGuard,IrqFlagsGuard) { + pub fn sched_info_mut(&self) -> RwLockWriteGuard { + return self.sched_info.write(); + } + + pub fn sched_info_mut_irqsave(&self) -> RwLockWriteGuard { return self.sched_info.write_irqsave(); } @@ -546,10 +533,10 @@ impl ProcessBasicInfo { #[derive(Debug)] pub struct ProcessSchedulerInfo { /// 当前进程所在的cpu - on_cpu: AtomicIsize, + on_cpu: AtomicI32, /// 如果当前进程等待被迁移到另一个cpu核心上(也就是flags中的PF_NEED_MIGRATE被置位), /// 该字段存储要被迁移到的目标处理器核心号 - migrate_to: AtomicIsize, + migrate_to: AtomicI32, /// 当前进程的状态 state: ProcessState, @@ -565,13 +552,13 @@ pub struct ProcessSchedulerInfo { impl ProcessSchedulerInfo { pub fn new(on_cpu: Option) -> RwLock { - let cpu_id = match on_cpu{ - Some(cpu_id)=> cpu_id as isize, - None=>-1, + let cpu_id = match on_cpu { + Some(cpu_id) => cpu_id as i32, + None => -1, }; return RwLock::new(Self { - on_cpu: AtomicIsize::new(cpu_id), - migrate_to: AtomicIsize::new(-1), + on_cpu: AtomicI32::new(cpu_id), + migrate_to: AtomicI32::new(-1), state: ProcessState::Blocked(false), sched_policy: SchedPolicy::CFS, virtual_runtime: AtomicIsize::new(0), @@ -582,31 +569,36 @@ impl ProcessSchedulerInfo { pub fn on_cpu(&self) -> Option { let on_cpu = self.on_cpu.load(Ordering::SeqCst); - if on_cpu== -1{ + if on_cpu == -1 { return None; - }else{return Some(on_cpu as u32)} + } else { + return Some(on_cpu as u32); + } } pub fn set_on_cpu(&self, on_cpu: Option) { - if let Some(cpu_id) = on_cpu{ - self.on_cpu.store(cpu_id as isize , Ordering::SeqCst); - } - else{ + if let Some(cpu_id) = on_cpu { + self.on_cpu.store(cpu_id as i32, Ordering::SeqCst); + } else { self.on_cpu.store(-1, Ordering::SeqCst); } } pub fn migrate_to(&self) -> Option { let migrate_to = self.migrate_to.load(Ordering::SeqCst); - if migrate_to==-1{ + if migrate_to == -1 { return None; - }else{return Some(migrate_to as u32)} + } else { + return Some(migrate_to as u32); + } } pub fn set_migrate_to(&self, migrate_to: Option) { - if let Some(data) = migrate_to{ - self.migrate_to.store(data as isize, Ordering::SeqCst); - }else{self.migrate_to.store(-1, Ordering::SeqCst)} + if let Some(data) = migrate_to { + self.migrate_to.store(data as i32, Ordering::SeqCst); + } else { + self.migrate_to.store(-1, Ordering::SeqCst) + } } pub fn state(&self) -> ProcessState { @@ -762,10 +754,8 @@ pub fn process_wakeup(pcb: Arc) { // return 0; if pcb.sched_info().state() != ProcessState::Runnable { - let (mut sched_info,irq_guard) = pcb.sched_info_mut(); - sched_info.set_state(ProcessState::Runnable); - drop(sched_info); - drop(irq_guard); + pcb.sched_info_mut_irqsave() + .set_state(ProcessState::Runnable); sched_enqueue(pcb, true); } } diff --git a/kernel/src/sched/core.rs b/kernel/src/sched/core.rs index 8aac10683..ae0416bf0 100644 --- a/kernel/src/sched/core.rs +++ b/kernel/src/sched/core.rs @@ -3,10 +3,9 @@ use core::sync::atomic::compiler_fence; use alloc::{sync::Arc, vec::Vec}; use crate::{ - arch:: cpu::current_cpu_id, - include::bindings::bindings::smp_get_total_cpu, + arch::cpu::current_cpu_id, include::bindings::bindings::process_control_block, - + include::bindings::bindings::smp_get_total_cpu, kinfo, mm::percpu::PerCpu, process::{AtomicPid, Pid, ProcessControlBlock, ProcessFlags, ProcessManager, ProcessState}, @@ -117,8 +116,7 @@ pub extern "C" fn sched_enqueue_old(pcb: &'static mut process_control_block, mut /// @param reset_time 是否重置虚拟运行时间 pub fn sched_enqueue(pcb: Arc, mut reset_time: bool) { compiler_fence(core::sync::atomic::Ordering::SeqCst); - if pcb.sched_info().state() != ProcessState::Runnable - { + if pcb.sched_info().state() != ProcessState::Runnable { return; } let cfs_scheduler = __get_cfs_scheduler(); @@ -132,8 +130,7 @@ pub fn sched_enqueue(pcb: Arc, mut reset_time: bool) { if pcb.flags().contains(ProcessFlags::NEED_MIGRATE) { // kdebug!("migrating pcb:{:?}", pcb); pcb.flags().remove(ProcessFlags::NEED_MIGRATE); - pcb.sched_info() - .set_on_cpu(pcb.sched_info().migrate_to()); + pcb.sched_info().set_on_cpu(pcb.sched_info().migrate_to()); reset_time = true; } From 0ae8dfcc2d1a1cd63e0ed19b7974d276b2346253 Mon Sep 17 00:00:00 2001 From: ChiChen <2531693734@qq.com> Date: Thu, 24 Aug 2023 21:35:29 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86=20write=5Firqsa?= =?UTF-8?q?ve=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/libs/rwlock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/src/libs/rwlock.rs b/kernel/src/libs/rwlock.rs index 0086f7b25..d6276a977 100644 --- a/kernel/src/libs/rwlock.rs +++ b/kernel/src/libs/rwlock.rs @@ -224,8 +224,8 @@ impl RwLock { #[inline] /// @brief 获取WRITER守卫并关中断 pub fn write_irqsave(&self) -> RwLockWriteGuard { - let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() }; loop { + let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() }; match self.try_write() { Some(mut guard) => { guard.irq_guard = Some(irq_guard); From 24146851352c5f73c89154328d0fbc9b0cf796d1 Mon Sep 17 00:00:00 2001 From: ChiChen <2531693734@qq.com> Date: Thu, 24 Aug 2023 21:46:45 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/libs/rwlock.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kernel/src/libs/rwlock.rs b/kernel/src/libs/rwlock.rs index d6276a977..ad3edd32e 100644 --- a/kernel/src/libs/rwlock.rs +++ b/kernel/src/libs/rwlock.rs @@ -520,10 +520,7 @@ impl<'rwlock, T> Drop for RwLockWriteGuard<'rwlock, T> { self.inner .lock .fetch_and(!(WRITER | UPGRADED), Ordering::Release); - if let Some(_) = self.irq_guard { - // 自动 drop 包含的 irq_guard,恢复中断 - self.irq_guard = None; - } + self.irq_guard.take(); ProcessManager::current_pcb().preempt_enable(); } } From 7a9209b08b573d41135addbfaef8d2d5b25c37bd Mon Sep 17 00:00:00 2001 From: ChiChen <2531693734@qq.com> Date: Thu, 24 Aug 2023 22:04:35 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=8A=8A=20set=20state=20=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E4=BB=8E=20wakup=20=E7=A7=BB=E5=8A=A8=E5=88=B0=20sche?= =?UTF-8?q?d=5Fenqueue=20=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/sched/core.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/src/sched/core.rs b/kernel/src/sched/core.rs index ae0416bf0..8ae589852 100644 --- a/kernel/src/sched/core.rs +++ b/kernel/src/sched/core.rs @@ -116,8 +116,10 @@ pub extern "C" fn sched_enqueue_old(pcb: &'static mut process_control_block, mut /// @param reset_time 是否重置虚拟运行时间 pub fn sched_enqueue(pcb: Arc, mut reset_time: bool) { compiler_fence(core::sync::atomic::Ordering::SeqCst); - if pcb.sched_info().state() != ProcessState::Runnable { - return; + + let mut writer = pcb.sched_info_mut(); + if writer.state() != ProcessState::Runnable { + writer.set_state(ProcessState::Runnable); } let cfs_scheduler = __get_cfs_scheduler(); let rt_scheduler = __get_rt_scheduler(); From 3ad49cf7db7b4b45226e2c298cbfc56380794b24 Mon Sep 17 00:00:00 2001 From: ChiChen <2531693734@qq.com> Date: Thu, 24 Aug 2023 23:25:19 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=B8=BA=E5=9C=A8=20wake?= =?UTF-8?q?up=20=E4=B8=AD=E8=AE=BE=E7=BD=AE=20running=20=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E4=BF=9D=E7=95=99=20set=5Fstate=20=E7=9A=84=E7=A7=81=E6=9C=89?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/process/mod.rs | 10 +++++++--- kernel/src/sched/core.rs | 7 ++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/kernel/src/process/mod.rs b/kernel/src/process/mod.rs index ad567f661..0833b1e2c 100644 --- a/kernel/src/process/mod.rs +++ b/kernel/src/process/mod.rs @@ -156,8 +156,12 @@ impl ProcessManager { /// 唤醒一个进程 pub fn wakeup(pcb: &Arc) -> Result<(), SystemError> { if pcb.sched_info().state() != ProcessState::Runnable { - sched_enqueue(pcb.clone(), true); - return Ok(()); + let mut writer = pcb.sched_info_mut(); + if writer.state() != ProcessState::Runnable { + writer.set_state(ProcessState::Runnable); + sched_enqueue(pcb.clone(), true); + return Ok(()); + } } return Err(SystemError::EAGAIN_OR_EWOULDBLOCK); } @@ -609,7 +613,7 @@ impl ProcessSchedulerInfo { return self.state; } - pub fn set_state(&mut self, state: ProcessState) { + fn set_state(&mut self, state: ProcessState) { self.state = state; } diff --git a/kernel/src/sched/core.rs b/kernel/src/sched/core.rs index 8ae589852..a4be5851d 100644 --- a/kernel/src/sched/core.rs +++ b/kernel/src/sched/core.rs @@ -116,14 +116,11 @@ pub extern "C" fn sched_enqueue_old(pcb: &'static mut process_control_block, mut /// @param reset_time 是否重置虚拟运行时间 pub fn sched_enqueue(pcb: Arc, mut reset_time: bool) { compiler_fence(core::sync::atomic::Ordering::SeqCst); - - let mut writer = pcb.sched_info_mut(); - if writer.state() != ProcessState::Runnable { - writer.set_state(ProcessState::Runnable); + if pcb.sched_info().state() != ProcessState::Runnable { + return; } let cfs_scheduler = __get_cfs_scheduler(); let rt_scheduler = __get_rt_scheduler(); - // 除了IDLE以外的进程,都进行负载均衡 if pcb.basic().pid().into() > 0 { loads_balance(pcb.clone()); From ac2fd94215eac6f5965f209b3ec07c40b8bf1a05 Mon Sep 17 00:00:00 2001 From: ChiChen <2531693734@qq.com> Date: Thu, 24 Aug 2023 23:54:37 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=BA=86=20process=5Fwak?= =?UTF-8?q?eup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/process/mod.rs | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/kernel/src/process/mod.rs b/kernel/src/process/mod.rs index 0833b1e2c..812272105 100644 --- a/kernel/src/process/mod.rs +++ b/kernel/src/process/mod.rs @@ -747,23 +747,3 @@ impl Drop for KernelStack { pub fn process_init() { ProcessManager::init(); } - -pub fn process_wakeup(pcb: Arc) { - // c版本代码 - // BUG_ON(pcb == NULL); - // if (pcb == NULL) - // return -EINVAL; - // // 如果pcb正在调度队列中,则不重复加入调度队列 - // if (pcb->state & PROC_RUNNING) - // return 0; - - // pcb->state |= PROC_RUNNING; - // sched_enqueue_old(pcb, true); - // return 0; - - if pcb.sched_info().state() != ProcessState::Runnable { - pcb.sched_info_mut_irqsave() - .set_state(ProcessState::Runnable); - sched_enqueue(pcb, true); - } -}