From e9abb0cc65eba379547f1f1d2a894639815f9eaf Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Thu, 7 Nov 2019 14:41:11 +0900 Subject: [PATCH] Use AtomicCell on targets with target_has_atomic less than 64 --- .github/workflows/ci.yml | 33 +++++++++++++++++++++++++++++++++ Cargo.toml | 6 +++--- src/sync/atomic.rs | 37 +++++++++++++++++++++++++++++++++++++ src/sync/mod.rs | 1 + src/task/spawn_blocking.rs | 3 ++- 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/sync/atomic.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 06712838e..dbfe3d9e4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,6 +46,39 @@ jobs: command: test args: --all --features unstable + cross: + name: Cross compile + runs-on: ubuntu-latest + strategy: + matrix: + target: + - i686-unknown-linux-gnu + - powerpc-unknown-linux-gnu + - powerpc64-unknown-linux-gnu + - mips-unknown-linux-gnu + - arm-linux-androideabi + + steps: + - uses: actions/checkout@master + + - name: Install nightly + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + + - name: Install cross + run: cargo install cross + + - name: check + run: cross check --all --target ${{ matrix.target }} + + - name: check unstable + run: cross check --all --features unstable --target ${{ matrix.target }} + + - name: test + run: cross test --all --features unstable --target ${{ matrix.target }} + check_fmt_and_docs: name: Checking fmt and docs runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index 9583cdee7..ea24e607a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,9 +29,9 @@ unstable = ["broadcaster"] async-macros = "1.0.0" async-task = "1.0.0" broadcaster = { version = "0.2.6", optional = true, default-features = false, features = ["default-channels"] } -crossbeam-channel = "0.3.9" -crossbeam-deque = "0.7.1" -crossbeam-utils = "0.6.6" +crossbeam-channel = "0.4.0" +crossbeam-deque = "0.7.2" +crossbeam-utils = "0.7.0" futures-core = "0.3.0" futures-io = "0.3.0" futures-timer = "1.0.2" diff --git a/src/sync/atomic.rs b/src/sync/atomic.rs new file mode 100644 index 000000000..d88520ed9 --- /dev/null +++ b/src/sync/atomic.rs @@ -0,0 +1,37 @@ +pub(crate) use self::imp::AtomicU64; + +// `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(not(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc")))] +mod imp { + pub(crate) use std::sync::atomic::AtomicU64; +} + +#[cfg(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc"))] +mod imp { + use std::sync::atomic::Ordering; + + use crossbeam_utils::atomic::AtomicCell; + + pub(crate) struct AtomicU64(AtomicCell); + + impl AtomicU64 { + pub(crate) const fn new(val: u64) -> Self { + Self(AtomicU64::new(val)) + } + + pub(crate) fn load(&self, _: Ordering) -> u64 { + self.0.load() + } + + pub(crate) fn fetch_add(&self, val: u64, _: Ordering) -> u64 { + self.0.fetch_add(val) + } + + pub(crate) fn fetch_sub(&self, val: u64, _: Ordering) -> u64 { + self.0.fetch_sub(val) + } + } +} diff --git a/src/sync/mod.rs b/src/sync/mod.rs index fdeb48c35..0bfeea124 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -181,6 +181,7 @@ pub use std::sync::{Arc, Weak}; pub use mutex::{Mutex, MutexGuard}; pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}; +pub(crate) mod atomic; mod mutex; mod rwlock; diff --git a/src/task/spawn_blocking.rs b/src/task/spawn_blocking.rs index 3f4f18a17..354e156ba 100644 --- a/src/task/spawn_blocking.rs +++ b/src/task/spawn_blocking.rs @@ -1,10 +1,11 @@ -use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::atomic::Ordering; use std::thread; use std::time::Duration; use crossbeam_channel::{unbounded, Receiver, Sender}; use once_cell::sync::Lazy; +use crate::sync::atomic::AtomicU64; use crate::task::{JoinHandle, Task}; use crate::utils::{abort_on_panic, random};