ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentLinkedDeque.java
Revision: 1.10
Committed: Mon Sep 13 01:48:29 2010 UTC (13 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +2 -2 lines
Log Message:
very small clarifications

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