kernel: more renaming context -> thread

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-07-21 10:12:49 +08:00
parent e1bf9965c3
commit cf571ad1ee
5 changed files with 63 additions and 57 deletions

View file

@ -258,7 +258,7 @@ pub fn idle() -> bool {
// Note that in this system, multiple processes can be active at once. This is // Note that in this system, multiple processes can be active at once. This is
// similar to having one core for each process. However, each process may only have // similar to having one core for each process. However, each process may only have
// one context running at a time. // one context running at a time.
SystemServices::with_mut(|ss| ss.switch_to(new_pid, Some(1))).unwrap(); SystemServices::with_mut(|ss| ss.switch_to_thread(new_pid, Some(1))).unwrap();
} }
ThreadMessage::SysCall(pid, thread_id, call) => { ThreadMessage::SysCall(pid, thread_id, call) => {
// println!("KERNEL({}): Received syscall {:?}", pid, call); // println!("KERNEL({}): Received syscall {:?}", pid, call);

View file

@ -6,7 +6,7 @@ use std::net::TcpStream;
use std::thread_local; use std::thread_local;
use xous::{ContextInit, TID, PID}; use xous::{ContextInit, TID, PID};
pub const INITIAL_CONTEXT: usize = 1; pub const INITIAL_TID: usize = 1;
pub struct Process { pub struct Process {
pid: PID, pid: PID,
@ -236,7 +236,7 @@ impl Process {
inner: Default::default(), inner: Default::default(),
conn: init_data.conn, conn: init_data.conn,
memory_to_return: None, memory_to_return: None,
current_context: INITIAL_CONTEXT, current_context: INITIAL_TID,
contexts: [Context {allocated: false}; MAX_CONTEXT], contexts: [Context {allocated: false}; MAX_CONTEXT],
}; };
if pid_idx >= process_table.table.len() { if pid_idx >= process_table.table.len() {

View file

@ -15,7 +15,7 @@ use xous::{pid_from_usize, ContextInit, Error, MemoryAddress, Message, TID, CID,
const MAX_PROCESS_COUNT: usize = 32; const MAX_PROCESS_COUNT: usize = 32;
const MAX_SERVER_COUNT: usize = 32; const MAX_SERVER_COUNT: usize = 32;
pub use crate::arch::process::INITIAL_CONTEXT; pub use crate::arch::process::INITIAL_TID;
/// A big unifying struct containing all of the system state. /// A big unifying struct containing all of the system state.
/// This is inherited from the stage 1 bootloader. /// This is inherited from the stage 1 bootloader.
@ -200,7 +200,7 @@ thread_local!(static SYSTEM_SERVICES: RefCell<SystemServices> = RefCell::new(Sys
pid: unsafe { PID::new_unchecked(1) }, pid: unsafe { PID::new_unchecked(1) },
mapping: arch::mem::DEFAULT_MEMORY_MAPPING, mapping: arch::mem::DEFAULT_MEMORY_MAPPING,
current_context: 0, current_context: 0,
previous_context: INITIAL_CONTEXT as u8, previous_context: INITIAL_TID as u8,
}; MAX_PROCESS_COUNT], }; MAX_PROCESS_COUNT],
// Note we can't use MAX_SERVER_COUNT here because of how Rust's // Note we can't use MAX_SERVER_COUNT here because of how Rust's
// macro tokenization works // macro tokenization works
@ -281,8 +281,8 @@ impl SystemServices {
// Set up our handle with a bogus sp and pc. These will get updated // Set up our handle with a bogus sp and pc. These will get updated
// once a context switch _away_ from the kernel occurs, however we need // once a context switch _away_ from the kernel occurs, however we need
// to make sure other fields such as "thread number" are all valid. // to make sure other fields such as "thread number" are all valid.
// ProcessHandle::get().init(0, 0, INITIAL_CONTEXT); // ProcessHandle::get().init(0, 0, INITIAL_TID);
self.processes[0].current_context = INITIAL_CONTEXT as u8; self.processes[0].current_context = INITIAL_TID as u8;
} }
/// Add a new entry to the process table. This results in a new address space /// Add a new entry to the process table. This results in a new address space
@ -480,24 +480,24 @@ impl SystemServices {
/// Mark the specified context as ready to run. If the thread is Sleeping, mark /// Mark the specified context as ready to run. If the thread is Sleeping, mark
/// it as Ready. /// it as Ready.
pub fn ready_context(&mut self, pid: PID, context: TID) -> Result<(), xous::Error> { pub fn ready_thread(&mut self, pid: PID, tid: TID) -> Result<(), xous::Error> {
assert!(context == INITIAL_CONTEXT); assert!(tid == INITIAL_TID);
let process = self.get_process_mut(pid)?; let process = self.get_process_mut(pid)?;
process.state = match process.state { process.state = match process.state {
ProcessState::Free => panic!( ProcessState::Free => panic!(
"PID {} was not running, so cannot wake context {}", "PID {} was not running, so cannot wake context {}",
pid, context pid, tid
), ),
ProcessState::Running(x) if x & (1 << context) == 0 => { ProcessState::Running(x) if x & (1 << tid) == 0 => {
ProcessState::Running(x | (1 << context)) ProcessState::Running(x | (1 << tid))
} }
ProcessState::Ready(x) if x & (1 << context) == 0 => { ProcessState::Ready(x) if x & (1 << tid) == 0 => {
ProcessState::Ready(x | (1 << context)) ProcessState::Ready(x | (1 << tid))
} }
ProcessState::Sleeping => ProcessState::Ready(1 << context), ProcessState::Sleeping => ProcessState::Ready(1 << tid),
other => panic!( other => panic!(
"PID {} was not in a state to wake context {}: {:?}", "PID {} was not in a state to wake context {}: {:?}",
pid, context, other pid, tid, other
), ),
}; };
// println!( // println!(
@ -512,7 +512,7 @@ impl SystemServices {
/// # Panics /// # Panics
/// ///
/// If the current process is not running, or if it's "Running" but has no free contexts /// If the current process is not running, or if it's "Running" but has no free contexts
pub fn switch_to(&mut self, pid: PID, context: Option<TID>) -> Result<(), xous::Error> { pub fn switch_to_thread(&mut self, pid: PID, context: Option<TID>) -> Result<(), xous::Error> {
let process = self.get_process_mut(pid)?; let process = self.get_process_mut(pid)?;
// println!( // println!(
// "switch_to({}:{:?}): Old state was {:?}", // "switch_to({}:{:?}): Old state was {:?}",
@ -528,17 +528,17 @@ impl SystemServices {
process.activate()?; process.activate()?;
// If a context is specified for a Setup task to switch to, // If a context is specified for a Setup task to switch to,
// ensure it's the INITIAL_CONTEXT. Otherwise it's not valid. // ensure it's the INITIAL_TID. Otherwise it's not valid.
if let Some(ctx) = context { if let Some(ctx) = context {
if ctx != INITIAL_CONTEXT { if ctx != INITIAL_TID {
return Err(xous::Error::InvalidContext); return Err(xous::Error::InvalidContext);
} }
} }
let mut p = crate::arch::process::Process::current(); let mut p = crate::arch::process::Process::current();
p.setup_context(INITIAL_CONTEXT, setup)?; p.setup_context(INITIAL_TID, setup)?;
p.set_context(INITIAL_CONTEXT)?; p.set_context(INITIAL_TID)?;
process.current_context = INITIAL_CONTEXT as u8; process.current_context = INITIAL_TID as u8;
// Mark the current proces state as "running, and no waiting contexts" // Mark the current proces state as "running, and no waiting contexts"
ProcessState::Running(0) ProcessState::Running(0)
@ -634,11 +634,10 @@ impl SystemServices {
/// # Panics /// # Panics
/// ///
/// If the current process is not running. /// If the current process is not running.
pub fn switch_from( pub fn switch_from_thread(
&mut self, &mut self,
pid: PID, pid: PID,
context: TID, tid: TID,
can_resume: bool,
) -> Result<(), xous::Error> { ) -> Result<(), xous::Error> {
let process = self.get_process_mut(pid)?; let process = self.get_process_mut(pid)?;
// println!( // println!(
@ -646,31 +645,31 @@ impl SystemServices {
// pid, context, process.state // pid, context, process.state
// ); // );
process.state = match process.state { process.state = match process.state {
ProcessState::Running(x) if x & (1 << context) != 0 => panic!( ProcessState::Running(x) if x & (1 << tid) != 0 => panic!(
"PID {} context {} was already queued for running when `switch_from()` was called", "PID {} thread {} was already queued for running when `switch_from_thread()` was called",
pid, context pid, tid
), ),
ProcessState::Running(x) if x == 0 => { ProcessState::Running(x) if x == 0 => {
if can_resume { // if can_resume {
if cfg!(baremetal) { // if cfg!(baremetal) {
ProcessState::Ready(1 << context) // ProcessState::Ready(1 << tid)
} else { // } else {
ProcessState::Running(1 << context) // ProcessState::Running(1 << tid)
} // }
} else { // } else {
ProcessState::Sleeping ProcessState::Sleeping
} // }
} }
ProcessState::Running(x) => { ProcessState::Running(x) => {
if cfg!(baremetal) { if cfg!(baremetal) {
ProcessState::Ready(x | if can_resume { 1 << context } else { 0 }) ProcessState::Ready(x)
} else { } else {
ProcessState::Running(x | if can_resume { 1 << context } else { 0 }) ProcessState::Running(x)
} }
} }
other => panic!( other => panic!(
"PID {} was not in a state to be switched from: {:?}", "PID {} TID {} was not in a state to be switched from: {:?}",
pid, other pid, tid, other
), ),
}; };
// println!( // println!(
@ -680,7 +679,7 @@ impl SystemServices {
Ok(()) Ok(())
} }
pub fn set_context_result( pub fn set_thread_result(
&mut self, &mut self,
pid: PID, pid: PID,
context: TID, context: TID,
@ -726,7 +725,7 @@ impl SystemServices {
// Ensure the new process can be run. // Ensure the new process can be run.
match new.state { match new.state {
ProcessState::Free => return Err(xous::Error::ProcessNotFound), ProcessState::Free => return Err(xous::Error::ProcessNotFound),
ProcessState::Setup(_) => new_context = INITIAL_CONTEXT, ProcessState::Setup(_) => new_context = INITIAL_TID,
ProcessState::Running(x) | ProcessState::Ready(x) => { ProcessState::Running(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
@ -780,7 +779,7 @@ impl SystemServices {
// "Initializing new process with stack size of {} bytes", // "Initializing new process with stack size of {} bytes",
// stack_size // stack_size
// ); // );
// // process.init(entrypoint, stack, INITIAL_CONTEXT); // // process.init(entrypoint, stack, INITIAL_TID);
// // Mark the stack as "unallocated-but-free" // // Mark the stack as "unallocated-but-free"
// let init_sp = stack & !0xfff; // let init_sp = stack & !0xfff;
// let mut memory_manager = MemoryManagerHandle::get(); // let mut memory_manager = MemoryManagerHandle::get();

View file

@ -123,7 +123,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
// Mark the server's context as "Ready". If this fails, return the context // Mark the server's context as "Ready". If this fails, return the context
// to the blocking list. // to the blocking list.
ss.ready_context(server_pid, ctx_number).or_else(|e| { ss.ready_thread(server_pid, ctx_number).or_else(|e| {
ss.server_from_sidx(sidx) ss.server_from_sidx(sidx)
.expect("server couldn't be located") .expect("server couldn't be located")
.return_available_thread(thread); .return_available_thread(thread);
@ -137,20 +137,20 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
.unwrap_or(Err(xous::Error::ProcessNotFound)) .unwrap_or(Err(xous::Error::ProcessNotFound))
} else if blocking && !cfg!(baremetal) { } else if blocking && !cfg!(baremetal) {
// println!("Blocking client, since it sent a blocking message"); // println!("Blocking client, since it sent a blocking message");
ss.switch_to(server_pid, None)?; ss.switch_to_thread(server_pid, None)?;
ss.set_context_result(server_pid, ctx_number, xous::Result::Message(envelope)) ss.set_thread_result(server_pid, ctx_number, xous::Result::Message(envelope))
.and_then(|_| ss.switch_from(pid, thread, false)) .and_then(|_| ss.switch_from_thread(pid, thread))
.map(|_| xous::Result::BlockedProcess) .map(|_| xous::Result::BlockedProcess)
} else if cfg!(baremetal) { } else if cfg!(baremetal) {
// println!("Setting the return value of the Server and returning to Client"); // println!("Setting the return value of the Server and returning to Client");
ss.set_context_result(server_pid, ctx_number, xous::Result::Message(envelope)) ss.set_thread_result(server_pid, ctx_number, xous::Result::Message(envelope))
.map(|_| xous::Result::Ok) .map(|_| xous::Result::Ok)
} else { } else {
// println!("Setting the return value of the Server and returning to Client"); // println!("Setting the return value of the Server and returning to Client");
// "Switch to" the server PID when not running on bare metal. This ensures // "Switch to" the server PID when not running on bare metal. This ensures
// that it's "Running". // that it's "Running".
ss.switch_to(server_pid, None)?; ss.switch_to_thread(server_pid, None)?;
ss.set_context_result(server_pid, ctx_number, xous::Result::Message(envelope)) ss.set_thread_result(server_pid, ctx_number, xous::Result::Message(envelope))
.map(|_| xous::Result::Ok) .map(|_| xous::Result::Ok)
} }
} else { } else {
@ -162,7 +162,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
// equivalent to a "Yield". // equivalent to a "Yield".
if blocking { if blocking {
if !cfg!(baremetal) { if !cfg!(baremetal) {
ss.switch_from(pid, thread, false)?; ss.switch_from_thread(pid, thread)?;
Ok(xous::Result::BlockedProcess) Ok(xous::Result::BlockedProcess)
} else { } else {
// println!("Returning to parent"); // println!("Returning to parent");
@ -222,7 +222,7 @@ fn receive_message(pid: PID, tid: TID, sid: SID) -> SysCallResult {
// For hosted targets, simply return `BlockedProcess` indicating we'll make // For hosted targets, simply return `BlockedProcess` indicating we'll make
// a callback to their socket at a later time. // a callback to their socket at a later time.
else { else {
ss.switch_from(pid, tid, false) ss.switch_from_thread(pid, tid)
.map(|_| xous::Result::BlockedProcess) .map(|_| xous::Result::BlockedProcess)
} }
}) })
@ -493,18 +493,18 @@ pub fn handle(
// "KERNEL({}): Unblocking PID {} CTX {}", // "KERNEL({}): Unblocking PID {} CTX {}",
// pid, client_pid, client_ctx // pid, client_pid, client_ctx
// ); // );
ss.ready_context(client_pid, client_ctx)?; ss.ready_thread(client_pid, client_ctx)?;
ss.set_context_result(client_pid, client_ctx, xous::Result::Ok)?; ss.set_thread_result(client_pid, client_ctx, xous::Result::Ok)?;
ss.switch_to(client_pid, Some(client_ctx))?; ss.switch_to_thread(client_pid, Some(client_ctx))?;
Ok(xous::Result::Ok) Ok(xous::Result::Ok)
}) })
} }
SysCall::SendMessage(cid, message) => send_message(pid, tid, cid, message), SysCall::SendMessage(cid, message) => send_message(pid, tid, cid, message),
SysCall::TerminateProcess => SystemServices::with_mut(|ss| { SysCall::TerminateProcess => SystemServices::with_mut(|ss| {
ss.switch_from(pid, tid, false)?; ss.switch_from_thread(pid, tid)?;
let ppid = ss.terminate_process(pid)?; let ppid = ss.terminate_process(pid)?;
if cfg!(baremetal) { if cfg!(baremetal) {
ss.switch_to(ppid, None) ss.switch_to_thread(ppid, None)
.map(|_| xous::Result::ResumeProcess) .map(|_| xous::Result::ResumeProcess)
} else { } else {
Ok(xous::Result::Ok) Ok(xous::Result::Ok)

View file

@ -482,3 +482,10 @@ fn multiple_contexts() {
main_thread.join().expect("couldn't join kernel process"); main_thread.join().expect("couldn't join kernel process");
} }
#[test]
fn multiple_multiple_contexts() {
for _ in 0..60 {
multiple_contexts();
}
}