kernel: services: fix sending messages within a process

This fixes the case where there is only one process active and it tries
to send messages to itself, or it yields to itself.

This closes issue #42.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-03-03 21:18:58 +08:00
parent cc4c0f3c58
commit db8e814cb7

View file

@ -942,50 +942,53 @@ impl SystemServices {
// previous_pid, previous.state, can_resume // previous_pid, previous.state, can_resume
// ); // );
} else { } else {
// if self.current_thread(previous_pid) == new_tid { let new = self.get_process_mut(new_pid)?;
// if !can_resume {
// panic!("tried to switch to our own context without resume");
// }
// return Ok(new_tid);
// }
// we can get here if we're activating a new thread in the same process // If we wanted to switch to a "new" thread, and it's the same
//return Ok(new_tid); // as the one we just switched from, do nothing.
if new.current_thread == new_tid {
let new = self.get_process_mut(new_pid)?; /* if !can_resume {
new.state = match new.state { panic!("tried to switch to our own context without resume");
ProcessState::Running(x) if (x & 1 << new_tid) == 0 => {
return Err(xous_kernel::Error::ProcessNotFound)
} }
// ProcessState::Running(x) => { return Ok(new_tid);
// if can_resume { }
// ProcessState::Running((x | (1 << previous_tid)) & !(1 << new_tid))
// } else { // Transition to the new state.
// ProcessState::Running(x | (1 << previous_tid)) new.state = if let ProcessState::Running(x) = new.state {
// } let previous_tid = new.current_thread;
// } // If no new thread is specified, take the previous
other => */ // thread. If that is not runnable, do a round-robin
// search for the next available thread.
if new_tid == 0 {
new_tid = (new.current_thread + 1) as usize;
while x & (1 << new_tid) == 0 {
new_tid += 1;
if new_tid > arch::process::MAX_THREAD {
new_tid = 0;
} else if new_tid == (new.current_thread + 1) as usize {
// If we've looped around, return an error.
return Err(xous_kernel::Error::ProcessNotFound);
}
}
new.current_thread = new_tid as _;
} else if x & (1 << new_tid) == 0 {
return Err(xous_kernel::Error::ProcessNotFound);
}
// Mark the previous TID as being runnable, and remove the new TID
// from the list of threads that can be run.
ProcessState::Running(x & !(1 << new_tid) | if can_resume { 1 << previous_tid } else { 0 })
} else {
panic!( panic!(
"PID {} invalid process state (not Running): {:?}", "PID {} invalid process state (not Running): {:?}",
previous_pid, new.state previous_pid, new.state
) /*, )
}*/ };
;
// if advance_thread {
// new.current_thread += 1;
// if new.current_thread as TID > arch::process::MAX_CONTEXT {
// new.current_thread = 0;
// }
// }
} }
// self.pid = new_pid;
let mut process = crate::arch::process::Process::current(); let mut process = crate::arch::process::Process::current();
// Restore the previous context, if one exists. // Restore the previous thread, if one exists.
process.set_thread(new_tid)?; process.set_thread(new_tid)?;
// self.processes[new_pid.get() as usize - 1].current_thread = new_tid as u8;
// let _ctx = process.current_context();
Ok(new_tid) Ok(new_tid)
} }