pub struct Counter {
count: std::sync::atomic::AtomicU64,
notify: tokio::sync::Notify,
}
impl Counter {
#[must_use]
pub fn new() -> Self {
Self {
count: std::sync::atomic::AtomicU64::new(0),
notify: tokio::sync::Notify::new(),
}
}
pub fn increment(&self) {
self.count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}
pub fn decrement(&self) {
if self.count.fetch_sub(1, std::sync::atomic::Ordering::SeqCst) == 1 {
self.notify.notify_one();
}
}
pub async fn wait_for_zero(&self) {
while self.count.load(std::sync::atomic::Ordering::SeqCst) != 0 {
self.notify.notified().await;
}
}
}
impl Default for Counter {
fn default() -> Self {
Self::new()
}
}