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