Skip to main content

vortex_metrics/
counter.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::sync::Arc;
5use std::sync::atomic::AtomicU64;
6use std::sync::atomic::Ordering;
7
8/// A commutative value that can only be increased, and starts at 0 on initialization.
9#[derive(Clone, Debug)]
10pub struct Counter(Arc<AtomicU64>);
11
12impl Counter {
13    pub(crate) fn new() -> Self {
14        Self(Default::default())
15    }
16
17    /// Adds `value` to the counter
18    pub fn add(&self, value: u64) {
19        self.0.fetch_add(value, Ordering::Release);
20    }
21
22    /// Returns the latest value stored in the counter
23    pub fn value(&self) -> u64 {
24        self.0.load(Ordering::Acquire)
25    }
26}