use std::sync::atomic::{AtomicUsize, Ordering};
static DIOS: AtomicUsize = AtomicUsize::new(0);
static TABLE_SCENERIES: AtomicUsize = AtomicUsize::new(0);
static RECORD_SCENERIES: AtomicUsize = AtomicUsize::new(0);
static SERVOS: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug)]
pub(crate) struct Tally(&'static AtomicUsize);
impl Tally {
fn claim(counter: &'static AtomicUsize) -> Self {
counter.fetch_add(1, Ordering::Relaxed);
Tally(counter)
}
pub(crate) fn dio() -> Self {
Self::claim(&DIOS)
}
pub(crate) fn table_scenery() -> Self {
Self::claim(&TABLE_SCENERIES)
}
pub(crate) fn record_scenery() -> Self {
Self::claim(&RECORD_SCENERIES)
}
pub(crate) fn servo() -> Self {
Self::claim(&SERVOS)
}
}
impl Drop for Tally {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::Relaxed);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LiveCounts {
pub dios: usize,
pub table_sceneries: usize,
pub record_sceneries: usize,
pub servos: usize,
}
pub fn live_counts() -> LiveCounts {
LiveCounts {
dios: DIOS.load(Ordering::Relaxed),
table_sceneries: TABLE_SCENERIES.load(Ordering::Relaxed),
record_sceneries: RECORD_SCENERIES.load(Ordering::Relaxed),
servos: SERVOS.load(Ordering::Relaxed),
}
}