axum_server/
notify_once.rs

1use std::sync::atomic::{AtomicBool, Ordering};
2use tokio::sync::Notify;
3
4#[derive(Debug, Default)]
5pub(crate) struct NotifyOnce {
6    notified: AtomicBool,
7    notify: Notify,
8}
9
10impl NotifyOnce {
11    pub(crate) fn notify_waiters(&self) {
12        self.notified.store(true, Ordering::SeqCst);
13
14        self.notify.notify_waiters();
15    }
16
17    pub(crate) fn is_notified(&self) -> bool {
18        self.notified.load(Ordering::SeqCst)
19    }
20
21    pub(crate) async fn notified(&self) {
22        let future = self.notify.notified();
23
24        if !self.notified.load(Ordering::SeqCst) {
25            future.await;
26        }
27    }
28}