Skip to content

Commit

Permalink
Use AtomicCell<u64> on targets with target_has_atomic less than 64
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Nov 8, 2019
1 parent ab2f64c commit a80d6ae
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
37 changes: 37 additions & 0 deletions src/sync/atomic.rs
Original file line number Diff line number Diff line change
@@ -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<u64>);

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)
}
}
}
4 changes: 4 additions & 0 deletions src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
mod mutex;
mod rwlock;

cfg_default! {
pub(crate) mod atomic;
}

cfg_unstable! {
pub use barrier::{Barrier, BarrierWaitResult};
pub use channel::{channel, Sender, Receiver};
Expand Down
4 changes: 3 additions & 1 deletion src/task/task_id.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down

0 comments on commit a80d6ae

Please sign in to comment.