Skip to content

Commit 4630ec0

Browse files
committed
Use our own AtomicU64 on targets with target_has_atomic less than 64
1 parent eb04949 commit 4630ec0

File tree

5 files changed

+97
-17
lines changed

5 files changed

+97
-17
lines changed

Diff for: .github/workflows/ci.yml

+35-13
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,44 @@ jobs:
1818
override: true
1919

2020
- name: check
21-
uses: actions-rs/cargo@v1
22-
with:
23-
command: check
24-
args: --all --benches --bins --examples --tests
21+
run: |
22+
cargo check --all --benches --bins --examples --tests
23+
cargo check --features unstable --all --benches --bins --examples --tests
2524
26-
- name: check unstable
27-
uses: actions-rs/cargo@v1
28-
with:
29-
command: check
30-
args: --features unstable --all --benches --bins --examples --tests
25+
- name: test
26+
run: cargo test --all --features unstable
27+
28+
cross:
29+
name: Cross compile
30+
runs-on: ubuntu-latest
31+
strategy:
32+
matrix:
33+
target:
34+
- i686-unknown-linux-gnu
35+
- powerpc-unknown-linux-gnu
36+
- powerpc64-unknown-linux-gnu
37+
- mips-unknown-linux-gnu
38+
- arm-linux-androideabi
39+
40+
steps:
41+
- uses: actions/checkout@master
3142

32-
- name: tests
33-
uses: actions-rs/cargo@v1
43+
- name: Install nightly
44+
uses: actions-rs/toolchain@v1
3445
with:
35-
command: test
36-
args: --all --doc --features unstable
46+
toolchain: nightly
47+
override: true
48+
49+
- name: Install cross
50+
run: cargo install cross
51+
52+
- name: check
53+
run: |
54+
cross check --all --target ${{ matrix.target }}
55+
cross check --all --features unstable --target ${{ matrix.target }}
56+
57+
- name: test
58+
run: cross test --all --features unstable --target ${{ matrix.target }}
3759

3860
check_fmt_and_docs:
3961
name: Checking fmt and docs

Diff for: src/sync/atomic.rs

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

Diff for: src/sync/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub use barrier::{Barrier, BarrierWaitResult};
3838
pub use mutex::{Mutex, MutexGuard};
3939
pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
4040

41+
pub(crate) mod atomic;
4142
#[cfg(any(feature = "unstable", feature = "docs"))]
4243
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
4344
mod barrier;

Diff for: src/task/blocking.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22
33
use std::fmt;
44
use std::pin::Pin;
5-
use std::sync::atomic::{AtomicU64, Ordering};
5+
use std::sync::atomic::Ordering;
66
use std::thread;
77
use std::time::Duration;
88

99
use crossbeam_channel::{bounded, Receiver, Sender};
1010
use lazy_static::lazy_static;
1111

1212
use crate::future::Future;
13+
use crate::sync::atomic::AtomicU64;
1314
use crate::task::{Context, Poll};
1415
use crate::utils::abort_on_panic;
1516

1617
const MAX_THREADS: u64 = 10_000;
1718

18-
static DYNAMIC_THREAD_COUNT: AtomicU64 = AtomicU64::new(0);
19+
cfg_if::cfg_if! {
20+
if #[cfg(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc"))] {
21+
lazy_static! {
22+
static ref DYNAMIC_THREAD_COUNT: AtomicU64 = AtomicU64::new(0);
23+
}
24+
} else {
25+
static DYNAMIC_THREAD_COUNT: AtomicU64 = AtomicU64::new(0);
26+
}
27+
}
1928

2029
struct Pool {
2130
sender: Sender<async_task::Task<()>>,

Diff for: src/task/task.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use std::i64;
33
use std::mem;
44
use std::num::NonZeroU64;
55
use std::pin::Pin;
6-
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
6+
use std::sync::atomic::{AtomicUsize, Ordering};
77
use std::sync::Arc;
88

99
use super::task_local;
1010
use crate::future::Future;
11+
use crate::sync::atomic::AtomicU64;
1112
use crate::task::{Context, Poll};
1213

1314
/// A handle to a task.
@@ -112,7 +113,15 @@ pub struct TaskId(NonZeroU64);
112113

113114
impl TaskId {
114115
pub(crate) fn new() -> TaskId {
115-
static COUNTER: AtomicU64 = AtomicU64::new(1);
116+
cfg_if::cfg_if! {
117+
if #[cfg(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc"))] {
118+
lazy_static::lazy_static! {
119+
static ref COUNTER: AtomicU64 = AtomicU64::new(1);
120+
}
121+
} else {
122+
static COUNTER: AtomicU64 = AtomicU64::new(1);
123+
}
124+
}
116125

117126
let id = COUNTER.fetch_add(1, Ordering::Relaxed);
118127

0 commit comments

Comments
 (0)