timer: use our own AtomicU64 on targets with target_has_atomic less than 64 (#1538)

This commit is contained in:
Taiki Endo
2019-09-13 10:18:32 -07:00
committed by Carl Lerche
parent 578a9aec16
commit efb27731ad
6 changed files with 94 additions and 18 deletions
+59
View File
@@ -0,0 +1,59 @@
//! Implementation of an atomic u64 cell. On 64 bit platforms, this is a
//! re-export of `AtomicU64`. On 32 bit platforms, this is implemented using a
//! `Mutex`.
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")]`.
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
mod imp {
pub(crate) use std::sync::atomic::AtomicU64;
}
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
mod imp {
use std::sync::atomic::Ordering;
use std::sync::Mutex;
#[derive(Debug)]
pub(crate) struct AtomicU64 {
inner: Mutex<u64>,
}
impl AtomicU64 {
pub(crate) fn new(val: u64) -> AtomicU64 {
AtomicU64 {
inner: Mutex::new(val),
}
}
pub(crate) fn load(&self, _: Ordering) -> u64 {
*self.inner.lock().unwrap()
}
pub(crate) fn store(&self, val: u64, _: Ordering) {
*self.inner.lock().unwrap() = val;
}
pub(crate) fn fetch_or(&self, val: u64, _: Ordering) -> u64 {
let mut lock = self.inner.lock().unwrap();
let prev = *lock;
*lock = prev | val;
prev
}
pub(crate) fn compare_and_swap(&self, old: u64, new: u64, _: Ordering) -> u64 {
let mut lock = self.inner.lock().unwrap();
let prev = *lock;
if prev != old {
return prev;
}
*lock = new;
prev
}
}
}
+1
View File
@@ -43,6 +43,7 @@ pub mod throttle;
pub mod timeout;
pub mod timer;
mod atomic;
mod delay;
mod error;
mod interval;
+2 -1
View File
@@ -1,10 +1,11 @@
use crate::atomic::AtomicU64;
use crate::timer::{HandlePriv, Inner};
use crate::Error;
use crossbeam_utils::CachePadded;
use std::cell::UnsafeCell;
use std::ptr;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::{Relaxed, SeqCst};
use std::sync::atomic::{AtomicBool, AtomicU64};
use std::sync::{Arc, Weak};
use std::task::{self, Poll};
use std::time::{Duration, Instant};
+2 -1
View File
@@ -49,10 +49,11 @@ pub use self::handle::{set_default, Handle};
pub use self::now::{Now, SystemNow};
pub(crate) use self::registration::Registration;
use crate::atomic::AtomicU64;
use crate::wheel;
use crate::Error;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::atomic::{AtomicU64, AtomicUsize};
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::usize;