|
| 1 | +/* Copyright (c) 2010-2011 Dmitry Vyukov. All rights reserved. |
| 2 | + * Redistribution and use in source and binary forms, with or without |
| 3 | + * modification, are permitted provided that the following conditions are met: |
| 4 | + * |
| 5 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 6 | + * this list of conditions and the following disclaimer. |
| 7 | + * |
| 8 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer in the |
| 10 | + * documentation and/or other materials provided with the distribution. |
| 11 | + * |
| 12 | + * THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV "AS IS" AND ANY EXPRESS OR IMPLIED |
| 13 | + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 14 | + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 15 | + * EVENT SHALL DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 16 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 17 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 18 | + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 19 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 20 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 21 | + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 22 | + * |
| 23 | + * The views and conclusions contained in the software and documentation are |
| 24 | + * those of the authors and should not be interpreted as representing official |
| 25 | + * policies, either expressed or implied, of Dmitry Vyukov. |
| 26 | + */ |
| 27 | + |
| 28 | +//! A mostly lock-free multi-producer, single consumer queue. |
| 29 | +//! |
| 30 | +//! This module implements an intrusive MPSC queue. This queue is incredibly |
| 31 | +//! unsafe (due to use of unsafe pointers for nodes), and hence is not public. |
| 32 | +
|
| 33 | +// http://www.1024cores.net/home/lock-free-algorithms |
| 34 | +// /queues/intrusive-mpsc-node-based-queue |
| 35 | + |
| 36 | +use std::cast; |
| 37 | +use std::sync::atomics; |
| 38 | + |
| 39 | +// NB: all links are done as AtomicUint instead of AtomicPtr to allow for static |
| 40 | +// initialization. |
| 41 | + |
| 42 | +pub struct Node<T> { |
| 43 | + next: atomics::AtomicUint, |
| 44 | + data: T, |
| 45 | +} |
| 46 | + |
| 47 | +pub struct DummyNode { |
| 48 | + next: atomics::AtomicUint, |
| 49 | +} |
| 50 | + |
| 51 | +pub struct Queue<T> { |
| 52 | + head: atomics::AtomicUint, |
| 53 | + tail: *mut Node<T>, |
| 54 | + stub: DummyNode, |
| 55 | +} |
| 56 | + |
| 57 | +impl<T: Send> Queue<T> { |
| 58 | + pub fn new() -> Queue<T> { |
| 59 | + Queue { |
| 60 | + head: atomics::AtomicUint::new(0), |
| 61 | + tail: 0 as *mut Node<T>, |
| 62 | + stub: DummyNode { |
| 63 | + next: atomics::AtomicUint::new(0), |
| 64 | + }, |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + pub unsafe fn push(&mut self, node: *mut Node<T>) { |
| 69 | + (*node).next.store(0, atomics::Release); |
| 70 | + let prev = self.head.swap(node as uint, atomics::AcqRel); |
| 71 | + |
| 72 | + // Note that this code is slightly modified to allow static |
| 73 | + // initialization of these queues with rust's flavor of static |
| 74 | + // initialization. |
| 75 | + if prev == 0 { |
| 76 | + self.stub.next.store(node as uint, atomics::Release); |
| 77 | + } else { |
| 78 | + let prev = prev as *mut Node<T>; |
| 79 | + (*prev).next.store(node as uint, atomics::Release); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// You'll note that the other MPSC queue in std::sync is non-intrusive and |
| 84 | + /// returns a `PopResult` here to indicate when the queue is inconsistent. |
| 85 | + /// An "inconsistent state" in the other queue means that a pusher has |
| 86 | + /// pushed, but it hasn't finished linking the rest of the chain. |
| 87 | + /// |
| 88 | + /// This queue also suffers from this problem, but I currently haven't been |
| 89 | + /// able to detangle when this actually happens. This code is translated |
| 90 | + /// verbatim from the website above, and is more complicated than the |
| 91 | + /// non-intrusive version. |
| 92 | + /// |
| 93 | + /// Right now consumers of this queue must be ready for this fact. Just |
| 94 | + /// because `pop` returns `None` does not mean that there is not data |
| 95 | + /// on the queue. |
| 96 | + pub unsafe fn pop(&mut self) -> Option<*mut Node<T>> { |
| 97 | + let tail = self.tail; |
| 98 | + let mut tail = if !tail.is_null() {tail} else { |
| 99 | + cast::transmute(&self.stub) |
| 100 | + }; |
| 101 | + let mut next = (*tail).next(atomics::Relaxed); |
| 102 | + if tail as uint == &self.stub as *DummyNode as uint { |
| 103 | + if next.is_null() { |
| 104 | + return None; |
| 105 | + } |
| 106 | + self.tail = next; |
| 107 | + tail = next; |
| 108 | + next = (*next).next(atomics::Relaxed); |
| 109 | + } |
| 110 | + if !next.is_null() { |
| 111 | + self.tail = next; |
| 112 | + return Some(tail); |
| 113 | + } |
| 114 | + let head = self.head.load(atomics::Acquire) as *mut Node<T>; |
| 115 | + if tail != head { |
| 116 | + return None; |
| 117 | + } |
| 118 | + let stub = cast::transmute(&self.stub); |
| 119 | + self.push(stub); |
| 120 | + next = (*tail).next(atomics::Relaxed); |
| 121 | + if !next.is_null() { |
| 122 | + self.tail = next; |
| 123 | + return Some(tail); |
| 124 | + } |
| 125 | + return None |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +impl<T: Send> Node<T> { |
| 130 | + pub fn new(t: T) -> Node<T> { |
| 131 | + Node { |
| 132 | + data: t, |
| 133 | + next: atomics::AtomicUint::new(0), |
| 134 | + } |
| 135 | + } |
| 136 | + pub unsafe fn next(&mut self, ord: atomics::Ordering) -> *mut Node<T> { |
| 137 | + cast::transmute::<uint, *mut Node<T>>(self.next.load(ord)) |
| 138 | + } |
| 139 | +} |
0 commit comments