diff --git a/kernel/context/context.rs b/kernel/context/context.rs index 89f8089..8ed1bec 100644 --- a/kernel/context/context.rs +++ b/kernel/context/context.rs @@ -5,18 +5,22 @@ use arch; use super::file::File; use super::memory::Memory; +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub enum Status { + Runnable, + Blocked, + Exited +} + /// A context, which identifies either a process or a thread #[derive(Debug)] pub struct Context { /// The ID of this context pub id: usize, - //TODO: Status enum - /// Running or not + /// Status of context + pub status: Status, + /// Context running or not pub running: bool, - /// Blocked or not - pub blocked: bool, - /// Exited or not` - pub exited: bool, /// The architecture specific context pub arch: arch::context::Context, /// Kernel stack @@ -36,9 +40,8 @@ impl Context { pub fn new(id: usize) -> Context { Context { id: id, + status: Status::Blocked, running: false, - blocked: true, - exited: false, arch: arch::context::Context::new(), kstack: None, image: Vec::new(), diff --git a/kernel/context/mod.rs b/kernel/context/mod.rs index d25ea81..7c85c7f 100644 --- a/kernel/context/mod.rs +++ b/kernel/context/mod.rs @@ -3,7 +3,7 @@ use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard}; -pub use self::context::Context; +pub use self::context::{Context, Status}; pub use self::list::ContextList; pub use self::switch::switch; @@ -38,8 +38,8 @@ pub fn init() { let mut contexts = contexts_mut(); let context_lock = contexts.new_context().expect("could not initialize first context"); let mut context = context_lock.write(); + context.status = Status::Runnable; context.running = true; - context.blocked = false; CONTEXT_ID.store(context.id, Ordering::SeqCst); } diff --git a/kernel/context/switch.rs b/kernel/context/switch.rs index db168d0..49902d2 100644 --- a/kernel/context/switch.rs +++ b/kernel/context/switch.rs @@ -1,7 +1,7 @@ use core::sync::atomic::Ordering; use arch; -use super::{contexts, Context, CONTEXT_ID}; +use super::{contexts, Context, Status, CONTEXT_ID}; /// Switch to the next context /// @@ -28,7 +28,7 @@ pub unsafe fn switch() { for (pid, context_lock) in contexts().iter() { if *pid > (*from_ptr).id { let mut context = context_lock.write(); - if ! context.running && ! context.blocked && ! context.exited { + if context.status == Status::Runnable && ! context.running { to_ptr = context.deref_mut() as *mut Context; break; } @@ -39,7 +39,7 @@ pub unsafe fn switch() { for (pid, context_lock) in contexts().iter() { if *pid < (*from_ptr).id { let mut context = context_lock.write(); - if ! context.running && ! context.blocked && ! context.exited { + if context.status == Status::Runnable && ! context.running { to_ptr = context.deref_mut() as *mut Context; break; } diff --git a/kernel/lib.rs b/kernel/lib.rs index 1e29fcd..dce4bce 100644 --- a/kernel/lib.rs +++ b/kernel/lib.rs @@ -149,7 +149,7 @@ pub extern fn kmain() { match context::contexts_mut().spawn(userspace_init) { Ok(context_lock) => { let mut context = context_lock.write(); - context.blocked = false; + context.status = context::Status::Runnable; }, Err(err) => { panic!("failed to spawn userspace_init: {:?}", err); diff --git a/kernel/syscall/process.rs b/kernel/syscall/process.rs index 5a4c83c..e8f2f46 100644 --- a/kernel/syscall/process.rs +++ b/kernel/syscall/process.rs @@ -228,7 +228,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result { context.arch.set_page_table(unsafe { new_table.address() }); - context.blocked = false; + context.status = context::Status::Runnable; } } @@ -247,7 +247,7 @@ pub fn exit(status: usize) -> ! { context.image.clear(); drop(context.heap.take()); drop(context.stack.take()); - context.exited = true; + context.status = context::Status::Exited; } unsafe { context::switch(); }