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::file::File;
|
||||||
use super::memory::Memory;
|
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
|
/// A context, which identifies either a process or a thread
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
/// The ID of this context
|
/// The ID of this context
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
//TODO: Status enum
|
/// Status of context
|
||||||
/// Running or not
|
pub status: Status,
|
||||||
|
/// Context running or not
|
||||||
pub running: bool,
|
pub running: bool,
|
||||||
/// Blocked or not
|
|
||||||
pub blocked: bool,
|
|
||||||
/// Exited or not`
|
|
||||||
pub exited: bool,
|
|
||||||
/// The architecture specific context
|
/// The architecture specific context
|
||||||
pub arch: arch::context::Context,
|
pub arch: arch::context::Context,
|
||||||
/// Kernel stack
|
/// Kernel stack
|
||||||
|
@ -36,9 +40,8 @@ impl Context {
|
||||||
pub fn new(id: usize) -> Context {
|
pub fn new(id: usize) -> Context {
|
||||||
Context {
|
Context {
|
||||||
id: id,
|
id: id,
|
||||||
|
status: Status::Blocked,
|
||||||
running: false,
|
running: false,
|
||||||
blocked: true,
|
|
||||||
exited: false,
|
|
||||||
arch: arch::context::Context::new(),
|
arch: arch::context::Context::new(),
|
||||||
kstack: None,
|
kstack: None,
|
||||||
image: Vec::new(),
|
image: Vec::new(),
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
||||||
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
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::list::ContextList;
|
||||||
pub use self::switch::switch;
|
pub use self::switch::switch;
|
||||||
|
|
||||||
|
@ -38,8 +38,8 @@ pub fn init() {
|
||||||
let mut contexts = contexts_mut();
|
let mut contexts = contexts_mut();
|
||||||
let context_lock = contexts.new_context().expect("could not initialize first context");
|
let context_lock = contexts.new_context().expect("could not initialize first context");
|
||||||
let mut context = context_lock.write();
|
let mut context = context_lock.write();
|
||||||
|
context.status = Status::Runnable;
|
||||||
context.running = true;
|
context.running = true;
|
||||||
context.blocked = false;
|
|
||||||
CONTEXT_ID.store(context.id, Ordering::SeqCst);
|
CONTEXT_ID.store(context.id, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use core::sync::atomic::Ordering;
|
use core::sync::atomic::Ordering;
|
||||||
|
|
||||||
use arch;
|
use arch;
|
||||||
use super::{contexts, Context, CONTEXT_ID};
|
use super::{contexts, Context, Status, CONTEXT_ID};
|
||||||
|
|
||||||
/// Switch to the next context
|
/// Switch to the next context
|
||||||
///
|
///
|
||||||
|
@ -28,7 +28,7 @@ pub unsafe fn switch() {
|
||||||
for (pid, context_lock) in contexts().iter() {
|
for (pid, context_lock) in contexts().iter() {
|
||||||
if *pid > (*from_ptr).id {
|
if *pid > (*from_ptr).id {
|
||||||
let mut context = context_lock.write();
|
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;
|
to_ptr = context.deref_mut() as *mut Context;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ pub unsafe fn switch() {
|
||||||
for (pid, context_lock) in contexts().iter() {
|
for (pid, context_lock) in contexts().iter() {
|
||||||
if *pid < (*from_ptr).id {
|
if *pid < (*from_ptr).id {
|
||||||
let mut context = context_lock.write();
|
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;
|
to_ptr = context.deref_mut() as *mut Context;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,7 +149,7 @@ pub extern fn kmain() {
|
||||||
match context::contexts_mut().spawn(userspace_init) {
|
match context::contexts_mut().spawn(userspace_init) {
|
||||||
Ok(context_lock) => {
|
Ok(context_lock) => {
|
||||||
let mut context = context_lock.write();
|
let mut context = context_lock.write();
|
||||||
context.blocked = false;
|
context.status = context::Status::Runnable;
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
panic!("failed to spawn userspace_init: {:?}", 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.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();
|
context.image.clear();
|
||||||
drop(context.heap.take());
|
drop(context.heap.take());
|
||||||
drop(context.stack.take());
|
drop(context.stack.take());
|
||||||
context.exited = true;
|
context.status = context::Status::Exited;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { context::switch(); }
|
unsafe { context::switch(); }
|
||||||
|
|
Loading…
Reference in a new issue