remove execution gate server and use deferred callback idiom instead

reduces one connection on all PIDs that require suspend/resume
callbacks; lightens kernel by about 16k runtime load
This commit is contained in:
bunnie 2022-04-07 01:13:49 +08:00
parent c36d0d4111
commit 1261d73451
3 changed files with 38 additions and 49 deletions

View file

@ -4,7 +4,6 @@
use utralib::generated::*;
pub(crate) const SERVER_NAME_SUSRES: &str = "_Suspend/resume manager_";
pub(crate) const SERVER_NAME_EXEC_GATE: &str = "_Suspend/resume execution gate_";
/// Note: there must be at least one subscriber to the `Last` suspend order event, otherwise
/// the logic will never terminate. There may be multiple `Last` subscribers, but the order at
@ -61,6 +60,9 @@ pub(crate) enum Opcode {
/// not tested - reboot address
RebootVector, //(u32),
/// used by processes to indicate they are suspending now; this blocks until resume using the "execution gate"
SuspendingNow,
/// exit the server
Quit,
}
@ -79,11 +81,6 @@ pub(crate) enum SuspendEventCallback {
Drop,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum ExecGateOpcode {
SuspendingNow,
Drop,
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////// suspend-resume hardware management primitives

View file

@ -14,14 +14,12 @@ use num_traits::{ToPrimitive, FromPrimitive};
pub struct Susres {
conn: CID,
suspend_cb_sid: Option<xous::SID>,
execution_gate_conn: CID,
}
impl Susres {
#[cfg(any(target_os = "none", target_os = "xous"))]
pub fn new(order: Option<SuspendOrder>, xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result<Self, xous::Error> {
REFCOUNT.fetch_add(1, Ordering::Relaxed);
let conn = xns.request_connection_blocking(api::SERVER_NAME_SUSRES).expect("Can't connect to SUSRES");
let execution_gate_conn = xns.request_connection_blocking(api::SERVER_NAME_EXEC_GATE).expect("Can't connect to the execution gate");
let sid = xous::create_server().unwrap();
let sid_tuple = sid.to_u32();
@ -39,7 +37,6 @@ impl Susres {
Ok(Susres {
conn,
suspend_cb_sid: Some(sid),
execution_gate_conn,
})
}
// suspend/resume is not implemented in hosted mode, and will break if you try to do it.
@ -53,7 +50,6 @@ impl Susres {
Ok(Susres {
conn: 0,
suspend_cb_sid: None,
execution_gate_conn: 0,
})
}
pub fn conn(&self) -> CID { self.conn }
@ -64,7 +60,6 @@ impl Susres {
Ok(Susres {
conn,
suspend_cb_sid: None,
execution_gate_conn: 0,
})
}
@ -86,9 +81,12 @@ impl Susres {
Message::new_scalar(Opcode::SuspendReady.to_usize().unwrap(), token, 0, 0, 0)
).map(|_|())?;
log::trace!("blocking until suspend");
// sometime between here and when this next message unblocks, the power went out...
// now block until we've resumed
send_message(self.execution_gate_conn,
Message::new_blocking_scalar(ExecGateOpcode::SuspendingNow.to_usize().unwrap(), 0, 0, 0, 0)
send_message(self.conn,
Message::new_blocking_scalar(Opcode::SuspendingNow.to_usize().unwrap(), 0, 0, 0, 0)
).map(|_|())?;
let response = send_message(self.conn,

View file

@ -6,7 +6,7 @@
mod murmur3;
mod api;
use api::{Opcode, ScalarHook, SuspendEventCallback, ExecGateOpcode};
use api::{Opcode, ScalarHook, SuspendEventCallback};
use num_traits::{ToPrimitive, FromPrimitive};
use xous_ipc::Buffer;
@ -530,35 +530,6 @@ pub fn timeout_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
}
static SHOULD_RESUME: AtomicBool = AtomicBool::new(false);
static RESUME_EXEC: AtomicBool = AtomicBool::new(false);
pub fn execution_gate() {
let xns = xous_names::XousNames::new().unwrap();
let execgate_sid = xns.register_name(api::SERVER_NAME_EXEC_GATE, None).expect("can't register execution gate");
log::trace!("execution_gate registered with NS -- {:?}", execgate_sid);
loop {
let msg = xous::receive_message(execgate_sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
// the entire purpose of SupendingNow is to block the thread that sent the message, until we're ready to resume.
Some(ExecGateOpcode::SuspendingNow) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
println!("checking exec gate:");
while !RESUME_EXEC.load(Ordering::Relaxed) {
println!("execution gate active");
xous::yield_slice();
}
println!("execution is ungated!");
xous::return_scalar(msg.sender, 0).expect("couldn't return dummy message to unblock execution");
}),
Some(ExecGateOpcode::Drop) => {
break;
}
None => {
log::error!("received unknown opcode in execution_gate");
}
}
}
xous::destroy_server(execgate_sid).unwrap();
}
#[xous::xous_main]
fn xmain() -> ! {
@ -578,9 +549,6 @@ fn xmain() -> ! {
// so this is the only way to debug what's going on.
println!("App UART debug printing is on!");
// start up the execution gate
xous::create_thread_0(execution_gate).unwrap();
let xns = xous_names::XousNames::new().unwrap();
// unlimited connections allowed
let susres_sid = xns.register_name(api::SERVER_NAME_SUSRES, None).expect("can't register server");
@ -603,6 +571,8 @@ fn xmain() -> ! {
let mut suspend_subscribers = Vec::<ScalarCallback>::new();
let mut current_op_order = crate::api::SuspendOrder::Early;
let mut gated_pids = Vec::<xous::MessageSender>::new();
loop {
let msg = xous::receive_message(susres_sid).unwrap();
if reboot_requested {
@ -634,6 +604,16 @@ fn xmain() -> ! {
let hookdata = buffer.to_original::<ScalarHook, _>().unwrap();
do_hook(hookdata, &mut suspend_subscribers);
},
Some(Opcode::SuspendingNow) => {
if !suspend_requested {
// this is harmless, it means a process' execution gate came a bit later than expected, so just ignore and tell it to resume
// the execution gate is only requested until *after* a process has checked in and said it is ready to suspend, anyways.
log::warn!("exec gate message received late, ignoring");
xous::return_scalar(msg.sender, 0).expect("couldn't return dummy message to unblock execution");
} else {
gated_pids.push(msg.sender);
}
},
Some(Opcode::SuspendReady) => msg_scalar_unpack!(msg, token, _, _, _, {
//log::trace!("suspendready with token {}", token);
if !suspend_requested {
@ -674,6 +654,11 @@ fn xmain() -> ! {
timeout_pending = false;
// susres_hw.debug_delay(500); // let the messages print
susres_hw.do_suspend(false);
// ---- power turns off ----
// ---- time passes while we are asleep ----
// ---- omg power came back! ---
// when do_suspend() returns, it means we've resumed
suspend_requested = false;
log_server::resume(); // log server is a special case, in order to avoid circular dependencies
@ -682,7 +667,9 @@ fn xmain() -> ! {
}
// this now allows all other threads to commence
log::trace!("low-level resume done, restoring execution");
RESUME_EXEC.store(true, Ordering::Relaxed);
for pid in gated_pids.drain(..) {
xous::return_scalar(pid, 0).expect("couldn't return dummy message to unblock execution");
}
susres_hw.restore_wfi();
} else if all_ready {
log::debug!("finished with {:?} going to next round", current_op_order);
@ -716,7 +703,6 @@ fn xmain() -> ! {
suspend_requested = true;
// clear the resume gate
SHOULD_RESUME.store(false, Ordering::Relaxed);
RESUME_EXEC.store(false, Ordering::Relaxed);
// clear the ready to suspend flag and failed to suspend flag
for sub in suspend_subscribers.iter_mut() {
sub.ready_to_suspend = false;
@ -755,6 +741,11 @@ fn xmain() -> ! {
// susres_hw.debug_delay(500); // let the messages print
// force a suspend
susres_hw.do_suspend(true);
// ---- power turns off ----
// ---- time passes while we are asleep ----
// ---- omg power came back! ---
// when do_suspend() returns, it means we've resumed
suspend_requested = false;
log_server::resume(); // log server is a special case, in order to avoid circular dependencies
@ -763,7 +754,10 @@ fn xmain() -> ! {
} else {
log::error!("We forced a suspend, but the bootloader is claiming we did a clean suspend. Internal state may be inconsistent.");
}
RESUME_EXEC.store(true, Ordering::Relaxed);
for pid in gated_pids.drain(..) {
xous::return_scalar(pid, 0).expect("couldn't return dummy message to unblock execution");
}
susres_hw.restore_wfi();
} else {
log::trace!("clean suspend timeout received, ignoring");
// this means we did a clean suspend, we've resumed, and the timeout came back after the resume