Debian Codegraph @

third_party/rust/tokio/src/util/metric_atomics.rs

raw
1 use std::sync::atomic::{AtomicUsize, Ordering};
2
3 cfg_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)]
12 pub(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)]
19 impl 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 }
48
49 #[cfg_attr(not(all(tokio_unstable, feature = "rt")), allow(dead_code))]
50 /// `AtomicUsize` for use in metrics.
51 ///
52 /// This exposes simplified APIs for use in metrics & uses `std::sync` instead of Loom to avoid polluting loom logs with metric information.
53 #[derive(Debug, Default)]
54 pub(crate) struct MetricAtomicUsize {
55 value: AtomicUsize,
56 }
57
58 #[cfg_attr(not(all(tokio_unstable, feature = "rt")), allow(dead_code))]
59 impl MetricAtomicUsize {
60 pub(crate) fn new(value: usize) -> Self {
61 Self {
62 value: AtomicUsize::new(value),
63 }
64 }
65
66 pub(crate) fn load(&self, ordering: Ordering) -> usize {
67 self.value.load(ordering)
68 }
69
70 pub(crate) fn store(&self, val: usize, ordering: Ordering) {
71 self.value.store(val, ordering)
72 }
73
74 pub(crate) fn increment(&self) -> usize {
75 self.value.fetch_add(1, Ordering::Relaxed)
76 }
77
78 pub(crate) fn decrement(&self) -> usize {
79 self.value.fetch_sub(1, Ordering::Relaxed)
80 }
81 }