From cbac6c4b8da0345517c93b6219004852e1ec8642 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 20 Oct 2016 21:50:11 -0600 Subject: [PATCH] Remove unnecessary context switch in waitpid --- kernel/syscall/process.rs | 42 ++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/kernel/syscall/process.rs b/kernel/syscall/process.rs index ddd2cec..5d35af6 100644 --- a/kernel/syscall/process.rs +++ b/kernel/syscall/process.rs @@ -896,33 +896,29 @@ pub fn virttophys(virtual_address: usize) -> Result { pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result { loop { + let mut exited = false; + let waitpid; { - let mut exited = false; - let waitpid; - { - let contexts = context::contexts(); - let context_lock = contexts.get(pid).ok_or(Error::new(ESRCH))?; - let context = context_lock.read(); - if let context::Status::Exited(status) = context.status { - if status_ptr != 0 { - let status_slice = validate_slice_mut(status_ptr as *mut usize, 1)?; - status_slice[0] = status; - } - exited = true; + let contexts = context::contexts(); + let context_lock = contexts.get(pid).ok_or(Error::new(ESRCH))?; + let context = context_lock.read(); + if let context::Status::Exited(status) = context.status { + if status_ptr != 0 { + let status_slice = validate_slice_mut(status_ptr as *mut usize, 1)?; + status_slice[0] = status; } - waitpid = context.waitpid.clone(); - } - - 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(); + exited = true; } + 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(); + } } }