373 |
|
} |
374 |
|
|
375 |
|
final boolean casItem(Object cmp, Object val) { |
376 |
< |
assert cmp.getClass() != Node.class; |
376 |
> |
assert cmp == null || cmp.getClass() != Node.class; |
377 |
|
return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); |
378 |
|
} |
379 |
|
|
410 |
|
*/ |
411 |
|
final boolean isMatched() { |
412 |
|
Object x = item; |
413 |
< |
return x == this || (x != null) != isData; |
413 |
> |
return (x == this) || ((x == null) == isData); |
414 |
> |
} |
415 |
> |
|
416 |
> |
/** |
417 |
> |
* Returns true if this is an unmatched request node. |
418 |
> |
*/ |
419 |
> |
final boolean isUnmatchedRequest() { |
420 |
> |
return !isData && item == null; |
421 |
|
} |
422 |
|
|
423 |
|
/** |
435 |
|
* Tries to artificially match a data node -- used by remove. |
436 |
|
*/ |
437 |
|
final boolean tryMatchData() { |
438 |
+ |
assert isData; |
439 |
|
Object x = item; |
440 |
|
if (x != null && x != this && casItem(x, null)) { |
441 |
|
LockSupport.unpark(waiter); |
487 |
|
private static final int SYNC = 2; // for transfer, take |
488 |
|
private static final int TIMEOUT = 3; // for timed poll, tryTransfer |
489 |
|
|
490 |
+ |
@SuppressWarnings("unchecked") |
491 |
+ |
static <E> E cast(Object item) { |
492 |
+ |
assert item == null || item.getClass() != Node.class; |
493 |
+ |
return (E) item; |
494 |
+ |
} |
495 |
+ |
|
496 |
|
/** |
497 |
|
* Implements all queuing methods. See above for explanation. |
498 |
|
* |
697 |
|
return null; |
698 |
|
} |
699 |
|
|
686 |
– |
@SuppressWarnings("unchecked") |
687 |
– |
static <E> E cast(Object item) { |
688 |
– |
assert item.getClass() != Node.class; |
689 |
– |
return (E) item; |
690 |
– |
} |
691 |
– |
|
700 |
|
/** |
701 |
|
* Returns the item in the first unmatched node with isData; or |
702 |
|
* null if none. Used by peek. |
787 |
|
Node<E> p = lastRet; |
788 |
|
if (p == null) throw new IllegalStateException(); |
789 |
|
lastRet = null; |
790 |
< |
findAndRemoveNode(p); |
790 |
> |
findAndRemoveDataNode(p); |
791 |
|
} |
792 |
|
} |
793 |
|
|
821 |
|
break; |
822 |
|
} |
823 |
|
if (oldpred == pred || // Already saved |
824 |
< |
(oldpred == null && casCleanMe(null, pred))) |
825 |
< |
break; // Postpone cleaning |
824 |
> |
((oldpred == null || oldpred.next == s) && |
825 |
> |
casCleanMe(oldpred, pred))) { |
826 |
> |
break; |
827 |
> |
} |
828 |
|
} |
829 |
|
} |
830 |
|
} |
865 |
|
|
866 |
|
/** |
867 |
|
* Main implementation of Iterator.remove(). Find |
868 |
< |
* and unsplice the given node. |
868 |
> |
* and unsplice the given data node. |
869 |
|
*/ |
870 |
< |
final void findAndRemoveNode(Node<E> s) { |
870 |
> |
final void findAndRemoveDataNode(Node<E> s) { |
871 |
> |
assert s.isData; |
872 |
|
if (s.tryMatchData()) { |
873 |
< |
Node<E> pred = null; |
863 |
< |
Node<E> p = head; |
864 |
< |
while (p != null) { |
873 |
> |
for (Node<E> pred = null, p = head; p != null; ) { |
874 |
|
if (p == s) { |
875 |
|
unsplice(pred, p); |
876 |
|
break; |
877 |
|
} |
878 |
< |
if (!p.isData && !p.isMatched()) |
878 |
> |
if (p.isUnmatchedRequest()) |
879 |
|
break; |
880 |
|
pred = p; |
881 |
|
if ((p = p.next) == pred) { // stale |
891 |
|
*/ |
892 |
|
private boolean findAndRemove(Object e) { |
893 |
|
if (e != null) { |
894 |
< |
Node<E> pred = null; |
886 |
< |
Node<E> p = head; |
887 |
< |
while (p != null) { |
894 |
> |
for (Node<E> pred = null, p = head; p != null; ) { |
895 |
|
Object item = p.item; |
896 |
|
if (p.isData) { |
897 |
|
if (item != null && item != p && e.equals(item) && |
903 |
|
else if (item == null) |
904 |
|
break; |
905 |
|
pred = p; |
906 |
< |
if ((p = p.next) == pred) { |
906 |
> |
if ((p = p.next) == pred) { // stale |
907 |
|
pred = null; |
908 |
|
p = head; |
909 |
|
} |