1- //! Implementation of [`Processor`] and Intersection of control flow
1+ //!Implementation of [`Processor`] and Intersection of control flow
22//!
33//! Here, the continuous operation of user apps in CPU is maintained,
44//! the current running state of CPU is recorded,
@@ -10,9 +10,12 @@ use super::{TaskContext, TaskControlBlock};
1010use crate :: sync:: UPSafeCell ;
1111use crate :: trap:: TrapContext ;
1212use alloc:: sync:: Arc ;
13+ use core:: cell:: RefMut ;
1314use lazy_static:: * ;
15+ use super :: task:: TaskControlBlockInner ;
1416
1517/// Processor management structure
18+ /// 只记录当前任务block与空闲状态下的任务上下文
1619pub struct Processor {
1720 ///The task currently executing on the current processor
1821 current : Option < Arc < TaskControlBlock > > ,
@@ -42,24 +45,31 @@ impl Processor {
4245
4346 ///Get current task in cloning semanteme
4447 pub fn current ( & self ) -> Option < Arc < TaskControlBlock > > {
48+ //map方法传入Option<T>,对T进行处理(闭包),并返回新的Option<T'>
4549 self . current . as_ref ( ) . map ( Arc :: clone)
4650 }
51+ ///获取当前task的访问权,但不获取其所有权
52+ pub fn _current_exclusive_access ( & self ) -> Option < RefMut < ' _ , TaskControlBlockInner > > {
53+ self . current . as_ref ( ) . map ( |task| task. inner_exclusive_access ( ) )
54+ }
4755}
4856
4957lazy_static ! {
50- /// Global processor instance.
58+ ///The global unique processor instance
5159 pub static ref PROCESSOR : UPSafeCell <Processor > = unsafe { UPSafeCell :: new( Processor :: new( ) ) } ;
5260}
5361
5462///The main part of process execution and scheduling
5563///Loop `fetch_task` to get the process that needs to run, and switch the process through `__switch`
5664pub fn run_tasks ( ) {
65+ //注意,这里是一个循环
5766 loop {
5867 let mut processor = PROCESSOR . exclusive_access ( ) ;
5968 if let Some ( task) = fetch_task ( ) {
6069 let idle_task_cx_ptr = processor. get_idle_task_cx_ptr ( ) ;
6170 // access coming task TCB exclusively
6271 let mut task_inner = task. inner_exclusive_access ( ) ;
72+ // 这玩意保存在内核堆区
6373 let next_task_cx_ptr = & task_inner. task_cx as * const TaskContext ;
6474 task_inner. task_status = TaskStatus :: Running ;
6575 // release coming task_inner manually
@@ -69,8 +79,12 @@ pub fn run_tasks() {
6979 // release processor manually
7080 drop ( processor) ;
7181 unsafe {
82+ // 调用前,a寄存器和t寄存器已经被保存好(调用者保存)
83+ // __switch会保存s寄存器
7284 __switch ( idle_task_cx_ptr, next_task_cx_ptr) ;
73- }
85+ }
86+ // 一段时间后后可能会返回到这里(切回空闲状态后)
87+ // 进入下一个循环
7488 } else {
7589 warn ! ( "no tasks available in run_tasks" ) ;
7690 }
@@ -93,15 +107,16 @@ pub fn current_user_token() -> usize {
93107 task. get_user_token ( )
94108}
95109
96- /// Get the mutable reference to trap context of current task
110+ ///Get the mutable reference to trap context of current task
97111pub fn current_trap_cx ( ) -> & ' static mut TrapContext {
98112 current_task ( )
99113 . unwrap ( )
100114 . inner_exclusive_access ( )
101115 . get_trap_cx ( )
102116}
103117
104- /// Return to idle control flow for new scheduling
118+ ///Return to idle control flow for new scheduling
119+ ///保存当前任务到传入参数地址(这时还是用户态),切换到空闲状态
105120pub fn schedule ( switched_task_cx_ptr : * mut TaskContext ) {
106121 let mut processor = PROCESSOR . exclusive_access ( ) ;
107122 let idle_task_cx_ptr = processor. get_idle_task_cx_ptr ( ) ;
0 commit comments