xous-rs: cache the current process ID

This value does not change, so cache it in order to reduce the number of
trips to the kernel.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2023-10-09 17:47:47 +08:00
parent c96514d365
commit c7c5a56a7d

View file

@ -1,9 +1,21 @@
use core::sync::atomic::{AtomicU32, Ordering};
static CACHED_PID: AtomicU32 = AtomicU32::new(0);
pub fn id() -> u32 { pub fn id() -> u32 {
let pid = CACHED_PID.load(Ordering::Relaxed);
if pid != 0 {
return pid;
}
if let Ok(pid) = crate::syscall::current_pid() { if let Ok(pid) = crate::syscall::current_pid() {
pid.get() as u32 CACHED_PID.store(pid.get() as u32, Ordering::Relaxed);
pid.get() as u32;
} else { } else {
0 0
} }
} }
pub fn set_id(_id: u32) {} pub fn set_id(id: u32) {
CACHED_PID.store(id, Ordering::Relaxed);
}