From 86e86df68c82cc4c6617b231cb0f3921f7980e19 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 22 Mar 2022 22:16:46 +0800 Subject: [PATCH] add a one-liner API call to block execution until PDDB is mounted --- services/pddb/src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 78b4e0646..1ef098e70 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -47,6 +47,7 @@ pub struct Pddb { /// like this are probably OK. keys: Arc >>>, trng: trng::Trng, + tt: ticktimer_server::Ticktimer, } impl Pddb { pub fn new() -> Self { @@ -86,6 +87,7 @@ impl Pddb { cb_handle: Some(handle), keys, trng: trng::Trng::new(&xns).unwrap(), + tt: ticktimer_server::Ticktimer::new().unwrap(), } } pub fn is_mounted(&self) -> bool { @@ -98,6 +100,19 @@ impl Pddb { _ => 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() { + self.tt.sleep_ms(interval as usize).unwrap(); + } + } pub fn try_mount(&self) -> bool { let ret = send_message(self.conn, Message::new_blocking_scalar( Opcode::TryMount.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't execute IsMounted query");