Skip to content

Commit 04453c4

Browse files
committed
atomic64: Use AtomicUsize on 64 bit targets
This builds on the work in tikv#64 to allow stable Rust users to get atomics if they run on 64-bit architectures. Tested: Ran tests on the following combinations (all unknown-linux-gnu): - stable Rust x86_64 - stable Rust i686 - nightly Rust x86_64 - nightly Rust i686 - nightly Rust x86_64 with `--features=nightly` - nightly Rust i686 with `--features=nightly`
1 parent 3026ba1 commit 04453c4

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/atomic64/atomic.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use std::sync::atomic::{AtomicU64, Ordering};
1+
use std::sync::atomic::Ordering;
22
use std::mem::transmute;
33

44
pub struct F64 {
5-
inner: AtomicU64,
5+
inner: super::AtomicU64Type,
66
}
77

88
impl F64 {
99
pub fn new(val: f64) -> F64 {
10-
F64 { inner: AtomicU64::new(f64_to_u64(val)) }
10+
F64 { inner: super::AtomicU64Type::new(f64_to_u64(val)) }
1111
}
1212

1313
#[inline]
@@ -34,30 +34,30 @@ impl F64 {
3434
}
3535
}
3636

37-
fn u64_to_f64(val: u64) -> f64 {
37+
fn u64_to_f64(val: super::U64Type) -> f64 {
3838
unsafe { transmute(val) }
3939
}
4040

41-
fn f64_to_u64(val: f64) -> u64 {
41+
fn f64_to_u64(val: f64) -> super::U64Type {
4242
unsafe { transmute(val) }
4343
}
4444

4545
pub struct U64 {
46-
inner: AtomicU64,
46+
inner: super::AtomicU64Type,
4747
}
4848

4949
impl U64 {
5050
pub fn new(val: u64) -> U64 {
51-
U64 { inner: AtomicU64::new(val) }
51+
U64 { inner: super::AtomicU64Type::new(val as super::U64Type) }
5252
}
5353

5454
#[inline]
5555
pub fn get(&self) -> u64 {
56-
self.inner.load(Ordering::Acquire)
56+
self.inner.load(Ordering::Acquire) as u64
5757
}
5858

5959
#[inline]
6060
pub fn inc_by(&self, delta: u64) {
61-
self.inner.fetch_add(delta, Ordering::Release);
61+
self.inner.fetch_add(delta as super::U64Type, Ordering::Release);
6262
}
6363
}

src/atomic64/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
cfg_if! {
1717
if #[cfg(feature = "nightly")] {
1818
// Prefer AtomicU64 if available.
19+
type AtomicU64Type = ::std::sync::atomic::AtomicU64;
20+
type U64Type = u64;
21+
22+
#[path = "atomic.rs"]
23+
mod imp;
24+
} else if #[cfg(target_pointer_width = "64")] {
25+
// Use AtomicUsize if pointer width is 64 bit. This *may* have issues if
26+
// a target has 64 bit pointer width but no atomic pointer-sized type,
27+
// but that is not the case for any of the major architectures.
28+
type AtomicU64Type = ::std::sync::atomic::AtomicUsize;
29+
type U64Type = usize;
30+
1931
#[path = "atomic.rs"]
2032
mod imp;
2133
} else {

0 commit comments

Comments
 (0)