Collapse status of context into one status variable
This commit is contained in:
parent
fbbfe16764
commit
6ad843184d
|
@ -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(),
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -228,7 +228,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
|
|||
|
||||
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(); }
|
||||
|
|
Loading…
Reference in a new issue