-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework 32 vs 64-bit arch atomics support + a lot of import consolidat…
…ion/cleanup
- Loading branch information
Showing
17 changed files
with
129 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters