Skip to main content

yui_core/util/
sync.rs

1//! [`SyncCounter`]: a thread-safe counter used to label objects during a build.
2
3use std::sync::atomic::{AtomicUsize, Ordering};
4
5/// A thread-safe `usize` counter, backed by [`AtomicUsize`].
6pub struct SyncCounter {
7    count: AtomicUsize,
8}
9
10impl SyncCounter {
11    pub fn new(n: usize) -> Self {
12        Self { count: AtomicUsize::new(n) }
13    }
14
15    pub fn count(&self) -> usize {
16        self.count.load(Ordering::Relaxed)
17    }
18
19    pub fn incr(&self) -> usize {
20        self.count.fetch_add(1, Ordering::Relaxed) + 1
21    }
22
23    pub fn set(&self, n: usize) {
24        self.count.store(n, Ordering::Relaxed)
25    }
26}