21 |
|
* producer. The <em>tail</em> of the queue is that element that has |
22 |
|
* been on the queue the shortest time for some producer. |
23 |
|
* |
24 |
< |
* <p>Beware that, unlike in most collections, the <tt>size</tt> |
24 |
> |
* <p>Beware that, unlike in most collections, the {@code size} |
25 |
|
* method is <em>NOT</em> a constant-time operation. Because of the |
26 |
|
* asynchronous nature of these queues, determining the current number |
27 |
|
* of elements requires a traversal of the elements. |
298 |
|
else if (s.waiter == null) |
299 |
|
s.waiter = w; |
300 |
|
else if (mode != TIMEOUT) { |
301 |
< |
// LockSupport.park(this); |
302 |
< |
LockSupport.park(); // allows run on java5 |
301 |
> |
LockSupport.park(this); |
302 |
|
s.waiter = null; |
303 |
|
spins = -1; |
304 |
|
} |
305 |
|
else if (nanos > spinForTimeoutThreshold) { |
306 |
< |
// LockSupport.parkNanos(this, nanos); |
308 |
< |
LockSupport.parkNanos(nanos); |
306 |
> |
LockSupport.parkNanos(this, nanos); |
307 |
|
s.waiter = null; |
308 |
|
spins = -1; |
309 |
|
} |
330 |
|
return t; |
331 |
|
} |
332 |
|
} |
333 |
< |
} |
333 |
> |
} |
334 |
|
|
335 |
|
/** |
336 |
|
* Gets rid of cancelled node s with original predecessor pred. |
357 |
|
QNode t = getValidatedTail(); |
358 |
|
if (s != t) { // If not tail, try to unsplice |
359 |
|
QNode sn = s.next; // s.next == s means s already off list |
360 |
< |
if (sn == s || pred.casNext(s, sn)) |
360 |
> |
if (sn == s || pred.casNext(s, sn)) |
361 |
|
break; |
362 |
|
} |
363 |
|
else if (oldpred == pred || // Already saved |
372 |
|
* @return current cleanMe node (or null) |
373 |
|
*/ |
374 |
|
private QNode reclean() { |
375 |
< |
/* |
375 |
> |
/* |
376 |
|
* cleanMe is, or at one time was, predecessor of cancelled |
377 |
|
* node s that was the tail so could not be unspliced. If s |
378 |
|
* is no longer the tail, try to unsplice if necessary and |
381 |
|
* points to a cancelled node that must be unspliced -- if |
382 |
|
* not, we can (must) clear cleanMe without unsplicing. |
383 |
|
* This can loop only due to contention on casNext or |
384 |
< |
* clearing cleanMe. |
384 |
> |
* clearing cleanMe. |
385 |
|
*/ |
386 |
|
QNode pred; |
387 |
|
while ((pred = cleanMe.get()) != null) { |
388 |
|
QNode t = getValidatedTail(); |
389 |
|
QNode s = pred.next; |
390 |
< |
if (s != t) { |
390 |
> |
if (s != t) { |
391 |
|
QNode sn; |
392 |
|
if (s == null || s == pred || s.get() != s || |
393 |
|
(sn = s.next) == s || pred.casNext(s, sn)) |
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* Creates an initially empty <tt>LinkedTransferQueue</tt>. |
403 |
> |
* Creates an initially empty {@code LinkedTransferQueue}. |
404 |
|
*/ |
405 |
|
public LinkedTransferQueue() { |
406 |
|
QNode dummy = new QNode(null, false); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* Creates a <tt>LinkedTransferQueue</tt> |
413 |
> |
* Creates a {@code LinkedTransferQueue} |
414 |
|
* initially containing the elements of the given collection, |
415 |
|
* added in traversal order of the collection's iterator. |
416 |
|
* @param c the collection of elements to initially contain |
653 |
|
|
654 |
|
/** |
655 |
|
* Returns the number of elements in this queue. If this queue |
656 |
< |
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
657 |
< |
* <tt>Integer.MAX_VALUE</tt>. |
656 |
> |
* contains more than {@code Integer.MAX_VALUE} elements, returns |
657 |
> |
* {@code Integer.MAX_VALUE}. |
658 |
|
* |
659 |
|
* <p>Beware that, unlike in most collections, this method is |
660 |
|
* <em>NOT</em> a constant-time operation. Because of the |
695 |
|
/** |
696 |
|
* Save the state to a stream (that is, serialize it). |
697 |
|
* |
698 |
< |
* @serialData All of the elements (each an <tt>E</tt>) in |
698 |
> |
* @serialData All of the elements (each an {@code E}) in |
699 |
|
* the proper order, followed by a null |
700 |
|
* @param s the stream |
701 |
|
*/ |
728 |
|
|
729 |
|
|
730 |
|
// Support for resetting head/tail while deserializing |
731 |
+ |
private void resetHeadAndTail() { |
732 |
+ |
QNode dummy = new QNode(null, false); |
733 |
+ |
_unsafe.putObjectVolatile(this, headOffset, |
734 |
+ |
new PaddedAtomicReference<QNode>(dummy)); |
735 |
+ |
_unsafe.putObjectVolatile(this, tailOffset, |
736 |
+ |
new PaddedAtomicReference<QNode>(dummy)); |
737 |
+ |
_unsafe.putObjectVolatile(this, cleanMeOffset, |
738 |
+ |
new PaddedAtomicReference<QNode>(null)); |
739 |
+ |
} |
740 |
|
|
741 |
|
// Temporary Unsafe mechanics for preliminary release |
742 |
+ |
private static Unsafe getUnsafe() throws Throwable { |
743 |
+ |
try { |
744 |
+ |
return Unsafe.getUnsafe(); |
745 |
+ |
} catch (SecurityException se) { |
746 |
+ |
try { |
747 |
+ |
return java.security.AccessController.doPrivileged |
748 |
+ |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
749 |
+ |
public Unsafe run() throws Exception { |
750 |
+ |
return getUnsafePrivileged(); |
751 |
+ |
}}); |
752 |
+ |
} catch (java.security.PrivilegedActionException e) { |
753 |
+ |
throw e.getCause(); |
754 |
+ |
} |
755 |
+ |
} |
756 |
+ |
} |
757 |
+ |
|
758 |
+ |
private static Unsafe getUnsafePrivileged() |
759 |
+ |
throws NoSuchFieldException, IllegalAccessException { |
760 |
+ |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
761 |
+ |
f.setAccessible(true); |
762 |
+ |
return (Unsafe)f.get(null); |
763 |
+ |
} |
764 |
+ |
|
765 |
+ |
private static long fieldOffset(String fieldName) |
766 |
+ |
throws NoSuchFieldException { |
767 |
+ |
return _unsafe.objectFieldOffset |
768 |
+ |
(LinkedTransferQueue.class.getDeclaredField(fieldName)); |
769 |
+ |
} |
770 |
+ |
|
771 |
|
private static final Unsafe _unsafe; |
772 |
|
private static final long headOffset; |
773 |
|
private static final long tailOffset; |
774 |
|
private static final long cleanMeOffset; |
775 |
|
static { |
776 |
|
try { |
777 |
< |
if (LinkedTransferQueue.class.getClassLoader() != null) { |
778 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
779 |
< |
f.setAccessible(true); |
780 |
< |
_unsafe = (Unsafe)f.get(null); |
781 |
< |
} |
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) { |
777 |
> |
_unsafe = getUnsafe(); |
778 |
> |
headOffset = fieldOffset("head"); |
779 |
> |
tailOffset = fieldOffset("tail"); |
780 |
> |
cleanMeOffset = fieldOffset("cleanMe"); |
781 |
> |
} catch (Throwable e) { |
782 |
|
throw new RuntimeException("Could not initialize intrinsics", e); |
783 |
|
} |
784 |
|
} |
785 |
|
|
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 |
– |
|
786 |
|
} |