Collapse status of context into one status variable

This commit is contained in:
Jeremy Soller 2016-09-16 11:10:53 -06:00
parent fbbfe16764
commit 6ad843184d
5 changed files with 19 additions and 16 deletions

View file

@ -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(),

View file

@ -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);
}

View file

@ -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;
}