From 63e6deae53e4b8558774d036417f75c9cb7392e0 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 1 Feb 2023 10:46:25 +0800 Subject: [PATCH] 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 --- kernel/src/services.rs | 24 ++++++++++++++++++++++++ kernel/src/syscall.rs | 27 +++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 7d8697e15..fc71f57b6 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -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 diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 7198e3401..c908c487c 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -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),