1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#[cfg(not(feature = "loom"))]
mod loom {
    pub use std::sync;
}

#[cfg(feature = "loom")]
use loom;

pub mod queue;

use std::num::NonZeroUsize;
use std::marker::PhantomData;
use cache_padded::CachePadded;
use per_thread_object::ThreadLocal;


pub struct WfQueue<T: Queueable> {
    queue: queue::WfQueue,
    context: ThreadLocal<Context>,
    _phantom: PhantomData<T>
}

pub trait Queueable {
    fn into_nonzero(self) -> NonZeroUsize;

    /// # Safety
    ///
    /// Unsafe conversion from `NonZeroUsize`.
    unsafe fn from_nonzero(n: NonZeroUsize) -> Self;
}

struct Context {
    enq: CachePadded<queue::EnqueueCtx>,
    deq: CachePadded<queue::DequeueCtx>
}

impl<T: Queueable> WfQueue<T> {
    pub fn new(cap: usize) -> WfQueue<T> {
        WfQueue {
            queue: queue::WfQueue::new(cap),
            context: ThreadLocal::new(),
            _phantom: PhantomData
        }
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.queue.len()
    }

    #[inline]
    pub fn capacity(&self) -> usize {
        self.queue.capacity()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }

    #[inline]
    pub fn is_full(&self) -> bool {
        self.queue.is_full()
    }

    pub fn push(&self, val: T) -> Result<(), T> {
        let ctx = self.context.get_or(Context::new);
        let val = val.into_nonzero();

        if self.queue.try_enqueue(&ctx.enq, val) {
            Ok(())
        } else {
            unsafe {
                Err(T::from_nonzero(val))
            }
        }
    }

    pub fn pop(&self) -> Option<T> {
        let ctx = self.context.get_or(Context::new);
        let val = self.queue.try_dequeue(&ctx.deq)?;

        unsafe {
            Some(T::from_nonzero(val))
        }
    }
}

impl<T: Queueable> Drop for WfQueue<T> {
    #[inline]
    fn drop(&mut self) {
        while self.pop().is_some() {}
    }
}

impl Context {
    pub const fn new() -> Context {
        Context {
            enq: CachePadded::new(queue::EnqueueCtx::new()),
            deq: CachePadded::new(queue::DequeueCtx::new())
        }
    }
}

// impl Queueable

impl Queueable for NonZeroUsize {
    #[inline]
    fn into_nonzero(self) -> NonZeroUsize {
        self
    }

    #[inline]
    unsafe fn from_nonzero(n: NonZeroUsize) -> Self {
        n
    }
}

impl<T> Queueable for &'static T {
    #[inline]
    fn into_nonzero(self) -> NonZeroUsize {
        unsafe {
            NonZeroUsize::new_unchecked(self as *const T as usize)
        }
    }

    #[inline]
    unsafe fn from_nonzero(n: NonZeroUsize) -> Self {
        &*(n.get() as *const T)
    }
}

impl<T> Queueable for Box<T> {
    #[inline]
    fn into_nonzero(self) -> NonZeroUsize {
        unsafe {
            NonZeroUsize::new_unchecked(Box::into_raw(self) as usize)
        }
    }

    #[inline]
    unsafe fn from_nonzero(n: NonZeroUsize) -> Self {
        Box::from_raw(n.get() as *mut _)
    }
}

use loom::sync::Arc;

impl<T> Queueable for Arc<T> {
    #[inline]
    fn into_nonzero(self) -> NonZeroUsize {
        unsafe {
            NonZeroUsize::new_unchecked(Arc::into_raw(self) as usize)
        }
    }

    #[inline]
    unsafe fn from_nonzero(n: NonZeroUsize) -> Self {
        Arc::from_raw(n.get() as *mut _)
    }
}

use std::ptr::NonNull;

impl<T> Queueable for NonNull<T> {
    #[inline]
    fn into_nonzero(self) -> NonZeroUsize {
        unsafe {
            NonZeroUsize::new_unchecked(self.as_ptr() as usize)
        }
    }

    #[inline]
    unsafe fn from_nonzero(n: NonZeroUsize) -> Self {
        NonNull::new_unchecked(n.get() as *mut _)
    }
}