eliminate yield loop on blocking mount poller

This commit is contained in:
bunnie 2022-04-07 00:04:25 +08:00
parent b75df1efb3
commit 92c6a93147
3 changed files with 23 additions and 24 deletions

View file

@ -107,7 +107,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc<AtomicU3
move || { move || {
let tt = ticktimer_server::Ticktimer::new().unwrap(); let tt = ticktimer_server::Ticktimer::new().unwrap();
let pddb = pddb::Pddb::new(); let pddb = pddb::Pddb::new();
pddb.is_mounted_blocking(None); pddb.is_mounted_blocking();
loop { loop {
let msg = xous::receive_message(sid).unwrap(); let msg = xous::receive_message(sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) { match FromPrimitive::from_usize(msg.body.id()) {

View file

@ -87,7 +87,6 @@ pub struct Pddb {
/// like this are probably OK. /// like this are probably OK.
keys: Arc<Mutex<HashMap<ApiToken, Box<dyn Fn() + 'static + Send> >>>, keys: Arc<Mutex<HashMap<ApiToken, Box<dyn Fn() + 'static + Send> >>>,
trng: trng::Trng, trng: trng::Trng,
tt: ticktimer_server::Ticktimer,
} }
impl Pddb { impl Pddb {
pub fn new() -> Self { pub fn new() -> Self {
@ -127,32 +126,21 @@ impl Pddb {
cb_handle: Some(handle), cb_handle: Some(handle),
keys, keys,
trng: trng::Trng::new(&xns).unwrap(), trng: trng::Trng::new(&xns).unwrap(),
tt: ticktimer_server::Ticktimer::new().unwrap(),
}
}
/// This call "sometimes blocks". It will block when the PDDB is busy performing other tasks, such as
/// mounting the PDDB, or requesting the user's password.
pub fn is_mounted_maybe_blocking(&self) -> bool {
let ret = send_message(self.conn, Message::new_blocking_scalar(
Opcode::IsMounted.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't execute IsMounted query");
match ret {
xous::Result::Scalar1(code) => {
if code == 0 {false} else {true}
},
_ => panic!("Internal error"),
} }
} }
/// This blocks until the PDDB is mounted by the end user. If `None` is specified for the poll_interval_ms, /// This blocks until the PDDB is mounted by the end user. If `None` is specified for the poll_interval_ms,
/// A random interval between 1 and 2 seconds is chosen for the poll wait time. Randomizing the waiting time /// A random interval between 1 and 2 seconds is chosen for the poll wait time. Randomizing the waiting time
/// helps to level out the task scheduler in the case that many threads are waiting on the PDDB simultaneously. /// helps to level out the task scheduler in the case that many threads are waiting on the PDDB simultaneously.
pub fn is_mounted_blocking(&self, poll_interval_ms: Option<u32>) { ///
let interval = if let Some(i) = poll_interval_ms { /// This is typically the API call one would use to hold execution of a service until the PDDB is mounted.
i pub fn is_mounted_blocking(&self) {
} else { let ret = send_message(self.conn, Message::new_blocking_scalar(
(self.trng.get_u32().unwrap() % 1000) + 1000 Opcode::IsMounted.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't execute IsMounted query");
}; match ret {
while !self.is_mounted_maybe_blocking() { xous::Result::Scalar1(_code) => {
self.tt.sleep_ms(interval as usize).unwrap(); ()
},
_ => panic!("Internal error"),
} }
} }
pub fn try_mount(&self) -> bool { pub fn try_mount(&self) -> bool {

View file

@ -518,6 +518,9 @@ fn xmain() -> ! {
// it is the only server capable of doing this. // it is the only server capable of doing this.
let time_resetter = xns.request_connection_blocking(crate::TIME_SERVER_PDDB).unwrap(); let time_resetter = xns.request_connection_blocking(crate::TIME_SERVER_PDDB).unwrap();
// track processes that want a notification of a mount event
let mut mount_notifications = Vec::<xous::MessageSender>::new();
// register a suspend/resume listener // register a suspend/resume listener
let mut susres = susres::Susres::new(Some(susres::SuspendOrder::Early), &xns, let mut susres = susres::Susres::new(Some(susres::SuspendOrder::Early), &xns,
Opcode::SuspendResume as u32, my_cid).expect("couldn't create suspend/resume object"); Opcode::SuspendResume as u32, my_cid).expect("couldn't create suspend/resume object");
@ -532,7 +535,7 @@ fn xmain() -> ! {
if basis_cache.basis_count() > 0 { // if there's anything in the cache, we're mounted. if basis_cache.basis_count() > 0 { // if there's anything in the cache, we're mounted.
xous::return_scalar(msg.sender, 1).expect("couldn't return scalar"); xous::return_scalar(msg.sender, 1).expect("couldn't return scalar");
} else { } else {
xous::return_scalar(msg.sender, 0).expect("couldn't return scalar"); mount_notifications.push(msg.sender); // defer response until later
} }
}), }),
Some(Opcode::TryMount) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, { Some(Opcode::TryMount) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
@ -547,6 +550,10 @@ fn xmain() -> ! {
PasswordState::Correct => { PasswordState::Correct => {
if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Correct, time_resetter) { if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Correct, time_resetter) {
is_mounted.store(true, Ordering::SeqCst); is_mounted.store(true, Ordering::SeqCst);
for requester in mount_notifications.drain(..) {
xous::return_scalar(requester, 1).expect("couldn't return scalar");
}
assert!(mount_notifications.len() == 0, "apparently I don't understand what drain() does");
xous::return_scalar(msg.sender, 1).expect("couldn't return scalar"); xous::return_scalar(msg.sender, 1).expect("couldn't return scalar");
} else { } else {
xous::return_scalar(msg.sender, 0).expect("couldn't return scalar"); xous::return_scalar(msg.sender, 0).expect("couldn't return scalar");
@ -555,6 +562,10 @@ fn xmain() -> ! {
PasswordState::Uninit => { PasswordState::Uninit => {
if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Uninit, time_resetter) { if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Uninit, time_resetter) {
is_mounted.store(true, Ordering::SeqCst); is_mounted.store(true, Ordering::SeqCst);
for requester in mount_notifications.drain(..) {
xous::return_scalar(requester, 1).expect("couldn't return scalar");
}
assert!(mount_notifications.len() == 0, "apparently I don't understand what drain() does");
xous::return_scalar(msg.sender, 1).expect("couldn't return scalar"); xous::return_scalar(msg.sender, 1).expect("couldn't return scalar");
} else { } else {
xous::return_scalar(msg.sender, 0).expect("couldn't return scalar"); xous::return_scalar(msg.sender, 0).expect("couldn't return scalar");