--- jsr166/src/jsr166y/LinkedTransferQueue.java 2009/11/15 02:00:26 1.69 +++ jsr166/src/jsr166y/LinkedTransferQueue.java 2010/08/17 18:30:33 1.73 @@ -15,6 +15,7 @@ import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; import java.util.concurrent.locks.LockSupport; + /** * An unbounded {@link TransferQueue} based on linked nodes. * This queue orders elements FIFO (first-in-first-out) with respect @@ -422,7 +423,7 @@ public class LinkedTransferQueue exte } final boolean casItem(Object cmp, Object val) { - assert cmp == null || cmp.getClass() != Node.class; + // assert cmp == null || cmp.getClass() != Node.class; return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); } @@ -446,7 +447,7 @@ public class LinkedTransferQueue exte /** * Sets item to self and waiter to null, to avoid garbage * retention after matching or cancelling. Uses relaxed writes - * bacause order is already constrained in the only calling + * because order is already constrained in the only calling * contexts: item is forgotten only after volatile/atomic * mechanics that extract items. Similarly, clearing waiter * follows either CAS or return from park (if ever parked; @@ -488,7 +489,7 @@ public class LinkedTransferQueue exte * Tries to artificially match a data node -- used by remove. */ final boolean tryMatchData() { - assert isData; + // assert isData; Object x = item; if (x != null && x != this && casItem(x, null)) { LockSupport.unpark(waiter); @@ -541,7 +542,7 @@ public class LinkedTransferQueue exte @SuppressWarnings("unchecked") static E cast(Object item) { - assert item == null || item.getClass() != Node.class; + // assert item == null || item.getClass() != Node.class; return (E) item; } @@ -656,7 +657,7 @@ public class LinkedTransferQueue exte for (;;) { Object item = s.item; if (item != e) { // matched - assert item != s; + // assert item != s; s.forgetContents(); // avoid garbage return this.cast(item); } @@ -876,17 +877,18 @@ public class LinkedTransferQueue exte } /** - * Unlink matched nodes encountered in a traversal from head + * Unlinks matched nodes encountered in a traversal from head. */ private void sweep() { - Node p = head, s, n; - while (p != null && (s = p.next) != null && (n = s.next) != null) { - if (p == s || s == n) - p = head; // stale - else if (s.isMatched()) - p.casNext(s, n); - else + for (Node p = head, s, n; p != null && (s = p.next) != null; ) { + if (p == s) // stale + p = head; + else if (!s.isMatched()) p = s; + else if ((n = s.next) == null) // trailing node is pinned + break; + else + p.casNext(s, n); } } @@ -1124,7 +1126,11 @@ public class LinkedTransferQueue exte * @return {@code true} if this queue contains no elements */ public boolean isEmpty() { - return firstOfMode(true) == null; + for (Node p = head; p != null; p = succ(p)) { + if (!p.isMatched()) + return !p.isData; + } + return true; } public boolean hasWaitingConsumer() {