Skip to content

Commit 2a92862

Browse files
committed
Merge branch 'patch-sched-rust' of https://gitee.com/codingwars/DragonOS into patch-sched-rust
2 parents 94a454f + c33bf56 commit 2a92862

File tree

5 files changed

+17
-65
lines changed

5 files changed

+17
-65
lines changed

kernel/src/process/proc-types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct process_control_block
105105
long pid;
106106
long priority; // 优先级
107107
int64_t virtual_runtime; // 虚拟运行时间
108-
int64_t time_slice;
108+
int64_t rt_time_slice;
109109

110110
// 进程拥有的文件描述符的指针数组
111111
// todo: 改用动态指针数组

kernel/src/process/process.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ struct process_control_block *process_init_rt_pcb(struct process_control_block *
521521
// 暂时将实时进程的优先级设置为10
522522
rt_pcb->priority = 10;
523523
rt_pcb->policy = SCHED_RR;
524-
rt_pcb->time_slice = 80;
524+
rt_pcb->rt_time_slice = 80;
525525
rt_pcb->virtual_runtime = 0x7fffffffffffffff;
526526
return rt_pcb;
527527
}
@@ -568,7 +568,6 @@ ul initial_kernel_thread(ul arg)
568568
// 测试实时进程
569569

570570
// struct process_control_block *test_pcb2 = kthread_run_rt(&test, NULL, "Video refresh daemon");
571-
// // 这里创建完进程之后,打印不完整
572571
// kdebug("process:pcb2 is created!!!!");
573572
// struct process_control_block *test_pcb3 = kthread_run_rt(&test1, NULL, "Video refresh daemon");
574573
// kdebug("process:pcb3 is created!");

kernel/src/sched/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub extern "C" fn sched_update_jiffies() {
9494
__get_cfs_scheduler().timer_update_jiffies();
9595
}
9696
SCHED_FIFO | SCHED_RR => {
97-
current_pcb().time_slice -= 1;
97+
current_pcb().rt_time_slice -= 1;
9898
}
9999
_ => {
100100
todo!()

kernel/src/sched/rt.h

Lines changed: 0 additions & 44 deletions
This file was deleted.

kernel/src/sched/rt.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,21 @@ impl RTQueue {
6464
self.lock.unlock();
6565
}
6666

67-
/// @brief 将pcb从调度队列中弹出,若队列为空,则返回IDLE进程的pcb
68-
pub fn dequeue(&mut self) -> &'static mut process_control_block {
69-
let res: &'static mut process_control_block;
67+
/// @brief 将pcb从调度队列中弹出,若队列为空,则返回None
68+
pub fn dequeue(&mut self) -> Option<&'static mut process_control_block> {
69+
let res: Option<&'static mut process_control_block>;
7070
self.lock.lock();
7171
if self.queue.len() > 0 {
7272
// 队列不为空,返回下一个要执行的pcb
73-
res = self.queue.pop().unwrap();
73+
res = Some(self.queue.pop().unwrap());
7474
} else {
75-
// 如果队列为空,则返回IDLE进程的pcb
76-
res = unsafe { &mut initial_proc_union.pcb };
75+
// 如果队列为空,则返回None
76+
res=None;
7777
}
7878
self.lock.unlock();
7979
return res;
8080
}
81+
8182
}
8283

8384
/// @brief RT调度器类
@@ -108,9 +109,9 @@ impl SchedulerRT {
108109
// 这里应该是优先级数量,而不是CPU数量,需要修改
109110
for i in 0..SchedulerRT::MAX_RT_PRIO {
110111
let cpu_queue_i: &mut RTQueue = self.cpu_queue[i as usize];
111-
let proc: &'static mut process_control_block = cpu_queue_i.dequeue();
112-
if proc.policy != SCHED_NORMAL {
113-
return Some(proc);
112+
let proc: Option<&'static mut process_control_block> = cpu_queue_i.dequeue();
113+
if proc.is_some(){
114+
return proc;
114115
}
115116
}
116117
// return 一个空值
@@ -123,12 +124,10 @@ impl Scheduler for SchedulerRT {
123124
/// 请注意,进入该函数之前,需要关中断
124125
fn sched(&mut self) -> Option<&'static mut process_control_block> {
125126
current_pcb().flags &= !(PF_NEED_SCHED as u64);
126-
127+
// 正常流程下,这里一定是会pick到next的pcb的,如果是None的话,要抛出错误
127128
let proc: &'static mut process_control_block =
128129
self.pick_next_task_rt().expect("No RT process found");
129130

130-
// 若队列中无下一个进程,则返回
131-
132131
// 如果是fifo策略,则可以一直占有cpu直到有优先级更高的任务就绪(即使优先级相同也不行)或者主动放弃(等待资源)
133132
if proc.policy == SCHED_FIFO {
134133
// 如果挑选的进程优先级小于当前进程,则不进行切换
@@ -146,23 +145,21 @@ impl Scheduler for SchedulerRT {
146145
// 同等优先级的,考虑切换
147146
if proc.priority >= current_pcb().priority {
148147
// 判断这个进程时间片是否耗尽,若耗尽则将其时间片赋初值然后入队
149-
if proc.time_slice <= 0 {
150-
proc.time_slice = SchedulerRT::RR_TIMESLICE;
148+
if proc.rt_time_slice <= 0 {
149+
proc.rt_time_slice = SchedulerRT::RR_TIMESLICE;
151150
proc.flags |= !(PF_NEED_SCHED as u64);
152151
sched_enqueue(proc);
153152
}
154153
// 目标进程时间片未耗尽,切换到目标进程
155154
else {
156-
proc.time_slice -= 1;
157155
// 将当前进程加进队列
158156
sched_enqueue(current_pcb());
159157
compiler_fence(core::sync::atomic::Ordering::SeqCst);
160158
return Some(proc);
161159
}
162160
}
163-
// curr优先级更大,说明一定是实时进程,则减去消耗时间片
161+
// curr优先级更大,说明一定是实时进程,将所选进程入队列
164162
else {
165-
current_pcb().time_slice -= 1;
166163
sched_enqueue(proc);
167164
}
168165
}

0 commit comments

Comments
 (0)