Skip to content

Commit ed637c7

Browse files
committed
Use AtomicCell<u64> on targets with target_has_atomic less than 64
1 parent 383057b commit ed637c7

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,39 @@ jobs:
7171
command: test
7272
args: --doc --features "unstable attributes"
7373

74+
cross:
75+
name: Cross compile
76+
runs-on: ubuntu-latest
77+
strategy:
78+
matrix:
79+
target:
80+
- i686-unknown-linux-gnu
81+
- powerpc-unknown-linux-gnu
82+
- powerpc64-unknown-linux-gnu
83+
- mips-unknown-linux-gnu
84+
- arm-linux-androideabi
85+
86+
steps:
87+
- uses: actions/checkout@master
88+
89+
- name: Install nightly
90+
uses: actions-rs/toolchain@v1
91+
with:
92+
toolchain: nightly
93+
override: true
94+
95+
- name: Install cross
96+
run: cargo install cross
97+
98+
- name: check
99+
run: cross check --all --target ${{ matrix.target }}
100+
101+
- name: check unstable
102+
run: cross check --all --features unstable --target ${{ matrix.target }}
103+
104+
- name: test
105+
run: cross test --all --features unstable --target ${{ matrix.target }}
106+
74107
check_fmt_and_docs:
75108
name: Checking fmt and docs
76109
runs-on: ubuntu-latest

src/sync/atomic.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pub(crate) use self::imp::AtomicU64;
2+
3+
// `AtomicU64` can only be used on targets with `target_has_atomic` is 64 or greater.
4+
// Once `cfg_target_has_atomic` feature is stable, we can replace it with
5+
// `#[cfg(target_has_atomic = "64")]`.
6+
// Refs: https://github.com/rust-lang/rust/tree/master/src/librustc_target
7+
#[cfg(not(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc")))]
8+
mod imp {
9+
pub(crate) use std::sync::atomic::AtomicU64;
10+
}
11+
12+
#[cfg(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc"))]
13+
mod imp {
14+
use std::sync::atomic::Ordering;
15+
16+
use crossbeam_utils::atomic::AtomicCell;
17+
18+
pub(crate) struct AtomicU64(AtomicCell<u64>);
19+
20+
impl AtomicU64 {
21+
pub(crate) const fn new(val: u64) -> Self {
22+
Self(AtomicCell::new(val))
23+
}
24+
25+
pub(crate) fn load(&self, _: Ordering) -> u64 {
26+
self.0.load()
27+
}
28+
29+
pub(crate) fn fetch_add(&self, val: u64, _: Ordering) -> u64 {
30+
self.0.fetch_add(val)
31+
}
32+
33+
pub(crate) fn fetch_sub(&self, val: u64, _: Ordering) -> u64 {
34+
self.0.fetch_sub(val)
35+
}
36+
}
37+
}

src/sync/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
182182
mod mutex;
183183
mod rwlock;
184184

185+
cfg_default! {
186+
pub(crate) mod atomic;
187+
}
188+
185189
cfg_unstable! {
186190
pub use barrier::{Barrier, BarrierWaitResult};
187191
pub use channel::{channel, Sender, Receiver};

src/task/task_id.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::fmt;
2-
use std::sync::atomic::{AtomicU64, Ordering};
2+
use std::sync::atomic::Ordering;
3+
4+
use crate::sync::atomic::AtomicU64;
35

46
/// A unique identifier for a task.
57
///

0 commit comments

Comments
 (0)