From b48511f4b86516d6bde370e689bd5ba96723f078 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Thu, 7 Nov 2019 14:50:47 +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/task_id.rs | 4 +++- 5 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/sync/atomic.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 031ffc96f..f83ea2264 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,6 +52,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 48b7d0b61..8aae7b1c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,9 +53,9 @@ async-attributes = { version = "1.1.0", optional = true } async-macros = { version = "1.0.0", optional = true } async-task = { version = "1.0.0", optional = true } broadcaster = { version = "0.2.6", optional = true, default-features = false, features = ["default-channels"] } -crossbeam-channel = { version = "0.3.9", optional = true } -crossbeam-deque = { version = "0.7.1", optional = true } -crossbeam-utils = { version = "0.6.6", optional = true } +crossbeam-channel = { version = "0.4.0", optional = true } +crossbeam-deque = { version = "0.7.2", optional = true } +crossbeam-utils = { version = "0.7.0", optional = true } futures-core = { version = "0.3.0", optional = true } futures-io = { version = "0.3.0", optional = true } futures-timer = { version = "1.0.2", optional = true } diff --git a/src/sync/atomic.rs b/src/sync/atomic.rs new file mode 100644 index 000000000..d0b6b1ca3 --- /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(AtomicCell::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/task_id.rs b/src/task/task_id.rs index 67eee154b..d73419c5f 100644 --- a/src/task/task_id.rs +++ b/src/task/task_id.rs @@ -1,5 +1,7 @@ use std::fmt; -use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::atomic::Ordering; + +use crate::sync::atomic::AtomicU64; /// A unique identifier for a task. ///