From c30f4208d47778d79210b1f8827c204a6df00a30 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 10 Sep 2016 19:18:59 -0600 Subject: [PATCH] Implement sched_yield, enable interrupts in userspace --- context/mod.rs | 11 ++++++----- syscall/mod.rs | 4 ++++ syscall/process.rs | 5 +++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/context/mod.rs b/context/mod.rs index e550b54..c0911cf 100644 --- a/context/mod.rs +++ b/context/mod.rs @@ -127,12 +127,11 @@ pub unsafe fn switch() { arch::interrupt::pause(); } - let from_ptr = if let Some(context_lock) = contexts().current() { + let from_ptr = { + let contexts = contexts(); + let context_lock = contexts.current().expect("context::switch: Not inside of context"); let mut context = context_lock.write(); context.deref_mut() as *mut Context - } else { - print!("NO FROM_PTR\n"); - return; }; let mut to_ptr = 0 as *mut Context; @@ -146,7 +145,9 @@ pub unsafe fn switch() { } if to_ptr as usize == 0 { - print!("NO TO_PTR\n"); + // TODO: Sleep, wait for interrupt + // Unset global lock if no context found + arch::context::CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst); return; } diff --git a/syscall/mod.rs b/syscall/mod.rs index dc3b387..6696ffd 100644 --- a/syscall/mod.rs +++ b/syscall/mod.rs @@ -30,6 +30,8 @@ pub enum Call { Exec = 11, /// Get process ID GetPid = 20, + /// Yield to scheduler + SchedYield = 158 } /// Convert numbers to calls @@ -44,6 +46,7 @@ impl Call { 6 => Ok(Call::Close), 11 => Ok(Call::Exec), 20 => Ok(Call::GetPid), + 158 => Ok(Call::SchedYield), _ => Err(Error::NoCall) } } @@ -97,6 +100,7 @@ pub fn handle(a: usize, b: usize, c: usize, d: usize, e: usize, _f: usize) -> Re Call::Close => close(b), Call::Exec => exec(convert_slice(b as *const u8, c)?, convert_slice(d as *const [usize; 2], e)?), Call::GetPid => getpid(), + Call::SchedYield => sched_yield() } } diff --git a/syscall/process.rs b/syscall/process.rs index 9db0bfa..0149aed 100644 --- a/syscall/process.rs +++ b/syscall/process.rs @@ -28,3 +28,8 @@ pub fn getpid() -> Result { let context = context_lock.read(); Ok(context.id) } + +pub fn sched_yield() -> Result { + unsafe { context::switch(); } + Ok(0) +}