44 |
|
* @since 1.7 |
45 |
|
* @author Doug Lea |
46 |
|
* @param <E> the type of elements held in this collection |
47 |
– |
* |
47 |
|
*/ |
48 |
|
public class LinkedTransferQueue<E> extends AbstractQueue<E> |
49 |
|
implements TransferQueue<E>, java.io.Serializable { |
80 |
|
* seems not to vary with number of CPUs (beyond 2) so is just |
81 |
|
* a constant. |
82 |
|
*/ |
83 |
< |
static final int maxTimedSpins = (NCPUS < 2)? 0 : 32; |
83 |
> |
static final int maxTimedSpins = (NCPUS < 2) ? 0 : 32; |
84 |
|
|
85 |
|
/** |
86 |
|
* The number of times to spin before blocking in untimed waits. |
165 |
|
* Puts or takes an item. Used for most queue operations (except |
166 |
|
* poll() and tryTransfer()). See the similar code in |
167 |
|
* SynchronousQueue for detailed explanation. |
168 |
+ |
* |
169 |
|
* @param e the item or if null, signifies that this is a take |
170 |
|
* @param mode the wait mode: NOWAIT, TIMEOUT, WAIT |
171 |
|
* @param nanos timeout in nanosecs, used only if mode is TIMEOUT |
202 |
|
Object x = first.get(); |
203 |
|
if (x != first && first.compareAndSet(x, e)) { |
204 |
|
LockSupport.unpark(first.waiter); |
205 |
< |
return isData? e : x; |
205 |
> |
return isData ? e : x; |
206 |
|
} |
207 |
|
} |
208 |
|
} |
212 |
|
|
213 |
|
/** |
214 |
|
* Version of xfer for poll() and tryTransfer, which |
215 |
< |
* simplifies control paths both here and in xfer |
215 |
> |
* simplifies control paths both here and in xfer. |
216 |
|
*/ |
217 |
|
private Object fulfill(Object e) { |
218 |
|
boolean isData = (e != null); |
240 |
|
Object x = first.get(); |
241 |
|
if (x != first && first.compareAndSet(x, e)) { |
242 |
|
LockSupport.unpark(first.waiter); |
243 |
< |
return isData? e : x; |
243 |
> |
return isData ? e : x; |
244 |
|
} |
245 |
|
} |
246 |
|
} |
263 |
|
if (mode == NOWAIT) |
264 |
|
return null; |
265 |
|
|
266 |
< |
long lastTime = (mode == TIMEOUT)? System.nanoTime() : 0; |
266 |
> |
long lastTime = (mode == TIMEOUT) ? System.nanoTime() : 0; |
267 |
|
Thread w = Thread.currentThread(); |
268 |
|
int spins = -1; // set to desired spin count below |
269 |
|
for (;;) { |
272 |
|
Object x = s.get(); |
273 |
|
if (x != e) { // Node was matched or cancelled |
274 |
|
advanceHead(pred, s); // unlink if head |
275 |
< |
if (x == s) { // was cancelled |
275 |
> |
if (x == s) { // was cancelled |
276 |
|
clean(pred, s); |
277 |
|
return null; |
278 |
|
} |
295 |
|
if (spins < 0) { |
296 |
|
QNode h = head.get(); // only spin if at head |
297 |
|
spins = ((h != null && h.next == s) ? |
298 |
< |
(mode == TIMEOUT? |
298 |
> |
((mode == TIMEOUT) ? |
299 |
|
maxTimedSpins : maxUntimedSpins) : 0); |
300 |
|
} |
301 |
|
if (spins > 0) |
316 |
|
} |
317 |
|
|
318 |
|
/** |
319 |
< |
* Returns validated tail for use in cleaning methods |
319 |
> |
* Returns validated tail for use in cleaning methods. |
320 |
|
*/ |
321 |
|
private QNode getValidatedTail() { |
322 |
|
for (;;) { |
339 |
|
|
340 |
|
/** |
341 |
|
* Gets rid of cancelled node s with original predecessor pred. |
342 |
+ |
* |
343 |
|
* @param pred predecessor of cancelled node |
344 |
|
* @param s the cancelled node |
345 |
|
*/ |
379 |
|
/** |
380 |
|
* Tries to unsplice the cancelled node held in cleanMe that was |
381 |
|
* previously uncleanable because it was at tail. |
382 |
+ |
* |
383 |
|
* @return current cleanMe node (or null) |
384 |
|
*/ |
385 |
|
private QNode reclean() { |
424 |
|
* Creates a {@code LinkedTransferQueue} |
425 |
|
* initially containing the elements of the given collection, |
426 |
|
* added in traversal order of the collection's iterator. |
427 |
+ |
* |
428 |
|
* @param c the collection of elements to initially contain |
429 |
|
* @throws NullPointerException if the specified collection or any |
430 |
|
* of its elements are null |
486 |
|
public E take() throws InterruptedException { |
487 |
|
Object e = xfer(null, WAIT, 0); |
488 |
|
if (e != null) |
489 |
< |
return (E)e; |
489 |
> |
return (E) e; |
490 |
|
Thread.interrupted(); |
491 |
|
throw new InterruptedException(); |
492 |
|
} |
494 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
495 |
|
Object e = xfer(null, TIMEOUT, unit.toNanos(timeout)); |
496 |
|
if (e != null || !Thread.interrupted()) |
497 |
< |
return (E)e; |
497 |
> |
return (E) e; |
498 |
|
throw new InterruptedException(); |
499 |
|
} |
500 |
|
|
501 |
|
public E poll() { |
502 |
< |
return (E)fulfill(null); |
502 |
> |
return (E) fulfill(null); |
503 |
|
} |
504 |
|
|
505 |
|
public int drainTo(Collection<? super E> c) { |
533 |
|
// Traversal-based methods |
534 |
|
|
535 |
|
/** |
536 |
< |
* Return head after performing any outstanding helping steps |
536 |
> |
* Returns head after performing any outstanding helping steps. |
537 |
|
*/ |
538 |
|
private QNode traversalHead() { |
539 |
|
for (;;) { |
578 |
|
QNode snext; // successor of next |
579 |
|
QNode curr; // last returned node, for remove() |
580 |
|
QNode pcurr; // predecessor of curr, for remove() |
581 |
< |
E nextItem; // Cache of next item, once commited to in next |
581 |
> |
E nextItem; // Cache of next item, once committed to in next |
582 |
|
|
583 |
|
Itr() { |
584 |
|
findNext(); |
585 |
|
} |
586 |
|
|
587 |
|
/** |
588 |
< |
* Ensure next points to next valid node, or null if none. |
588 |
> |
* Ensures next points to next valid node, or null if none. |
589 |
|
*/ |
590 |
|
void findNext() { |
591 |
|
for (;;) { |
602 |
|
Object x = q.get(); |
603 |
|
QNode s = q.next; |
604 |
|
if (x != null && q != x && q != s) { |
605 |
< |
nextItem = (E)x; |
605 |
> |
nextItem = (E) x; |
606 |
|
snext = s; |
607 |
|
pnext = pred; |
608 |
|
next = q; |
649 |
|
if (!p.isData) |
650 |
|
return null; |
651 |
|
if (x != null) |
652 |
< |
return (E)x; |
652 |
> |
return (E) x; |
653 |
|
} |
654 |
|
} |
655 |
|
} |
735 |
|
if (q == pred) // restart |
736 |
|
break; |
737 |
|
Object x = q.get(); |
738 |
< |
if (x != null && x != q && o.equals(x) && |
738 |
> |
if (x != null && x != q && o.equals(x) && |
739 |
|
q.compareAndSet(x, q)) { |
740 |
|
clean(pred, q); |
741 |
|
return true; |
755 |
|
private void writeObject(java.io.ObjectOutputStream s) |
756 |
|
throws java.io.IOException { |
757 |
|
s.defaultWriteObject(); |
758 |
< |
for (Iterator<E> it = iterator(); it.hasNext(); ) |
759 |
< |
s.writeObject(it.next()); |
758 |
> |
for (E e : this) |
759 |
> |
s.writeObject(e); |
760 |
|
// Use trailing null as sentinel |
761 |
|
s.writeObject(null); |
762 |
|
} |
764 |
|
/** |
765 |
|
* Reconstitute the Queue instance from a stream (that is, |
766 |
|
* deserialize it). |
767 |
+ |
* |
768 |
|
* @param s the stream |
769 |
|
*/ |
770 |
|
private void readObject(java.io.ObjectInputStream s) |
772 |
|
s.defaultReadObject(); |
773 |
|
resetHeadAndTail(); |
774 |
|
for (;;) { |
775 |
< |
E item = (E)s.readObject(); |
775 |
> |
E item = (E) s.readObject(); |
776 |
|
if (item == null) |
777 |
|
break; |
778 |
|
else |
784 |
|
// Support for resetting head/tail while deserializing |
785 |
|
private void resetHeadAndTail() { |
786 |
|
QNode dummy = new QNode(null, false); |
787 |
< |
_unsafe.putObjectVolatile(this, headOffset, |
787 |
> |
UNSAFE.putObjectVolatile(this, headOffset, |
788 |
|
new PaddedAtomicReference<QNode>(dummy)); |
789 |
< |
_unsafe.putObjectVolatile(this, tailOffset, |
789 |
> |
UNSAFE.putObjectVolatile(this, tailOffset, |
790 |
|
new PaddedAtomicReference<QNode>(dummy)); |
791 |
< |
_unsafe.putObjectVolatile(this, cleanMeOffset, |
791 |
> |
UNSAFE.putObjectVolatile(this, cleanMeOffset, |
792 |
|
new PaddedAtomicReference<QNode>(null)); |
793 |
|
} |
794 |
|
|
818 |
|
|
819 |
|
private static long fieldOffset(String fieldName) |
820 |
|
throws NoSuchFieldException { |
821 |
< |
return _unsafe.objectFieldOffset |
821 |
> |
return UNSAFE.objectFieldOffset |
822 |
|
(LinkedTransferQueue.class.getDeclaredField(fieldName)); |
823 |
|
} |
824 |
|
|
825 |
< |
private static final Unsafe _unsafe; |
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 |
< |
_unsafe = getUnsafe(); |
831 |
> |
UNSAFE = getUnsafe(); |
832 |
|
headOffset = fieldOffset("head"); |
833 |
|
tailOffset = fieldOffset("tail"); |
834 |
|
cleanMeOffset = fieldOffset("cleanMe"); |