--- jsr166/src/jsr166y/LinkedTransferQueue.java 2009/10/30 12:06:31 1.60 +++ jsr166/src/jsr166y/LinkedTransferQueue.java 2009/11/02 00:28:28 1.61 @@ -361,14 +361,14 @@ public class LinkedTransferQueue exte * precede or follow CASes use simple relaxed forms. Other * cleanups use releasing/lazy writes. */ - static final class Node { + static final class Node { final boolean isData; // false if this is a request node volatile Object item; // initially non-null if isData; CASed to match - volatile Node next; + volatile Node next; volatile Thread waiter; // null until waiting // CAS methods for fields - final boolean casNext(Node cmp, Node val) { + final boolean casNext(Node cmp, Node val) { return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); } @@ -381,7 +381,7 @@ public class LinkedTransferQueue exte * Creates a new node. Uses relaxed write because item can only * be seen if followed by CAS. */ - Node(E item, boolean isData) { + Node(Object item, boolean isData) { UNSAFE.putObject(this, itemOffset, item); // relaxed write this.isData = isData; } @@ -457,24 +457,24 @@ public class LinkedTransferQueue exte } /** head of the queue; null until first enqueue */ - transient volatile Node head; + transient volatile Node head; /** predecessor of dangling unspliceable node */ - private transient volatile Node cleanMe; // decl here reduces contention + private transient volatile Node cleanMe; // decl here reduces contention /** tail of the queue; null until first append */ - private transient volatile Node tail; + private transient volatile Node tail; // CAS methods for fields - private boolean casTail(Node cmp, Node val) { + private boolean casTail(Node cmp, Node val) { return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val); } - private boolean casHead(Node cmp, Node val) { + private boolean casHead(Node cmp, Node val) { return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val); } - private boolean casCleanMe(Node cmp, Node val) { + private boolean casCleanMe(Node cmp, Node val) { return UNSAFE.compareAndSwapObject(this, cleanMeOffset, cmp, val); } @@ -506,20 +506,19 @@ public class LinkedTransferQueue exte private E xfer(E e, boolean haveData, int how, long nanos) { if (haveData && (e == null)) throw new NullPointerException(); - Node s = null; // the node to append, if needed + Node s = null; // the node to append, if needed retry: for (;;) { // restart on append race - for (Node h = head, p = h; p != null;) { - // find & match first node + for (Node h = head, p = h; p != null;) { // find & match first node boolean isData = p.isData; Object item = p.item; if (item != p && (item != null) == isData) { // unmatched if (isData == haveData) // can't match break; if (p.casItem(item, e)) { // match - for (Node q = p; q != h;) { - Node n = q.next; // update head by 2 + for (Node q = p; q != h;) { + Node n = q.next; // update head by 2 if (n != null) // unless singleton q = n; if (head == h && casHead(h, q)) { @@ -534,14 +533,14 @@ public class LinkedTransferQueue exte return this.cast(item); } } - Node n = p.next; + Node n = p.next; p = (p != n) ? n : (h = head); // Use head if p offlist } if (how >= ASYNC) { // No matches available if (s == null) - s = new Node(e, haveData); - Node pred = tryAppend(s, haveData); + s = new Node(e, haveData); + Node pred = tryAppend(s, haveData); if (pred == null) continue retry; // lost race vs opposite mode if (how >= SYNC) @@ -560,9 +559,9 @@ public class LinkedTransferQueue exte * different mode, else s's predecessor, or s itself if no * predecessor */ - private Node tryAppend(Node s, boolean haveData) { - for (Node t = tail, p = t;;) { // move p to last node and append - Node n, u; // temps for reads of next & tail + private Node tryAppend(Node s, boolean haveData) { + for (Node t = tail, p = t;;) { // move p to last node and append + Node n, u; // temps for reads of next & tail if (p == null && (p = head) == null) { if (casHead(null, s)) return s; // initialize @@ -598,7 +597,7 @@ public class LinkedTransferQueue exte * @param nanos timeout value * @return matched item, or e if unmatched on interrupt or timeout */ - private E awaitMatch(Node s, Node pred, E e, int how, long nanos) { + private E awaitMatch(Node s, Node pred, E e, int how, long nanos) { long lastTime = (how == TIMEOUT) ? System.nanoTime() : 0L; Thread w = Thread.currentThread(); int spins = -1; // initialized after first item and cancel checks @@ -648,7 +647,7 @@ public class LinkedTransferQueue exte * Returns spin/yield value for a node with given predecessor and * data mode. See above for explanation. */ - private static int spinsFor(Node pred, boolean haveData) { + private static int spinsFor(Node pred, boolean haveData) { if (MP && pred != null) { if (pred.isData != haveData) // phase change return FRONT_SPINS + CHAINED_SPINS; @@ -665,10 +664,10 @@ public class LinkedTransferQueue exte * or trailing node; failing on contention. */ private void shortenHeadPath() { - Node h, hn, p, q; + Node h, hn, p, q; if ((p = h = head) != null && h.isMatched() && (q = hn = h.next) != null) { - Node n; + Node n; while ((n = q.next) != q) { if (n == null || !q.isMatched()) { if (hn != q && h.next == hn) @@ -687,11 +686,11 @@ public class LinkedTransferQueue exte * Returns the first unmatched node of the given mode, or null if * none. Used by methods isEmpty, hasWaitingConsumer. */ - private Node firstOfMode(boolean data) { - for (Node p = head; p != null; ) { + private Node firstOfMode(boolean data) { + for (Node p = head; p != null; ) { if (!p.isMatched()) return (p.isData == data) ? p : null; - Node n = p.next; + Node n = p.next; p = (n != p) ? n : head; } return null; @@ -702,12 +701,12 @@ public class LinkedTransferQueue exte * null if none. Used by peek. */ private E firstDataItem() { - for (Node p = head; p != null; ) { + for (Node p = head; p != null; ) { boolean isData = p.isData; Object item = p.item; if (item != p && (item != null) == isData) return isData ? this.cast(item) : null; - Node n = p.next; + Node n = p.next; p = (n != p) ? n : head; } return null; @@ -719,14 +718,14 @@ public class LinkedTransferQueue exte */ private int countOfMode(boolean data) { int count = 0; - for (Node p = head; p != null; ) { + for (Node p = head; p != null; ) { if (!p.isMatched()) { if (p.isData != data) return 0; if (++count == Integer.MAX_VALUE) // saturated break; } - Node n = p.next; + Node n = p.next; if (n != p) p = n; else { @@ -738,18 +737,18 @@ public class LinkedTransferQueue exte } final class Itr implements Iterator { - private Node nextNode; // next node to return item for - private E nextItem; // the corresponding item - private Node lastRet; // last returned node, to support remove - private Node lastPred; // predecessor to unlink lastRet + private Node nextNode; // next node to return item for + private E nextItem; // the corresponding item + private Node lastRet; // last returned node, to support remove + private Node lastPred; // predecessor to unlink lastRet /** * Moves to next node after prev, or first node if prev null. */ - private void advance(Node prev) { + private void advance(Node prev) { lastPred = lastRet; lastRet = prev; - Node p; + Node p; if (prev == null || (p = prev.next) == prev) p = head; while (p != null) { @@ -763,7 +762,7 @@ public class LinkedTransferQueue exte } else if (item == null) break; - Node n = p.next; + Node n = p.next; p = (n != p) ? n : head; } nextNode = null; @@ -778,7 +777,7 @@ public class LinkedTransferQueue exte } public final E next() { - Node p = nextNode; + Node p = nextNode; if (p == null) throw new NoSuchElementException(); E e = nextItem; advance(p); @@ -786,7 +785,7 @@ public class LinkedTransferQueue exte } public final void remove() { - Node p = lastRet; + Node p = lastRet; if (p == null) throw new IllegalStateException(); findAndRemoveDataNode(lastPred, p); } @@ -801,7 +800,7 @@ public class LinkedTransferQueue exte * @param pred predecessor of node to be unspliced * @param s the node to be unspliced */ - private void unsplice(Node pred, Node s) { + private void unsplice(Node pred, Node s) { s.forgetContents(); // clear unneeded fields /* * At any given time, exactly one node on list cannot be @@ -814,8 +813,8 @@ public class LinkedTransferQueue exte */ if (pred != null && pred != s) { while (pred.next == s) { - Node oldpred = (cleanMe == null) ? null : reclean(); - Node n = s.next; + Node oldpred = (cleanMe == null) ? null : reclean(); + Node n = s.next; if (n != null) { if (n != s) pred.casNext(s, n); @@ -836,7 +835,7 @@ public class LinkedTransferQueue exte * * @return current cleanMe node (or null) */ - private Node reclean() { + private Node reclean() { /* * cleanMe is, or at one time was, predecessor of a cancelled * node s that was the tail so could not be unspliced. If it @@ -847,10 +846,10 @@ public class LinkedTransferQueue exte * we can (must) clear cleanMe without unsplicing. This can * loop only due to contention. */ - Node pred; + Node pred; while ((pred = cleanMe) != null) { - Node s = pred.next; - Node n; + Node s = pred.next; + Node n; if (s == null || s == pred || !s.isMatched()) casCleanMe(pred, null); // already gone else if ((n = s.next) != null) { @@ -870,13 +869,13 @@ public class LinkedTransferQueue exte * @param possiblePred possible predecessor of s * @param s the node to remove */ - final void findAndRemoveDataNode(Node possiblePred, Node s) { + final void findAndRemoveDataNode(Node possiblePred, Node s) { assert s.isData; if (s.tryMatchData()) { if (possiblePred != null && possiblePred.next == s) unsplice(possiblePred, s); // was actual predecessor else { - for (Node pred = null, p = head; p != null; ) { + for (Node pred = null, p = head; p != null; ) { if (p == s) { unsplice(pred, p); break; @@ -898,7 +897,7 @@ public class LinkedTransferQueue exte */ private boolean findAndRemove(Object e) { if (e != null) { - for (Node pred = null, p = head; p != null; ) { + for (Node pred = null, p = head; p != null; ) { Object item = p.item; if (p.isData) { if (item != null && item != p && e.equals(item) &&