ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentLinkedDeque.java
Revision: 1.3
Committed: Wed Aug 25 21:40:03 2010 UTC (13 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +327 -222 lines
Log Message:
Finish ConcurrentLinkedDeque feature

File Contents

# User Rev Content
1 jsr166 1.1 /*
2     * Written by Doug Lea and Martin Buchholz with assistance from members of
3     * JCP JSR-166 Expert Group and released to the public domain, as explained
4     * at http://creativecommons.org/licenses/publicdomain
5     */
6    
7     package java.util.concurrent;
8    
9     import java.util.AbstractCollection;
10     import java.util.ArrayList;
11     import java.util.Collection;
12     import java.util.Deque;
13     import java.util.Iterator;
14     import java.util.ConcurrentModificationException;
15     import java.util.NoSuchElementException;
16     import java.util.concurrent.atomic.AtomicReference;
17    
18     /**
19 jsr166 1.3 * An unbounded concurrent {@linkplain Deque deque} based on linked nodes.
20     * Concurrent insertion, removal, and access operations execute safely
21     * across multiple threads.
22     * A {@code ConcurrentLinkedDeque} is an appropriate choice when
23     * many threads will share access to a common collection.
24     * Like most other concurrent collection implementations, this class
25     * does not permit the use of {@code null} elements.
26     *
27     * <p>Iterators are <i>weakly consistent</i>, returning elements
28     * reflecting the state of the deque at some point at or since the
29     * creation of the iterator. They do <em>not</em> throw {@link
30     * java.util.ConcurrentModificationException
31 jsr166 1.1 * ConcurrentModificationException}, and may proceed concurrently with
32     * other operations.
33     *
34 jsr166 1.3 * <p>Beware that, unlike in most collections, the {@code size}
35 jsr166 1.1 * method is <em>NOT</em> a constant-time operation. Because of the
36     * asynchronous nature of these deques, determining the current number
37 jsr166 1.3 * of elements requires a traversal of the elements.
38     *
39     * <p>This class and its iterator implement all of the <em>optional</em>
40     * methods of the {@link Deque} and {@link Iterator} interfaces.
41 jsr166 1.1 *
42 jsr166 1.3 * <p>Memory consistency effects: As with other concurrent collections,
43     * actions in a thread prior to placing an object into a
44     * {@code ConcurrentLinkedDeque}
45     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
46     * actions subsequent to the access or removal of that element from
47     * the {@code ConcurrentLinkedDeque} in another thread.
48 jsr166 1.1 *
49 jsr166 1.3 * <p>This class is a member of the
50     * <a href="{@docRoot}/../technotes/guides/collections/index.html">
51     * Java Collections Framework</a>.
52     *
53     * @since 1.7
54     * @author Doug Lea
55     * @author Martin Buchholz
56 jsr166 1.1 * @param <E> the type of elements held in this collection
57     */
58    
59     public class ConcurrentLinkedDeque<E>
60     extends AbstractCollection<E>
61     implements Deque<E>, java.io.Serializable {
62    
63     /*
64     * This is an implementation of a concurrent lock-free deque
65     * supporting interior removes but not interior insertions, as
66 jsr166 1.3 * required to support the entire Deque interface.
67     *
68     * We extend the techniques developed for ConcurrentLinkedQueue and
69     * LinkedTransferQueue (see the internal docs for those classes).
70     *
71     * The data structure is a symmetrical doubly-linked "GC-robust"
72     * linked list of nodes. We minimize the number of volatile writes
73     * using two techniques: advancing multiple hops with a single CAS
74     * and mixing volatile and non-volatile writes of the same memory
75     * locations.
76     *
77     * A node contains the expected E ("item") and links to predecessor
78     * ("prev") and successor ("next") nodes:
79     *
80     * class Node<E> { volatile Node<E> prev, next; volatile E item; }
81     *
82     * A node p is considered "live" if it contains a non-null item
83     * (p.item != null). When an item is CASed to null, the item is
84     * atomically logically deleted from the collection.
85     *
86     * At any time, there is precisely one "first" node with a null
87     * prev reference that terminates any chain of prev references
88     * starting at a live node. Similarly there is precisely one
89     * "last" node terminating any chain of next references starting at
90     * a live node. The "first" and "last" nodes may or may not be live.
91     * The "first" and "last" nodes are always mutually reachable.
92     *
93     * A new element is added atomically by CASing the null prev or
94     * next reference in the first or last node to a fresh node
95     * containing the element.
96     *
97     * A node is considered "active" if it is a live node, or the
98     * first or last node. Active nodes cannot be unlinked.
99     *
100     * A "self-link" is a next or prev reference that is the same node:
101     * p.prev == p or p.next == p
102     * Self-links are used in the node unlinking process. Active nodes
103     * never have self-links.
104 jsr166 1.1 *
105 jsr166 1.3 * A node p is active if and only if:
106 jsr166 1.1 *
107     * p.item != null ||
108     * (p.prev == null && p.next != p) ||
109     * (p.next == null && p.prev != p)
110     *
111 jsr166 1.3 * The deque object has two node references, "head" and "tail".
112     * The head and tail are only approximations to the first and last
113     * nodes of the deque. The first node can always be found by
114 jsr166 1.1 * following prev pointers from head; likewise for tail. However,
115 jsr166 1.3 * it is permissible for head and tail to be referring to deleted
116     * nodes that have been unlinked and so may not be reachable from
117     * any live node.
118     *
119     * There are 3 stages of node deletion;
120     * "logical deletion", "unlinking", and "gc-unlinking".
121     *
122     * 1. "logical deletion" by CASing item to null atomically removes
123     * the element from the collection, and makes the containing node
124     * eligible for unlinking.
125     *
126     * 2. "unlinking" makes a deleted node unreachable from active
127     * nodes, and thus eventually reclaimable by GC. Unlinked nodes
128     * may remain reachable indefinitely from an iterator.
129     *
130     * Physical node unlinking is merely an optimization (albeit a
131     * critical one), and so can be performed at our convenience. At
132     * any time, the set of live nodes maintained by prev and next
133     * links are identical, that is, the live nodes found via next
134     * links from the first node is equal to the elements found via
135     * prev links from the last node. However, this is not true for
136     * nodes that have already been logically deleted - such nodes may
137     * be reachable in one direction only.
138     *
139     * 3. "gc-unlinking" takes unlinking further by making active
140     * nodes unreachable from deleted nodes, making it easier for the
141     * GC to reclaim future deleted nodes. This step makes the data
142     * structure "gc-robust", as first described in detail by Boehm
143     * (http://portal.acm.org/citation.cfm?doid=503272.503282).
144     *
145     * GC-unlinked nodes may remain reachable indefinitely from an
146     * iterator, but unlike unlinked nodes, are never reachable from
147     * head or tail.
148     *
149     * Making the data structure GC-robust will eliminate the risk of
150     * unbounded memory retention with conservative GCs and is likely
151     * to improve performance with generational GCs.
152     *
153     * When a node is dequeued at either end, e.g. via poll(), we would
154     * like to break any references from the node to active nodes. We
155     * develop further the use of self-links that was very effective in
156     * other concurrent collection classes. The idea is to replace
157     * prev and next pointers with special values that are interpreted
158     * to mean off-the-list-at-one-end. These are approximations, but
159     * good enough to preserve the properties we want in our
160     * traversals, e.g. we guarantee that a traversal will never visit
161     * the same element twice, but we don't guarantee whether a
162     * traversal that runs out of elements will be able to see more
163     * elements later after enqueues at that end. Doing gc-unlinking
164     * safely is particularly tricky, since any node can be in use
165     * indefinitely (for example by an iterator). We must ensure that
166     * the nodes pointed at by head/tail never get gc-unlinked, since
167     * head/tail are needed to get "back on track" by other nodes that
168     * are gc-unlinked. gc-unlinking accounts for much of the
169     * implementation complexity.
170 jsr166 1.1 *
171     * Since neither unlinking nor gc-unlinking are necessary for
172     * correctness, there are many implementation choices regarding
173     * frequency (eagerness) of these operations. Since volatile
174     * reads are likely to be much cheaper than CASes, saving CASes by
175     * unlinking multiple adjacent nodes at a time may be a win.
176     * gc-unlinking can be performed rarely and still be effective,
177     * since it is most important that long chains of deleted nodes
178     * are occasionally broken.
179     *
180     * The actual representation we use is that p.next == p means to
181 jsr166 1.3 * goto the first node (which in turn is reached by following prev
182     * pointers from head), and p.next == null && p.prev == p means
183 jsr166 1.1 * that the iteration is at an end and that p is a (final static)
184     * dummy node, NEXT_TERMINATOR, and not the last active node.
185     * Finishing the iteration when encountering such a TERMINATOR is
186 jsr166 1.3 * good enough for read-only traversals, so such traversals can use
187     * p.next == null as the termination condition. When we need to
188     * find the last (active) node, for enqueueing a new node, we need
189     * to check whether we have reached a TERMINATOR node; if so,
190     * restart traversal from tail.
191 jsr166 1.1 *
192     * The implementation is completely directionally symmetrical,
193     * except that most public methods that iterate through the list
194     * follow next pointers ("forward" direction).
195     *
196     * There is one desirable property we would like to have, but
197     * don't: it is possible, when an addFirst(A) is racing with
198     * pollFirst() removing B, for an iterating observer to see A B C
199     * and subsequently see A C, even though no interior removes are
200     * ever performed. I believe this wart can only be removed at
201     * significant runtime cost.
202     *
203     * Empirically, microbenchmarks suggest that this class adds about
204     * 40% overhead relative to ConcurrentLinkedQueue, which feels as
205     * good as we can hope for.
206     */
207    
208 jsr166 1.3 private static final long serialVersionUID = 876323262645176354L;
209    
210 jsr166 1.1 /**
211 jsr166 1.3 * A node from which the first node on list (that is, the unique node p
212     * with p.prev == null && p.next != p) can be reached in O(1) time.
213 jsr166 1.1 * Invariants:
214     * - the first node is always O(1) reachable from head via prev links
215     * - all live nodes are reachable from the first node via succ()
216     * - head != null
217     * - (tmp = head).next != tmp || tmp != head
218 jsr166 1.3 * - head is never gc-unlinked (but may be unlinked)
219 jsr166 1.1 * Non-invariants:
220     * - head.item may or may not be null
221     * - head may not be reachable from the first or last node, or from tail
222     */
223 jsr166 1.3 private transient volatile Node<E> head;
224    
225     /**
226     * A node from which the last node on list (that is, the unique node p
227     * with p.next == null && p.prev != p) can be reached in O(1) time.
228     * Invariants:
229     * - the last node is always O(1) reachable from tail via next links
230     * - all live nodes are reachable from the last node via pred()
231     * - tail != null
232     * - tail is never gc-unlinked (but may be unlinked)
233     * Non-invariants:
234     * - tail.item may or may not be null
235     * - tail may not be reachable from the first or last node, or from head
236     */
237     private transient volatile Node<E> tail;
238 jsr166 1.1
239     private final static Node<Object> PREV_TERMINATOR, NEXT_TERMINATOR;
240    
241     static {
242     PREV_TERMINATOR = new Node<Object>(null);
243     PREV_TERMINATOR.next = PREV_TERMINATOR;
244     NEXT_TERMINATOR = new Node<Object>(null);
245     NEXT_TERMINATOR.prev = NEXT_TERMINATOR;
246     }
247    
248     @SuppressWarnings("unchecked")
249     Node<E> prevTerminator() {
250     return (Node<E>) PREV_TERMINATOR;
251     }
252    
253     @SuppressWarnings("unchecked")
254     Node<E> nextTerminator() {
255     return (Node<E>) NEXT_TERMINATOR;
256     }
257    
258     static final class Node<E> {
259     volatile Node<E> prev;
260     volatile E item;
261     volatile Node<E> next;
262    
263     Node(E item) {
264     // Piggyback on imminent casNext() or casPrev()
265     lazySetItem(item);
266     }
267    
268     boolean casItem(E cmp, E val) {
269     return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val);
270     }
271    
272     void lazySetItem(E val) {
273     UNSAFE.putOrderedObject(this, itemOffset, val);
274     }
275    
276     void lazySetNext(Node<E> val) {
277     UNSAFE.putOrderedObject(this, nextOffset, val);
278     }
279    
280     boolean casNext(Node<E> cmp, Node<E> val) {
281     return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
282     }
283    
284     void lazySetPrev(Node<E> val) {
285     UNSAFE.putOrderedObject(this, prevOffset, val);
286     }
287    
288     boolean casPrev(Node<E> cmp, Node<E> val) {
289     return UNSAFE.compareAndSwapObject(this, prevOffset, cmp, val);
290     }
291    
292     // Unsafe mechanics
293    
294     private static final sun.misc.Unsafe UNSAFE =
295     sun.misc.Unsafe.getUnsafe();
296     private static final long prevOffset =
297     objectFieldOffset(UNSAFE, "prev", Node.class);
298     private static final long itemOffset =
299     objectFieldOffset(UNSAFE, "item", Node.class);
300     private static final long nextOffset =
301     objectFieldOffset(UNSAFE, "next", Node.class);
302     }
303    
304     /**
305     * Links e as first element.
306     */
307     private void linkFirst(E e) {
308     checkNotNull(e);
309     final Node<E> newNode = new Node<E>(e);
310    
311     retry:
312     for (;;) {
313     for (Node<E> h = head, p = h;;) {
314     Node<E> q = p.prev;
315     if (q == null) {
316 jsr166 1.3 if (p.next == p) // PREV_TERMINATOR
317 jsr166 1.1 continue retry;
318 jsr166 1.3 // p is first node
319 jsr166 1.1 newNode.lazySetNext(p); // CAS piggyback
320     if (p.casPrev(null, newNode)) {
321     if (p != h) // hop two nodes at a time
322     casHead(h, newNode);
323     return;
324     } else {
325     p = p.prev; // lost CAS race to another thread
326     }
327     }
328     else if (p == q)
329     continue retry;
330     else
331     p = q;
332     }
333     }
334     }
335    
336     /**
337     * Links e as last element.
338     */
339     private void linkLast(E e) {
340     checkNotNull(e);
341     final Node<E> newNode = new Node<E>(e);
342    
343     retry:
344     for (;;) {
345     for (Node<E> t = tail, p = t;;) {
346     Node<E> q = p.next;
347     if (q == null) {
348 jsr166 1.3 if (p.prev == p) // NEXT_TERMINATOR
349 jsr166 1.1 continue retry;
350 jsr166 1.3 // p is last node
351 jsr166 1.1 newNode.lazySetPrev(p); // CAS piggyback
352     if (p.casNext(null, newNode)) {
353     if (p != t) // hop two nodes at a time
354     casTail(t, newNode);
355     return;
356     } else {
357     p = p.next; // lost CAS race to another thread
358     }
359     }
360     else if (p == q)
361     continue retry;
362     else
363     p = q;
364     }
365     }
366     }
367    
368     private final static int HOPS = 2;
369    
370     /**
371     * Unlinks non-null node x.
372     */
373     void unlink(Node<E> x) {
374 jsr166 1.3 // assert x != null;
375     // assert x.item == null;
376     // assert x != PREV_TERMINATOR;
377     // assert x != NEXT_TERMINATOR;
378 jsr166 1.1
379     final Node<E> prev = x.prev;
380     final Node<E> next = x.next;
381     if (prev == null) {
382     unlinkFirst(x, next);
383     } else if (next == null) {
384     unlinkLast(x, prev);
385     } else {
386     // Unlink interior node.
387     //
388     // This is the common case, since a series of polls at the
389     // same end will be "interior" removes, except perhaps for
390 jsr166 1.3 // the first one, since end nodes cannot be unlinked.
391 jsr166 1.1 //
392     // At any time, all active nodes are mutually reachable by
393     // following a sequence of either next or prev pointers.
394     //
395     // Our strategy is to find the unique active predecessor
396     // and successor of x. Try to fix up their links so that
397     // they point to each other, leaving x unreachable from
398     // active nodes. If successful, and if x has no live
399 jsr166 1.3 // predecessor/successor, we additionally try to gc-unlink,
400     // leaving active nodes unreachable from x, by rechecking
401     // that the status of predecessor and successor are
402     // unchanged and ensuring that x is not reachable from
403     // tail/head, before setting x's prev/next links to their
404     // logical approximate replacements, self/TERMINATOR.
405 jsr166 1.1 Node<E> activePred, activeSucc;
406     boolean isFirst, isLast;
407     int hops = 1;
408    
409     // Find active predecessor
410 jsr166 1.3 for (Node<E> p = prev; ; ++hops) {
411 jsr166 1.1 if (p.item != null) {
412     activePred = p;
413     isFirst = false;
414     break;
415     }
416     Node<E> q = p.prev;
417     if (q == null) {
418 jsr166 1.3 if (p.next == p)
419 jsr166 1.1 return;
420     activePred = p;
421     isFirst = true;
422     break;
423     }
424     else if (p == q)
425     return;
426     else
427     p = q;
428     }
429    
430     // Find active successor
431 jsr166 1.3 for (Node<E> p = next; ; ++hops) {
432 jsr166 1.1 if (p.item != null) {
433     activeSucc = p;
434     isLast = false;
435     break;
436     }
437     Node<E> q = p.next;
438     if (q == null) {
439 jsr166 1.3 if (p.prev == p)
440 jsr166 1.1 return;
441     activeSucc = p;
442     isLast = true;
443     break;
444     }
445     else if (p == q)
446     return;
447     else
448     p = q;
449     }
450    
451     // TODO: better HOP heuristics
452     if (hops < HOPS
453     // always squeeze out interior deleted nodes
454     && (isFirst | isLast))
455     return;
456    
457     // Squeeze out deleted nodes between activePred and
458     // activeSucc, including x.
459     skipDeletedSuccessors(activePred);
460     skipDeletedPredecessors(activeSucc);
461    
462     // Try to gc-unlink, if possible
463     if ((isFirst | isLast) &&
464    
465     // Recheck expected state of predecessor and successor
466     (activePred.next == activeSucc) &&
467     (activeSucc.prev == activePred) &&
468     (isFirst ? activePred.prev == null : activePred.item != null) &&
469     (isLast ? activeSucc.next == null : activeSucc.item != null)) {
470    
471     // Ensure x is not reachable from head or tail
472     updateHead();
473     updateTail();
474 jsr166 1.3
475     // Finally, actually gc-unlink
476 jsr166 1.1 x.lazySetPrev(isFirst ? prevTerminator() : x);
477     x.lazySetNext(isLast ? nextTerminator() : x);
478     }
479     }
480     }
481    
482     /**
483     * Unlinks non-null first node.
484     */
485     private void unlinkFirst(Node<E> first, Node<E> next) {
486 jsr166 1.3 // assert first != null && next != null && first.item == null;
487 jsr166 1.1 Node<E> o = null, p = next;
488 jsr166 1.3 for (int hops = 0; ; ++hops) {
489 jsr166 1.1 Node<E> q;
490     if (p.item != null || (q = p.next) == null) {
491 jsr166 1.3 if (hops >= HOPS && p.prev != p && first.casNext(next, p)) {
492     skipDeletedPredecessors(p);
493     if (first.prev == null &&
494     (p.next == null || p.item != null) &&
495     p.prev == first) {
496    
497     updateHead();
498     updateTail();
499     o.lazySetNext(o);
500     o.lazySetPrev(prevTerminator());
501 jsr166 1.1 }
502     }
503     return;
504     }
505     else if (p == q)
506     return;
507     else {
508     o = p;
509     p = q;
510     }
511     }
512     }
513    
514     /**
515     * Unlinks non-null last node.
516     */
517     private void unlinkLast(Node<E> last, Node<E> prev) {
518 jsr166 1.3 // assert last != null && prev != null && last.item == null;
519 jsr166 1.1 Node<E> o = null, p = prev;
520 jsr166 1.3 for (int hops = 0; ; ++hops) {
521 jsr166 1.1 Node<E> q;
522     if (p.item != null || (q = p.prev) == null) {
523 jsr166 1.3 if (hops >= HOPS && p.next != p && last.casPrev(prev, p)) {
524     skipDeletedSuccessors(p);
525     if (last.next == null &&
526     (p.prev == null || p.item != null) &&
527     p.next == last) {
528    
529     updateHead();
530     updateTail();
531     o.lazySetPrev(o);
532     o.lazySetNext(nextTerminator());
533 jsr166 1.1 }
534     }
535     return;
536     }
537     else if (p == q)
538     return;
539     else {
540     o = p;
541     p = q;
542     }
543     }
544     }
545    
546 jsr166 1.3 /**
547     * Sets head to first node. Guarantees that any node which was
548     * unlinked before a call to this method will be unreachable from
549     * head after it returns.
550     */
551 jsr166 1.1 private final void updateHead() {
552     first();
553     }
554    
555 jsr166 1.3 /**
556     * Sets tail to last node. Guarantees that any node which was
557     * unlinked before a call to this method will be unreachable from
558     * tail after it returns.
559     */
560 jsr166 1.1 private final void updateTail() {
561     last();
562     }
563    
564     private void skipDeletedPredecessors(Node<E> x) {
565     whileActive:
566     do {
567     Node<E> prev = x.prev;
568 jsr166 1.3 // assert prev != null;
569     // assert x != NEXT_TERMINATOR;
570     // assert x != PREV_TERMINATOR;
571 jsr166 1.1 Node<E> p = prev;
572     findActive:
573     for (;;) {
574     if (p.item != null)
575     break findActive;
576     Node<E> q = p.prev;
577     if (q == null) {
578     if (p.next == p)
579     continue whileActive;
580     break findActive;
581     }
582     else if (p == q)
583     continue whileActive;
584     else
585     p = q;
586     }
587    
588     // found active CAS target
589     if (prev == p || x.casPrev(prev, p))
590     return;
591    
592     } while (x.item != null || x.next == null);
593     }
594    
595     private void skipDeletedSuccessors(Node<E> x) {
596     whileActive:
597     do {
598     Node<E> next = x.next;
599 jsr166 1.3 // assert next != null;
600     // assert x != NEXT_TERMINATOR;
601     // assert x != PREV_TERMINATOR;
602 jsr166 1.1 Node<E> p = next;
603     findActive:
604     for (;;) {
605     if (p.item != null)
606     break findActive;
607     Node<E> q = p.next;
608     if (q == null) {
609     if (p.prev == p)
610     continue whileActive;
611     break findActive;
612     }
613     else if (p == q)
614     continue whileActive;
615     else
616     p = q;
617     }
618    
619     // found active CAS target
620     if (next == p || x.casNext(next, p))
621     return;
622    
623     } while (x.item != null || x.prev == null);
624     }
625    
626     /**
627     * Returns the successor of p, or the first node if p.next has been
628     * linked to self, which will only be true if traversing with a
629     * stale pointer that is now off the list.
630     */
631     final Node<E> succ(Node<E> p) {
632     // TODO: should we skip deleted nodes here?
633     Node<E> q = p.next;
634     return (p == q) ? first() : q;
635     }
636    
637     /**
638     * Returns the predecessor of p, or the last node if p.prev has been
639     * linked to self, which will only be true if traversing with a
640     * stale pointer that is now off the list.
641     */
642     final Node<E> pred(Node<E> p) {
643     Node<E> q = p.prev;
644     return (p == q) ? last() : q;
645     }
646    
647     /**
648 jsr166 1.3 * Returns the first node, the unique node p for which:
649     * p.prev == null && p.next != p
650 jsr166 1.1 * The returned node may or may not be logically deleted.
651     * Guarantees that head is set to the returned node.
652     */
653     Node<E> first() {
654     retry:
655     for (;;) {
656     for (Node<E> h = head, p = h;;) {
657     Node<E> q = p.prev;
658     if (q == null) {
659     if (p == h
660     // It is possible that p is PREV_TERMINATOR,
661 jsr166 1.3 // but if so, the CAS is guaranteed to fail.
662 jsr166 1.1 || casHead(h, p))
663     return p;
664     else
665     continue retry;
666     } else if (p == q) {
667     continue retry;
668     } else {
669     p = q;
670     }
671     }
672     }
673     }
674    
675     /**
676 jsr166 1.3 * Returns the last node, the unique node p for which:
677     * p.next == null && p.prev != p
678 jsr166 1.1 * The returned node may or may not be logically deleted.
679     * Guarantees that tail is set to the returned node.
680     */
681     Node<E> last() {
682     retry:
683     for (;;) {
684     for (Node<E> t = tail, p = t;;) {
685     Node<E> q = p.next;
686     if (q == null) {
687     if (p == t
688     // It is possible that p is NEXT_TERMINATOR,
689 jsr166 1.3 // but if so, the CAS is guaranteed to fail.
690 jsr166 1.1 || casTail(t, p))
691     return p;
692     else
693     continue retry;
694     } else if (p == q) {
695     continue retry;
696     } else {
697     p = q;
698     }
699     }
700     }
701     }
702    
703     // Minor convenience utilities
704    
705     /**
706     * Throws NullPointerException if argument is null.
707     *
708     * @param v the element
709     */
710     private static void checkNotNull(Object v) {
711     if (v == null)
712     throw new NullPointerException();
713     }
714    
715     /**
716     * Returns element unless it is null, in which case throws
717     * NoSuchElementException.
718     *
719     * @param v the element
720     * @return the element
721     */
722     private E screenNullResult(E v) {
723     if (v == null)
724     throw new NoSuchElementException();
725     return v;
726     }
727    
728     /**
729     * Creates an array list and fills it with elements of this list.
730     * Used by toArray.
731     *
732     * @return the arrayList
733     */
734     private ArrayList<E> toArrayList() {
735 jsr166 1.3 ArrayList<E> list = new ArrayList<E>();
736 jsr166 1.1 for (Node<E> p = first(); p != null; p = succ(p)) {
737     E item = p.item;
738     if (item != null)
739 jsr166 1.3 list.add(item);
740 jsr166 1.1 }
741 jsr166 1.3 return list;
742 jsr166 1.1 }
743    
744     /**
745     * Constructs an empty deque.
746     */
747 jsr166 1.3 public ConcurrentLinkedDeque() {
748     head = tail = new Node<E>(null);
749     }
750 jsr166 1.1
751     /**
752     * Constructs a deque initially containing the elements of
753     * the given collection, added in traversal order of the
754     * collection's iterator.
755     *
756     * @param c the collection of elements to initially contain
757     * @throws NullPointerException if the specified collection or any
758     * of its elements are null
759     */
760 jsr166 1.3 public ConcurrentLinkedDeque(Collection<? extends E> c) {
761     // Copy c into a private chain of Nodes
762     Node<E> h = null, t = null;
763     for (E e : c) {
764     checkNotNull(e);
765     Node<E> newNode = new Node<E>(e);
766     if (h == null)
767     h = t = newNode;
768     else {
769     t.next = newNode;
770     newNode.prev = t;
771     t = newNode;
772     }
773     }
774     if (h == null)
775     h = t = new Node<E>(null);
776     head = h;
777     tail = t;
778     }
779 jsr166 1.1
780     /**
781     * Inserts the specified element at the front of this deque.
782     *
783     * @throws NullPointerException {@inheritDoc}
784     */
785     public void addFirst(E e) {
786     linkFirst(e);
787     }
788    
789     /**
790     * Inserts the specified element at the end of this deque.
791 jsr166 1.3 *
792     * <p>This method is equivalent to {@link #add}.
793 jsr166 1.1 *
794     * @throws NullPointerException {@inheritDoc}
795     */
796     public void addLast(E e) {
797     linkLast(e);
798     }
799    
800     /**
801     * Inserts the specified element at the front of this deque.
802     *
803     * @return {@code true} always
804     * @throws NullPointerException {@inheritDoc}
805     */
806     public boolean offerFirst(E e) {
807     linkFirst(e);
808     return true;
809     }
810    
811     /**
812     * Inserts the specified element at the end of this deque.
813     *
814     * <p>This method is equivalent to {@link #add}.
815     *
816     * @return {@code true} always
817     * @throws NullPointerException {@inheritDoc}
818     */
819     public boolean offerLast(E e) {
820     linkLast(e);
821     return true;
822     }
823    
824     public E peekFirst() {
825     for (Node<E> p = first(); p != null; p = succ(p)) {
826     E item = p.item;
827     if (item != null)
828     return item;
829     }
830     return null;
831     }
832    
833     public E peekLast() {
834     for (Node<E> p = last(); p != null; p = pred(p)) {
835     E item = p.item;
836     if (item != null)
837     return item;
838     }
839     return null;
840     }
841    
842     /**
843     * @throws NoSuchElementException {@inheritDoc}
844     */
845     public E getFirst() {
846     return screenNullResult(peekFirst());
847     }
848    
849     /**
850     * @throws NoSuchElementException {@inheritDoc}
851     */
852     public E getLast() {
853     return screenNullResult(peekLast());
854     }
855    
856     public E pollFirst() {
857     for (Node<E> p = first(); p != null; p = succ(p)) {
858     E item = p.item;
859     if (item != null && p.casItem(item, null)) {
860     unlink(p);
861     return item;
862     }
863     }
864     return null;
865     }
866    
867     public E pollLast() {
868     for (Node<E> p = last(); p != null; p = pred(p)) {
869     E item = p.item;
870     if (item != null && p.casItem(item, null)) {
871     unlink(p);
872     return item;
873     }
874     }
875     return null;
876     }
877    
878     /**
879     * @throws NoSuchElementException {@inheritDoc}
880     */
881     public E removeFirst() {
882     return screenNullResult(pollFirst());
883     }
884    
885     /**
886     * @throws NoSuchElementException {@inheritDoc}
887     */
888     public E removeLast() {
889     return screenNullResult(pollLast());
890     }
891    
892     // *** Queue and stack methods ***
893    
894     /**
895     * Inserts the specified element at the tail of this deque.
896     *
897     * @return {@code true} (as specified by {@link Queue#offer})
898     * @throws NullPointerException if the specified element is null
899     */
900     public boolean offer(E e) {
901     return offerLast(e);
902     }
903    
904     /**
905     * Inserts the specified element at the tail of this deque.
906     *
907     * @return {@code true} (as specified by {@link Collection#add})
908     * @throws NullPointerException if the specified element is null
909     */
910     public boolean add(E e) {
911     return offerLast(e);
912     }
913    
914     public E poll() { return pollFirst(); }
915     public E remove() { return removeFirst(); }
916     public E peek() { return peekFirst(); }
917     public E element() { return getFirst(); }
918     public void push(E e) { addFirst(e); }
919     public E pop() { return removeFirst(); }
920    
921     /**
922     * Removes the first element {@code e} such that
923     * {@code o.equals(e)}, if such an element exists in this deque.
924     * If the deque does not contain the element, it is unchanged.
925     *
926     * @param o element to be removed from this deque, if present
927     * @return {@code true} if the deque contained the specified element
928     * @throws NullPointerException if the specified element is {@code null}
929     */
930     public boolean removeFirstOccurrence(Object o) {
931     checkNotNull(o);
932     for (Node<E> p = first(); p != null; p = succ(p)) {
933     E item = p.item;
934     if (item != null && o.equals(item) && p.casItem(item, null)) {
935     unlink(p);
936     return true;
937     }
938     }
939     return false;
940     }
941    
942     /**
943     * Removes the last element {@code e} such that
944     * {@code o.equals(e)}, if such an element exists in this deque.
945     * If the deque does not contain the element, it is unchanged.
946     *
947     * @param o element to be removed from this deque, if present
948     * @return {@code true} if the deque contained the specified element
949     * @throws NullPointerException if the specified element is {@code null}
950     */
951     public boolean removeLastOccurrence(Object o) {
952     checkNotNull(o);
953     for (Node<E> p = last(); p != null; p = pred(p)) {
954     E item = p.item;
955     if (item != null && o.equals(item) && p.casItem(item, null)) {
956     unlink(p);
957     return true;
958     }
959     }
960     return false;
961     }
962    
963     /**
964     * Returns {@code true} if this deque contains at least one
965     * element {@code e} such that {@code o.equals(e)}.
966     *
967     * @param o element whose presence in this deque is to be tested
968     * @return {@code true} if this deque contains the specified element
969     */
970     public boolean contains(Object o) {
971     if (o == null) return false;
972     for (Node<E> p = first(); p != null; p = succ(p)) {
973     E item = p.item;
974     if (item != null && o.equals(item))
975     return true;
976     }
977     return false;
978     }
979    
980     /**
981     * Returns {@code true} if this collection contains no elements.
982     *
983     * @return {@code true} if this collection contains no elements
984     */
985     public boolean isEmpty() {
986     return peekFirst() == null;
987     }
988    
989     /**
990     * Returns the number of elements in this deque. If this deque
991     * contains more than {@code Integer.MAX_VALUE} elements, it
992     * returns {@code Integer.MAX_VALUE}.
993     *
994     * <p>Beware that, unlike in most collections, this method is
995     * <em>NOT</em> a constant-time operation. Because of the
996     * asynchronous nature of these deques, determining the current
997     * number of elements requires traversing them all to count them.
998     * Additionally, it is possible for the size to change during
999     * execution of this method, in which case the returned result
1000     * will be inaccurate. Thus, this method is typically not very
1001     * useful in concurrent applications.
1002     *
1003     * @return the number of elements in this deque
1004     */
1005     public int size() {
1006     long count = 0;
1007     for (Node<E> p = first(); p != null; p = succ(p))
1008     if (p.item != null)
1009     ++count;
1010     return (count >= Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) count;
1011     }
1012    
1013     /**
1014     * Removes the first element {@code e} such that
1015     * {@code o.equals(e)}, if such an element exists in this deque.
1016     * If the deque does not contain the element, it is unchanged.
1017     *
1018     * @param o element to be removed from this deque, if present
1019     * @return {@code true} if the deque contained the specified element
1020     * @throws NullPointerException if the specified element is {@code null}
1021     */
1022     public boolean remove(Object o) {
1023     return removeFirstOccurrence(o);
1024     }
1025    
1026     /**
1027     * Appends all of the elements in the specified collection to the end of
1028     * this deque, in the order that they are returned by the specified
1029 jsr166 1.3 * collection's iterator. Attempts to {@code addAll} of a deque to
1030     * itself result in {@code IllegalArgumentException}.
1031 jsr166 1.1 *
1032     * @param c the elements to be inserted into this deque
1033     * @return {@code true} if this deque changed as a result of the call
1034 jsr166 1.3 * @throws NullPointerException if the specified collection or any
1035     * of its elements are null
1036     * @throws IllegalArgumentException if the collection is this deque
1037 jsr166 1.1 */
1038     public boolean addAll(Collection<? extends E> c) {
1039 jsr166 1.3 if (c == this)
1040     // As historically specified in AbstractQueue#addAll
1041     throw new IllegalArgumentException();
1042    
1043     // Copy c into a private chain of Nodes
1044     Node<E> splice = null, last = null;
1045     for (E e : c) {
1046     checkNotNull(e);
1047     Node<E> newNode = new Node<E>(e);
1048     if (splice == null)
1049     splice = last = newNode;
1050     else {
1051     last.next = newNode;
1052     newNode.prev = last;
1053     last = newNode;
1054     }
1055     }
1056     if (splice == null)
1057 jsr166 1.1 return false;
1058 jsr166 1.3
1059     // Atomically splice the chain as the tail of this collection
1060     retry:
1061     for (;;) {
1062     for (Node<E> t = tail, p = t;;) {
1063     Node<E> q = p.next;
1064     if (q == null) {
1065     if (p.prev == p) // NEXT_TERMINATOR
1066     continue retry;
1067     // p is last node
1068     splice.lazySetPrev(p); // CAS piggyback
1069     if (p.casNext(null, splice)) {
1070     if (! casTail(t, last)) {
1071     // Try a little harder to update tail,
1072     // since we may be adding many elements.
1073     t = tail;
1074     if (last.next == null)
1075     casTail(t, last);
1076     }
1077     return true;
1078     } else {
1079     p = p.next; // lost CAS race to another thread
1080     }
1081     }
1082     else if (p == q)
1083     continue retry;
1084     else
1085     p = q;
1086     }
1087     }
1088 jsr166 1.1 }
1089    
1090     /**
1091     * Removes all of the elements from this deque.
1092     */
1093     public void clear() {
1094     while (pollFirst() != null)
1095     ;
1096     }
1097    
1098     /**
1099     * Returns an array containing all of the elements in this deque, in
1100     * proper sequence (from first to last element).
1101     *
1102     * <p>The returned array will be "safe" in that no references to it are
1103     * maintained by this deque. (In other words, this method must allocate
1104     * a new array). The caller is thus free to modify the returned array.
1105     *
1106     * <p>This method acts as bridge between array-based and collection-based
1107     * APIs.
1108     *
1109     * @return an array containing all of the elements in this deque
1110     */
1111     public Object[] toArray() {
1112     return toArrayList().toArray();
1113     }
1114    
1115     /**
1116     * Returns an array containing all of the elements in this deque,
1117     * in proper sequence (from first to last element); the runtime
1118     * type of the returned array is that of the specified array. If
1119     * the deque fits in the specified array, it is returned therein.
1120     * Otherwise, a new array is allocated with the runtime type of
1121     * the specified array and the size of this deque.
1122     *
1123     * <p>If this deque fits in the specified array with room to spare
1124     * (i.e., the array has more elements than this deque), the element in
1125     * the array immediately following the end of the deque is set to
1126     * {@code null}.
1127     *
1128 jsr166 1.3 * <p>Like the {@link #toArray()} method, this method acts as
1129     * bridge between array-based and collection-based APIs. Further,
1130     * this method allows precise control over the runtime type of the
1131     * output array, and may, under certain circumstances, be used to
1132     * save allocation costs.
1133 jsr166 1.1 *
1134     * <p>Suppose {@code x} is a deque known to contain only strings.
1135     * The following code can be used to dump the deque into a newly
1136     * allocated array of {@code String}:
1137     *
1138     * <pre>
1139     * String[] y = x.toArray(new String[0]);</pre>
1140     *
1141     * Note that {@code toArray(new Object[0])} is identical in function to
1142     * {@code toArray()}.
1143     *
1144     * @param a the array into which the elements of the deque are to
1145     * be stored, if it is big enough; otherwise, a new array of the
1146     * same runtime type is allocated for this purpose
1147     * @return an array containing all of the elements in this deque
1148     * @throws ArrayStoreException if the runtime type of the specified array
1149     * is not a supertype of the runtime type of every element in
1150     * this deque
1151     * @throws NullPointerException if the specified array is null
1152     */
1153     public <T> T[] toArray(T[] a) {
1154     return toArrayList().toArray(a);
1155     }
1156    
1157     /**
1158     * Returns an iterator over the elements in this deque in proper sequence.
1159     * The elements will be returned in order from first (head) to last (tail).
1160     *
1161     * <p>The returned {@code Iterator} is a "weakly consistent" iterator that
1162     * will never throw {@link java.util.ConcurrentModificationException
1163     * ConcurrentModificationException},
1164     * and guarantees to traverse elements as they existed upon
1165     * construction of the iterator, and may (but is not guaranteed to)
1166     * reflect any modifications subsequent to construction.
1167     *
1168     * @return an iterator over the elements in this deque in proper sequence
1169     */
1170     public Iterator<E> iterator() {
1171     return new Itr();
1172     }
1173    
1174     /**
1175     * Returns an iterator over the elements in this deque in reverse
1176     * sequential order. The elements will be returned in order from
1177     * last (tail) to first (head).
1178     *
1179     * <p>The returned {@code Iterator} is a "weakly consistent" iterator that
1180     * will never throw {@link java.util.ConcurrentModificationException
1181     * ConcurrentModificationException},
1182     * and guarantees to traverse elements as they existed upon
1183     * construction of the iterator, and may (but is not guaranteed to)
1184     * reflect any modifications subsequent to construction.
1185 jsr166 1.3 *
1186     * @return an iterator over the elements in this deque in reverse order
1187 jsr166 1.1 */
1188     public Iterator<E> descendingIterator() {
1189     return new DescendingItr();
1190     }
1191    
1192     private abstract class AbstractItr implements Iterator<E> {
1193     /**
1194     * Next node to return item for.
1195     */
1196     private Node<E> nextNode;
1197    
1198     /**
1199     * nextItem holds on to item fields because once we claim
1200     * that an element exists in hasNext(), we must return it in
1201     * the following next() call even if it was in the process of
1202     * being removed when hasNext() was called.
1203     */
1204     private E nextItem;
1205    
1206     /**
1207     * Node returned by most recent call to next. Needed by remove.
1208     * Reset to null if this element is deleted by a call to remove.
1209     */
1210     private Node<E> lastRet;
1211    
1212     abstract Node<E> startNode();
1213     abstract Node<E> nextNode(Node<E> p);
1214    
1215     AbstractItr() {
1216     advance();
1217     }
1218    
1219     /**
1220     * Sets nextNode and nextItem to next valid node, or to null
1221     * if no such.
1222     */
1223     private void advance() {
1224     lastRet = nextNode;
1225    
1226     Node<E> p = (nextNode == null) ? startNode() : nextNode(nextNode);
1227     for (;; p = nextNode(p)) {
1228     if (p == null) {
1229     // p might be active end or TERMINATOR node; both are OK
1230     nextNode = null;
1231     nextItem = null;
1232     break;
1233     }
1234     E item = p.item;
1235     if (item != null) {
1236     nextNode = p;
1237     nextItem = item;
1238     break;
1239     }
1240     }
1241     }
1242    
1243     public boolean hasNext() {
1244     return nextItem != null;
1245     }
1246    
1247     public E next() {
1248     E item = nextItem;
1249     if (item == null) throw new NoSuchElementException();
1250     advance();
1251     return item;
1252     }
1253    
1254     public void remove() {
1255     Node<E> l = lastRet;
1256     if (l == null) throw new IllegalStateException();
1257     l.item = null;
1258     unlink(l);
1259     lastRet = null;
1260     }
1261     }
1262    
1263     /** Forward iterator */
1264     private class Itr extends AbstractItr {
1265     Node<E> startNode() { return first(); }
1266     Node<E> nextNode(Node<E> p) { return succ(p); }
1267     }
1268    
1269     /** Descending iterator */
1270     private class DescendingItr extends AbstractItr {
1271     Node<E> startNode() { return last(); }
1272     Node<E> nextNode(Node<E> p) { return pred(p); }
1273     }
1274    
1275     /**
1276 jsr166 1.3 * Saves the state to a stream (that is, serializes it).
1277 jsr166 1.1 *
1278     * @serialData All of the elements (each an {@code E}) in
1279     * the proper order, followed by a null
1280     * @param s the stream
1281     */
1282     private void writeObject(java.io.ObjectOutputStream s)
1283     throws java.io.IOException {
1284    
1285     // Write out any hidden stuff
1286     s.defaultWriteObject();
1287    
1288     // Write out all elements in the proper order.
1289     for (Node<E> p = first(); p != null; p = succ(p)) {
1290     Object item = p.item;
1291     if (item != null)
1292     s.writeObject(item);
1293     }
1294    
1295     // Use trailing null as sentinel
1296     s.writeObject(null);
1297     }
1298    
1299     /**
1300 jsr166 1.3 * Reconstitutes the instance from a stream (that is, deserializes it).
1301 jsr166 1.1 * @param s the stream
1302     */
1303     private void readObject(java.io.ObjectInputStream s)
1304     throws java.io.IOException, ClassNotFoundException {
1305     s.defaultReadObject();
1306 jsr166 1.3
1307     // Read in elements until trailing null sentinel found
1308     Node<E> h = null, t = null;
1309     Object item;
1310     while ((item = s.readObject()) != null) {
1311 jsr166 1.1 @SuppressWarnings("unchecked")
1312 jsr166 1.3 Node<E> newNode = new Node<E>((E) item);
1313     if (h == null)
1314     h = t = newNode;
1315     else {
1316     t.next = newNode;
1317     newNode.prev = t;
1318     t = newNode;
1319     }
1320 jsr166 1.1 }
1321 jsr166 1.3 if (h == null)
1322     h = t = new Node<E>(null);
1323     head = h;
1324     tail = t;
1325 jsr166 1.1 }
1326    
1327     // Unsafe mechanics
1328    
1329     private static final sun.misc.Unsafe UNSAFE =
1330     sun.misc.Unsafe.getUnsafe();
1331     private static final long headOffset =
1332     objectFieldOffset(UNSAFE, "head", ConcurrentLinkedDeque.class);
1333     private static final long tailOffset =
1334     objectFieldOffset(UNSAFE, "tail", ConcurrentLinkedDeque.class);
1335    
1336     private boolean casHead(Node<E> cmp, Node<E> val) {
1337     return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val);
1338     }
1339    
1340     private boolean casTail(Node<E> cmp, Node<E> val) {
1341     return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val);
1342     }
1343    
1344     static long objectFieldOffset(sun.misc.Unsafe UNSAFE,
1345     String field, Class<?> klazz) {
1346     try {
1347     return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
1348     } catch (NoSuchFieldException e) {
1349     // Convert Exception to corresponding Error
1350     NoSuchFieldError error = new NoSuchFieldError(field);
1351     error.initCause(e);
1352     throw error;
1353     }
1354     }
1355     }