mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
kernel: services: fix corner cases on thread selection
There were a few corner cases when it came to thread selection. Namely, the same thread had a higher chance of repeatedly being executed. Clean up these corner cases, and as a result get libstd tests to pass. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
af3f89d245
commit
8875d9b4b4
1 changed files with 61 additions and 70 deletions
|
|
@ -285,9 +285,10 @@ impl core::fmt::Debug for Process {
|
|||
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
|
||||
write!(
|
||||
fmt,
|
||||
"Process {} state: {:?} Memory mapping: {:?}",
|
||||
"Process {} state: {:?} TID: {} Memory mapping: {:?}",
|
||||
self.pid.get(),
|
||||
self.state,
|
||||
self.current_thread,
|
||||
self.mapping
|
||||
)
|
||||
}
|
||||
|
|
@ -547,8 +548,7 @@ impl SystemServices {
|
|||
process.activate()?;
|
||||
|
||||
// Activate the current context
|
||||
let mut arch_process = ArchProcess::current();
|
||||
arch_process.set_tid(tid)?;
|
||||
ArchProcess::current().set_tid(tid)?;
|
||||
process.current_thread = tid;
|
||||
}
|
||||
// self.pid = pid;
|
||||
|
|
@ -587,11 +587,10 @@ impl SystemServices {
|
|||
let mut current = self
|
||||
.get_process_mut(current_pid)
|
||||
.expect("couldn't get current PID");
|
||||
assert!(arch::process::current_tid() == current.current_thread);
|
||||
// let old_state = current.state;
|
||||
current.state = match current.state {
|
||||
ProcessState::Running(x) => {
|
||||
ProcessState::Ready(x | (1 << arch::process::current_tid()))
|
||||
}
|
||||
ProcessState::Running(x) => ProcessState::Ready(x | (1 << current.current_thread)),
|
||||
y => panic!("current process was {:?}, not 'Running(_)'", y),
|
||||
};
|
||||
// log_process_update(file!(), line!(), current, old_state);
|
||||
|
|
@ -657,7 +656,8 @@ impl SystemServices {
|
|||
let process = self.get_process(pid)?;
|
||||
if let Some(tid) = tid {
|
||||
Ok(match process.state {
|
||||
ProcessState::Running(x) | ProcessState::Ready(x) if x & (1 << tid) != 0 => true,
|
||||
ProcessState::Running(x) => tid == process.current_thread || x & (1 << tid) != 0,
|
||||
ProcessState::Ready(x) if x & (1 << tid) != 0 => true,
|
||||
ProcessState::Sleeping => true,
|
||||
_ => false,
|
||||
})
|
||||
|
|
@ -785,63 +785,39 @@ impl SystemServices {
|
|||
ProcessState::Ready(0) => {
|
||||
panic!("ProcessState was `Ready(0)`, which is invalid!");
|
||||
}
|
||||
ProcessState::Ready(x) => {
|
||||
let new_thread =
|
||||
tid.unwrap_or_else(|| Self::find_next_thread(x, process.current_thread));
|
||||
if x & (1 << new_thread) == 0 {
|
||||
ProcessState::Ready(ready_threads) => {
|
||||
let new_thread = tid.unwrap_or_else(|| {
|
||||
Self::find_next_thread(ready_threads, process.current_thread)
|
||||
});
|
||||
|
||||
if ready_threads & (1 << new_thread) == 0 {
|
||||
panic!("invalid thread ID");
|
||||
}
|
||||
|
||||
process.activate()?;
|
||||
let mut p = ArchProcess::current();
|
||||
// FIXME: What happens if this fails? We're currently in the new process
|
||||
// but without a context to switch to.
|
||||
p.set_tid(new_thread)?;
|
||||
|
||||
ArchProcess::current().set_tid(new_thread)?;
|
||||
process.current_thread = new_thread as _;
|
||||
|
||||
// Remove the new context from the available context list
|
||||
ProcessState::Running(x & !(1 << new_thread))
|
||||
}
|
||||
ProcessState::Running(0) => {
|
||||
// TODO: If `context` is not `None`, what do we do here?
|
||||
|
||||
// This process is already running, and there aren't any new available
|
||||
// contexts, so keep on going.
|
||||
ProcessState::Running(0)
|
||||
ProcessState::Running(ready_threads & !(1 << new_thread))
|
||||
}
|
||||
ProcessState::Running(ready_threads) => {
|
||||
let mut p = ArchProcess::current();
|
||||
// let current_thread = p.current_thread();
|
||||
let new_thread = match tid {
|
||||
None => {
|
||||
let mut new_thread = 0;
|
||||
// Ensure we can switch back to this thread, if necessary
|
||||
let ready_threads = ready_threads | (1 << process.current_thread);
|
||||
|
||||
while ready_threads & (1 << new_thread) == 0 {
|
||||
new_thread += 1;
|
||||
if new_thread > arch::process::MAX_THREAD {
|
||||
new_thread = 0;
|
||||
}
|
||||
}
|
||||
new_thread
|
||||
}
|
||||
Some(tid) => {
|
||||
// Ensure the specified context is ready to run, or is
|
||||
// currently running.
|
||||
if ready_threads & (1 << tid) == 0 {
|
||||
return Err(xous_kernel::Error::InvalidThread);
|
||||
}
|
||||
tid
|
||||
}
|
||||
};
|
||||
let new_thread = tid.unwrap_or_else(|| {
|
||||
Self::find_next_thread(ready_threads, process.current_thread)
|
||||
});
|
||||
|
||||
// Remove the new thread ID from the list of thread IDs
|
||||
let new_mask = ready_threads & !(1 << new_thread);
|
||||
// Ensure the specified context is ready to run, or is
|
||||
// currently running.
|
||||
if ready_threads & (1 << new_thread) == 0 {
|
||||
return Err(xous_kernel::Error::InvalidThread);
|
||||
}
|
||||
|
||||
// Activate this process on this CPU
|
||||
process.activate()?;
|
||||
p.set_tid(new_thread)?;
|
||||
ArchProcess::current().set_tid(new_thread)?;
|
||||
process.current_thread = new_thread as _;
|
||||
ProcessState::Running(new_mask)
|
||||
ProcessState::Running(ready_threads & !(1 << new_thread))
|
||||
}
|
||||
};
|
||||
// log_process_update(file!(), line!(), process, old_state);
|
||||
|
|
@ -933,6 +909,11 @@ impl SystemServices {
|
|||
// Temporarily switch into the target process memory space
|
||||
// in order to pass the return value.
|
||||
let current_pid = self.current_pid();
|
||||
if current_pid == pid {
|
||||
ArchProcess::current().set_thread_result(tid, result);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
{
|
||||
let target_process = self.get_process(pid)?;
|
||||
target_process.activate()?;
|
||||
|
|
@ -983,7 +964,7 @@ impl SystemServices {
|
|||
new_tid = crate::arch::process::EXCEPTION_TID;
|
||||
// new.current_thread = new_tid;
|
||||
}
|
||||
ProcessState::Running(x) | ProcessState::Ready(x) => {
|
||||
ProcessState::Ready(x) => {
|
||||
// If no new context is specified, take the previous
|
||||
// context. If that is not runnable, do a round-robin
|
||||
// search for the next available context.
|
||||
|
|
@ -1004,6 +985,9 @@ impl SystemServices {
|
|||
}
|
||||
new.current_thread = new_tid as _;
|
||||
}
|
||||
ProcessState::Running(_) => {
|
||||
panic!("process was running even though the pid was different")
|
||||
}
|
||||
ProcessState::Sleeping
|
||||
| ProcessState::Debug(_)
|
||||
| ProcessState::BlockedException(_) => {
|
||||
|
|
@ -1053,15 +1037,20 @@ impl SystemServices {
|
|||
.get_process_mut(previous_pid)
|
||||
.expect("couldn't get previous pid");
|
||||
let _oldstate = previous.state; // for tracking state in the debug print after the following closure
|
||||
if previous.current_thread != previous_tid {
|
||||
println!(
|
||||
"WARNING: previous.current_thread {} != previous_tid {}",
|
||||
previous.current_thread, previous_tid
|
||||
);
|
||||
}
|
||||
previous.current_thread = previous_tid;
|
||||
previous.state = match previous.state {
|
||||
// If the previous process had exactly one thread that can be
|
||||
// run, then the Running thread list will be 0. In that case,
|
||||
// we will either need to Sleep this process, or mark it as
|
||||
// being Ready to run.
|
||||
ProcessState::Running(x) if x == 0 => {
|
||||
if can_resume
|
||||
/*|| advance_thread*/
|
||||
{
|
||||
if can_resume {
|
||||
ProcessState::Ready(1 << previous_tid)
|
||||
} else {
|
||||
ProcessState::Sleeping
|
||||
|
|
@ -1121,15 +1110,22 @@ impl SystemServices {
|
|||
// Transition to the new state.
|
||||
// let old_state = new.state;
|
||||
new.state = if let ProcessState::Running(x) = new.state {
|
||||
let previous_tid = new.current_thread;
|
||||
assert!(x & (1 << new.current_thread) == 0);
|
||||
|
||||
// If the current process can be resumed, add it to the list
|
||||
// of potential threads
|
||||
let x = x | if can_resume {
|
||||
1 << new.current_thread
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
// If no new thread is specified, take the previous
|
||||
// thread. If that is not runnable, do a round-robin
|
||||
// search for the next available thread.
|
||||
let new_tid = if new_tid == 0 {
|
||||
Self::find_next_thread(x, new.current_thread)
|
||||
} else {
|
||||
new_tid
|
||||
};
|
||||
if new_tid == 0 {
|
||||
new_tid = Self::find_next_thread(x, new.current_thread);
|
||||
}
|
||||
|
||||
if x & (1 << new_tid) == 0 {
|
||||
return Err(xous_kernel::Error::ThreadNotAvailable);
|
||||
|
|
@ -1137,11 +1133,8 @@ impl SystemServices {
|
|||
|
||||
new.current_thread = new_tid as _;
|
||||
|
||||
// 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 },
|
||||
)
|
||||
// Remove the new TID from the list of threads that can be run.
|
||||
ProcessState::Running(x & !(1 << new_tid))
|
||||
} else {
|
||||
panic!(
|
||||
"PID {} invalid process state (not Running): {:?}",
|
||||
|
|
@ -1151,10 +1144,8 @@ impl SystemServices {
|
|||
// log_process_update(file!(), line!(), new, old_state);
|
||||
}
|
||||
|
||||
let mut process = ArchProcess::current();
|
||||
|
||||
// Restore the previous thread, if one exists.
|
||||
process.set_tid(new_tid)?;
|
||||
ArchProcess::current().set_tid(new_tid)?;
|
||||
|
||||
klog!(
|
||||
"Activated process {}:{}, new state: {:?}",
|
||||
|
|
|
|||
Loading…
Reference in a new issue