tokio/util/
metric_atomics.rs

1use std::sync::atomic::Ordering;
2
3cfg_64bit_metrics! {
4    use std::sync::atomic::AtomicU64;
5}
6
7/// `AtomicU64` that is is a no-op on platforms without 64-bit atomics
8///
9/// When used on platforms without 64-bit atomics, writes to this are no-ops.
10/// The `load` method is only defined when 64-bit atomics are available.
11#[derive(Debug, Default)]
12pub(crate) struct MetricAtomicU64 {
13    #[cfg(target_has_atomic = "64")]
14    value: AtomicU64,
15}
16
17// some of these are currently only used behind cfg_unstable
18#[allow(dead_code)]
19impl MetricAtomicU64 {
20    // Load is only defined when supported
21    cfg_64bit_metrics! {
22        pub(crate) fn load(&self, ordering: Ordering) -> u64 {
23            self.value.load(ordering)
24        }
25    }
26
27    cfg_64bit_metrics! {
28        pub(crate) fn store(&self, val: u64, ordering: Ordering) {
29            self.value.store(val, ordering)
30        }
31
32        pub(crate) fn new(value: u64) -> Self {
33            Self { value: AtomicU64::new(value) }
34        }
35
36        pub(crate) fn add(&self, value: u64, ordering: Ordering) {
37            self.value.fetch_add(value, ordering);
38        }
39    }
40
41    cfg_no_64bit_metrics! {
42        pub(crate) fn store(&self, _val: u64, _ordering: Ordering) { }
43        // on platforms without 64-bit atomics, fetch-add returns unit
44        pub(crate) fn add(&self, _value: u64, _ordering: Ordering) {  }
45        pub(crate) fn new(_value: u64) -> Self { Self { } }
46    }
47}