Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions tokio-timer/src/atomic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! 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;

#[cfg(target_pointer_width = "64")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be #[cfg(target_has_atomic = "64")], not #[cfg(target_pointer_width = "64")], but since feature(cfg_target_has_atomic) is not stable, will use an alternative.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3e86b76

mod imp {
pub(crate) use std::sync::atomic::AtomicU64;
}

#[cfg(not(target_pointer_width = "64"))]
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 change: 1 addition & 0 deletions tokio-timer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub mod throttle;
pub mod timeout;
pub mod timer;

mod atomic;
mod delay;
mod error;
mod interval;
Expand Down
3 changes: 2 additions & 1 deletion tokio-timer/src/timer/entry.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
3 changes: 2 additions & 1 deletion tokio-timer/src/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down