--- jsr166/src/jsr166y/LinkedTransferQueue.java 2008/11/16 20:24:54 1.9 +++ jsr166/src/jsr166y/LinkedTransferQueue.java 2009/07/24 23:48:26 1.25 @@ -10,8 +10,6 @@ import java.util.concurrent.locks.*; import java.util.concurrent.atomic.*; import java.util.*; import java.io.*; -import sun.misc.Unsafe; -import java.lang.reflect.*; /** * An unbounded {@linkplain TransferQueue} based on linked nodes. @@ -21,7 +19,7 @@ import java.lang.reflect.*; * producer. The tail of the queue is that element that has * been on the queue the shortest time for some producer. * - *

Beware that, unlike in most collections, the size + *

Beware that, unlike in most collections, the {@code size} * method is NOT a constant-time operation. Because of the * asynchronous nature of these queues, determining the current number * of elements requires a traversal of the elements. @@ -44,7 +42,6 @@ import java.lang.reflect.*; * @since 1.7 * @author Doug Lea * @param the type of elements held in this collection - * */ public class LinkedTransferQueue extends AbstractQueue implements TransferQueue, java.io.Serializable { @@ -81,7 +78,7 @@ public class LinkedTransferQueue exte * seems not to vary with number of CPUs (beyond 2) so is just * a constant. */ - static final int maxTimedSpins = (NCPUS < 2)? 0 : 32; + static final int maxTimedSpins = (NCPUS < 2) ? 0 : 32; /** * The number of times to spin before blocking in untimed waits. @@ -103,22 +100,30 @@ public class LinkedTransferQueue exte * garbage retention. Similarly, setting the next field to this is * used as sentinel that node is off list. */ - static final class QNode extends AtomicReference { - volatile QNode next; + static final class Node extends AtomicReference { + volatile Node next; volatile Thread waiter; // to control park/unpark final boolean isData; - QNode(Object item, boolean isData) { + + Node(E item, boolean isData) { super(item); this.isData = isData; } - static final AtomicReferenceFieldUpdater + @SuppressWarnings("rawtypes") + static final AtomicReferenceFieldUpdater nextUpdater = AtomicReferenceFieldUpdater.newUpdater - (QNode.class, QNode.class, "next"); + (Node.class, Node.class, "next"); - boolean casNext(QNode cmp, QNode val) { + final boolean casNext(Node cmp, Node val) { return nextUpdater.compareAndSet(this, cmp, val); } + + final void clearNext() { + nextUpdater.lazySet(this, this); + } + + private static final long serialVersionUID = -3375979862319811754L; } /** @@ -130,28 +135,30 @@ public class LinkedTransferQueue exte // enough padding for 64bytes with 4byte refs Object p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pa, pb, pc, pd, pe; PaddedAtomicReference(T r) { super(r); } + private static final long serialVersionUID = 8170090609809740854L; } /** head of the queue */ - private transient final PaddedAtomicReference head; + private transient final PaddedAtomicReference> head; + /** tail of the queue */ - private transient final PaddedAtomicReference tail; + private transient final PaddedAtomicReference> tail; /** * Reference to a cancelled node that might not yet have been * unlinked from queue because it was the last inserted node * when it cancelled. */ - private transient final PaddedAtomicReference cleanMe; + private transient final PaddedAtomicReference> cleanMe; /** * Tries to cas nh as new head; if successful, unlink * old head's next node to avoid garbage retention. */ - private boolean advanceHead(QNode h, QNode nh) { + private boolean advanceHead(Node h, Node nh) { if (h == head.get() && head.compareAndSet(h, nh)) { - h.next = h; // forget old next + h.clearNext(); // forget old next return true; } return false; @@ -161,25 +168,26 @@ public class LinkedTransferQueue exte * Puts or takes an item. Used for most queue operations (except * poll() and tryTransfer()). See the similar code in * SynchronousQueue for detailed explanation. + * * @param e the item or if null, signifies that this is a take * @param mode the wait mode: NOWAIT, TIMEOUT, WAIT * @param nanos timeout in nanosecs, used only if mode is TIMEOUT * @return an item, or null on failure */ - private Object xfer(Object e, int mode, long nanos) { + private E xfer(E e, int mode, long nanos) { boolean isData = (e != null); - QNode s = null; - final PaddedAtomicReference head = this.head; - final PaddedAtomicReference tail = this.tail; + Node s = null; + final PaddedAtomicReference> head = this.head; + final PaddedAtomicReference> tail = this.tail; for (;;) { - QNode t = tail.get(); - QNode h = head.get(); + Node t = tail.get(); + Node h = head.get(); if (t != null && (t == h || t.isData == isData)) { if (s == null) - s = new QNode(e, isData); - QNode last = t.next; + s = new Node(e, isData); + Node last = t.next; if (last != null) { if (t == tail.get()) tail.compareAndSet(t, last); @@ -191,13 +199,13 @@ public class LinkedTransferQueue exte } else if (h != null) { - QNode first = h.next; + Node first = h.next; if (t == tail.get() && first != null && advanceHead(h, first)) { Object x = first.get(); if (x != first && first.compareAndSet(x, e)) { LockSupport.unpark(first.waiter); - return isData? e : x; + return isData ? e : (E) x; } } } @@ -207,19 +215,19 @@ public class LinkedTransferQueue exte /** * Version of xfer for poll() and tryTransfer, which - * simplifies control paths both here and in xfer + * simplifies control paths both here and in xfer. */ - private Object fulfill(Object e) { + private E fulfill(E e) { boolean isData = (e != null); - final PaddedAtomicReference head = this.head; - final PaddedAtomicReference tail = this.tail; + final PaddedAtomicReference> head = this.head; + final PaddedAtomicReference> tail = this.tail; for (;;) { - QNode t = tail.get(); - QNode h = head.get(); + Node t = tail.get(); + Node h = head.get(); if (t != null && (t == h || t.isData == isData)) { - QNode last = t.next; + Node last = t.next; if (t == tail.get()) { if (last != null) tail.compareAndSet(t, last); @@ -228,14 +236,14 @@ public class LinkedTransferQueue exte } } else if (h != null) { - QNode first = h.next; + Node first = h.next; if (t == tail.get() && first != null && advanceHead(h, first)) { Object x = first.get(); if (x != first && first.compareAndSet(x, e)) { LockSupport.unpark(first.waiter); - return isData? e : x; + return isData ? e : (E) x; } } } @@ -253,12 +261,12 @@ public class LinkedTransferQueue exte * @param nanos timeout value * @return matched item, or s if cancelled */ - private Object awaitFulfill(QNode pred, QNode s, Object e, - int mode, long nanos) { + private E awaitFulfill(Node pred, Node s, E e, + int mode, long nanos) { if (mode == NOWAIT) return null; - long lastTime = (mode == TIMEOUT)? System.nanoTime() : 0; + long lastTime = (mode == TIMEOUT) ? System.nanoTime() : 0; Thread w = Thread.currentThread(); int spins = -1; // set to desired spin count below for (;;) { @@ -267,13 +275,13 @@ public class LinkedTransferQueue exte Object x = s.get(); if (x != e) { // Node was matched or cancelled advanceHead(pred, s); // unlink if head - if (x == s) { // was cancelled + if (x == s) { // was cancelled clean(pred, s); return null; } else if (x != null) { s.set(s); // avoid garbage retention - return x; + return (E) x; } else return e; @@ -288,9 +296,9 @@ public class LinkedTransferQueue exte } } if (spins < 0) { - QNode h = head.get(); // only spin if at head + Node h = head.get(); // only spin if at head spins = ((h != null && h.next == s) ? - (mode == TIMEOUT? + ((mode == TIMEOUT) ? maxTimedSpins : maxUntimedSpins) : 0); } if (spins > 0) @@ -298,14 +306,12 @@ public class LinkedTransferQueue exte else if (s.waiter == null) s.waiter = w; else if (mode != TIMEOUT) { - // LockSupport.park(this); - LockSupport.park(); // allows run on java5 + LockSupport.park(this); s.waiter = null; spins = -1; } else if (nanos > spinForTimeoutThreshold) { - // LockSupport.parkNanos(this, nanos); - LockSupport.parkNanos(nanos); + LockSupport.parkNanos(this, nanos); s.waiter = null; spins = -1; } @@ -313,18 +319,18 @@ public class LinkedTransferQueue exte } /** - * Returns validated tail for use in cleaning methods + * Returns validated tail for use in cleaning methods. */ - private QNode getValidatedTail() { + private Node getValidatedTail() { for (;;) { - QNode h = head.get(); - QNode first = h.next; + Node h = head.get(); + Node first = h.next; if (first != null && first.next == first) { // help advance advanceHead(h, first); continue; } - QNode t = tail.get(); - QNode last = t.next; + Node t = tail.get(); + Node last = t.next; if (t == tail.get()) { if (last != null) tail.compareAndSet(t, last); // help advance @@ -332,20 +338,25 @@ public class LinkedTransferQueue exte return t; } } - } + } /** * Gets rid of cancelled node s with original predecessor pred. + * * @param pred predecessor of cancelled node * @param s the cancelled node */ - private void clean(QNode pred, QNode s) { + private void clean(Node pred, Node s) { Thread w = s.waiter; if (w != null) { // Wake up thread s.waiter = null; if (w != Thread.currentThread()) LockSupport.unpark(w); } + + if (pred == null) + return; + /* * At any given time, exactly one node on list cannot be * deleted -- the last inserted node. To accommodate this, if @@ -355,11 +366,11 @@ public class LinkedTransferQueue exte * processed, so this always terminates. */ while (pred.next == s) { - QNode oldpred = reclean(); // First, help get rid of cleanMe - QNode t = getValidatedTail(); + Node oldpred = reclean(); // First, help get rid of cleanMe + Node t = getValidatedTail(); if (s != t) { // If not tail, try to unsplice - QNode sn = s.next; // s.next == s means s already off list - if (sn == s || pred.casNext(s, sn)) + Node sn = s.next; // s.next == s means s already off list + if (sn == s || pred.casNext(s, sn)) break; } else if (oldpred == pred || // Already saved @@ -371,10 +382,11 @@ public class LinkedTransferQueue exte /** * Tries to unsplice the cancelled node held in cleanMe that was * previously uncleanable because it was at tail. + * * @return current cleanMe node (or null) */ - private QNode reclean() { - /* + private Node reclean() { + /* * cleanMe is, or at one time was, predecessor of cancelled * node s that was the tail so could not be unspliced. If s * is no longer the tail, try to unsplice if necessary and @@ -383,14 +395,14 @@ public class LinkedTransferQueue exte * points to a cancelled node that must be unspliced -- if * not, we can (must) clear cleanMe without unsplicing. * This can loop only due to contention on casNext or - * clearing cleanMe. + * clearing cleanMe. */ - QNode pred; + Node pred; while ((pred = cleanMe.get()) != null) { - QNode t = getValidatedTail(); - QNode s = pred.next; - if (s != t) { - QNode sn; + Node t = getValidatedTail(); + Node s = pred.next; + if (s != t) { + Node sn; if (s == null || s == pred || s.get() != s || (sn = s.next) == s || pred.casNext(s, sn)) cleanMe.compareAndSet(pred, null); @@ -402,19 +414,20 @@ public class LinkedTransferQueue exte } /** - * Creates an initially empty LinkedTransferQueue. + * Creates an initially empty {@code LinkedTransferQueue}. */ public LinkedTransferQueue() { - QNode dummy = new QNode(null, false); - head = new PaddedAtomicReference(dummy); - tail = new PaddedAtomicReference(dummy); - cleanMe = new PaddedAtomicReference(null); + Node dummy = new Node(null, false); + head = new PaddedAtomicReference>(dummy); + tail = new PaddedAtomicReference>(dummy); + cleanMe = new PaddedAtomicReference>(null); } /** - * Creates a LinkedTransferQueue + * Creates a {@code LinkedTransferQueue} * initially containing the elements of the given collection, * added in traversal order of the collection's iterator. + * * @param c the collection of elements to initially contain * @throws NullPointerException if the specified collection or any * of its elements are null @@ -444,6 +457,12 @@ public class LinkedTransferQueue exte return true; } + public boolean add(E e) { + if (e == null) throw new NullPointerException(); + xfer(e, NOWAIT, 0); + return true; + } + public void transfer(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); if (xfer(e, WAIT, 0) == null) { @@ -470,7 +489,7 @@ public class LinkedTransferQueue exte public E take() throws InterruptedException { Object e = xfer(null, WAIT, 0); if (e != null) - return (E)e; + return (E) e; Thread.interrupted(); throw new InterruptedException(); } @@ -478,12 +497,12 @@ public class LinkedTransferQueue exte public E poll(long timeout, TimeUnit unit) throws InterruptedException { Object e = xfer(null, TIMEOUT, unit.toNanos(timeout)); if (e != null || !Thread.interrupted()) - return (E)e; + return (E) e; throw new InterruptedException(); } public E poll() { - return (E)fulfill(null); + return fulfill(null); } public int drainTo(Collection c) { @@ -517,15 +536,15 @@ public class LinkedTransferQueue exte // Traversal-based methods /** - * Return head after performing any outstanding helping steps + * Returns head after performing any outstanding helping steps. */ - private QNode traversalHead() { + private Node traversalHead() { for (;;) { - QNode t = tail.get(); - QNode h = head.get(); + Node t = tail.get(); + Node h = head.get(); if (h != null && t != null) { - QNode last = t.next; - QNode first = h.next; + Node last = t.next; + Node first = h.next; if (t == tail.get()) { if (last != null) tail.compareAndSet(t, last); @@ -540,6 +559,7 @@ public class LinkedTransferQueue exte return h; } } + reclean(); } } @@ -556,63 +576,75 @@ public class LinkedTransferQueue exte * if subsequently removed. */ class Itr implements Iterator { - QNode nextNode; // Next node to return next - QNode currentNode; // last returned node, for remove() - QNode prevNode; // predecessor of last returned node - E nextItem; // Cache of next item, once commited to in next + Node next; // node to return next + Node pnext; // predecessor of next + Node snext; // successor of next + Node curr; // last returned node, for remove() + Node pcurr; // predecessor of curr, for remove() + E nextItem; // Cache of next item, once committed to in next Itr() { - nextNode = traversalHead(); - advance(); + findNext(); } - E advance() { - prevNode = currentNode; - currentNode = nextNode; - E x = nextItem; - - QNode p = nextNode.next; + /** + * Ensures next points to next valid node, or null if none. + */ + void findNext() { for (;;) { - if (p == null || !p.isData) { - nextNode = null; - nextItem = null; - return x; - } - Object item = p.get(); - if (item != p && item != null) { - nextNode = p; - nextItem = (E)item; - return x; + Node pred = pnext; + Node q = next; + if (pred == null || pred == q) { + pred = traversalHead(); + q = pred.next; + } + if (q == null || !q.isData) { + next = null; + return; + } + Object x = q.get(); + Node s = q.next; + if (x != null && q != x && q != s) { + nextItem = (E) x; + snext = s; + pnext = pred; + next = q; + return; } - prevNode = p; - p = p.next; + pnext = q; + next = s; } } public boolean hasNext() { - return nextNode != null; + return next != null; } public E next() { - if (nextNode == null) throw new NoSuchElementException(); - return advance(); + if (next == null) throw new NoSuchElementException(); + pcurr = pnext; + curr = next; + pnext = next; + next = snext; + E x = nextItem; + findNext(); + return x; } public void remove() { - QNode p = currentNode; - QNode prev = prevNode; - if (prev == null || p == null) + Node p = curr; + if (p == null) throw new IllegalStateException(); Object x = p.get(); if (x != null && x != p && p.compareAndSet(x, p)) - clean(prev, p); + clean(pcurr, p); } } public E peek() { for (;;) { - QNode h = traversalHead(); - QNode p = h.next; + Node h = traversalHead(); + Node p = h.next; if (p == null) return null; Object x = p.get(); @@ -620,15 +652,15 @@ public class LinkedTransferQueue exte if (!p.isData) return null; if (x != null) - return (E)x; + return (E) x; } } } public boolean isEmpty() { for (;;) { - QNode h = traversalHead(); - QNode p = h.next; + Node h = traversalHead(); + Node p = h.next; if (p == null) return true; Object x = p.get(); @@ -643,8 +675,8 @@ public class LinkedTransferQueue exte public boolean hasWaitingConsumer() { for (;;) { - QNode h = traversalHead(); - QNode p = h.next; + Node h = traversalHead(); + Node p = h.next; if (p == null) return false; Object x = p.get(); @@ -655,8 +687,8 @@ public class LinkedTransferQueue exte /** * Returns the number of elements in this queue. If this queue - * contains more than Integer.MAX_VALUE elements, returns - * Integer.MAX_VALUE. + * contains more than {@code Integer.MAX_VALUE} elements, returns + * {@code Integer.MAX_VALUE}. * *

Beware that, unlike in most collections, this method is * NOT a constant-time operation. Because of the @@ -667,8 +699,8 @@ public class LinkedTransferQueue exte */ public int size() { int count = 0; - QNode h = traversalHead(); - for (QNode p = h.next; p != null && p.isData; p = p.next) { + Node h = traversalHead(); + for (Node p = h.next; p != null && p.isData; p = p.next) { Object x = p.get(); if (x != null && x != p) { if (++count == Integer.MAX_VALUE) // saturated @@ -680,8 +712,8 @@ public class LinkedTransferQueue exte public int getWaitingConsumerCount() { int count = 0; - QNode h = traversalHead(); - for (QNode p = h.next; p != null && !p.isData; p = p.next) { + Node h = traversalHead(); + for (Node p = h.next; p != null && !p.isData; p = p.next) { if (p.get() == null) { if (++count == Integer.MAX_VALUE) break; @@ -694,18 +726,40 @@ public class LinkedTransferQueue exte return Integer.MAX_VALUE; } + public boolean remove(Object o) { + if (o == null) + return false; + for (;;) { + Node pred = traversalHead(); + for (;;) { + Node q = pred.next; + if (q == null || !q.isData) + return false; + if (q == pred) // restart + break; + Object x = q.get(); + if (x != null && x != q && o.equals(x) && + q.compareAndSet(x, q)) { + clean(pred, q); + return true; + } + pred = q; + } + } + } + /** * Save the state to a stream (that is, serialize it). * - * @serialData All of the elements (each an E) in + * @serialData All of the elements (each an {@code E}) in * the proper order, followed by a null * @param s the stream */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.defaultWriteObject(); - for (Iterator it = iterator(); it.hasNext(); ) - s.writeObject(it.next()); + for (E e : this) + s.writeObject(e); // Use trailing null as sentinel s.writeObject(null); } @@ -713,6 +767,7 @@ public class LinkedTransferQueue exte /** * Reconstitute the Queue instance from a stream (that is, * deserialize it). + * * @param s the stream */ private void readObject(java.io.ObjectInputStream s) @@ -720,7 +775,7 @@ public class LinkedTransferQueue exte s.defaultReadObject(); resetHeadAndTail(); for (;;) { - E item = (E)s.readObject(); + @SuppressWarnings("unchecked") E item = (E) s.readObject(); if (item == null) break; else @@ -728,43 +783,60 @@ public class LinkedTransferQueue exte } } - // Support for resetting head/tail while deserializing + private void resetHeadAndTail() { + Node dummy = new Node(null, false); + UNSAFE.putObjectVolatile(this, headOffset, + new PaddedAtomicReference>(dummy)); + UNSAFE.putObjectVolatile(this, tailOffset, + new PaddedAtomicReference>(dummy)); + UNSAFE.putObjectVolatile(this, cleanMeOffset, + new PaddedAtomicReference>(null)); + } - // Temporary Unsafe mechanics for preliminary release - private static final Unsafe _unsafe; - private static final long headOffset; - private static final long tailOffset; - private static final long cleanMeOffset; - static { + // Unsafe mechanics for jsr166y 3rd party package. + private static sun.misc.Unsafe getUnsafe() { try { - if (LinkedTransferQueue.class.getClassLoader() != null) { - Field f = Unsafe.class.getDeclaredField("theUnsafe"); - f.setAccessible(true); - _unsafe = (Unsafe)f.get(null); + return sun.misc.Unsafe.getUnsafe(); + } catch (SecurityException se) { + try { + return java.security.AccessController.doPrivileged + (new java.security.PrivilegedExceptionAction() { + public sun.misc.Unsafe run() throws Exception { + return getUnsafeByReflection(); + }}); + } catch (java.security.PrivilegedActionException e) { + throw new RuntimeException("Could not initialize intrinsics", + e.getCause()); } - else - _unsafe = Unsafe.getUnsafe(); - headOffset = _unsafe.objectFieldOffset - (LinkedTransferQueue.class.getDeclaredField("head")); - tailOffset = _unsafe.objectFieldOffset - (LinkedTransferQueue.class.getDeclaredField("tail")); - cleanMeOffset = _unsafe.objectFieldOffset - (LinkedTransferQueue.class.getDeclaredField("cleanMe")); - } catch (Exception e) { - throw new RuntimeException("Could not initialize intrinsics", e); } } - private void resetHeadAndTail() { - QNode dummy = new QNode(null, false); - _unsafe.putObjectVolatile(this, headOffset, - new PaddedAtomicReference(dummy)); - _unsafe.putObjectVolatile(this, tailOffset, - new PaddedAtomicReference(dummy)); - _unsafe.putObjectVolatile(this, cleanMeOffset, - new PaddedAtomicReference(null)); + private static sun.misc.Unsafe getUnsafeByReflection() + throws NoSuchFieldException, IllegalAccessException { + java.lang.reflect.Field f = + sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); + f.setAccessible(true); + return (sun.misc.Unsafe) f.get(null); + } + private static long fieldOffset(String fieldName, Class klazz) { + try { + return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); + } catch (NoSuchFieldException e) { + // Convert Exception to Error + NoSuchFieldError error = new NoSuchFieldError(fieldName); + error.initCause(e); + throw error; + } } + private static final sun.misc.Unsafe UNSAFE = getUnsafe(); + static final long headOffset = + fieldOffset("head", LinkedTransferQueue.class); + static final long tailOffset = + fieldOffset("tail", LinkedTransferQueue.class); + static final long cleanMeOffset = + fieldOffset("cleanMe", LinkedTransferQueue.class); + }