ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentLinkedDeque.java
Revision: 1.5
Committed: Sat Sep 11 03:53:44 2010 UTC (13 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +21 -13 lines
Log Message:
internal doc improvements

File Contents

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