--- jsr166/src/jsr166y/LinkedTransferQueue.java 2009/03/30 04:32:23 1.16 +++ jsr166/src/jsr166y/LinkedTransferQueue.java 2009/07/23 23:23:41 1.24 @@ -44,7 +44,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 +80,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. @@ -124,6 +123,7 @@ public class LinkedTransferQueue exte nextUpdater.lazySet(this, this); } + private static final long serialVersionUID = -3375979862319811754L; } /** @@ -135,11 +135,13 @@ 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; + /** tail of the queue */ private transient final PaddedAtomicReference tail; @@ -166,6 +168,7 @@ 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 @@ -202,7 +205,7 @@ public class LinkedTransferQueue exte Object x = first.get(); if (x != first && first.compareAndSet(x, e)) { LockSupport.unpark(first.waiter); - return isData? e : x; + return isData ? e : x; } } } @@ -212,7 +215,7 @@ 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) { boolean isData = (e != null); @@ -240,7 +243,7 @@ public class LinkedTransferQueue exte Object x = first.get(); if (x != first && first.compareAndSet(x, e)) { LockSupport.unpark(first.waiter); - return isData? e : x; + return isData ? e : x; } } } @@ -263,7 +266,7 @@ public class LinkedTransferQueue exte 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 (;;) { @@ -272,7 +275,7 @@ 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; } @@ -295,7 +298,7 @@ public class LinkedTransferQueue exte if (spins < 0) { QNode h = head.get(); // only spin if at head spins = ((h != null && h.next == s) ? - (mode == TIMEOUT? + ((mode == TIMEOUT) ? maxTimedSpins : maxUntimedSpins) : 0); } if (spins > 0) @@ -316,7 +319,7 @@ public class LinkedTransferQueue exte } /** - * Returns validated tail for use in cleaning methods + * Returns validated tail for use in cleaning methods. */ private QNode getValidatedTail() { for (;;) { @@ -339,6 +342,7 @@ public class LinkedTransferQueue exte /** * Gets rid of cancelled node s with original predecessor pred. + * * @param pred predecessor of cancelled node * @param s the cancelled node */ @@ -378,6 +382,7 @@ 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() { @@ -422,6 +427,7 @@ public class LinkedTransferQueue exte * 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 @@ -483,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(); } @@ -491,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 (E) fulfill(null); } public int drainTo(Collection c) { @@ -530,7 +536,7 @@ 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() { for (;;) { @@ -575,14 +581,14 @@ public class LinkedTransferQueue exte QNode snext; // successor of next QNode curr; // last returned node, for remove() QNode pcurr; // predecessor of curr, for remove() - E nextItem; // Cache of next item, once commited to in next + E nextItem; // Cache of next item, once committed to in next Itr() { findNext(); } /** - * Ensure next points to next valid node, or null if none. + * Ensures next points to next valid node, or null if none. */ void findNext() { for (;;) { @@ -599,7 +605,7 @@ public class LinkedTransferQueue exte Object x = q.get(); QNode s = q.next; if (x != null && q != x && q != s) { - nextItem = (E)x; + nextItem = (E) x; snext = s; pnext = pred; next = q; @@ -646,7 +652,7 @@ public class LinkedTransferQueue exte if (!p.isData) return null; if (x != null) - return (E)x; + return (E) x; } } } @@ -732,7 +738,7 @@ public class LinkedTransferQueue exte if (q == pred) // restart break; Object x = q.get(); - if (x != null && x != q && o.equals(x) && + if (x != null && x != q && o.equals(x) && q.compareAndSet(x, q)) { clean(pred, q); return true; @@ -761,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) @@ -768,7 +775,7 @@ public class LinkedTransferQueue exte s.defaultReadObject(); resetHeadAndTail(); for (;;) { - E item = (E)s.readObject(); + E item = (E) s.readObject(); if (item == null) break; else @@ -780,11 +787,11 @@ public class LinkedTransferQueue exte // Support for resetting head/tail while deserializing private void resetHeadAndTail() { QNode dummy = new QNode(null, false); - _unsafe.putObjectVolatile(this, headOffset, + UNSAFE.putObjectVolatile(this, headOffset, new PaddedAtomicReference(dummy)); - _unsafe.putObjectVolatile(this, tailOffset, + UNSAFE.putObjectVolatile(this, tailOffset, new PaddedAtomicReference(dummy)); - _unsafe.putObjectVolatile(this, cleanMeOffset, + UNSAFE.putObjectVolatile(this, cleanMeOffset, new PaddedAtomicReference(null)); } @@ -814,17 +821,17 @@ public class LinkedTransferQueue exte private static long fieldOffset(String fieldName) throws NoSuchFieldException { - return _unsafe.objectFieldOffset + return UNSAFE.objectFieldOffset (LinkedTransferQueue.class.getDeclaredField(fieldName)); } - private static final Unsafe _unsafe; + private static final Unsafe UNSAFE; private static final long headOffset; private static final long tailOffset; private static final long cleanMeOffset; static { try { - _unsafe = getUnsafe(); + UNSAFE = getUnsafe(); headOffset = fieldOffset("head"); tailOffset = fieldOffset("tail"); cleanMeOffset = fieldOffset("cleanMe");