metrics/
atomics.rs

1//! Atomic types used for metrics.
2//!
3//! As the most commonly used types for metrics storage are atomic integers, implementations of
4//! `CounterFn` and `GaugeFn` must be provided in this crate due to Rust's "orphan rules", which
5//! disallow a crate from implementing a foreign trait on a foreign type.
6//!
7//! Further, we always require an atomic integer of a certain size regardless of whether the
8//! standard library exposes an atomic integer of that size for the target architecture.
9//!
10//! As such, the atomic types that we provide handle implementations for are publicly re-exporter
11//! here for downstream crates to utilize.
12
13use std::sync::atomic::Ordering;
14
15#[cfg(target_pointer_width = "32")]
16pub use portable_atomic::AtomicU64;
17#[cfg(not(target_pointer_width = "32"))]
18pub use std::sync::atomic::AtomicU64;
19
20use super::{CounterFn, GaugeFn};
21
22impl CounterFn for AtomicU64 {
23    fn increment(&self, value: u64) {
24        let _ = self.fetch_add(value, Ordering::Release);
25    }
26
27    fn absolute(&self, value: u64) {
28        let _ = self.fetch_max(value, Ordering::AcqRel);
29    }
30}
31
32impl GaugeFn for AtomicU64 {
33    fn increment(&self, value: f64) {
34        loop {
35            let result = self.fetch_update(Ordering::AcqRel, Ordering::Relaxed, |curr| {
36                let input = f64::from_bits(curr);
37                let output = input + value;
38                Some(output.to_bits())
39            });
40
41            if result.is_ok() {
42                break;
43            }
44        }
45    }
46
47    fn decrement(&self, value: f64) {
48        loop {
49            let result = self.fetch_update(Ordering::AcqRel, Ordering::Relaxed, |curr| {
50                let input = f64::from_bits(curr);
51                let output = input - value;
52                Some(output.to_bits())
53            });
54
55            if result.is_ok() {
56                break;
57            }
58        }
59    }
60
61    fn set(&self, value: f64) {
62        let _ = self.swap(value.to_bits(), Ordering::AcqRel);
63    }
64}