From a03302fff4a3ec6f1d84d2cc32eb9f057f717112 Mon Sep 17 00:00:00 2001 From: koushiro Date: Wed, 6 Mar 2024 19:15:53 +0800 Subject: [PATCH] feat(metrics): impl Atomic for AtomicU32 --- src/metrics/gauge.rs | 58 ++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/src/metrics/gauge.rs b/src/metrics/gauge.rs index 98671280..7b268427 100644 --- a/src/metrics/gauge.rs +++ b/src/metrics/gauge.rs @@ -6,7 +6,7 @@ use crate::encoding::{EncodeGaugeValue, EncodeMetric, MetricEncoder}; use super::{MetricType, TypedMetric}; use std::marker::PhantomData; -use std::sync::atomic::{AtomicI32, Ordering}; +use std::sync::atomic::{AtomicI32, AtomicU32, Ordering}; #[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))] use std::sync::atomic::{AtomicI64, AtomicU64}; use std::sync::Arc; @@ -134,55 +134,81 @@ pub trait Atomic { fn get(&self) -> N; } -#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))] -impl Atomic for AtomicI64 { - fn inc(&self) -> i64 { +impl Atomic for AtomicI32 { + fn inc(&self) -> i32 { self.inc_by(1) } - fn inc_by(&self, v: i64) -> i64 { + fn inc_by(&self, v: i32) -> i32 { self.fetch_add(v, Ordering::Relaxed) } - fn dec(&self) -> i64 { + fn dec(&self) -> i32 { self.dec_by(1) } - fn dec_by(&self, v: i64) -> i64 { + fn dec_by(&self, v: i32) -> i32 { self.fetch_sub(v, Ordering::Relaxed) } - fn set(&self, v: i64) -> i64 { + fn set(&self, v: i32) -> i32 { self.swap(v, Ordering::Relaxed) } - fn get(&self) -> i64 { + fn get(&self) -> i32 { self.load(Ordering::Relaxed) } } -impl Atomic for AtomicI32 { - fn inc(&self) -> i32 { +impl Atomic for AtomicU32 { + fn inc(&self) -> u32 { self.inc_by(1) } - fn inc_by(&self, v: i32) -> i32 { + fn inc_by(&self, v: u32) -> u32 { self.fetch_add(v, Ordering::Relaxed) } - fn dec(&self) -> i32 { + fn dec(&self) -> u32 { self.dec_by(1) } - fn dec_by(&self, v: i32) -> i32 { + fn dec_by(&self, v: u32) -> u32 { self.fetch_sub(v, Ordering::Relaxed) } - fn set(&self, v: i32) -> i32 { + fn set(&self, v: u32) -> u32 { self.swap(v, Ordering::Relaxed) } - fn get(&self) -> i32 { + fn get(&self) -> u32 { + self.load(Ordering::Relaxed) + } +} + +#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))] +impl Atomic for AtomicI64 { + fn inc(&self) -> i64 { + self.inc_by(1) + } + + fn inc_by(&self, v: i64) -> i64 { + self.fetch_add(v, Ordering::Relaxed) + } + + fn dec(&self) -> i64 { + self.dec_by(1) + } + + fn dec_by(&self, v: i64) -> i64 { + self.fetch_sub(v, Ordering::Relaxed) + } + + fn set(&self, v: i64) -> i64 { + self.swap(v, Ordering::Relaxed) + } + + fn get(&self) -> i64 { self.load(Ordering::Relaxed) } }