|
|
@ -39,23 +39,17 @@ pub struct ProcessManager {
|
|
|
|
scheduler: Mutex<Box<Scheduler>>,
|
|
|
|
scheduler: Mutex<Box<Scheduler>>,
|
|
|
|
wait_queue: Vec<Mutex<Vec<Pid>>>,
|
|
|
|
wait_queue: Vec<Mutex<Vec<Pid>>>,
|
|
|
|
event_hub: Mutex<EventHub<Event>>,
|
|
|
|
event_hub: Mutex<EventHub<Event>>,
|
|
|
|
|
|
|
|
exit_handler: fn(Pid),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ProcessManager {
|
|
|
|
impl ProcessManager {
|
|
|
|
pub fn new(scheduler: Box<Scheduler>, max_proc_num: usize) -> Self {
|
|
|
|
pub fn new(scheduler: Box<Scheduler>, max_proc_num: usize, exit_handler: fn(Pid)) -> Self {
|
|
|
|
ProcessManager {
|
|
|
|
ProcessManager {
|
|
|
|
procs: {
|
|
|
|
procs: new_vec_default(max_proc_num),
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
|
|
|
|
vec.resize_default(max_proc_num);
|
|
|
|
|
|
|
|
vec
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
scheduler: Mutex::new(scheduler),
|
|
|
|
scheduler: Mutex::new(scheduler),
|
|
|
|
wait_queue: {
|
|
|
|
wait_queue: new_vec_default(max_proc_num),
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
|
|
|
|
vec.resize_default(max_proc_num);
|
|
|
|
|
|
|
|
vec
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
event_hub: Mutex::new(EventHub::new()),
|
|
|
|
event_hub: Mutex::new(EventHub::new()),
|
|
|
|
|
|
|
|
exit_handler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -124,7 +118,7 @@ impl ProcessManager {
|
|
|
|
proc.context = Some(context);
|
|
|
|
proc.context = Some(context);
|
|
|
|
match proc.status {
|
|
|
|
match proc.status {
|
|
|
|
Status::Ready => self.scheduler.lock().insert(pid),
|
|
|
|
Status::Ready => self.scheduler.lock().insert(pid),
|
|
|
|
Status::Exited(_) => proc.context = None,
|
|
|
|
Status::Exited(_) => self.exit_handler(pid, proc),
|
|
|
|
_ => {}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -152,7 +146,7 @@ impl ProcessManager {
|
|
|
|
_ => proc.status = status,
|
|
|
|
_ => proc.status = status,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match proc.status {
|
|
|
|
match proc.status {
|
|
|
|
Status::Exited(_) => proc.context = None,
|
|
|
|
Status::Exited(_) => self.exit_handler(pid, proc),
|
|
|
|
_ => {}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -189,8 +183,18 @@ impl ProcessManager {
|
|
|
|
|
|
|
|
|
|
|
|
pub fn exit(&self, pid: Pid, code: ExitCode) {
|
|
|
|
pub fn exit(&self, pid: Pid, code: ExitCode) {
|
|
|
|
self.set_status(pid, Status::Exited(code));
|
|
|
|
self.set_status(pid, Status::Exited(code));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when a process exit
|
|
|
|
|
|
|
|
fn exit_handler(&self, pid: Pid, proc: &mut Process) {
|
|
|
|
for waiter in self.wait_queue[pid].lock().drain(..) {
|
|
|
|
for waiter in self.wait_queue[pid].lock().drain(..) {
|
|
|
|
self.wakeup(waiter);
|
|
|
|
self.wakeup(waiter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc.context = None;
|
|
|
|
|
|
|
|
(self.exit_handler)(pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_vec_default<T: Default>(size: usize) -> Vec<T> {
|
|
|
|
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
|
|
|
|
vec.resize_default(size);
|
|
|
|
|
|
|
|
vec
|
|
|
|
|
|
|
|
}
|