From 92c6a93147578eede4033df5a52f2a10525927a7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 7 Apr 2022 00:04:25 +0800 Subject: [PATCH] eliminate yield loop on blocking mount poller --- services/net/src/connection_manager.rs | 2 +- services/pddb/src/lib.rs | 32 ++++++++------------------ services/pddb/src/main.rs | 13 ++++++++++- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/services/net/src/connection_manager.rs b/services/net/src/connection_manager.rs index 9282264ee..a72932f78 100644 --- a/services/net/src/connection_manager.rs +++ b/services/net/src/connection_manager.rs @@ -107,7 +107,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc >>>, trng: trng::Trng, - tt: ticktimer_server::Ticktimer, } impl Pddb { pub fn new() -> Self { @@ -127,32 +126,21 @@ impl Pddb { cb_handle: Some(handle), keys, 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, /// 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. - pub fn is_mounted_blocking(&self, poll_interval_ms: Option) { - let interval = if let Some(i) = poll_interval_ms { - i - } else { - (self.trng.get_u32().unwrap() % 1000) + 1000 - }; - while !self.is_mounted_maybe_blocking() { - self.tt.sleep_ms(interval as usize).unwrap(); + /// + /// This is typically the API call one would use to hold execution of a service until the PDDB is mounted. + pub fn is_mounted_blocking(&self) { + 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) => { + () + }, + _ => panic!("Internal error"), } } pub fn try_mount(&self) -> bool { diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index d024ad322..bc9eeea69 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -518,6 +518,9 @@ fn xmain() -> ! { // it is the only server capable of doing this. 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::::new(); + // register a suspend/resume listener let mut susres = susres::Susres::new(Some(susres::SuspendOrder::Early), &xns, 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. xous::return_scalar(msg.sender, 1).expect("couldn't return scalar"); } 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, _, _, _, _, { @@ -547,6 +550,10 @@ fn xmain() -> ! { PasswordState::Correct => { if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Correct, time_resetter) { 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"); } else { xous::return_scalar(msg.sender, 0).expect("couldn't return scalar"); @@ -555,6 +562,10 @@ fn xmain() -> ! { PasswordState::Uninit => { if try_mount_or_format(&modals, &mut pddb_os, &mut basis_cache, PasswordState::Uninit, time_resetter) { 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"); } else { xous::return_scalar(msg.sender, 0).expect("couldn't return scalar");