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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! wfqueue implemention

#![deny(unsafe_code)]

use std::cell::Cell;
use std::num::NonZeroUsize;
use cache_padded::CachePadded;
use crate::loom::sync::atomic::{ AtomicUsize, Ordering };


#[cfg(not(feature = "loom"))]
const MAX_TRY: usize = 128;

#[cfg(feature = "loom")]
const MAX_TRY: usize = 1;

pub struct WfQueue {
    head: CachePadded<AtomicUsize>,
    tail: CachePadded<AtomicUsize>,
    nptr: Box<[CachePadded<AtomicUsize>]>
}

pub struct EnqueueCtx {
    index: Index
}

pub struct DequeueCtx {
    index: Index
}

struct Index(Cell<usize>);

impl WfQueue {
    pub fn new(cap: usize) -> WfQueue {
        assert_ne!(cap, 0, "The capacity is not allowed to be zero");

        let mut nptr = Vec::with_capacity(cap);

        for _ in 0..cap {
            nptr.push(CachePadded::new(AtomicUsize::new(0)));
        }

        let nptr = nptr.into_boxed_slice();

        WfQueue {
            head: CachePadded::new(AtomicUsize::new(0)),
            tail: CachePadded::new(AtomicUsize::new(0)),
            nptr
        }
    }

    pub fn len(&self) -> usize {
        let head = self.head.load(Ordering::Relaxed);
        let tail = self.tail.load(Ordering::Relaxed);

        head.saturating_sub(tail)
    }

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

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

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

    /// Each queue should use a fixed enqueue context in each thread.
    /// If the wrong context is used, it may lead to logic confusion.
    pub fn try_enqueue(&self, ctx: &EnqueueCtx, val: NonZeroUsize) -> bool {
        macro_rules! enqueue {
            ( $ptr:expr, $val:expr ; $ok:expr ; $fail:expr ) => {
                let mut curr = $ptr.load(Ordering::Acquire);

                for _ in 0..MAX_TRY {
                    if curr == 0 {
                        if $ptr.compare_exchange_weak(curr, $val.get(), Ordering::Release, Ordering::Relaxed).is_ok() {
                            $ok;
                            return true;
                        }
                    } else {
                        curr = $ptr.load(Ordering::Acquire);
                    }
                }

                $fail;
                return false;
            }
        }

        if let Some(index) = ctx.index.load() {
            let nptr = &self.nptr[index];

            enqueue!{
                nptr, val;
                {
                    ctx.index.clean();
                };
                {}
            }
        }

        let head = self.head.fetch_add(1, Ordering::Relaxed) % self.nptr.len();
        let nptr = &self.nptr[head];

        enqueue!{
            nptr, val;
            {};
            {
                ctx.index.store(head);
            }
        }
    }

    /// Each queue should use a fixed enqueue context in each thread.
    /// If the wrong context is used, it may lead to logic confusion.
    pub fn try_dequeue(&self, ctx: &DequeueCtx) -> Option<NonZeroUsize> {
        macro_rules! dequeue {
            ( $ptr:expr ; $ok:expr ; $fail:expr ) => {
                let mut val = $ptr.load(Ordering::Acquire);

                for _ in 0..MAX_TRY {
                    match NonZeroUsize::new(val) {
                        Some(nzval) => if $ptr.compare_exchange(val, 0, Ordering::Release, Ordering::Relaxed).is_ok() {
                            $ok;
                            return Some(nzval);
                        },
                        None => {
                            val = $ptr.load(Ordering::Acquire);
                        }
                    }
                }

                $fail;
                return None;
            }
        }

        if let Some(index) = ctx.index.load() {
            let nptr = &self.nptr[index];

            dequeue!{
                nptr;
                {
                    ctx.index.clean();
                };
                {}
            }
        }

        let tail = self.tail.fetch_add(1, Ordering::Relaxed) % self.nptr.len();
        let nptr = &self.nptr[tail];

        dequeue!{
            nptr;
            {};
            {
                ctx.index.store(tail);
            }
        }
    }
}

impl EnqueueCtx {
    pub const fn new() -> EnqueueCtx {
        EnqueueCtx { index: Index(Cell::new(0)) }
    }
}

impl DequeueCtx {
    pub const fn new() -> DequeueCtx {
        DequeueCtx { index: Index(Cell::new(0)) }
    }
}

impl Index {
    #[inline]
    pub fn load(&self) -> Option<usize> {
        self.0.get().checked_sub(1)
    }

    #[inline]
    pub fn clean(&self) {
        self.0.set(0);
    }

    #[inline]
    pub fn store(&self, val: usize) {
        self.0.set(val + 1);
    }
}