+/// A struct which can be used to select across many [`Future`]s at once without relying on a full
+/// async context.
+#[cfg(feature = "std")]
+pub struct Sleeper {
+ notifiers: Vec<Arc<Mutex<FutureState>>>,
+}
+
+#[cfg(feature = "std")]
+impl Sleeper {
+ /// Constructs a new sleeper from one future, allowing blocking on it.
+ pub fn from_single_future(future: Future) -> Self {
+ Self { notifiers: vec![future.state] }
+ }
+ /// Constructs a new sleeper from two futures, allowing blocking on both at once.
+ // Note that this is the common case - a ChannelManager and ChainMonitor.
+ pub fn from_two_futures(fut_a: Future, fut_b: Future) -> Self {
+ Self { notifiers: vec![fut_a.state, fut_b.state] }
+ }
+ /// Constructs a new sleeper on many futures, allowing blocking on all at once.
+ pub fn new(futures: Vec<Future>) -> Self {
+ Self { notifiers: futures.into_iter().map(|f| f.state).collect() }
+ }
+ /// Prepares to go into a wait loop body, creating a condition variable which we can block on
+ /// and an `Arc<Mutex<Option<_>>>` which gets set to the waking `Future`'s state prior to the
+ /// condition variable being woken.
+ fn setup_wait(&self) -> (Arc<Condvar>, Arc<Mutex<Option<Arc<Mutex<FutureState>>>>>) {
+ let cv = Arc::new(Condvar::new());
+ let notified_fut_mtx = Arc::new(Mutex::new(None));
+ {
+ for notifier_mtx in self.notifiers.iter() {
+ let cv_ref = Arc::clone(&cv);
+ let notified_fut_ref = Arc::clone(¬ified_fut_mtx);
+ let mut notifier = notifier_mtx.lock().unwrap();
+ if notifier.complete {
+ *notified_fut_mtx.lock().unwrap() = Some(Arc::clone(¬ifier_mtx));
+ break;
+ }
+ notifier.callbacks_with_state.push((false, Box::new(move |notifier_ref| {
+ *notified_fut_ref.lock().unwrap() = Some(Arc::clone(notifier_ref));
+ cv_ref.notify_all();
+ })));
+ }
+ }
+ (cv, notified_fut_mtx)
+ }
+
+ /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
+ pub fn wait(&self) {
+ let (cv, notified_fut_mtx) = self.setup_wait();
+ let notified_fut = cv.wait_while(notified_fut_mtx.lock().unwrap(), |fut_opt| fut_opt.is_none())
+ .unwrap().take().expect("CV wait shouldn't have returned until the notifying future was set");
+ notified_fut.lock().unwrap().callbacks_made = true;
+ }
+
+ /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the
+ /// given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time
+ /// elapsed.
+ pub fn wait_timeout(&self, max_wait: Duration) -> bool {
+ let (cv, notified_fut_mtx) = self.setup_wait();
+ let notified_fut =
+ match cv.wait_timeout_while(notified_fut_mtx.lock().unwrap(), max_wait, |fut_opt| fut_opt.is_none()) {
+ Ok((_, e)) if e.timed_out() => return false,
+ Ok((mut notified_fut, _)) =>
+ notified_fut.take().expect("CV wait shouldn't have returned until the notifying future was set"),
+ Err(_) => panic!("Previous panic while a lock was held led to a lock panic"),
+ };
+ notified_fut.lock().unwrap().callbacks_made = true;
+ true
+ }
+}
+