ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/LinkedTransferQueue.java
Revision: 1.31
Committed: Wed Jul 29 02:17:02 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +42 -12 lines
Log Message:
Use Unsafe instead of AtomicReferenceFieldUpdater

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5     */
6    
7     package jsr166y;
8 jsr166 1.26
9 dl 1.1 import java.util.concurrent.*;
10 jsr166 1.26
11     import java.util.AbstractQueue;
12     import java.util.Collection;
13     import java.util.Iterator;
14     import java.util.NoSuchElementException;
15     import java.util.concurrent.locks.LockSupport;
16     import java.util.concurrent.atomic.AtomicReference;
17 dl 1.1
18     /**
19     * An unbounded {@linkplain TransferQueue} based on linked nodes.
20     * This queue orders elements FIFO (first-in-first-out) with respect
21     * to any given producer. The <em>head</em> of the queue is that
22     * element that has been on the queue the longest time for some
23     * producer. The <em>tail</em> of the queue is that element that has
24     * been on the queue the shortest time for some producer.
25     *
26 jsr166 1.11 * <p>Beware that, unlike in most collections, the {@code size}
27 dl 1.1 * method is <em>NOT</em> a constant-time operation. Because of the
28     * asynchronous nature of these queues, determining the current number
29     * of elements requires a traversal of the elements.
30     *
31     * <p>This class and its iterator implement all of the
32     * <em>optional</em> methods of the {@link Collection} and {@link
33     * Iterator} interfaces.
34     *
35     * <p>Memory consistency effects: As with other concurrent
36     * collections, actions in a thread prior to placing an object into a
37     * {@code LinkedTransferQueue}
38     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
39     * actions subsequent to the access or removal of that element from
40     * the {@code LinkedTransferQueue} in another thread.
41     *
42     * <p>This class is a member of the
43     * <a href="{@docRoot}/../technotes/guides/collections/index.html">
44     * Java Collections Framework</a>.
45     *
46 dl 1.3 * @since 1.7
47 dl 1.1 * @author Doug Lea
48     * @param <E> the type of elements held in this collection
49     */
50     public class LinkedTransferQueue<E> extends AbstractQueue<E>
51     implements TransferQueue<E>, java.io.Serializable {
52     private static final long serialVersionUID = -3223113410248163686L;
53    
54     /*
55     * This class extends the approach used in FIFO-mode
56     * SynchronousQueues. See the internal documentation, as well as
57     * the PPoPP 2006 paper "Scalable Synchronous Queues" by Scherer,
58     * Lea & Scott
59     * (http://www.cs.rice.edu/~wns1/papers/2006-PPoPP-SQ.pdf)
60     *
61 dl 1.9 * The main extension is to provide different Wait modes for the
62     * main "xfer" method that puts or takes items. These don't
63     * impact the basic dual-queue logic, but instead control whether
64     * or how threads block upon insertion of request or data nodes
65     * into the dual queue. It also uses slightly different
66     * conventions for tracking whether nodes are off-list or
67     * cancelled.
68 dl 1.1 */
69    
70     // Wait modes for xfer method
71     static final int NOWAIT = 0;
72     static final int TIMEOUT = 1;
73     static final int WAIT = 2;
74    
75     /** The number of CPUs, for spin control */
76     static final int NCPUS = Runtime.getRuntime().availableProcessors();
77    
78     /**
79     * The number of times to spin before blocking in timed waits.
80     * The value is empirically derived -- it works well across a
81     * variety of processors and OSes. Empirically, the best value
82     * seems not to vary with number of CPUs (beyond 2) so is just
83     * a constant.
84     */
85 jsr166 1.22 static final int maxTimedSpins = (NCPUS < 2) ? 0 : 32;
86 dl 1.1
87     /**
88     * The number of times to spin before blocking in untimed waits.
89     * This is greater than timed value because untimed waits spin
90     * faster since they don't need to check times on each spin.
91     */
92     static final int maxUntimedSpins = maxTimedSpins * 16;
93    
94     /**
95     * The number of nanoseconds for which it is faster to spin
96     * rather than to use timed park. A rough estimate suffices.
97     */
98     static final long spinForTimeoutThreshold = 1000L;
99    
100 jsr166 1.5 /**
101 dl 1.9 * Node class for LinkedTransferQueue. Opportunistically
102     * subclasses from AtomicReference to represent item. Uses Object,
103     * not E, to allow setting item to "this" after use, to avoid
104     * garbage retention. Similarly, setting the next field to this is
105     * used as sentinel that node is off list.
106 dl 1.1 */
107 jsr166 1.25 static final class Node<E> extends AtomicReference<Object> {
108     volatile Node<E> next;
109 dl 1.1 volatile Thread waiter; // to control park/unpark
110     final boolean isData;
111 jsr166 1.25
112     Node(E item, boolean isData) {
113 dl 1.1 super(item);
114     this.isData = isData;
115     }
116    
117 jsr166 1.25 final boolean casNext(Node<E> cmp, Node<E> val) {
118 jsr166 1.31 return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
119 dl 1.1 }
120 dl 1.15
121     final void clearNext() {
122 jsr166 1.31 UNSAFE.putOrderedObject(this, nextOffset, this);
123     }
124    
125     // Unsafe mechanics
126    
127     private static final sun.misc.Unsafe UNSAFE = getUnsafe();
128     private static final long nextOffset =
129     objectFieldOffset(UNSAFE, "next", Node.class);
130    
131     /**
132     * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package.
133     * Replace with a simple call to Unsafe.getUnsafe when integrating
134     * into a jdk.
135     *
136     * @return a sun.misc.Unsafe
137     */
138     private static sun.misc.Unsafe getUnsafe() {
139     try {
140     return sun.misc.Unsafe.getUnsafe();
141     } catch (SecurityException se) {
142     try {
143     return java.security.AccessController.doPrivileged
144     (new java.security
145     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
146     public sun.misc.Unsafe run() throws Exception {
147     java.lang.reflect.Field f = sun.misc
148     .Unsafe.class.getDeclaredField("theUnsafe");
149     f.setAccessible(true);
150     return (sun.misc.Unsafe) f.get(null);
151     }});
152     } catch (java.security.PrivilegedActionException e) {
153     throw new RuntimeException("Could not initialize intrinsics",
154     e.getCause());
155     }
156     }
157 dl 1.15 }
158    
159 jsr166 1.24 private static final long serialVersionUID = -3375979862319811754L;
160 dl 1.1 }
161    
162     /**
163     * Padded version of AtomicReference used for head, tail and
164     * cleanMe, to alleviate contention across threads CASing one vs
165     * the other.
166     */
167     static final class PaddedAtomicReference<T> extends AtomicReference<T> {
168     // enough padding for 64bytes with 4byte refs
169     Object p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pa, pb, pc, pd, pe;
170     PaddedAtomicReference(T r) { super(r); }
171 jsr166 1.24 private static final long serialVersionUID = 8170090609809740854L;
172 dl 1.1 }
173    
174    
175 dl 1.7 /** head of the queue */
176 jsr166 1.25 private transient final PaddedAtomicReference<Node<E>> head;
177 jsr166 1.23
178 dl 1.7 /** tail of the queue */
179 jsr166 1.25 private transient final PaddedAtomicReference<Node<E>> tail;
180 dl 1.1
181     /**
182     * Reference to a cancelled node that might not yet have been
183     * unlinked from queue because it was the last inserted node
184     * when it cancelled.
185     */
186 jsr166 1.25 private transient final PaddedAtomicReference<Node<E>> cleanMe;
187 dl 1.1
188     /**
189     * Tries to cas nh as new head; if successful, unlink
190     * old head's next node to avoid garbage retention.
191     */
192 jsr166 1.25 private boolean advanceHead(Node<E> h, Node<E> nh) {
193 dl 1.1 if (h == head.get() && head.compareAndSet(h, nh)) {
194 dl 1.15 h.clearNext(); // forget old next
195 dl 1.1 return true;
196     }
197     return false;
198     }
199 jsr166 1.5
200 dl 1.1 /**
201     * Puts or takes an item. Used for most queue operations (except
202 dl 1.9 * poll() and tryTransfer()). See the similar code in
203     * SynchronousQueue for detailed explanation.
204 jsr166 1.17 *
205 jsr166 1.4 * @param e the item or if null, signifies that this is a take
206 dl 1.1 * @param mode the wait mode: NOWAIT, TIMEOUT, WAIT
207     * @param nanos timeout in nanosecs, used only if mode is TIMEOUT
208     * @return an item, or null on failure
209     */
210 jsr166 1.25 private E xfer(E e, int mode, long nanos) {
211 dl 1.1 boolean isData = (e != null);
212 jsr166 1.25 Node<E> s = null;
213     final PaddedAtomicReference<Node<E>> head = this.head;
214     final PaddedAtomicReference<Node<E>> tail = this.tail;
215 dl 1.1
216     for (;;) {
217 jsr166 1.25 Node<E> t = tail.get();
218     Node<E> h = head.get();
219 dl 1.1
220     if (t != null && (t == h || t.isData == isData)) {
221     if (s == null)
222 jsr166 1.25 s = new Node<E>(e, isData);
223     Node<E> last = t.next;
224 dl 1.1 if (last != null) {
225     if (t == tail.get())
226     tail.compareAndSet(t, last);
227     }
228     else if (t.casNext(null, s)) {
229     tail.compareAndSet(t, s);
230     return awaitFulfill(t, s, e, mode, nanos);
231     }
232     }
233 jsr166 1.5
234 dl 1.1 else if (h != null) {
235 jsr166 1.25 Node<E> first = h.next;
236 jsr166 1.5 if (t == tail.get() && first != null &&
237 dl 1.1 advanceHead(h, first)) {
238     Object x = first.get();
239     if (x != first && first.compareAndSet(x, e)) {
240     LockSupport.unpark(first.waiter);
241 jsr166 1.25 return isData ? e : (E) x;
242 dl 1.1 }
243     }
244     }
245     }
246     }
247    
248    
249     /**
250     * Version of xfer for poll() and tryTransfer, which
251 jsr166 1.17 * simplifies control paths both here and in xfer.
252 dl 1.1 */
253 jsr166 1.25 private E fulfill(E e) {
254 dl 1.1 boolean isData = (e != null);
255 jsr166 1.25 final PaddedAtomicReference<Node<E>> head = this.head;
256     final PaddedAtomicReference<Node<E>> tail = this.tail;
257 dl 1.1
258     for (;;) {
259 jsr166 1.25 Node<E> t = tail.get();
260     Node<E> h = head.get();
261 dl 1.1
262     if (t != null && (t == h || t.isData == isData)) {
263 jsr166 1.25 Node<E> last = t.next;
264 dl 1.1 if (t == tail.get()) {
265     if (last != null)
266     tail.compareAndSet(t, last);
267     else
268     return null;
269     }
270     }
271     else if (h != null) {
272 jsr166 1.25 Node<E> first = h.next;
273 jsr166 1.5 if (t == tail.get() &&
274 dl 1.1 first != null &&
275     advanceHead(h, first)) {
276     Object x = first.get();
277     if (x != first && first.compareAndSet(x, e)) {
278     LockSupport.unpark(first.waiter);
279 jsr166 1.25 return isData ? e : (E) x;
280 dl 1.1 }
281     }
282     }
283     }
284     }
285    
286     /**
287     * Spins/blocks until node s is fulfilled or caller gives up,
288     * depending on wait mode.
289     *
290     * @param pred the predecessor of waiting node
291     * @param s the waiting node
292     * @param e the comparison value for checking match
293     * @param mode mode
294     * @param nanos timeout value
295     * @return matched item, or s if cancelled
296     */
297 jsr166 1.25 private E awaitFulfill(Node<E> pred, Node<E> s, E e,
298     int mode, long nanos) {
299 dl 1.1 if (mode == NOWAIT)
300     return null;
301    
302 jsr166 1.22 long lastTime = (mode == TIMEOUT) ? System.nanoTime() : 0;
303 dl 1.1 Thread w = Thread.currentThread();
304     int spins = -1; // set to desired spin count below
305     for (;;) {
306     if (w.isInterrupted())
307     s.compareAndSet(e, s);
308     Object x = s.get();
309     if (x != e) { // Node was matched or cancelled
310     advanceHead(pred, s); // unlink if head
311 jsr166 1.17 if (x == s) { // was cancelled
312 dl 1.9 clean(pred, s);
313     return null;
314     }
315 jsr166 1.5 else if (x != null) {
316 dl 1.1 s.set(s); // avoid garbage retention
317 jsr166 1.25 return (E) x;
318 dl 1.1 }
319     else
320     return e;
321     }
322     if (mode == TIMEOUT) {
323     long now = System.nanoTime();
324     nanos -= now - lastTime;
325     lastTime = now;
326     if (nanos <= 0) {
327     s.compareAndSet(e, s); // try to cancel
328     continue;
329     }
330     }
331     if (spins < 0) {
332 jsr166 1.25 Node<E> h = head.get(); // only spin if at head
333 dl 1.1 spins = ((h != null && h.next == s) ?
334 jsr166 1.22 ((mode == TIMEOUT) ?
335 dl 1.1 maxTimedSpins : maxUntimedSpins) : 0);
336     }
337     if (spins > 0)
338     --spins;
339     else if (s.waiter == null)
340     s.waiter = w;
341     else if (mode != TIMEOUT) {
342 dl 1.12 LockSupport.park(this);
343 dl 1.1 s.waiter = null;
344     spins = -1;
345     }
346     else if (nanos > spinForTimeoutThreshold) {
347 dl 1.12 LockSupport.parkNanos(this, nanos);
348 dl 1.1 s.waiter = null;
349     spins = -1;
350     }
351     }
352     }
353    
354     /**
355 jsr166 1.17 * Returns validated tail for use in cleaning methods.
356 dl 1.9 */
357 jsr166 1.25 private Node<E> getValidatedTail() {
358 dl 1.9 for (;;) {
359 jsr166 1.25 Node<E> h = head.get();
360     Node<E> first = h.next;
361 dl 1.9 if (first != null && first.next == first) { // help advance
362     advanceHead(h, first);
363     continue;
364     }
365 jsr166 1.25 Node<E> t = tail.get();
366     Node<E> last = t.next;
367 dl 1.9 if (t == tail.get()) {
368     if (last != null)
369     tail.compareAndSet(t, last); // help advance
370     else
371     return t;
372     }
373     }
374 jsr166 1.10 }
375 dl 1.9
376     /**
377 dl 1.1 * Gets rid of cancelled node s with original predecessor pred.
378 jsr166 1.17 *
379 dl 1.9 * @param pred predecessor of cancelled node
380     * @param s the cancelled node
381 dl 1.1 */
382 jsr166 1.25 private void clean(Node<E> pred, Node<E> s) {
383 dl 1.1 Thread w = s.waiter;
384     if (w != null) { // Wake up thread
385     s.waiter = null;
386     if (w != Thread.currentThread())
387     LockSupport.unpark(w);
388     }
389 dl 1.15
390     if (pred == null)
391     return;
392    
393 dl 1.9 /*
394     * At any given time, exactly one node on list cannot be
395     * deleted -- the last inserted node. To accommodate this, if
396     * we cannot delete s, we save its predecessor as "cleanMe",
397     * processing the previously saved version first. At least one
398     * of node s or the node previously saved can always be
399     * processed, so this always terminates.
400     */
401     while (pred.next == s) {
402 jsr166 1.25 Node<E> oldpred = reclean(); // First, help get rid of cleanMe
403     Node<E> t = getValidatedTail();
404 dl 1.9 if (s != t) { // If not tail, try to unsplice
405 jsr166 1.25 Node<E> sn = s.next; // s.next == s means s already off list
406 jsr166 1.10 if (sn == s || pred.casNext(s, sn))
407 dl 1.9 break;
408     }
409     else if (oldpred == pred || // Already saved
410     (oldpred == null && cleanMe.compareAndSet(null, pred)))
411     break; // Postpone cleaning
412     }
413     }
414 jsr166 1.5
415 dl 1.9 /**
416     * Tries to unsplice the cancelled node held in cleanMe that was
417     * previously uncleanable because it was at tail.
418 jsr166 1.17 *
419 dl 1.9 * @return current cleanMe node (or null)
420     */
421 jsr166 1.25 private Node<E> reclean() {
422 jsr166 1.10 /*
423 dl 1.9 * cleanMe is, or at one time was, predecessor of cancelled
424     * node s that was the tail so could not be unspliced. If s
425     * is no longer the tail, try to unsplice if necessary and
426     * make cleanMe slot available. This differs from similar
427     * code in clean() because we must check that pred still
428     * points to a cancelled node that must be unspliced -- if
429     * not, we can (must) clear cleanMe without unsplicing.
430     * This can loop only due to contention on casNext or
431 jsr166 1.10 * clearing cleanMe.
432 dl 1.9 */
433 jsr166 1.25 Node<E> pred;
434 dl 1.9 while ((pred = cleanMe.get()) != null) {
435 jsr166 1.25 Node<E> t = getValidatedTail();
436     Node<E> s = pred.next;
437 jsr166 1.10 if (s != t) {
438 jsr166 1.25 Node<E> sn;
439 dl 1.9 if (s == null || s == pred || s.get() != s ||
440     (sn = s.next) == s || pred.casNext(s, sn))
441     cleanMe.compareAndSet(pred, null);
442 dl 1.1 }
443 dl 1.9 else // s is still tail; cannot clean
444     break;
445 dl 1.1 }
446 dl 1.9 return pred;
447 dl 1.1 }
448 jsr166 1.5
449 dl 1.1 /**
450 jsr166 1.11 * Creates an initially empty {@code LinkedTransferQueue}.
451 dl 1.1 */
452     public LinkedTransferQueue() {
453 jsr166 1.25 Node<E> dummy = new Node<E>(null, false);
454     head = new PaddedAtomicReference<Node<E>>(dummy);
455     tail = new PaddedAtomicReference<Node<E>>(dummy);
456     cleanMe = new PaddedAtomicReference<Node<E>>(null);
457 dl 1.1 }
458    
459     /**
460 jsr166 1.11 * Creates a {@code LinkedTransferQueue}
461 dl 1.1 * initially containing the elements of the given collection,
462     * added in traversal order of the collection's iterator.
463 jsr166 1.17 *
464 dl 1.1 * @param c the collection of elements to initially contain
465     * @throws NullPointerException if the specified collection or any
466     * of its elements are null
467     */
468     public LinkedTransferQueue(Collection<? extends E> c) {
469 dl 1.7 this();
470 dl 1.1 addAll(c);
471     }
472    
473 jsr166 1.29 /**
474     * @throws InterruptedException {@inheritDoc}
475     * @throws NullPointerException {@inheritDoc}
476     */
477 dl 1.1 public void put(E e) throws InterruptedException {
478     if (e == null) throw new NullPointerException();
479     if (Thread.interrupted()) throw new InterruptedException();
480     xfer(e, NOWAIT, 0);
481     }
482    
483 jsr166 1.29 /**
484     * @throws InterruptedException {@inheritDoc}
485     * @throws NullPointerException {@inheritDoc}
486     */
487 jsr166 1.5 public boolean offer(E e, long timeout, TimeUnit unit)
488 dl 1.1 throws InterruptedException {
489     if (e == null) throw new NullPointerException();
490     if (Thread.interrupted()) throw new InterruptedException();
491     xfer(e, NOWAIT, 0);
492     return true;
493     }
494    
495 jsr166 1.29 /**
496     * @throws NullPointerException {@inheritDoc}
497     */
498 dl 1.1 public boolean offer(E e) {
499     if (e == null) throw new NullPointerException();
500     xfer(e, NOWAIT, 0);
501     return true;
502     }
503    
504 jsr166 1.29 /**
505     * @throws NullPointerException {@inheritDoc}
506     */
507 dl 1.15 public boolean add(E e) {
508     if (e == null) throw new NullPointerException();
509     xfer(e, NOWAIT, 0);
510     return true;
511     }
512    
513 jsr166 1.29 /**
514     * @throws InterruptedException {@inheritDoc}
515     * @throws NullPointerException {@inheritDoc}
516     */
517 dl 1.1 public void transfer(E e) throws InterruptedException {
518     if (e == null) throw new NullPointerException();
519     if (xfer(e, WAIT, 0) == null) {
520 jsr166 1.6 Thread.interrupted();
521 dl 1.1 throw new InterruptedException();
522 jsr166 1.6 }
523 dl 1.1 }
524    
525 jsr166 1.29 /**
526     * @throws InterruptedException {@inheritDoc}
527     * @throws NullPointerException {@inheritDoc}
528     */
529 dl 1.1 public boolean tryTransfer(E e, long timeout, TimeUnit unit)
530     throws InterruptedException {
531     if (e == null) throw new NullPointerException();
532     if (xfer(e, TIMEOUT, unit.toNanos(timeout)) != null)
533     return true;
534     if (!Thread.interrupted())
535     return false;
536     throw new InterruptedException();
537     }
538    
539 jsr166 1.29 /**
540     * @throws NullPointerException {@inheritDoc}
541     */
542 dl 1.1 public boolean tryTransfer(E e) {
543     if (e == null) throw new NullPointerException();
544     return fulfill(e) != null;
545     }
546    
547 jsr166 1.29 /**
548     * @throws InterruptedException {@inheritDoc}
549     */
550 dl 1.1 public E take() throws InterruptedException {
551     Object e = xfer(null, WAIT, 0);
552     if (e != null)
553 jsr166 1.22 return (E) e;
554 jsr166 1.6 Thread.interrupted();
555 dl 1.1 throw new InterruptedException();
556     }
557    
558 jsr166 1.29 /**
559     * @throws InterruptedException {@inheritDoc}
560     */
561 dl 1.1 public E poll(long timeout, TimeUnit unit) throws InterruptedException {
562     Object e = xfer(null, TIMEOUT, unit.toNanos(timeout));
563     if (e != null || !Thread.interrupted())
564 jsr166 1.22 return (E) e;
565 dl 1.1 throw new InterruptedException();
566     }
567    
568     public E poll() {
569 jsr166 1.25 return fulfill(null);
570 dl 1.1 }
571    
572 jsr166 1.29 /**
573 jsr166 1.30 * @throws NullPointerException {@inheritDoc}
574     * @throws IllegalArgumentException {@inheritDoc}
575 jsr166 1.29 */
576 dl 1.1 public int drainTo(Collection<? super E> c) {
577     if (c == null)
578     throw new NullPointerException();
579     if (c == this)
580     throw new IllegalArgumentException();
581     int n = 0;
582     E e;
583     while ( (e = poll()) != null) {
584     c.add(e);
585     ++n;
586     }
587     return n;
588     }
589    
590 jsr166 1.29 /**
591 jsr166 1.30 * @throws NullPointerException {@inheritDoc}
592     * @throws IllegalArgumentException {@inheritDoc}
593 jsr166 1.29 */
594 dl 1.1 public int drainTo(Collection<? super E> c, int maxElements) {
595     if (c == null)
596     throw new NullPointerException();
597     if (c == this)
598     throw new IllegalArgumentException();
599     int n = 0;
600     E e;
601     while (n < maxElements && (e = poll()) != null) {
602     c.add(e);
603     ++n;
604     }
605     return n;
606     }
607    
608     // Traversal-based methods
609    
610     /**
611 jsr166 1.17 * Returns head after performing any outstanding helping steps.
612 dl 1.1 */
613 jsr166 1.25 private Node<E> traversalHead() {
614 dl 1.1 for (;;) {
615 jsr166 1.25 Node<E> t = tail.get();
616     Node<E> h = head.get();
617 dl 1.1 if (h != null && t != null) {
618 jsr166 1.25 Node<E> last = t.next;
619     Node<E> first = h.next;
620 dl 1.1 if (t == tail.get()) {
621 jsr166 1.5 if (last != null)
622 dl 1.1 tail.compareAndSet(t, last);
623     else if (first != null) {
624     Object x = first.get();
625 jsr166 1.5 if (x == first)
626     advanceHead(h, first);
627 dl 1.1 else
628     return h;
629     }
630     else
631     return h;
632     }
633     }
634 dl 1.15 reclean();
635 dl 1.1 }
636     }
637    
638    
639     public Iterator<E> iterator() {
640     return new Itr();
641     }
642    
643     /**
644 jsr166 1.4 * Iterators. Basic strategy is to traverse list, treating
645 dl 1.1 * non-data (i.e., request) nodes as terminating list.
646     * Once a valid data node is found, the item is cached
647     * so that the next call to next() will return it even
648     * if subsequently removed.
649     */
650     class Itr implements Iterator<E> {
651 jsr166 1.25 Node<E> next; // node to return next
652     Node<E> pnext; // predecessor of next
653     Node<E> snext; // successor of next
654     Node<E> curr; // last returned node, for remove()
655     Node<E> pcurr; // predecessor of curr, for remove()
656 jsr166 1.18 E nextItem; // Cache of next item, once committed to in next
657 jsr166 1.5
658 dl 1.1 Itr() {
659 dl 1.15 findNext();
660 dl 1.1 }
661 jsr166 1.5
662 dl 1.15 /**
663 jsr166 1.17 * Ensures next points to next valid node, or null if none.
664 dl 1.15 */
665     void findNext() {
666 dl 1.1 for (;;) {
667 jsr166 1.25 Node<E> pred = pnext;
668     Node<E> q = next;
669 dl 1.15 if (pred == null || pred == q) {
670     pred = traversalHead();
671     q = pred.next;
672     }
673     if (q == null || !q.isData) {
674     next = null;
675     return;
676     }
677     Object x = q.get();
678 jsr166 1.25 Node<E> s = q.next;
679 dl 1.15 if (x != null && q != x && q != s) {
680 jsr166 1.22 nextItem = (E) x;
681 dl 1.15 snext = s;
682     pnext = pred;
683     next = q;
684     return;
685 dl 1.1 }
686 dl 1.15 pnext = q;
687     next = s;
688 dl 1.1 }
689     }
690 jsr166 1.5
691 dl 1.1 public boolean hasNext() {
692 dl 1.15 return next != null;
693 dl 1.1 }
694 jsr166 1.5
695 dl 1.1 public E next() {
696 dl 1.15 if (next == null) throw new NoSuchElementException();
697     pcurr = pnext;
698     curr = next;
699     pnext = next;
700     next = snext;
701     E x = nextItem;
702     findNext();
703     return x;
704 dl 1.1 }
705 jsr166 1.5
706 dl 1.1 public void remove() {
707 jsr166 1.25 Node<E> p = curr;
708 dl 1.15 if (p == null)
709 dl 1.1 throw new IllegalStateException();
710     Object x = p.get();
711     if (x != null && x != p && p.compareAndSet(x, p))
712 dl 1.15 clean(pcurr, p);
713 dl 1.1 }
714     }
715    
716     public E peek() {
717     for (;;) {
718 jsr166 1.25 Node<E> h = traversalHead();
719     Node<E> p = h.next;
720 dl 1.1 if (p == null)
721     return null;
722     Object x = p.get();
723     if (p != x) {
724     if (!p.isData)
725     return null;
726     if (x != null)
727 jsr166 1.22 return (E) x;
728 dl 1.1 }
729     }
730     }
731    
732 dl 1.2 public boolean isEmpty() {
733     for (;;) {
734 jsr166 1.25 Node<E> h = traversalHead();
735     Node<E> p = h.next;
736 dl 1.2 if (p == null)
737     return true;
738     Object x = p.get();
739     if (p != x) {
740     if (!p.isData)
741     return true;
742     if (x != null)
743     return false;
744     }
745     }
746     }
747    
748 dl 1.1 public boolean hasWaitingConsumer() {
749     for (;;) {
750 jsr166 1.25 Node<E> h = traversalHead();
751     Node<E> p = h.next;
752 dl 1.1 if (p == null)
753     return false;
754     Object x = p.get();
755 jsr166 1.5 if (p != x)
756 dl 1.1 return !p.isData;
757     }
758     }
759 jsr166 1.5
760 dl 1.1 /**
761     * Returns the number of elements in this queue. If this queue
762 jsr166 1.11 * contains more than {@code Integer.MAX_VALUE} elements, returns
763     * {@code Integer.MAX_VALUE}.
764 dl 1.1 *
765     * <p>Beware that, unlike in most collections, this method is
766     * <em>NOT</em> a constant-time operation. Because of the
767     * asynchronous nature of these queues, determining the current
768     * number of elements requires an O(n) traversal.
769     *
770     * @return the number of elements in this queue
771     */
772     public int size() {
773     int count = 0;
774 jsr166 1.25 Node<E> h = traversalHead();
775     for (Node<E> p = h.next; p != null && p.isData; p = p.next) {
776 dl 1.1 Object x = p.get();
777 jsr166 1.5 if (x != null && x != p) {
778 dl 1.1 if (++count == Integer.MAX_VALUE) // saturated
779     break;
780     }
781     }
782     return count;
783     }
784    
785     public int getWaitingConsumerCount() {
786     int count = 0;
787 jsr166 1.25 Node<E> h = traversalHead();
788     for (Node<E> p = h.next; p != null && !p.isData; p = p.next) {
789 dl 1.1 if (p.get() == null) {
790     if (++count == Integer.MAX_VALUE)
791     break;
792     }
793     }
794     return count;
795     }
796    
797     public int remainingCapacity() {
798     return Integer.MAX_VALUE;
799     }
800    
801 dl 1.15 public boolean remove(Object o) {
802     if (o == null)
803     return false;
804     for (;;) {
805 jsr166 1.25 Node<E> pred = traversalHead();
806 dl 1.15 for (;;) {
807 jsr166 1.25 Node<E> q = pred.next;
808 dl 1.15 if (q == null || !q.isData)
809     return false;
810     if (q == pred) // restart
811     break;
812     Object x = q.get();
813 jsr166 1.17 if (x != null && x != q && o.equals(x) &&
814 dl 1.15 q.compareAndSet(x, q)) {
815     clean(pred, q);
816     return true;
817     }
818     pred = q;
819     }
820     }
821     }
822    
823 dl 1.1 /**
824     * Save the state to a stream (that is, serialize it).
825     *
826 jsr166 1.11 * @serialData All of the elements (each an {@code E}) in
827 dl 1.1 * the proper order, followed by a null
828     * @param s the stream
829     */
830     private void writeObject(java.io.ObjectOutputStream s)
831     throws java.io.IOException {
832     s.defaultWriteObject();
833 jsr166 1.16 for (E e : this)
834     s.writeObject(e);
835 dl 1.1 // Use trailing null as sentinel
836     s.writeObject(null);
837     }
838    
839     /**
840     * Reconstitute the Queue instance from a stream (that is,
841     * deserialize it).
842 jsr166 1.19 *
843 dl 1.1 * @param s the stream
844     */
845     private void readObject(java.io.ObjectInputStream s)
846     throws java.io.IOException, ClassNotFoundException {
847     s.defaultReadObject();
848 dl 1.7 resetHeadAndTail();
849 dl 1.1 for (;;) {
850 jsr166 1.25 @SuppressWarnings("unchecked") E item = (E) s.readObject();
851 dl 1.1 if (item == null)
852     break;
853     else
854     offer(item);
855     }
856     }
857 dl 1.7
858     // Support for resetting head/tail while deserializing
859 dl 1.12 private void resetHeadAndTail() {
860 jsr166 1.25 Node<E> dummy = new Node<E>(null, false);
861 jsr166 1.20 UNSAFE.putObjectVolatile(this, headOffset,
862 jsr166 1.25 new PaddedAtomicReference<Node<E>>(dummy));
863 jsr166 1.20 UNSAFE.putObjectVolatile(this, tailOffset,
864 jsr166 1.25 new PaddedAtomicReference<Node<E>>(dummy));
865 jsr166 1.20 UNSAFE.putObjectVolatile(this, cleanMeOffset,
866 jsr166 1.25 new PaddedAtomicReference<Node<E>>(null));
867 dl 1.12 }
868 dl 1.7
869 jsr166 1.28 // Unsafe mechanics
870    
871     private static final sun.misc.Unsafe UNSAFE = getUnsafe();
872     private static final long headOffset =
873 jsr166 1.31 objectFieldOffset(UNSAFE, "head", LinkedTransferQueue.class);
874 jsr166 1.28 private static final long tailOffset =
875 jsr166 1.31 objectFieldOffset(UNSAFE, "tail", LinkedTransferQueue.class);
876 jsr166 1.28 private static final long cleanMeOffset =
877 jsr166 1.31 objectFieldOffset(UNSAFE, "cleanMe", LinkedTransferQueue.class);
878    
879 jsr166 1.28
880 jsr166 1.31 static long objectFieldOffset(sun.misc.Unsafe UNSAFE,
881     String field, Class<?> klazz) {
882 jsr166 1.28 try {
883     return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
884     } catch (NoSuchFieldException e) {
885     // Convert Exception to corresponding Error
886     NoSuchFieldError error = new NoSuchFieldError(field);
887     error.initCause(e);
888     throw error;
889     }
890     }
891    
892     /**
893     * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package.
894     * Replace with a simple call to Unsafe.getUnsafe when integrating
895     * into a jdk.
896     *
897     * @return a sun.misc.Unsafe
898     */
899 jsr166 1.25 private static sun.misc.Unsafe getUnsafe() {
900 jsr166 1.13 try {
901 jsr166 1.25 return sun.misc.Unsafe.getUnsafe();
902 jsr166 1.13 } catch (SecurityException se) {
903     try {
904     return java.security.AccessController.doPrivileged
905 jsr166 1.28 (new java.security
906     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
907 jsr166 1.25 public sun.misc.Unsafe run() throws Exception {
908 jsr166 1.28 java.lang.reflect.Field f = sun.misc
909     .Unsafe.class.getDeclaredField("theUnsafe");
910     f.setAccessible(true);
911     return (sun.misc.Unsafe) f.get(null);
912 jsr166 1.13 }});
913     } catch (java.security.PrivilegedActionException e) {
914 jsr166 1.25 throw new RuntimeException("Could not initialize intrinsics",
915     e.getCause());
916 jsr166 1.13 }
917     }
918     }
919 dl 1.1 }