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 7, 2019
1 parent 2cb887e commit e9abb0c
Show file tree
Hide file tree
Showing 5 changed files with 76 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 @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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(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)
}
}
}
1 change: 1 addition & 0 deletions src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/task/spawn_blocking.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down

0 comments on commit e9abb0c

Please sign in to comment.