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

Avoid lossy ptr-int transmutes by using AtomicPtr #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 14 additions & 14 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,8 @@

use crate::Instant;
use std::cmp::Ordering;
use std::mem;
use std::pin::Pin;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::{AtomicPtr, AtomicUsize};
use std::sync::atomic::Ordering::SeqCst;
use std::sync::{Arc, Mutex, Weak};
use std::task::{Context, Poll};
Expand Down Expand Up @@ -484,7 +483,8 @@ impl Ord for HeapTimer {
}
}

static HANDLE_FALLBACK: AtomicUsize = AtomicUsize::new(0);
static HANDLE_FALLBACK: AtomicPtr<Inner> = AtomicPtr::new(EMPTY_HANDLE);
const EMPTY_HANDLE: *mut Inner = std::ptr::null_mut();

/// Error returned from `TimerHandle::set_fallback`.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -515,23 +515,23 @@ impl TimerHandle {
/// successful then no future calls may succeed.
pub fn set_as_global_fallback(self) -> Result<(), SetDefaultError> {
unsafe {
let val = self.into_usize();
match HANDLE_FALLBACK.compare_exchange(0, val, SeqCst, SeqCst) {
let val = self.into_raw();
match HANDLE_FALLBACK.compare_exchange(EMPTY_HANDLE, val, SeqCst, SeqCst) {
Ok(_) => Ok(()),
Err(_) => {
drop(TimerHandle::from_usize(val));
drop(TimerHandle::from_raw(val));
Err(SetDefaultError(()))
}
}
}
}

fn into_usize(self) -> usize {
unsafe { mem::transmute::<Weak<Inner>, usize>(self.inner) }
fn into_raw(self) -> *mut Inner {
self.inner.into_raw() as *mut Inner
}

unsafe fn from_usize(val: usize) -> TimerHandle {
let inner = mem::transmute::<usize, Weak<Inner>>(val);
unsafe fn from_raw(raw: *mut Inner) -> TimerHandle {
let inner = Weak::from_raw(raw);
TimerHandle { inner }
}
}
Expand All @@ -546,7 +546,7 @@ impl Default for TimerHandle {
// actually create a helper thread then we'll just return a "defunkt"
// handle which will return errors when timer objects are attempted to
// be associated.
if fallback == 0 {
if fallback == EMPTY_HANDLE {
let helper = match global::HelperThread::new() {
Ok(helper) => helper,
Err(_) => return TimerHandle { inner: Weak::new() },
Expand All @@ -570,11 +570,11 @@ impl Default for TimerHandle {
// At this point our fallback handle global was configured so we use
// its value to reify a handle, clone it, and then forget our reified
// handle as we don't actually have an owning reference to it.
assert!(fallback != 0);
assert!(fallback != EMPTY_HANDLE);
unsafe {
let handle = TimerHandle::from_usize(fallback);
let handle = TimerHandle::from_raw(fallback);
let ret = handle.clone();
drop(handle.into_usize());
drop(handle.into_raw());
return ret;
}
}
Expand Down
34 changes: 20 additions & 14 deletions src/timer/arc_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
use std::marker;
use std::ops::Deref;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::sync::atomic::{AtomicBool, AtomicPtr};
use std::sync::Arc;

pub struct ArcList<T> {
list: AtomicUsize,
list: AtomicPtr<Node<T>>,
_marker: marker::PhantomData<T>,
}

impl<T> ArcList<T> {
pub fn new() -> ArcList<T> {
ArcList {
list: AtomicUsize::new(0),
list: AtomicPtr::new(Node::EMPTY),
_marker: marker::PhantomData,
}
}
Expand All @@ -31,12 +31,12 @@ impl<T> ArcList<T> {
return Ok(());
}
let mut head = self.list.load(SeqCst);
let node = Arc::into_raw(data.clone()) as usize;
let node = Arc::into_raw(data.clone()) as *mut Node<T>;
loop {
// If we've been sealed off, abort and return an error
if head == 1 {
if head == Node::sealed() {
unsafe {
drop(Arc::from_raw(node as *mut Node<T>));
drop(Arc::from_raw(node));
}
return Err(());
}
Expand All @@ -55,16 +55,16 @@ impl<T> ArcList<T> {
pub fn take(&self) -> ArcList<T> {
let mut list = self.list.load(SeqCst);
loop {
if list == 1 {
if list == Node::sealed() {
break;
}
match self.list.compare_exchange(list, 0, SeqCst, SeqCst) {
match self.list.compare_exchange(list, Node::EMPTY, SeqCst, SeqCst) {
Ok(_) => break,
Err(l) => list = l,
}
}
ArcList {
list: AtomicUsize::new(list),
list: AtomicPtr::new(list),
_marker: marker::PhantomData,
}
}
Expand All @@ -73,7 +73,7 @@ impl<T> ArcList<T> {
/// `push`.
pub fn take_and_seal(&self) -> ArcList<T> {
ArcList {
list: AtomicUsize::new(self.list.swap(1, SeqCst)),
list: AtomicPtr::new(self.list.swap(Node::sealed(), SeqCst)),
_marker: marker::PhantomData,
}
}
Expand All @@ -82,10 +82,10 @@ impl<T> ArcList<T> {
/// empty list.
pub fn pop(&mut self) -> Option<Arc<Node<T>>> {
let head = *self.list.get_mut();
if head == 0 || head == 1 {
if head == Node::EMPTY || head == Node::sealed() {
return None;
}
let head = unsafe { Arc::from_raw(head as *const Node<T>) };
let head = unsafe { Arc::from_raw(head) };
*self.list.get_mut() = head.next.load(SeqCst);
// At this point, the node is out of the list, so store `false` so we
// can enqueue it again and see further changes.
Expand All @@ -103,15 +103,21 @@ impl<T> Drop for ArcList<T> {
}

pub struct Node<T> {
next: AtomicUsize,
next: AtomicPtr<Node<T>>,
enqueued: AtomicBool,
data: T,
}

impl<T> Node<T> {
const EMPTY: *mut Node<T> = std::ptr::null_mut();

const fn sealed() -> *mut Node<T> {
std::ptr::null_mut::<Node<T>>().wrapping_add(1)
}

pub fn new(data: T) -> Node<T> {
Node {
next: AtomicUsize::new(0),
next: AtomicPtr::new(Node::EMPTY),
enqueued: AtomicBool::new(false),
data: data,
}
Expand Down