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:
Sean Cross 2023-01-30 21:57:46 +08:00
parent af3f89d245
commit 8875d9b4b4

View file

@ -285,9 +285,10 @@ impl core::fmt::Debug for Process {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> { fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
write!( write!(
fmt, fmt,
"Process {} state: {:?} Memory mapping: {:?}", "Process {} state: {:?} TID: {} Memory mapping: {:?}",
self.pid.get(), self.pid.get(),
self.state, self.state,
self.current_thread,
self.mapping self.mapping
) )
} }
@ -547,8 +548,7 @@ impl SystemServices {
process.activate()?; process.activate()?;
// Activate the current context // Activate the current context
let mut arch_process = ArchProcess::current(); ArchProcess::current().set_tid(tid)?;
arch_process.set_tid(tid)?;
process.current_thread = tid; process.current_thread = tid;
} }
// self.pid = pid; // self.pid = pid;
@ -587,11 +587,10 @@ impl SystemServices {
let mut current = self let mut current = self
.get_process_mut(current_pid) .get_process_mut(current_pid)
.expect("couldn't get current PID"); .expect("couldn't get current PID");
assert!(arch::process::current_tid() == current.current_thread);
// let old_state = current.state; // let old_state = current.state;
current.state = match current.state { current.state = match current.state {
ProcessState::Running(x) => { ProcessState::Running(x) => ProcessState::Ready(x | (1 << current.current_thread)),
ProcessState::Ready(x | (1 << arch::process::current_tid()))
}
y => panic!("current process was {:?}, not 'Running(_)'", y), y => panic!("current process was {:?}, not 'Running(_)'", y),
}; };
// log_process_update(file!(), line!(), current, old_state); // log_process_update(file!(), line!(), current, old_state);
@ -657,7 +656,8 @@ impl SystemServices {
let process = self.get_process(pid)?; let process = self.get_process(pid)?;
if let Some(tid) = tid { if let Some(tid) = tid {
Ok(match process.state { 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, ProcessState::Sleeping => true,
_ => false, _ => false,
}) })
@ -785,63 +785,39 @@ impl SystemServices {
ProcessState::Ready(0) => { ProcessState::Ready(0) => {
panic!("ProcessState was `Ready(0)`, which is invalid!"); panic!("ProcessState was `Ready(0)`, which is invalid!");
} }
ProcessState::Ready(x) => { ProcessState::Ready(ready_threads) => {
let new_thread = let new_thread = tid.unwrap_or_else(|| {
tid.unwrap_or_else(|| Self::find_next_thread(x, process.current_thread)); Self::find_next_thread(ready_threads, process.current_thread)
if x & (1 << new_thread) == 0 { });
if ready_threads & (1 << new_thread) == 0 {
panic!("invalid thread ID"); panic!("invalid thread ID");
} }
process.activate()?; process.activate()?;
let mut p = ArchProcess::current();
// FIXME: What happens if this fails? We're currently in the new process ArchProcess::current().set_tid(new_thread)?;
// but without a context to switch to.
p.set_tid(new_thread)?;
process.current_thread = new_thread as _; process.current_thread = new_thread as _;
ProcessState::Running(ready_threads & !(1 << new_thread))
// 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) => { ProcessState::Running(ready_threads) => {
let mut p = ArchProcess::current(); // Ensure we can switch back to this thread, if necessary
// let current_thread = p.current_thread(); let ready_threads = ready_threads | (1 << process.current_thread);
let new_thread = match tid {
None => {
let mut new_thread = 0;
while ready_threads & (1 << new_thread) == 0 { let new_thread = tid.unwrap_or_else(|| {
new_thread += 1; Self::find_next_thread(ready_threads, process.current_thread)
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
}
};
// Remove the new thread ID from the list of thread IDs // Ensure the specified context is ready to run, or is
let new_mask = ready_threads & !(1 << new_thread); // currently running.
if ready_threads & (1 << new_thread) == 0 {
return Err(xous_kernel::Error::InvalidThread);
}
// Activate this process on this CPU // Activate this process on this CPU
process.activate()?; ArchProcess::current().set_tid(new_thread)?;
p.set_tid(new_thread)?;
process.current_thread = new_thread as _; 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); // log_process_update(file!(), line!(), process, old_state);
@ -933,6 +909,11 @@ impl SystemServices {
// Temporarily switch into the target process memory space // Temporarily switch into the target process memory space
// in order to pass the return value. // in order to pass the return value.
let current_pid = self.current_pid(); 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)?; let target_process = self.get_process(pid)?;
target_process.activate()?; target_process.activate()?;
@ -983,7 +964,7 @@ impl SystemServices {
new_tid = crate::arch::process::EXCEPTION_TID; new_tid = crate::arch::process::EXCEPTION_TID;
// new.current_thread = new_tid; // new.current_thread = new_tid;
} }
ProcessState::Running(x) | ProcessState::Ready(x) => { ProcessState::Ready(x) => {
// If no new context is specified, take the previous // If no new context is specified, take the previous
// context. If that is not runnable, do a round-robin // context. If that is not runnable, do a round-robin
// search for the next available context. // search for the next available context.
@ -1004,6 +985,9 @@ impl SystemServices {
} }
new.current_thread = new_tid as _; new.current_thread = new_tid as _;
} }
ProcessState::Running(_) => {
panic!("process was running even though the pid was different")
}
ProcessState::Sleeping ProcessState::Sleeping
| ProcessState::Debug(_) | ProcessState::Debug(_)
| ProcessState::BlockedException(_) => { | ProcessState::BlockedException(_) => {
@ -1053,15 +1037,20 @@ impl SystemServices {
.get_process_mut(previous_pid) .get_process_mut(previous_pid)
.expect("couldn't get previous pid"); .expect("couldn't get previous pid");
let _oldstate = previous.state; // for tracking state in the debug print after the following closure 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 { previous.state = match previous.state {
// If the previous process had exactly one thread that can be // If the previous process had exactly one thread that can be
// run, then the Running thread list will be 0. In that case, // run, then the Running thread list will be 0. In that case,
// we will either need to Sleep this process, or mark it as // we will either need to Sleep this process, or mark it as
// being Ready to run. // being Ready to run.
ProcessState::Running(x) if x == 0 => { ProcessState::Running(x) if x == 0 => {
if can_resume if can_resume {
/*|| advance_thread*/
{
ProcessState::Ready(1 << previous_tid) ProcessState::Ready(1 << previous_tid)
} else { } else {
ProcessState::Sleeping ProcessState::Sleeping
@ -1121,15 +1110,22 @@ impl SystemServices {
// Transition to the new state. // Transition to the new state.
// let old_state = new.state; // let old_state = new.state;
new.state = if let ProcessState::Running(x) = 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 // If no new thread is specified, take the previous
// thread. If that is not runnable, do a round-robin // thread. If that is not runnable, do a round-robin
// search for the next available thread. // search for the next available thread.
let new_tid = if new_tid == 0 { if new_tid == 0 {
Self::find_next_thread(x, new.current_thread) new_tid = Self::find_next_thread(x, new.current_thread);
} else { }
new_tid
};
if x & (1 << new_tid) == 0 { if x & (1 << new_tid) == 0 {
return Err(xous_kernel::Error::ThreadNotAvailable); return Err(xous_kernel::Error::ThreadNotAvailable);
@ -1137,11 +1133,8 @@ impl SystemServices {
new.current_thread = new_tid as _; new.current_thread = new_tid as _;
// Mark the previous TID as being runnable, and remove the new TID // Remove the new TID from the list of threads that can be run.
// from the list of threads that can be run. ProcessState::Running(x & !(1 << new_tid))
ProcessState::Running(
x & !(1 << new_tid) | if can_resume { 1 << previous_tid } else { 0 },
)
} else { } else {
panic!( panic!(
"PID {} invalid process state (not Running): {:?}", "PID {} invalid process state (not Running): {:?}",
@ -1151,10 +1144,8 @@ impl SystemServices {
// log_process_update(file!(), line!(), new, old_state); // log_process_update(file!(), line!(), new, old_state);
} }
let mut process = ArchProcess::current();
// Restore the previous thread, if one exists. // Restore the previous thread, if one exists.
process.set_tid(new_tid)?; ArchProcess::current().set_tid(new_tid)?;
klog!( klog!(
"Activated process {}:{}, new state: {:?}", "Activated process {}:{}, new state: {:?}",