10 |
|
import java.util.concurrent.atomic.*; |
11 |
|
import java.util.*; |
12 |
|
import java.io.*; |
13 |
+ |
import sun.misc.Unsafe; |
14 |
+ |
import java.lang.reflect.*; |
15 |
|
|
16 |
|
/** |
17 |
|
* An unbounded {@linkplain TransferQueue} based on linked nodes. |
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. |
51 |
|
private static final long serialVersionUID = -3223113410248163686L; |
52 |
|
|
53 |
|
/* |
52 |
– |
* This is still a work in progress... |
53 |
– |
* |
54 |
|
* This class extends the approach used in FIFO-mode |
55 |
|
* SynchronousQueues. See the internal documentation, as well as |
56 |
|
* the PPoPP 2006 paper "Scalable Synchronous Queues" by Scherer, |
57 |
|
* Lea & Scott |
58 |
|
* (http://www.cs.rice.edu/~wns1/papers/2006-PPoPP-SQ.pdf) |
59 |
|
* |
60 |
< |
* The main extension is to provide different Wait modes |
61 |
< |
* for the main "xfer" method that puts or takes items. |
62 |
< |
* These don't impact the basic dual-queue logic, but instead |
63 |
< |
* control whether or how threads block upon insertion |
64 |
< |
* of request or data nodes into the dual queue. |
60 |
> |
* The main extension is to provide different Wait modes for the |
61 |
> |
* main "xfer" method that puts or takes items. These don't |
62 |
> |
* impact the basic dual-queue logic, but instead control whether |
63 |
> |
* or how threads block upon insertion of request or data nodes |
64 |
> |
* into the dual queue. It also uses slightly different |
65 |
> |
* conventions for tracking whether nodes are off-list or |
66 |
> |
* cancelled. |
67 |
|
*/ |
68 |
|
|
69 |
|
// Wait modes for xfer method |
97 |
|
static final long spinForTimeoutThreshold = 1000L; |
98 |
|
|
99 |
|
/** |
100 |
< |
* Node class for LinkedTransferQueue. Opportunistically subclasses from |
101 |
< |
* AtomicReference to represent item. Uses Object, not E, to allow |
102 |
< |
* setting item to "this" after use, to avoid garbage |
103 |
< |
* retention. Similarly, setting the next field to this is used as |
104 |
< |
* sentinel that node is off list. |
100 |
> |
* Node class for LinkedTransferQueue. Opportunistically |
101 |
> |
* subclasses from AtomicReference to represent item. Uses Object, |
102 |
> |
* not E, to allow setting item to "this" after use, to avoid |
103 |
> |
* garbage retention. Similarly, setting the next field to this is |
104 |
> |
* used as sentinel that node is off list. |
105 |
|
*/ |
106 |
|
static final class QNode extends AtomicReference<Object> { |
107 |
|
volatile QNode next; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
< |
private final QNode dummy = new QNode(null, false); |
137 |
< |
private final PaddedAtomicReference<QNode> head = |
138 |
< |
new PaddedAtomicReference<QNode>(dummy); |
139 |
< |
private final PaddedAtomicReference<QNode> tail = |
138 |
< |
new PaddedAtomicReference<QNode>(dummy); |
136 |
> |
/** head of the queue */ |
137 |
> |
private transient final PaddedAtomicReference<QNode> head; |
138 |
> |
/** tail of the queue */ |
139 |
> |
private transient final PaddedAtomicReference<QNode> tail; |
140 |
|
|
141 |
|
/** |
142 |
|
* Reference to a cancelled node that might not yet have been |
143 |
|
* unlinked from queue because it was the last inserted node |
144 |
|
* when it cancelled. |
145 |
|
*/ |
146 |
< |
private final PaddedAtomicReference<QNode> cleanMe = |
146 |
< |
new PaddedAtomicReference<QNode>(null); |
146 |
> |
private transient final PaddedAtomicReference<QNode> cleanMe; |
147 |
|
|
148 |
|
/** |
149 |
|
* Tries to cas nh as new head; if successful, unlink |
159 |
|
|
160 |
|
/** |
161 |
|
* Puts or takes an item. Used for most queue operations (except |
162 |
< |
* poll() and tryTransfer()) |
162 |
> |
* poll() and tryTransfer()). See the similar code in |
163 |
> |
* SynchronousQueue for detailed explanation. |
164 |
|
* @param e the item or if null, signifies that this is a take |
165 |
|
* @param mode the wait mode: NOWAIT, TIMEOUT, WAIT |
166 |
|
* @param nanos timeout in nanosecs, used only if mode is TIMEOUT |
267 |
|
Object x = s.get(); |
268 |
|
if (x != e) { // Node was matched or cancelled |
269 |
|
advanceHead(pred, s); // unlink if head |
270 |
< |
if (x == s) // was cancelled |
271 |
< |
return clean(pred, s); |
270 |
> |
if (x == s) { // was cancelled |
271 |
> |
clean(pred, s); |
272 |
> |
return null; |
273 |
> |
} |
274 |
|
else if (x != null) { |
275 |
|
s.set(s); // avoid garbage retention |
276 |
|
return x; |
278 |
|
else |
279 |
|
return e; |
280 |
|
} |
278 |
– |
|
281 |
|
if (mode == TIMEOUT) { |
282 |
|
long now = System.nanoTime(); |
283 |
|
nanos -= now - lastTime; |
298 |
|
else if (s.waiter == null) |
299 |
|
s.waiter = w; |
300 |
|
else if (mode != TIMEOUT) { |
301 |
< |
// LockSupport.park(this); |
300 |
< |
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); |
306 |
< |
LockSupport.parkNanos(nanos); |
306 |
> |
LockSupport.parkNanos(this, nanos); |
307 |
|
s.waiter = null; |
308 |
|
spins = -1; |
309 |
|
} |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
+ |
* Returns validated tail for use in cleaning methods |
315 |
+ |
*/ |
316 |
+ |
private QNode getValidatedTail() { |
317 |
+ |
for (;;) { |
318 |
+ |
QNode h = head.get(); |
319 |
+ |
QNode first = h.next; |
320 |
+ |
if (first != null && first.next == first) { // help advance |
321 |
+ |
advanceHead(h, first); |
322 |
+ |
continue; |
323 |
+ |
} |
324 |
+ |
QNode t = tail.get(); |
325 |
+ |
QNode last = t.next; |
326 |
+ |
if (t == tail.get()) { |
327 |
+ |
if (last != null) |
328 |
+ |
tail.compareAndSet(t, last); // help advance |
329 |
+ |
else |
330 |
+ |
return t; |
331 |
+ |
} |
332 |
+ |
} |
333 |
+ |
} |
334 |
+ |
|
335 |
+ |
/** |
336 |
|
* Gets rid of cancelled node s with original predecessor pred. |
337 |
< |
* @return null (to simplify use by callers) |
337 |
> |
* @param pred predecessor of cancelled node |
338 |
> |
* @param s the cancelled node |
339 |
|
*/ |
340 |
< |
private Object clean(QNode pred, QNode s) { |
340 |
> |
private void clean(QNode pred, QNode s) { |
341 |
|
Thread w = s.waiter; |
342 |
|
if (w != null) { // Wake up thread |
343 |
|
s.waiter = null; |
344 |
|
if (w != Thread.currentThread()) |
345 |
|
LockSupport.unpark(w); |
346 |
|
} |
347 |
< |
|
348 |
< |
for (;;) { |
349 |
< |
if (pred.next != s) // already cleaned |
350 |
< |
return null; |
351 |
< |
QNode h = head.get(); |
352 |
< |
QNode hn = h.next; // Absorb cancelled first node as head |
353 |
< |
if (hn != null && hn.next == hn) { |
354 |
< |
advanceHead(h, hn); |
355 |
< |
continue; |
356 |
< |
} |
357 |
< |
QNode t = tail.get(); // Ensure consistent read for tail |
358 |
< |
if (t == h) |
359 |
< |
return null; |
337 |
< |
QNode tn = t.next; |
338 |
< |
if (t != tail.get()) |
339 |
< |
continue; |
340 |
< |
if (tn != null) { // Help advance tail |
341 |
< |
tail.compareAndSet(t, tn); |
342 |
< |
continue; |
343 |
< |
} |
344 |
< |
if (s != t) { // If not tail, try to unsplice |
345 |
< |
QNode sn = s.next; |
347 |
> |
/* |
348 |
> |
* At any given time, exactly one node on list cannot be |
349 |
> |
* deleted -- the last inserted node. To accommodate this, if |
350 |
> |
* we cannot delete s, we save its predecessor as "cleanMe", |
351 |
> |
* processing the previously saved version first. At least one |
352 |
> |
* of node s or the node previously saved can always be |
353 |
> |
* processed, so this always terminates. |
354 |
> |
*/ |
355 |
> |
while (pred.next == s) { |
356 |
> |
QNode oldpred = reclean(); // First, help get rid of cleanMe |
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)) |
361 |
< |
return null; |
361 |
> |
break; |
362 |
|
} |
363 |
< |
QNode dp = cleanMe.get(); |
364 |
< |
if (dp != null) { // Try unlinking previous cancelled node |
365 |
< |
QNode d = dp.next; |
366 |
< |
QNode dn; |
367 |
< |
if (d == null || // d is gone or |
368 |
< |
d == dp || // d is off list or |
369 |
< |
d.get() != d || // d not cancelled or |
370 |
< |
(d != t && // d not tail and |
371 |
< |
(dn = d.next) != null && // has successor |
372 |
< |
dn != d && // that is on list |
373 |
< |
dp.casNext(d, dn))) // d unspliced |
374 |
< |
cleanMe.compareAndSet(dp, null); |
375 |
< |
if (dp == pred) |
376 |
< |
return null; // s is already saved node |
363 |
> |
else if (oldpred == pred || // Already saved |
364 |
> |
(oldpred == null && cleanMe.compareAndSet(null, pred))) |
365 |
> |
break; // Postpone cleaning |
366 |
> |
} |
367 |
> |
} |
368 |
> |
|
369 |
> |
/** |
370 |
> |
* Tries to unsplice the cancelled node held in cleanMe that was |
371 |
> |
* previously uncleanable because it was at tail. |
372 |
> |
* @return current cleanMe node (or null) |
373 |
> |
*/ |
374 |
> |
private QNode reclean() { |
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 |
379 |
> |
* make cleanMe slot available. This differs from similar |
380 |
> |
* code in clean() because we must check that pred still |
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. |
385 |
> |
*/ |
386 |
> |
QNode pred; |
387 |
> |
while ((pred = cleanMe.get()) != null) { |
388 |
> |
QNode t = getValidatedTail(); |
389 |
> |
QNode s = pred.next; |
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)) |
394 |
> |
cleanMe.compareAndSet(pred, null); |
395 |
|
} |
396 |
< |
else if (cleanMe.compareAndSet(null, pred)) |
397 |
< |
return null; // Postpone cleaning s |
396 |
> |
else // s is still tail; cannot clean |
397 |
> |
break; |
398 |
|
} |
399 |
+ |
return pred; |
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); |
407 |
+ |
head = new PaddedAtomicReference<QNode>(dummy); |
408 |
+ |
tail = new PaddedAtomicReference<QNode>(dummy); |
409 |
+ |
cleanMe = new PaddedAtomicReference<QNode>(null); |
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 |
418 |
|
* of its elements are null |
419 |
|
*/ |
420 |
|
public LinkedTransferQueue(Collection<? extends E> c) { |
421 |
+ |
this(); |
422 |
|
addAll(c); |
423 |
|
} |
424 |
|
|
445 |
|
public void transfer(E e) throws InterruptedException { |
446 |
|
if (e == null) throw new NullPointerException(); |
447 |
|
if (xfer(e, WAIT, 0) == null) { |
448 |
< |
Thread.interrupted(); |
448 |
> |
Thread.interrupted(); |
449 |
|
throw new InterruptedException(); |
450 |
< |
} |
450 |
> |
} |
451 |
|
} |
452 |
|
|
453 |
|
public boolean tryTransfer(E e, long timeout, TimeUnit unit) |
469 |
|
Object e = xfer(null, WAIT, 0); |
470 |
|
if (e != null) |
471 |
|
return (E)e; |
472 |
< |
Thread.interrupted(); |
472 |
> |
Thread.interrupted(); |
473 |
|
throw new InterruptedException(); |
474 |
|
} |
475 |
|
|
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 |
|
*/ |
716 |
|
private void readObject(java.io.ObjectInputStream s) |
717 |
|
throws java.io.IOException, ClassNotFoundException { |
718 |
|
s.defaultReadObject(); |
719 |
+ |
resetHeadAndTail(); |
720 |
|
for (;;) { |
721 |
|
E item = (E)s.readObject(); |
722 |
|
if (item == null) |
725 |
|
offer(item); |
726 |
|
} |
727 |
|
} |
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 |
+ |
_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 |
+ |
|
786 |
|
} |