361 |
|
* precede or follow CASes use simple relaxed forms. Other |
362 |
|
* cleanups use releasing/lazy writes. |
363 |
|
*/ |
364 |
< |
static final class Node { |
364 |
> |
static final class Node<E> { |
365 |
|
final boolean isData; // false if this is a request node |
366 |
|
volatile Object item; // initially non-null if isData; CASed to match |
367 |
< |
volatile Node next; |
367 |
> |
volatile Node<E> next; |
368 |
|
volatile Thread waiter; // null until waiting |
369 |
|
|
370 |
|
// CAS methods for fields |
371 |
< |
final boolean casNext(Node cmp, Node val) { |
371 |
> |
final boolean casNext(Node<E> cmp, Node<E> val) { |
372 |
|
return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); |
373 |
|
} |
374 |
|
|
375 |
|
final boolean casItem(Object cmp, Object val) { |
376 |
+ |
assert cmp == null || cmp.getClass() != Node.class; |
377 |
|
return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); |
378 |
|
} |
379 |
|
|
381 |
|
* Creates a new node. Uses relaxed write because item can only |
382 |
|
* be seen if followed by CAS. |
383 |
|
*/ |
384 |
< |
Node(Object item, boolean isData) { |
384 |
> |
Node(E item, boolean isData) { |
385 |
|
UNSAFE.putObject(this, itemOffset, item); // relaxed write |
386 |
|
this.isData = isData; |
387 |
|
} |
449 |
|
} |
450 |
|
|
451 |
|
/** head of the queue; null until first enqueue */ |
452 |
< |
private transient volatile Node head; |
452 |
> |
transient volatile Node<E> head; |
453 |
|
|
454 |
|
/** predecessor of dangling unspliceable node */ |
455 |
< |
private transient volatile Node cleanMe; // decl here to reduce contention |
455 |
> |
private transient volatile Node<E> cleanMe; // decl here reduces contention |
456 |
|
|
457 |
|
/** tail of the queue; null until first append */ |
458 |
< |
private transient volatile Node tail; |
458 |
> |
private transient volatile Node<E> tail; |
459 |
|
|
460 |
|
// CAS methods for fields |
461 |
< |
private boolean casTail(Node cmp, Node val) { |
461 |
> |
private boolean casTail(Node<E> cmp, Node<E> val) { |
462 |
|
return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val); |
463 |
|
} |
464 |
|
|
465 |
< |
private boolean casHead(Node cmp, Node val) { |
465 |
> |
private boolean casHead(Node<E> cmp, Node<E> val) { |
466 |
|
return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val); |
467 |
|
} |
468 |
|
|
469 |
< |
private boolean casCleanMe(Node cmp, Node val) { |
469 |
> |
private boolean casCleanMe(Node<E> cmp, Node<E> val) { |
470 |
|
return UNSAFE.compareAndSwapObject(this, cleanMeOffset, cmp, val); |
471 |
|
} |
472 |
|
|
479 |
|
private static final int SYNC = 2; // for transfer, take |
480 |
|
private static final int TIMEOUT = 3; // for timed poll, tryTransfer |
481 |
|
|
482 |
+ |
@SuppressWarnings("unchecked") |
483 |
+ |
static <E> E cast(Object item) { |
484 |
+ |
assert item == null || item.getClass() != Node.class; |
485 |
+ |
return (E) item; |
486 |
+ |
} |
487 |
+ |
|
488 |
|
/** |
489 |
|
* Implements all queuing methods. See above for explanation. |
490 |
|
* |
495 |
|
* @return an item if matched, else e |
496 |
|
* @throws NullPointerException if haveData mode but e is null |
497 |
|
*/ |
498 |
< |
private Object xfer(Object e, boolean haveData, int how, long nanos) { |
498 |
> |
private E xfer(E e, boolean haveData, int how, long nanos) { |
499 |
|
if (haveData && (e == null)) |
500 |
|
throw new NullPointerException(); |
501 |
< |
Node s = null; // the node to append, if needed |
501 |
> |
Node<E> s = null; // the node to append, if needed |
502 |
|
|
503 |
|
retry: for (;;) { // restart on append race |
504 |
|
|
505 |
< |
for (Node h = head, p = h; p != null;) { // find & match first node |
505 |
> |
for (Node<E> h = head, p = h; p != null;) { |
506 |
> |
// find & match first node |
507 |
|
boolean isData = p.isData; |
508 |
|
Object item = p.item; |
509 |
|
if (item != p && (item != null) == isData) { // unmatched |
510 |
|
if (isData == haveData) // can't match |
511 |
|
break; |
512 |
|
if (p.casItem(item, e)) { // match |
513 |
< |
Thread w = p.waiter; |
514 |
< |
while (p != h) { // update head |
515 |
< |
Node n = p.next; // by 2 unless singleton |
516 |
< |
if (n != null) |
517 |
< |
p = n; |
510 |
< |
if (head == h && casHead(h, p)) { |
513 |
> |
for (Node<E> q = p; q != h;) { |
514 |
> |
Node<E> n = q.next; // update head by 2 |
515 |
> |
if (n != null) // unless singleton |
516 |
> |
q = n; |
517 |
> |
if (head == h && casHead(h, q)) { |
518 |
|
h.forgetNext(); |
519 |
|
break; |
520 |
|
} // advance and retry |
521 |
|
if ((h = head) == null || |
522 |
< |
(p = h.next) == null || !p.isMatched()) |
522 |
> |
(q = h.next) == null || !q.isMatched()) |
523 |
|
break; // unless slack < 2 |
524 |
|
} |
525 |
< |
LockSupport.unpark(w); |
526 |
< |
return item; |
525 |
> |
LockSupport.unpark(p.waiter); |
526 |
> |
return this.<E>cast(item); |
527 |
|
} |
528 |
|
} |
529 |
< |
Node n = p.next; |
529 |
> |
Node<E> n = p.next; |
530 |
|
p = (p != n) ? n : (h = head); // Use head if p offlist |
531 |
|
} |
532 |
|
|
533 |
|
if (how >= ASYNC) { // No matches available |
534 |
|
if (s == null) |
535 |
< |
s = new Node(e, haveData); |
536 |
< |
Node pred = tryAppend(s, haveData); |
535 |
> |
s = new Node<E>(e, haveData); |
536 |
> |
Node<E> pred = tryAppend(s, haveData); |
537 |
|
if (pred == null) |
538 |
|
continue retry; // lost race vs opposite mode |
539 |
|
if (how >= SYNC) |
552 |
|
* different mode, else s's predecessor, or s itself if no |
553 |
|
* predecessor |
554 |
|
*/ |
555 |
< |
private Node tryAppend(Node s, boolean haveData) { |
556 |
< |
for (Node t = tail, p = t;;) { // move p to last node and append |
557 |
< |
Node n, u; // temps for reads of next & tail |
555 |
> |
private Node<E> tryAppend(Node<E> s, boolean haveData) { |
556 |
> |
for (Node<E> t = tail, p = t;;) { // move p to last node and append |
557 |
> |
Node<E> n, u; // temps for reads of next & tail |
558 |
|
if (p == null && (p = head) == null) { |
559 |
|
if (casHead(null, s)) |
560 |
|
return s; // initialize |
590 |
|
* @param nanos timeout value |
591 |
|
* @return matched item, or e if unmatched on interrupt or timeout |
592 |
|
*/ |
593 |
< |
private Object awaitMatch(Node s, Node pred, Object e, |
587 |
< |
int how, long nanos) { |
593 |
> |
private E awaitMatch(Node<E> s, Node<E> pred, E e, int how, long nanos) { |
594 |
|
long lastTime = (how == TIMEOUT) ? System.nanoTime() : 0L; |
595 |
|
Thread w = Thread.currentThread(); |
596 |
|
int spins = -1; // initialized after first item and cancel checks |
599 |
|
for (;;) { |
600 |
|
Object item = s.item; |
601 |
|
if (item != e) { // matched |
602 |
+ |
assert item != s; |
603 |
|
s.forgetContents(); // avoid garbage |
604 |
< |
return item; |
604 |
> |
return this.<E>cast(item); |
605 |
|
} |
606 |
|
if ((w.isInterrupted() || (how == TIMEOUT && nanos <= 0)) && |
607 |
< |
s.casItem(e, s)) { // cancel |
607 |
> |
s.casItem(e, s)) { // cancel |
608 |
|
unsplice(pred, s); |
609 |
|
return e; |
610 |
|
} |
620 |
|
Thread.yield(); // occasionally yield |
621 |
|
} |
622 |
|
else if (s.waiter == null) { |
623 |
< |
s.waiter = w; // request unpark |
623 |
> |
s.waiter = w; // request unpark then recheck |
624 |
|
} |
625 |
|
else if (how == TIMEOUT) { |
626 |
|
long now = System.nanoTime(); |
630 |
|
} |
631 |
|
else { |
632 |
|
LockSupport.park(this); |
633 |
+ |
s.waiter = null; |
634 |
|
spins = -1; // spin if front upon wakeup |
635 |
|
} |
636 |
|
} |
640 |
|
* Returns spin/yield value for a node with given predecessor and |
641 |
|
* data mode. See above for explanation. |
642 |
|
*/ |
643 |
< |
private static int spinsFor(Node pred, boolean haveData) { |
643 |
> |
private static int spinsFor(Node<?> pred, boolean haveData) { |
644 |
|
if (MP && pred != null) { |
645 |
|
if (pred.isData != haveData) // phase change |
646 |
|
return FRONT_SPINS + CHAINED_SPINS; |
657 |
|
* or trailing node; failing on contention. |
658 |
|
*/ |
659 |
|
private void shortenHeadPath() { |
660 |
< |
Node h, hn, p, q; |
660 |
> |
Node<E> h, hn, p, q; |
661 |
|
if ((p = h = head) != null && h.isMatched() && |
662 |
|
(q = hn = h.next) != null) { |
663 |
< |
Node n; |
663 |
> |
Node<E> n; |
664 |
|
while ((n = q.next) != q) { |
665 |
|
if (n == null || !q.isMatched()) { |
666 |
|
if (hn != q && h.next == hn) |
679 |
|
* Returns the first unmatched node of the given mode, or null if |
680 |
|
* none. Used by methods isEmpty, hasWaitingConsumer. |
681 |
|
*/ |
682 |
< |
private Node firstOfMode(boolean data) { |
683 |
< |
for (Node p = head; p != null; ) { |
682 |
> |
private Node<E> firstOfMode(boolean data) { |
683 |
> |
for (Node<E> p = head; p != null; ) { |
684 |
|
if (!p.isMatched()) |
685 |
|
return (p.isData == data) ? p : null; |
686 |
< |
Node n = p.next; |
686 |
> |
Node<E> n = p.next; |
687 |
|
p = (n != p) ? n : head; |
688 |
|
} |
689 |
|
return null; |
691 |
|
|
692 |
|
/** |
693 |
|
* Returns the item in the first unmatched node with isData; or |
694 |
< |
* null if none. Used by peek. |
694 |
> |
* null if none. Used by peek. |
695 |
|
*/ |
696 |
< |
private Object firstDataItem() { |
697 |
< |
for (Node p = head; p != null; ) { |
696 |
> |
private E firstDataItem() { |
697 |
> |
for (Node<E> p = head; p != null; ) { |
698 |
|
boolean isData = p.isData; |
699 |
|
Object item = p.item; |
700 |
|
if (item != p && (item != null) == isData) |
701 |
< |
return isData ? item : null; |
702 |
< |
Node n = p.next; |
701 |
> |
return isData ? this.<E>cast(item) : null; |
702 |
> |
Node<E> n = p.next; |
703 |
|
p = (n != p) ? n : head; |
704 |
|
} |
705 |
|
return null; |
711 |
|
*/ |
712 |
|
private int countOfMode(boolean data) { |
713 |
|
int count = 0; |
714 |
< |
for (Node p = head; p != null; ) { |
714 |
> |
for (Node<E> p = head; p != null; ) { |
715 |
|
if (!p.isMatched()) { |
716 |
|
if (p.isData != data) |
717 |
|
return 0; |
718 |
|
if (++count == Integer.MAX_VALUE) // saturated |
719 |
|
break; |
720 |
|
} |
721 |
< |
Node n = p.next; |
721 |
> |
Node<E> n = p.next; |
722 |
|
if (n != p) |
723 |
|
p = n; |
724 |
|
else { |
730 |
|
} |
731 |
|
|
732 |
|
final class Itr implements Iterator<E> { |
733 |
< |
private Node nextNode; // next node to return item for |
734 |
< |
private Object nextItem; // the corresponding item |
735 |
< |
private Node lastRet; // last returned node, to support remove |
733 |
> |
private Node<E> nextNode; // next node to return item for |
734 |
> |
private E nextItem; // the corresponding item |
735 |
> |
private Node<E> lastRet; // last returned node, to support remove |
736 |
|
|
737 |
|
/** |
738 |
|
* Moves to next node after prev, or first node if prev null. |
739 |
|
*/ |
740 |
< |
private void advance(Node prev) { |
740 |
> |
private void advance(Node<E> prev) { |
741 |
|
lastRet = prev; |
742 |
< |
Node p; |
742 |
> |
Node<E> p; |
743 |
|
if (prev == null || (p = prev.next) == prev) |
744 |
|
p = head; |
745 |
|
while (p != null) { |
746 |
|
Object item = p.item; |
747 |
|
if (p.isData) { |
748 |
|
if (item != null && item != p) { |
749 |
< |
nextItem = item; |
749 |
> |
nextItem = LinkedTransferQueue.this.<E>cast(item); |
750 |
|
nextNode = p; |
751 |
|
return; |
752 |
|
} |
753 |
|
} |
754 |
|
else if (item == null) |
755 |
|
break; |
756 |
< |
Node n = p.next; |
756 |
> |
Node<E> n = p.next; |
757 |
|
p = (n != p) ? n : head; |
758 |
|
} |
759 |
|
nextNode = null; |
768 |
|
} |
769 |
|
|
770 |
|
public final E next() { |
771 |
< |
Node p = nextNode; |
771 |
> |
Node<E> p = nextNode; |
772 |
|
if (p == null) throw new NoSuchElementException(); |
773 |
< |
Object e = nextItem; |
773 |
> |
E e = nextItem; |
774 |
|
advance(p); |
775 |
< |
return (E) e; |
775 |
> |
return e; |
776 |
|
} |
777 |
|
|
778 |
|
public final void remove() { |
779 |
< |
Node p = lastRet; |
779 |
> |
Node<E> p = lastRet; |
780 |
|
if (p == null) throw new IllegalStateException(); |
781 |
|
lastRet = null; |
782 |
|
findAndRemoveNode(p); |
792 |
|
* @param pred predecessor of node to be unspliced |
793 |
|
* @param s the node to be unspliced |
794 |
|
*/ |
795 |
< |
private void unsplice(Node pred, Node s) { |
795 |
> |
private void unsplice(Node<E> pred, Node<E> s) { |
796 |
|
s.forgetContents(); // clear unneeded fields |
797 |
|
/* |
798 |
|
* At any given time, exactly one node on list cannot be |
805 |
|
*/ |
806 |
|
if (pred != null && pred != s) { |
807 |
|
while (pred.next == s) { |
808 |
< |
Node oldpred = (cleanMe == null) ? null : reclean(); |
809 |
< |
Node n = s.next; |
808 |
> |
Node<E> oldpred = (cleanMe == null) ? null : reclean(); |
809 |
> |
Node<E> n = s.next; |
810 |
|
if (n != null) { |
811 |
|
if (n != s) |
812 |
|
pred.casNext(s, n); |
825 |
|
* |
826 |
|
* @return current cleanMe node (or null) |
827 |
|
*/ |
828 |
< |
private Node reclean() { |
828 |
> |
private Node<E> reclean() { |
829 |
|
/* |
830 |
|
* cleanMe is, or at one time was, predecessor of a cancelled |
831 |
|
* node s that was the tail so could not be unspliced. If it |
836 |
|
* we can (must) clear cleanMe without unsplicing. This can |
837 |
|
* loop only due to contention. |
838 |
|
*/ |
839 |
< |
Node pred; |
839 |
> |
Node<E> pred; |
840 |
|
while ((pred = cleanMe) != null) { |
841 |
< |
Node s = pred.next; |
842 |
< |
Node n; |
841 |
> |
Node<E> s = pred.next; |
842 |
> |
Node<E> n; |
843 |
|
if (s == null || s == pred || !s.isMatched()) |
844 |
|
casCleanMe(pred, null); // already gone |
845 |
|
else if ((n = s.next) != null) { |
857 |
|
* Main implementation of Iterator.remove(). Find |
858 |
|
* and unsplice the given node. |
859 |
|
*/ |
860 |
< |
final void findAndRemoveNode(Node s) { |
860 |
> |
final void findAndRemoveNode(Node<E> s) { |
861 |
|
if (s.tryMatchData()) { |
862 |
< |
Node pred = null; |
863 |
< |
Node p = head; |
862 |
> |
Node<E> pred = null; |
863 |
> |
Node<E> p = head; |
864 |
|
while (p != null) { |
865 |
|
if (p == s) { |
866 |
|
unsplice(pred, p); |
882 |
|
*/ |
883 |
|
private boolean findAndRemove(Object e) { |
884 |
|
if (e != null) { |
885 |
< |
Node pred = null; |
886 |
< |
Node p = head; |
885 |
> |
Node<E> pred = null; |
886 |
> |
Node<E> p = head; |
887 |
|
while (p != null) { |
888 |
|
Object item = p.item; |
889 |
|
if (p.isData) { |
1032 |
|
} |
1033 |
|
|
1034 |
|
public E take() throws InterruptedException { |
1035 |
< |
Object e = xfer(null, false, SYNC, 0); |
1035 |
> |
E e = xfer(null, false, SYNC, 0); |
1036 |
|
if (e != null) |
1037 |
< |
return (E)e; |
1037 |
> |
return e; |
1038 |
|
Thread.interrupted(); |
1039 |
|
throw new InterruptedException(); |
1040 |
|
} |
1041 |
|
|
1042 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
1043 |
< |
Object e = xfer(null, false, TIMEOUT, unit.toNanos(timeout)); |
1043 |
> |
E e = xfer(null, false, TIMEOUT, unit.toNanos(timeout)); |
1044 |
|
if (e != null || !Thread.interrupted()) |
1045 |
< |
return (E)e; |
1045 |
> |
return e; |
1046 |
|
throw new InterruptedException(); |
1047 |
|
} |
1048 |
|
|
1049 |
|
public E poll() { |
1050 |
< |
return (E)xfer(null, false, NOW, 0); |
1050 |
> |
return xfer(null, false, NOW, 0); |
1051 |
|
} |
1052 |
|
|
1053 |
|
/** |
1104 |
|
} |
1105 |
|
|
1106 |
|
public E peek() { |
1107 |
< |
return (E) firstDataItem(); |
1107 |
> |
return firstDataItem(); |
1108 |
|
} |
1109 |
|
|
1110 |
|
/** |
1200 |
|
} |
1201 |
|
} |
1202 |
|
|
1195 |
– |
|
1203 |
|
// Unsafe mechanics |
1204 |
|
|
1205 |
|
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1222 |
|
} |
1223 |
|
} |
1224 |
|
|
1225 |
< |
private static sun.misc.Unsafe getUnsafe() { |
1225 |
> |
/** |
1226 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
1227 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
1228 |
> |
* into a jdk. |
1229 |
> |
* |
1230 |
> |
* @return a sun.misc.Unsafe |
1231 |
> |
*/ |
1232 |
> |
static sun.misc.Unsafe getUnsafe() { |
1233 |
|
try { |
1234 |
|
return sun.misc.Unsafe.getUnsafe(); |
1235 |
|
} catch (SecurityException se) { |