-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use our own AtomicU64 on targets with target_has_atomic less than 64
- Loading branch information
Showing
5 changed files
with
97 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// `AtomicU64` can only be used on targets with `target_has_atomic` is 64 or greater. | ||
// Once `cfg_target_has_atomic` feature is stable, we can replace it with | ||
// `#[cfg(target_has_atomic = "64")]`. | ||
// Refs: https://github.com/rust-lang/rust/tree/master/src/librustc_target | ||
cfg_if::cfg_if! { | ||
if #[cfg(not(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc")))] { | ||
pub(crate) use std::sync::atomic::AtomicU64; | ||
} else { | ||
use std::sync::atomic::Ordering; | ||
use std::sync::Mutex; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct AtomicU64(Mutex<u64>); | ||
|
||
impl AtomicU64 { | ||
pub(crate) fn new(val: u64) -> Self { | ||
Self(Mutex::new(val)) | ||
} | ||
|
||
pub(crate) fn load(&self, _: Ordering) -> u64 { | ||
*self.0.lock().unwrap() | ||
} | ||
|
||
pub(crate) fn fetch_add(&self, val: u64, _: Ordering) -> u64 { | ||
let mut lock = self.0.lock().unwrap(); | ||
let prev = *lock; | ||
*lock = prev + val; | ||
prev | ||
} | ||
|
||
pub(crate) fn fetch_sub(&self, val: u64, _: Ordering) -> u64 { | ||
let mut lock = self.0.lock().unwrap(); | ||
let prev = *lock; | ||
*lock = prev - val; | ||
prev | ||
} | ||
} | ||
} | ||
} |
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