mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
kernel: fix thread termination and waiting
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
18a86a9a19
commit
be6e0a9895
4 changed files with 42 additions and 17 deletions
|
|
@ -154,7 +154,9 @@ pub extern "C" fn trap_handler(
|
|||
|
||||
// This address indicates a thread has exited. Destroy the thread.
|
||||
// This activates another thread within this process.
|
||||
SystemServices::with_mut(|ss| ss.destroy_thread(pid, tid)).unwrap();
|
||||
if SystemServices::with_mut(|ss| ss.destroy_thread(pid, tid)).unwrap() {
|
||||
crate::syscall::reset_switchto_caller();
|
||||
}
|
||||
|
||||
// Resume the new thread within the same process.
|
||||
ArchProcess::with_current_mut(|p| {
|
||||
|
|
|
|||
|
|
@ -58,9 +58,12 @@ struct ProcessImpl {
|
|||
/// Global parameters used by the operating system
|
||||
pub inner: ProcessInner,
|
||||
|
||||
/// The last thread ID that was allocated
|
||||
last_tid_allocated: u8,
|
||||
|
||||
/// Pad everything to 128 bytes, so the Thread slice starts at
|
||||
/// offset 128.
|
||||
_padding: [u32; 14],
|
||||
_padding: [u32; 13],
|
||||
|
||||
/// This enables the kernel to keep track of threads in the
|
||||
/// target process, and know which threads are ready to
|
||||
|
|
@ -224,9 +227,18 @@ impl Process {
|
|||
|
||||
pub fn find_free_thread(&self) -> Option<TID> {
|
||||
let process = unsafe { &mut *PROCESS };
|
||||
for (index, thread) in process.threads.iter().enumerate() {
|
||||
if index != IRQ_TID && thread.sepc == 0 {
|
||||
return Some(index as TID);
|
||||
let start_tid = process.last_tid_allocated as usize;
|
||||
let a = &process.threads[start_tid..process.threads.len()];
|
||||
let b = &process.threads[0..start_tid];
|
||||
for (index, thread) in a.iter().chain(b.iter()).enumerate() {
|
||||
let mut tid = index + start_tid;
|
||||
if tid >= process.threads.len() {
|
||||
tid -= process.threads.len()
|
||||
}
|
||||
|
||||
if tid != IRQ_TID && thread.sepc == 0 {
|
||||
process.last_tid_allocated = tid as _;
|
||||
return Some(tid as TID);
|
||||
}
|
||||
}
|
||||
None
|
||||
|
|
@ -508,7 +520,6 @@ impl Thread {
|
|||
pub fn a1(&self) -> usize {
|
||||
self.registers[10]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn set_current_pid(pid: PID) {
|
||||
|
|
|
|||
|
|
@ -1409,13 +1409,12 @@ impl SystemServices {
|
|||
Ok(new_tid)
|
||||
}
|
||||
|
||||
/// Destroy the given thread.
|
||||
/// NOTE: You MUST immediately switch away from this process.
|
||||
/// Destroy the given thread. Returns `true` if the PID has been updated.
|
||||
/// # Errors
|
||||
///
|
||||
/// * **ThreadNotAvailable**: The thread does not exist in this process
|
||||
#[cfg(baremetal)]
|
||||
pub fn destroy_thread(&mut self, pid: PID, tid: TID) -> Result<usize, xous_kernel::Error> {
|
||||
pub fn destroy_thread(&mut self, pid: PID, tid: TID) -> Result<bool, xous_kernel::Error> {
|
||||
let current_pid = self.current_pid();
|
||||
assert_eq!(pid, current_pid);
|
||||
|
||||
|
|
@ -1426,7 +1425,7 @@ impl SystemServices {
|
|||
|
||||
// Destroy the thread at a hardware level
|
||||
let mut arch_process = crate::arch::process::Process::current();
|
||||
let return_value = arch_process.destroy_thread(tid).unwrap();
|
||||
let return_value = arch_process.destroy_thread(tid).unwrap_or_default();
|
||||
|
||||
// If there's another thread waiting on the return value of this thread,
|
||||
// wake it up and set its return value.
|
||||
|
|
@ -1440,15 +1439,23 @@ impl SystemServices {
|
|||
waiting_threads |= 1 << waiting_tid;
|
||||
}
|
||||
|
||||
// Mark this process as `Ready`.
|
||||
// We can do this because the current thread has just exited. Note that if there
|
||||
// are no threads available, this is an error.
|
||||
self.get_process_mut(pid)?.state = ProcessState::Ready(waiting_threads);
|
||||
// Mark this process as `Ready` if there are waiting threads, or `Sleeping` if
|
||||
// there are no waiting threads.
|
||||
let mut new_pid = pid;
|
||||
{
|
||||
let process = self.get_process_mut(pid)?;
|
||||
process.state = if waiting_threads == 0 {
|
||||
new_pid = process.ppid;
|
||||
ProcessState::Sleeping
|
||||
} else {
|
||||
ProcessState::Ready(waiting_threads)
|
||||
};
|
||||
}
|
||||
|
||||
// Switch to the next available TID. This moves the process back to a `Running` state.
|
||||
self.switch_to_thread(pid, None)?;
|
||||
self.switch_to_thread(new_pid, None)?;
|
||||
|
||||
Ok(return_value)
|
||||
Ok(new_pid != pid)
|
||||
}
|
||||
|
||||
/// Park this thread if the target thread is currently running. Otherwise,
|
||||
|
|
@ -1477,7 +1484,8 @@ impl SystemServices {
|
|||
.unwrap_or(Err(xous_kernel::Error::ProcessNotFound))
|
||||
} else {
|
||||
// The thread does not exist -- continue execution
|
||||
Err(xous_kernel::Error::ThreadNotAvailable)
|
||||
// Err(xous_kernel::Error::ThreadNotAvailable)
|
||||
Ok(xous_kernel::Result::Scalar1(0))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,6 +30,10 @@ enum ExecutionType {
|
|||
NonBlocking,
|
||||
}
|
||||
|
||||
pub fn reset_switchto_caller() {
|
||||
unsafe { SWITCHTO_CALLER = None };
|
||||
}
|
||||
|
||||
fn retry_syscall(pid: PID, tid: TID) -> SysCallResult {
|
||||
if cfg!(baremetal) {
|
||||
arch::process::Process::with_current_mut(|p| p.retry_instruction(tid))?;
|
||||
|
|
|
|||
Loading…
Reference in a new issue