kernel: save current thread at context switch

Note the current thread in the original process when its quantum
expires. This will allow us to clean up after ourselves when a context
switch occurs, and will help to prevent thread starvation.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2023-02-01 10:46:25 +08:00
parent 4ec43363f5
commit 63e6deae53
2 changed files with 49 additions and 2 deletions

View file

@ -749,6 +749,30 @@ impl SystemServices {
}
}
/// Set the "current thread" of a given process. It is designed
/// to set where the next thread will run in order to avoid starving threads
/// when messages are passed around.
/// For example, if there are three threads (1, 2, 3), then there is a case where
/// thread 1 sends a message to thread 3, which yields. In this case, thread 1 will be
/// picked to run next, completely skipping thread 2.
/// Use `set_last_thread()` when a process' quantum is up in order to tell the scheduler
/// which thread to use as a reference for picking the next runnable thread.
pub fn set_last_thread(&mut self, pid: PID, tid: TID) -> Result<(), xous_kernel::Error> {
let process = self.get_process_mut(pid)?;
match process.state {
ProcessState::Ready(runnable) if runnable & (1 << tid) != 0 => {
process.current_thread = tid;
Ok(())
}
ProcessState::Ready(_) | ProcessState::Sleeping => {
Err(xous_kernel::Error::ThreadNotAvailable)
}
ProcessState::Running(_) => panic!("thread was still running"),
_ => Err(xous_kernel::Error::ProcessNotFound),
}
}
/// Mark the current process as "Ready to run".
///
/// # Panics

View file

@ -213,9 +213,22 @@ fn send_message(pid: PID, tid: TID, cid: CID, message: Message) -> SysCallResult
xous_kernel::Result::MessageEnvelope(envelope),
)
.expect("couldn't set result for server thread");
ss.activate_process_thread(tid, ppid, ptid, false)
let result = ss
.activate_process_thread(tid, ppid, ptid, false)
.map(|_| Ok(xous_kernel::Result::ResumeProcess))
.unwrap_or(Err(xous_kernel::Error::ProcessNotFound))
.unwrap_or(Err(xous_kernel::Error::ProcessNotFound));
// Keep track of which process owned the quantum. This ensures that the next
// thread in sequence gets to run when this process is activated again.
SystemServices::with_mut(|ss| {
ss.set_last_thread(
PID::new(ORIGINAL_PID.load(Relaxed)).unwrap(),
ORIGINAL_TID.load(Relaxed),
)
.ok()
});
result
} else {
// Switch to the server, since it's in a state to be run.
klog!("Activating Server context and switching away from Client");
@ -928,6 +941,16 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
crate::arch::irq::set_isr_return_pair(parent_pid, parent_ctx)
}
};
// Keep track of which process owned the quantum. This ensures that the next
// thread in sequence gets to run when this process is activated again.
SystemServices::with_mut(|ss| {
ss.set_last_thread(
PID::new(ORIGINAL_PID.load(Relaxed)).unwrap(),
ORIGINAL_TID.load(Relaxed),
)
.ok()
});
Ok(xous_kernel::Result::ResumeProcess)
}
SysCall::ReceiveMessage(sid) => receive_message(pid, tid, sid, ExecutionType::Blocking),