116 |
|
nextUpdater = AtomicReferenceFieldUpdater.newUpdater |
117 |
|
(QNode.class, QNode.class, "next"); |
118 |
|
|
119 |
< |
boolean casNext(QNode cmp, QNode val) { |
119 |
> |
final boolean casNext(QNode cmp, QNode val) { |
120 |
|
return nextUpdater.compareAndSet(this, cmp, val); |
121 |
|
} |
122 |
+ |
|
123 |
+ |
final void clearNext() { |
124 |
+ |
nextUpdater.lazySet(this, this); |
125 |
+ |
} |
126 |
+ |
|
127 |
|
} |
128 |
|
|
129 |
|
/** |
156 |
|
*/ |
157 |
|
private boolean advanceHead(QNode h, QNode nh) { |
158 |
|
if (h == head.get() && head.compareAndSet(h, nh)) { |
159 |
< |
h.next = h; // forget old next |
159 |
> |
h.clearNext(); // forget old next |
160 |
|
return true; |
161 |
|
} |
162 |
|
return false; |
166 |
|
* Puts or takes an item. Used for most queue operations (except |
167 |
|
* poll() and tryTransfer()). See the similar code in |
168 |
|
* SynchronousQueue for detailed explanation. |
169 |
+ |
* |
170 |
|
* @param e the item or if null, signifies that this is a take |
171 |
|
* @param mode the wait mode: NOWAIT, TIMEOUT, WAIT |
172 |
|
* @param nanos timeout in nanosecs, used only if mode is TIMEOUT |
213 |
|
|
214 |
|
/** |
215 |
|
* Version of xfer for poll() and tryTransfer, which |
216 |
< |
* simplifies control paths both here and in xfer |
216 |
> |
* simplifies control paths both here and in xfer. |
217 |
|
*/ |
218 |
|
private Object fulfill(Object e) { |
219 |
|
boolean isData = (e != null); |
273 |
|
Object x = s.get(); |
274 |
|
if (x != e) { // Node was matched or cancelled |
275 |
|
advanceHead(pred, s); // unlink if head |
276 |
< |
if (x == s) { // was cancelled |
276 |
> |
if (x == s) { // was cancelled |
277 |
|
clean(pred, s); |
278 |
|
return null; |
279 |
|
} |
304 |
|
else if (s.waiter == null) |
305 |
|
s.waiter = w; |
306 |
|
else if (mode != TIMEOUT) { |
307 |
< |
// LockSupport.park(this); |
302 |
< |
LockSupport.park(); // allows run on java5 |
307 |
> |
LockSupport.park(this); |
308 |
|
s.waiter = null; |
309 |
|
spins = -1; |
310 |
|
} |
311 |
|
else if (nanos > spinForTimeoutThreshold) { |
312 |
< |
// LockSupport.parkNanos(this, nanos); |
308 |
< |
LockSupport.parkNanos(nanos); |
312 |
> |
LockSupport.parkNanos(this, nanos); |
313 |
|
s.waiter = null; |
314 |
|
spins = -1; |
315 |
|
} |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* Returns validated tail for use in cleaning methods |
320 |
> |
* Returns validated tail for use in cleaning methods. |
321 |
|
*/ |
322 |
|
private QNode getValidatedTail() { |
323 |
|
for (;;) { |
340 |
|
|
341 |
|
/** |
342 |
|
* Gets rid of cancelled node s with original predecessor pred. |
343 |
+ |
* |
344 |
|
* @param pred predecessor of cancelled node |
345 |
|
* @param s the cancelled node |
346 |
|
*/ |
351 |
|
if (w != Thread.currentThread()) |
352 |
|
LockSupport.unpark(w); |
353 |
|
} |
354 |
+ |
|
355 |
+ |
if (pred == null) |
356 |
+ |
return; |
357 |
+ |
|
358 |
|
/* |
359 |
|
* At any given time, exactly one node on list cannot be |
360 |
|
* deleted -- the last inserted node. To accommodate this, if |
380 |
|
/** |
381 |
|
* Tries to unsplice the cancelled node held in cleanMe that was |
382 |
|
* previously uncleanable because it was at tail. |
383 |
+ |
* |
384 |
|
* @return current cleanMe node (or null) |
385 |
|
*/ |
386 |
|
private QNode reclean() { |
425 |
|
* Creates a {@code LinkedTransferQueue} |
426 |
|
* initially containing the elements of the given collection, |
427 |
|
* added in traversal order of the collection's iterator. |
428 |
+ |
* |
429 |
|
* @param c the collection of elements to initially contain |
430 |
|
* @throws NullPointerException if the specified collection or any |
431 |
|
* of its elements are null |
455 |
|
return true; |
456 |
|
} |
457 |
|
|
458 |
+ |
public boolean add(E e) { |
459 |
+ |
if (e == null) throw new NullPointerException(); |
460 |
+ |
xfer(e, NOWAIT, 0); |
461 |
+ |
return true; |
462 |
+ |
} |
463 |
+ |
|
464 |
|
public void transfer(E e) throws InterruptedException { |
465 |
|
if (e == null) throw new NullPointerException(); |
466 |
|
if (xfer(e, WAIT, 0) == null) { |
534 |
|
// Traversal-based methods |
535 |
|
|
536 |
|
/** |
537 |
< |
* Return head after performing any outstanding helping steps |
537 |
> |
* Returns head after performing any outstanding helping steps. |
538 |
|
*/ |
539 |
|
private QNode traversalHead() { |
540 |
|
for (;;) { |
557 |
|
return h; |
558 |
|
} |
559 |
|
} |
560 |
+ |
reclean(); |
561 |
|
} |
562 |
|
} |
563 |
|
|
574 |
|
* if subsequently removed. |
575 |
|
*/ |
576 |
|
class Itr implements Iterator<E> { |
577 |
< |
QNode nextNode; // Next node to return next |
578 |
< |
QNode currentNode; // last returned node, for remove() |
579 |
< |
QNode prevNode; // predecessor of last returned node |
580 |
< |
E nextItem; // Cache of next item, once commited to in next |
577 |
> |
QNode next; // node to return next |
578 |
> |
QNode pnext; // predecessor of next |
579 |
> |
QNode snext; // successor of next |
580 |
> |
QNode curr; // last returned node, for remove() |
581 |
> |
QNode pcurr; // predecessor of curr, for remove() |
582 |
> |
E nextItem; // Cache of next item, once committed to in next |
583 |
|
|
584 |
|
Itr() { |
585 |
< |
nextNode = traversalHead(); |
566 |
< |
advance(); |
585 |
> |
findNext(); |
586 |
|
} |
587 |
|
|
588 |
< |
E advance() { |
589 |
< |
prevNode = currentNode; |
590 |
< |
currentNode = nextNode; |
591 |
< |
E x = nextItem; |
573 |
< |
|
574 |
< |
QNode p = nextNode.next; |
588 |
> |
/** |
589 |
> |
* Ensures next points to next valid node, or null if none. |
590 |
> |
*/ |
591 |
> |
void findNext() { |
592 |
|
for (;;) { |
593 |
< |
if (p == null || !p.isData) { |
594 |
< |
nextNode = null; |
595 |
< |
nextItem = null; |
596 |
< |
return x; |
597 |
< |
} |
598 |
< |
Object item = p.get(); |
599 |
< |
if (item != p && item != null) { |
600 |
< |
nextNode = p; |
601 |
< |
nextItem = (E)item; |
602 |
< |
return x; |
593 |
> |
QNode pred = pnext; |
594 |
> |
QNode q = next; |
595 |
> |
if (pred == null || pred == q) { |
596 |
> |
pred = traversalHead(); |
597 |
> |
q = pred.next; |
598 |
> |
} |
599 |
> |
if (q == null || !q.isData) { |
600 |
> |
next = null; |
601 |
> |
return; |
602 |
> |
} |
603 |
> |
Object x = q.get(); |
604 |
> |
QNode s = q.next; |
605 |
> |
if (x != null && q != x && q != s) { |
606 |
> |
nextItem = (E)x; |
607 |
> |
snext = s; |
608 |
> |
pnext = pred; |
609 |
> |
next = q; |
610 |
> |
return; |
611 |
|
} |
612 |
< |
prevNode = p; |
613 |
< |
p = p.next; |
612 |
> |
pnext = q; |
613 |
> |
next = s; |
614 |
|
} |
615 |
|
} |
616 |
|
|
617 |
|
public boolean hasNext() { |
618 |
< |
return nextNode != null; |
618 |
> |
return next != null; |
619 |
|
} |
620 |
|
|
621 |
|
public E next() { |
622 |
< |
if (nextNode == null) throw new NoSuchElementException(); |
623 |
< |
return advance(); |
622 |
> |
if (next == null) throw new NoSuchElementException(); |
623 |
> |
pcurr = pnext; |
624 |
> |
curr = next; |
625 |
> |
pnext = next; |
626 |
> |
next = snext; |
627 |
> |
E x = nextItem; |
628 |
> |
findNext(); |
629 |
> |
return x; |
630 |
|
} |
631 |
|
|
632 |
|
public void remove() { |
633 |
< |
QNode p = currentNode; |
634 |
< |
QNode prev = prevNode; |
604 |
< |
if (prev == null || p == null) |
633 |
> |
QNode p = curr; |
634 |
> |
if (p == null) |
635 |
|
throw new IllegalStateException(); |
636 |
|
Object x = p.get(); |
637 |
|
if (x != null && x != p && p.compareAndSet(x, p)) |
638 |
< |
clean(prev, p); |
638 |
> |
clean(pcurr, p); |
639 |
|
} |
640 |
|
} |
641 |
|
|
724 |
|
return Integer.MAX_VALUE; |
725 |
|
} |
726 |
|
|
727 |
+ |
public boolean remove(Object o) { |
728 |
+ |
if (o == null) |
729 |
+ |
return false; |
730 |
+ |
for (;;) { |
731 |
+ |
QNode pred = traversalHead(); |
732 |
+ |
for (;;) { |
733 |
+ |
QNode q = pred.next; |
734 |
+ |
if (q == null || !q.isData) |
735 |
+ |
return false; |
736 |
+ |
if (q == pred) // restart |
737 |
+ |
break; |
738 |
+ |
Object x = q.get(); |
739 |
+ |
if (x != null && x != q && o.equals(x) && |
740 |
+ |
q.compareAndSet(x, q)) { |
741 |
+ |
clean(pred, q); |
742 |
+ |
return true; |
743 |
+ |
} |
744 |
+ |
pred = q; |
745 |
+ |
} |
746 |
+ |
} |
747 |
+ |
} |
748 |
+ |
|
749 |
|
/** |
750 |
|
* Save the state to a stream (that is, serialize it). |
751 |
|
* |
756 |
|
private void writeObject(java.io.ObjectOutputStream s) |
757 |
|
throws java.io.IOException { |
758 |
|
s.defaultWriteObject(); |
759 |
< |
for (Iterator<E> it = iterator(); it.hasNext(); ) |
760 |
< |
s.writeObject(it.next()); |
759 |
> |
for (E e : this) |
760 |
> |
s.writeObject(e); |
761 |
|
// Use trailing null as sentinel |
762 |
|
s.writeObject(null); |
763 |
|
} |
782 |
|
|
783 |
|
|
784 |
|
// Support for resetting head/tail while deserializing |
785 |
+ |
private void resetHeadAndTail() { |
786 |
+ |
QNode dummy = new QNode(null, false); |
787 |
+ |
_unsafe.putObjectVolatile(this, headOffset, |
788 |
+ |
new PaddedAtomicReference<QNode>(dummy)); |
789 |
+ |
_unsafe.putObjectVolatile(this, tailOffset, |
790 |
+ |
new PaddedAtomicReference<QNode>(dummy)); |
791 |
+ |
_unsafe.putObjectVolatile(this, cleanMeOffset, |
792 |
+ |
new PaddedAtomicReference<QNode>(null)); |
793 |
+ |
} |
794 |
|
|
795 |
|
// Temporary Unsafe mechanics for preliminary release |
796 |
+ |
private static Unsafe getUnsafe() throws Throwable { |
797 |
+ |
try { |
798 |
+ |
return Unsafe.getUnsafe(); |
799 |
+ |
} catch (SecurityException se) { |
800 |
+ |
try { |
801 |
+ |
return java.security.AccessController.doPrivileged |
802 |
+ |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
803 |
+ |
public Unsafe run() throws Exception { |
804 |
+ |
return getUnsafePrivileged(); |
805 |
+ |
}}); |
806 |
+ |
} catch (java.security.PrivilegedActionException e) { |
807 |
+ |
throw e.getCause(); |
808 |
+ |
} |
809 |
+ |
} |
810 |
+ |
} |
811 |
+ |
|
812 |
+ |
private static Unsafe getUnsafePrivileged() |
813 |
+ |
throws NoSuchFieldException, IllegalAccessException { |
814 |
+ |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
815 |
+ |
f.setAccessible(true); |
816 |
+ |
return (Unsafe) f.get(null); |
817 |
+ |
} |
818 |
+ |
|
819 |
+ |
private static long fieldOffset(String fieldName) |
820 |
+ |
throws NoSuchFieldException { |
821 |
+ |
return _unsafe.objectFieldOffset |
822 |
+ |
(LinkedTransferQueue.class.getDeclaredField(fieldName)); |
823 |
+ |
} |
824 |
+ |
|
825 |
|
private static final Unsafe _unsafe; |
826 |
|
private static final long headOffset; |
827 |
|
private static final long tailOffset; |
828 |
|
private static final long cleanMeOffset; |
829 |
|
static { |
830 |
|
try { |
831 |
< |
if (LinkedTransferQueue.class.getClassLoader() != null) { |
832 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
833 |
< |
f.setAccessible(true); |
834 |
< |
_unsafe = (Unsafe)f.get(null); |
835 |
< |
} |
746 |
< |
else |
747 |
< |
_unsafe = Unsafe.getUnsafe(); |
748 |
< |
headOffset = _unsafe.objectFieldOffset |
749 |
< |
(LinkedTransferQueue.class.getDeclaredField("head")); |
750 |
< |
tailOffset = _unsafe.objectFieldOffset |
751 |
< |
(LinkedTransferQueue.class.getDeclaredField("tail")); |
752 |
< |
cleanMeOffset = _unsafe.objectFieldOffset |
753 |
< |
(LinkedTransferQueue.class.getDeclaredField("cleanMe")); |
754 |
< |
} catch (Exception e) { |
831 |
> |
_unsafe = getUnsafe(); |
832 |
> |
headOffset = fieldOffset("head"); |
833 |
> |
tailOffset = fieldOffset("tail"); |
834 |
> |
cleanMeOffset = fieldOffset("cleanMe"); |
835 |
> |
} catch (Throwable e) { |
836 |
|
throw new RuntimeException("Could not initialize intrinsics", e); |
837 |
|
} |
838 |
|
} |
839 |
|
|
759 |
– |
private void resetHeadAndTail() { |
760 |
– |
QNode dummy = new QNode(null, false); |
761 |
– |
_unsafe.putObjectVolatile(this, headOffset, |
762 |
– |
new PaddedAtomicReference<QNode>(dummy)); |
763 |
– |
_unsafe.putObjectVolatile(this, tailOffset, |
764 |
– |
new PaddedAtomicReference<QNode>(dummy)); |
765 |
– |
_unsafe.putObjectVolatile(this, cleanMeOffset, |
766 |
– |
new PaddedAtomicReference<QNode>(null)); |
767 |
– |
|
768 |
– |
} |
769 |
– |
|
840 |
|
} |