Remove unnecessary context switch in waitpid

This commit is contained in:
Jeremy Soller 2016-10-20 21:50:11 -06:00
parent 38531e2ecc
commit cbac6c4b8d

View file

@ -896,33 +896,29 @@ pub fn virttophys(virtual_address: usize) -> Result<usize> {
pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> { pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> {
loop { loop {
let mut exited = false;
let waitpid;
{ {
let mut exited = false; let contexts = context::contexts();
let waitpid; let context_lock = contexts.get(pid).ok_or(Error::new(ESRCH))?;
{ let context = context_lock.read();
let contexts = context::contexts(); if let context::Status::Exited(status) = context.status {
let context_lock = contexts.get(pid).ok_or(Error::new(ESRCH))?; if status_ptr != 0 {
let context = context_lock.read(); let status_slice = validate_slice_mut(status_ptr as *mut usize, 1)?;
if let context::Status::Exited(status) = context.status { status_slice[0] = status;
if status_ptr != 0 {
let status_slice = validate_slice_mut(status_ptr as *mut usize, 1)?;
status_slice[0] = status;
}
exited = true;
} }
waitpid = context.waitpid.clone(); exited = true;
}
if exited {
let mut contexts = context::contexts_mut();
return contexts.remove(pid).ok_or(Error::new(ESRCH)).and(Ok(pid));
} else if flags & WNOHANG == WNOHANG {
return Ok(0);
} else {
waitpid.wait();
} }
waitpid = context.waitpid.clone();
} }
unsafe { context::switch(); } //TODO: Block if exited {
let mut contexts = context::contexts_mut();
return contexts.remove(pid).ok_or(Error::new(ESRCH)).and(Ok(pid));
} else if flags & WNOHANG == WNOHANG {
return Ok(0);
} else {
waitpid.wait();
}
} }
} }