Skip to content

Commit

Permalink
Use our own AtomicU64 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 Oct 17, 2019
1 parent 46f0fb1 commit 958557a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 17 deletions.
48 changes: 35 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,44 @@ jobs:
override: true

- name: check
uses: actions-rs/cargo@v1
with:
command: check
args: --all --benches --bins --examples --tests
run: |
cargo check --all --benches --bins --examples --tests
cargo check --features unstable --all --benches --bins --examples --tests
- name: check unstable
uses: actions-rs/cargo@v1
with:
command: check
args: --features unstable --all --benches --bins --examples --tests
- name: test
run: cargo test --all --features unstable

- name: tests
uses: actions-rs/cargo@v1
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:
command: test
args: --all --features unstable
toolchain: nightly
override: true

- name: Install cross
run: cargo install cross

- name: check
run: |
cross check --all --target ${{ matrix.target }}
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
Expand Down
39 changes: 39 additions & 0 deletions src/sync/atomic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// `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_if::cfg_if! {
if #[cfg(not(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc")))] {
pub(crate) use std::sync::atomic::AtomicU64;
} else {
use std::sync::atomic::Ordering;
use std::sync::Mutex;

#[derive(Debug)]
pub(crate) struct AtomicU64(Mutex<u64>);

impl AtomicU64 {
pub(crate) fn new(val: u64) -> Self {
Self(Mutex::new(val))
}

pub(crate) fn load(&self, _: Ordering) -> u64 {
*self.0.lock().unwrap()
}

pub(crate) fn fetch_add(&self, val: u64, _: Ordering) -> u64 {
let mut lock = self.0.lock().unwrap();
let prev = *lock;
*lock = prev + val;
prev
}

pub(crate) fn fetch_sub(&self, val: u64, _: Ordering) -> u64 {
let mut lock = self.0.lock().unwrap();
let prev = *lock;
*lock = prev - val;
prev
}
}
}
}
1 change: 1 addition & 0 deletions src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub use barrier::{Barrier, BarrierWaitResult};
pub use mutex::{Mutex, MutexGuard};
pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};

pub(crate) mod atomic;
#[cfg(any(feature = "unstable", feature = "docs"))]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
mod barrier;
Expand Down
13 changes: 11 additions & 2 deletions src/task/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
//! A thread pool for running blocking functions asynchronously.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::atomic::Ordering;
use std::thread;
use std::time::Duration;

use crossbeam_channel::{bounded, Receiver, Sender};
use lazy_static::lazy_static;

use crate::sync::atomic::AtomicU64;
use crate::task::task::{JoinHandle, Tag};
use crate::utils::abort_on_panic;

const MAX_THREADS: u64 = 10_000;

static DYNAMIC_THREAD_COUNT: AtomicU64 = AtomicU64::new(0);
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc"))] {
lazy_static! {
static ref DYNAMIC_THREAD_COUNT: AtomicU64 = AtomicU64::new(0);
}
} else {
static DYNAMIC_THREAD_COUNT: AtomicU64 = AtomicU64::new(0);
}
}

struct Pool {
sender: Sender<async_task::Task<Tag>>,
Expand Down
13 changes: 11 additions & 2 deletions src/task/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use std::i64;
use std::mem;
use std::num::NonZeroU64;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use super::task_local;
use crate::sync::atomic::AtomicU64;
use crate::task::{Context, Poll};

/// A handle to a task.
Expand Down Expand Up @@ -112,7 +113,15 @@ pub struct TaskId(NonZeroU64);

impl TaskId {
pub(crate) fn new() -> TaskId {
static COUNTER: AtomicU64 = AtomicU64::new(1);
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "arm", target_arch = "mips", target_arch = "powerpc"))] {
lazy_static::lazy_static! {
static ref COUNTER: AtomicU64 = AtomicU64::new(1);
}
} else {
static COUNTER: AtomicU64 = AtomicU64::new(1);
}
}

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

Expand Down

0 comments on commit 958557a

Please sign in to comment.