1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::arch::wait_forever;
use crate::arch;
use crate::trap::usertrapret;
use crate::symbols::*;
use crate::process::{ProcInPool, PROCS_POOL, ProcessState, swtch, Register, Context, my_cpu, Process};
use crate::{info, println};
use crate::panic;
use alloc::boxed::Box;
use core::borrow::BorrowMut;
use crate::jump::*;
fn find_next_runnable_proc(from_pid: usize) -> Option<Box<Process>> {
let mut pool = PROCS_POOL.lock();
for pid in from_pid..NMAXPROCS {
let in_pool = &mut pool[pid];
let schedule_this = match in_pool {
ProcInPool::Pooling(p) => p.state == ProcessState::RUNNABLE,
_ => false
};
if schedule_this {
let p = core::mem::replace(in_pool, ProcInPool::Scheduled);
if let ProcInPool::Pooling(p) = p {
return Some(p);
}
}
}
None
}
pub fn put_back_proc(mut p: Box<Process>) {
let mut pool = PROCS_POOL.lock();
let p_in_pool = &mut pool[p.pid as usize];
p.drop_on_put_back = None;
match p_in_pool {
ProcInPool::Pooling(_) => { panic!("pid {} already occupied", p.pid); }
_ => { core::mem::replace(p_in_pool, ProcInPool::Pooling(p)); }
}
}
pub fn scheduler() -> ! {
let c = my_cpu();
let mut lst_pid = 0;
loop {
arch::intr_on();
if let Some(p) = find_next_runnable_proc(lst_pid) {
c.process = Some(p);
let p = c.process.as_mut().unwrap();
p.state = ProcessState::RUNNING;
let ctx = core::mem::replace(&mut p.context, box Context::zero());
swtch(&mut c.scheduler_context, *ctx);
let p = core::mem::replace(&mut c.process, None).unwrap();
lst_pid = p.pid as usize + 1;
if lst_pid >= NMAXPROCS {
lst_pid = 0;
}
put_back_proc(p);
} else {
lst_pid = 0;
}
}
}