Add CPU ID lock

This commit is contained in:
Jeremy Soller 2016-10-23 11:24:10 -06:00
parent 9836b3cb56
commit 221b4c0116
3 changed files with 17 additions and 11 deletions

View file

@ -35,6 +35,8 @@ pub struct Context {
pub status: Status, pub status: Status,
/// Context running or not /// Context running or not
pub running: bool, pub running: bool,
/// CPU ID, if locked
pub cpuid: Option<usize>,
/// Context is halting parent /// Context is halting parent
pub vfork: bool, pub vfork: bool,
/// Context is being waited on /// Context is being waited on
@ -79,6 +81,7 @@ impl Context {
egid: 0, egid: 0,
status: Status::Blocked, status: Status::Blocked,
running: false, running: false,
cpuid: None,
vfork: false, vfork: false,
waitpid: Arc::new(WaitCondition::new()), waitpid: Arc::new(WaitCondition::new()),
wake: None, wake: None,

View file

@ -50,6 +50,7 @@ pub fn init() {
context.kfx = Some(fx); context.kfx = Some(fx);
context.status = Status::Runnable; context.status = Status::Runnable;
context.running = true; context.running = true;
context.cpuid = Some(::cpu_id());
CONTEXT_ID.store(context.id, Ordering::SeqCst); CONTEXT_ID.store(context.id, Ordering::SeqCst);
} }

View file

@ -27,20 +27,22 @@ pub unsafe fn switch() -> bool {
} }
let check_context = |context: &mut Context| -> bool { let check_context = |context: &mut Context| -> bool {
if context.status == Status::Blocked && context.wake.is_some() { if context.cpuid == None || context.cpuid == Some(::cpu_id()) {
let wake = context.wake.expect("context::switch: wake not set"); if context.status == Status::Blocked && context.wake.is_some() {
let wake = context.wake.expect("context::switch: wake not set");
let current = arch::time::monotonic(); let current = arch::time::monotonic();
if current.0 > wake.0 || (current.0 == wake.0 && current.1 >= wake.1) { if current.0 > wake.0 || (current.0 == wake.0 && current.1 >= wake.1) {
context.unblock(); context.unblock();
}
}
if context.status == Status::Runnable && ! context.running {
return true;
} }
} }
if context.status == Status::Runnable && ! context.running { false
true
} else {
false
}
}; };
for (pid, context_lock) in contexts.iter() { for (pid, context_lock) in contexts.iter() {
@ -72,7 +74,7 @@ pub unsafe fn switch() -> bool {
return false; return false;
} }
//println!("Switch {} to {}", (&*from_ptr).id, (&*to_ptr).id); // println!("{}: Switch {} to {}", ::cpu_id(), (&*from_ptr).id, (&*to_ptr).id);
(&mut *from_ptr).running = false; (&mut *from_ptr).running = false;
(&mut *to_ptr).running = true; (&mut *to_ptr).running = true;