Skip to content

Commit bbd761b

Browse files
committed
Use our own AtomicU64 on targets with target_has_atomic less than 64
1 parent ec23632 commit bbd761b

File tree

5 files changed

+92
-15
lines changed

5 files changed

+92
-15
lines changed

Diff for: .github/workflows/ci.yml

+35-13
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,44 @@ jobs:
2626
override: true
2727

2828
- name: check
29-
uses: actions-rs/cargo@v1
30-
with:
31-
command: check
32-
args: --all --benches --bins --examples --tests
29+
run: |
30+
cargo check --all --benches --bins --examples --tests
31+
cargo check --features unstable --all --benches --bins --examples --tests
3332
34-
- name: check unstable
35-
uses: actions-rs/cargo@v1
36-
with:
37-
command: check
38-
args: --features unstable --all --benches --bins --examples --tests
33+
- name: test
34+
run: cargo test --all --features unstable
3935

40-
- name: tests
41-
uses: actions-rs/cargo@v1
36+
cross:
37+
name: Cross compile
38+
runs-on: ubuntu-latest
39+
strategy:
40+
matrix:
41+
target:
42+
- i686-unknown-linux-gnu
43+
- powerpc-unknown-linux-gnu
44+
- powerpc64-unknown-linux-gnu
45+
- mips-unknown-linux-gnu
46+
- arm-linux-androideabi
47+
48+
steps:
49+
- uses: actions/checkout@master
50+
51+
- name: Install nightly
52+
uses: actions-rs/toolchain@v1
4253
with:
43-
command: test
44-
args: --all --features unstable
54+
toolchain: nightly
55+
override: true
56+
57+
- name: Install cross
58+
run: cargo install cross
59+
60+
- name: check
61+
run: |
62+
cross check --all --target ${{ matrix.target }}
63+
cross check --all --features unstable --target ${{ matrix.target }}
64+
65+
- name: test
66+
run: cross test --all --features unstable --target ${{ matrix.target }}
4567

4668
check_fmt_and_docs:
4769
name: Checking fmt and docs

Diff for: src/sync/atomic.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
6+
#[cfg(not(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc")))]
7+
mod imp {
8+
pub(crate) use std::sync::atomic::AtomicU64;
9+
}
10+
11+
#[cfg(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc"))]
12+
mod imp {
13+
use std::sync::atomic::Ordering;
14+
use std::sync::Mutex;
15+
16+
#[derive(Debug)]
17+
pub(crate) struct AtomicU64(Mutex<u64>);
18+
19+
impl AtomicU64 {
20+
pub(crate) fn new(val: u64) -> Self {
21+
Self(Mutex::new(val))
22+
}
23+
24+
pub(crate) fn load(&self, _: Ordering) -> u64 {
25+
*self.0.lock().unwrap()
26+
}
27+
28+
pub(crate) fn fetch_add(&self, val: u64, _: Ordering) -> u64 {
29+
let mut lock = self.0.lock().unwrap();
30+
let prev = *lock;
31+
*lock = prev + val;
32+
prev
33+
}
34+
35+
pub(crate) fn fetch_sub(&self, val: u64, _: Ordering) -> u64 {
36+
let mut lock = self.0.lock().unwrap();
37+
let prev = *lock;
38+
*lock = prev - val;
39+
prev
40+
}
41+
}
42+
}

Diff for: src/sync/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub use std::sync::{Arc, Weak};
3535
pub use mutex::{Mutex, MutexGuard};
3636
pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
3737

38+
pub(crate) mod atomic;
3839
mod mutex;
3940
mod rwlock;
4041

Diff for: src/task/blocking.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
//! A thread pool for running blocking functions asynchronously.
22
3-
use std::sync::atomic::{AtomicU64, Ordering};
3+
use std::sync::atomic::Ordering;
44
use std::thread;
55
use std::time::Duration;
66

77
use crossbeam_channel::{bounded, Receiver, Sender};
88
use lazy_static::lazy_static;
99

10+
use crate::sync::atomic::AtomicU64;
1011
use crate::task::task::{JoinHandle, Tag};
1112
use crate::utils::abort_on_panic;
1213

1314
const MAX_THREADS: u64 = 10_000;
1415

16+
#[cfg(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc"))]
17+
lazy_static! {
18+
static ref DYNAMIC_THREAD_COUNT: AtomicU64 = AtomicU64::new(0);
19+
}
20+
#[cfg(not(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc")))]
1521
static DYNAMIC_THREAD_COUNT: AtomicU64 = AtomicU64::new(0);
1622

1723
struct Pool {

Diff for: src/task/task.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use std::i64;
44
use std::mem;
55
use std::num::NonZeroU64;
66
use std::pin::Pin;
7-
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
7+
use std::sync::atomic::{AtomicUsize, Ordering};
88
use std::sync::Arc;
99

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

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

113114
impl TaskId {
114115
pub(crate) fn new() -> TaskId {
116+
#[cfg(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc"))]
117+
lazy_static::lazy_static! {
118+
static ref COUNTER: AtomicU64 = AtomicU64::new(1);
119+
}
120+
#[cfg(not(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc")))]
115121
static COUNTER: AtomicU64 = AtomicU64::new(1);
116122

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

0 commit comments

Comments
 (0)