ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentLinkedDeque.java
Revision: 1.21
Committed: Sat Oct 16 16:48:01 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +1 -1 lines
Log Message:
whitespace

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.ConcurrentModificationException;
13 import java.util.Deque;
14 import java.util.Iterator;
15 import java.util.NoSuchElementException;
16 import java.util.Queue;
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. The element's node atomically becomes
98 * "live" at that point.
99 *
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 *
108 * A node p is active if and only if:
109 *
110 * p.item != null ||
111 * (p.prev == null && p.next != p) ||
112 * (p.next == null && p.prev != p)
113 *
114 * 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 * following prev pointers from head; likewise for tail. However,
118 * 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 *
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 * 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 * that the iteration is at an end and that p is a (static final)
187 * dummy node, NEXT_TERMINATOR, and not the last active node.
188 * Finishing the iteration when encountering such a TERMINATOR is
189 * 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 *
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 * 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 *
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 private static final long serialVersionUID = 876323262645176354L;
215
216 /**
217 * 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 * 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 * - head is never gc-unlinked (but may be unlinked)
225 * 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 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
245 private static final 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 /**
270 * Constructs a new node. Uses relaxed write because item can
271 * only be seen after publication via casNext or casPrev.
272 */
273 Node(E item) {
274 UNSAFE.putObject(this, itemOffset, item);
275 }
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 restartFromHead:
317 for (;;)
318 for (Node<E> h = head, p = h, q;;) {
319 if ((q = p.prev) != null &&
320 (q = (p = q).prev) != null)
321 // Check for head updates every other hop.
322 // If p == q, we are sure to follow head instead.
323 p = (h != (h = head)) ? h : q;
324 else if (p.next == p) // PREV_TERMINATOR
325 continue restartFromHead;
326 else {
327 // p is first node
328 newNode.lazySetNext(p); // CAS piggyback
329 if (p.casPrev(null, newNode)) {
330 // Successful CAS is the linearization point
331 // for e to become an element of this deque,
332 // and for newNode to become "live".
333 if (p != h) // hop two nodes at a time
334 casHead(h, newNode); // Failure is OK.
335 return;
336 }
337 // Lost CAS race to another thread; re-read prev
338 }
339 }
340 }
341
342 /**
343 * Links e as last element.
344 */
345 private void linkLast(E e) {
346 checkNotNull(e);
347 final Node<E> newNode = new Node<E>(e);
348
349 restartFromTail:
350 for (;;)
351 for (Node<E> t = tail, p = t, q;;) {
352 if ((q = p.next) != null &&
353 (q = (p = q).next) != null)
354 // Check for tail updates every other hop.
355 // If p == q, we are sure to follow tail instead.
356 p = (t != (t = tail)) ? t : q;
357 else if (p.prev == p) // NEXT_TERMINATOR
358 continue restartFromTail;
359 else {
360 // p is last node
361 newNode.lazySetPrev(p); // CAS piggyback
362 if (p.casNext(null, newNode)) {
363 // Successful CAS is the linearization point
364 // for e to become an element of this deque,
365 // and for newNode to become "live".
366 if (p != t) // hop two nodes at a time
367 casTail(t, newNode); // Failure is OK.
368 return;
369 }
370 // Lost CAS race to another thread; re-read next
371 }
372 }
373 }
374
375 private static final int HOPS = 2;
376
377 /**
378 * Unlinks non-null node x.
379 */
380 void unlink(Node<E> x) {
381 // assert x != null;
382 // assert x.item == null;
383 // assert x != PREV_TERMINATOR;
384 // assert x != NEXT_TERMINATOR;
385
386 final Node<E> prev = x.prev;
387 final Node<E> next = x.next;
388 if (prev == null) {
389 unlinkFirst(x, next);
390 } else if (next == null) {
391 unlinkLast(x, prev);
392 } else {
393 // Unlink interior node.
394 //
395 // This is the common case, since a series of polls at the
396 // same end will be "interior" removes, except perhaps for
397 // the first one, since end nodes cannot be unlinked.
398 //
399 // At any time, all active nodes are mutually reachable by
400 // following a sequence of either next or prev pointers.
401 //
402 // Our strategy is to find the unique active predecessor
403 // and successor of x. Try to fix up their links so that
404 // they point to each other, leaving x unreachable from
405 // active nodes. If successful, and if x has no live
406 // predecessor/successor, we additionally try to gc-unlink,
407 // leaving active nodes unreachable from x, by rechecking
408 // that the status of predecessor and successor are
409 // unchanged and ensuring that x is not reachable from
410 // tail/head, before setting x's prev/next links to their
411 // logical approximate replacements, self/TERMINATOR.
412 Node<E> activePred, activeSucc;
413 boolean isFirst, isLast;
414 int hops = 1;
415
416 // Find active predecessor
417 for (Node<E> p = prev; ; ++hops) {
418 if (p.item != null) {
419 activePred = p;
420 isFirst = false;
421 break;
422 }
423 Node<E> q = p.prev;
424 if (q == null) {
425 if (p.next == p)
426 return;
427 activePred = p;
428 isFirst = true;
429 break;
430 }
431 else if (p == q)
432 return;
433 else
434 p = q;
435 }
436
437 // Find active successor
438 for (Node<E> p = next; ; ++hops) {
439 if (p.item != null) {
440 activeSucc = p;
441 isLast = false;
442 break;
443 }
444 Node<E> q = p.next;
445 if (q == null) {
446 if (p.prev == p)
447 return;
448 activeSucc = p;
449 isLast = true;
450 break;
451 }
452 else if (p == q)
453 return;
454 else
455 p = q;
456 }
457
458 // TODO: better HOP heuristics
459 if (hops < HOPS
460 // always squeeze out interior deleted nodes
461 && (isFirst | isLast))
462 return;
463
464 // Squeeze out deleted nodes between activePred and
465 // activeSucc, including x.
466 skipDeletedSuccessors(activePred);
467 skipDeletedPredecessors(activeSucc);
468
469 // Try to gc-unlink, if possible
470 if ((isFirst | isLast) &&
471
472 // Recheck expected state of predecessor and successor
473 (activePred.next == activeSucc) &&
474 (activeSucc.prev == activePred) &&
475 (isFirst ? activePred.prev == null : activePred.item != null) &&
476 (isLast ? activeSucc.next == null : activeSucc.item != null)) {
477
478 updateHead(); // Ensure x is not reachable from head
479 updateTail(); // Ensure x is not reachable from tail
480
481 // Finally, actually gc-unlink
482 x.lazySetPrev(isFirst ? prevTerminator() : x);
483 x.lazySetNext(isLast ? nextTerminator() : x);
484 }
485 }
486 }
487
488 /**
489 * Unlinks non-null first node.
490 */
491 private void unlinkFirst(Node<E> first, Node<E> next) {
492 // assert first != null;
493 // assert next != null;
494 // assert first.item == null;
495 for (Node<E> o = null, p = next, q;;) {
496 if (p.item != null || (q = p.next) == null) {
497 if (o != null && p.prev != p && first.casNext(next, p)) {
498 skipDeletedPredecessors(p);
499 if (first.prev == null &&
500 (p.next == null || p.item != null) &&
501 p.prev == first) {
502
503 updateHead(); // Ensure o is not reachable from head
504 updateTail(); // Ensure o is not reachable from tail
505
506 // Finally, actually gc-unlink
507 o.lazySetNext(o);
508 o.lazySetPrev(prevTerminator());
509 }
510 }
511 return;
512 }
513 else if (p == q)
514 return;
515 else {
516 o = p;
517 p = q;
518 }
519 }
520 }
521
522 /**
523 * Unlinks non-null last node.
524 */
525 private void unlinkLast(Node<E> last, Node<E> prev) {
526 // assert last != null;
527 // assert prev != null;
528 // assert last.item == null;
529 for (Node<E> o = null, p = prev, q;;) {
530 if (p.item != null || (q = p.prev) == null) {
531 if (o != null && p.next != p && last.casPrev(prev, p)) {
532 skipDeletedSuccessors(p);
533 if (last.next == null &&
534 (p.prev == null || p.item != null) &&
535 p.next == last) {
536
537 updateHead(); // Ensure o is not reachable from head
538 updateTail(); // Ensure o is not reachable from tail
539
540 // Finally, actually gc-unlink
541 o.lazySetPrev(o);
542 o.lazySetNext(nextTerminator());
543 }
544 }
545 return;
546 }
547 else if (p == q)
548 return;
549 else {
550 o = p;
551 p = q;
552 }
553 }
554 }
555
556 /**
557 * Guarantees that any node which was unlinked before a call to
558 * this method will be unreachable from head after it returns.
559 * Does not guarantee to eliminate slack, only that head will
560 * point to a node that was active while this method was running.
561 */
562 private final void updateHead() {
563 // Either head already points to an active node, or we keep
564 // trying to cas it to the first node until it does.
565 Node<E> h, p, q;
566 restartFromHead:
567 while ((h = head).item == null && (p = h.prev) != null) {
568 for (;;) {
569 if ((q = p.prev) == null ||
570 (q = (p = q).prev) == null) {
571 // It is possible that p is PREV_TERMINATOR,
572 // but if so, the CAS is guaranteed to fail.
573 if (casHead(h, p))
574 return;
575 else
576 continue restartFromHead;
577 }
578 else if (h != head)
579 continue restartFromHead;
580 else
581 p = q;
582 }
583 }
584 }
585
586 /**
587 * Guarantees that any node which was unlinked before a call to
588 * this method will be unreachable from tail after it returns.
589 * Does not guarantee to eliminate slack, only that tail will
590 * point to a node that was active while this method was running.
591 */
592 private final void updateTail() {
593 // Either tail already points to an active node, or we keep
594 // trying to cas it to the last node until it does.
595 Node<E> t, p, q;
596 restartFromTail:
597 while ((t = tail).item == null && (p = t.next) != null) {
598 for (;;) {
599 if ((q = p.next) == null ||
600 (q = (p = q).next) == null) {
601 // It is possible that p is NEXT_TERMINATOR,
602 // but if so, the CAS is guaranteed to fail.
603 if (casTail(t, p))
604 return;
605 else
606 continue restartFromTail;
607 }
608 else if (t != tail)
609 continue restartFromTail;
610 else
611 p = q;
612 }
613 }
614 }
615
616 private void skipDeletedPredecessors(Node<E> x) {
617 whileActive:
618 do {
619 Node<E> prev = x.prev;
620 // assert prev != null;
621 // assert x != NEXT_TERMINATOR;
622 // assert x != PREV_TERMINATOR;
623 Node<E> p = prev;
624 findActive:
625 for (;;) {
626 if (p.item != null)
627 break findActive;
628 Node<E> q = p.prev;
629 if (q == null) {
630 if (p.next == p)
631 continue whileActive;
632 break findActive;
633 }
634 else if (p == q)
635 continue whileActive;
636 else
637 p = q;
638 }
639
640 // found active CAS target
641 if (prev == p || x.casPrev(prev, p))
642 return;
643
644 } while (x.item != null || x.next == null);
645 }
646
647 private void skipDeletedSuccessors(Node<E> x) {
648 whileActive:
649 do {
650 Node<E> next = x.next;
651 // assert next != null;
652 // assert x != NEXT_TERMINATOR;
653 // assert x != PREV_TERMINATOR;
654 Node<E> p = next;
655 findActive:
656 for (;;) {
657 if (p.item != null)
658 break findActive;
659 Node<E> q = p.next;
660 if (q == null) {
661 if (p.prev == p)
662 continue whileActive;
663 break findActive;
664 }
665 else if (p == q)
666 continue whileActive;
667 else
668 p = q;
669 }
670
671 // found active CAS target
672 if (next == p || x.casNext(next, p))
673 return;
674
675 } while (x.item != null || x.prev == null);
676 }
677
678 /**
679 * Returns the successor of p, or the first node if p.next has been
680 * linked to self, which will only be true if traversing with a
681 * stale pointer that is now off the list.
682 */
683 final Node<E> succ(Node<E> p) {
684 // TODO: should we skip deleted nodes here?
685 Node<E> q = p.next;
686 return (p == q) ? first() : q;
687 }
688
689 /**
690 * Returns the predecessor of p, or the last node if p.prev has been
691 * linked to self, which will only be true if traversing with a
692 * stale pointer that is now off the list.
693 */
694 final Node<E> pred(Node<E> p) {
695 Node<E> q = p.prev;
696 return (p == q) ? last() : q;
697 }
698
699 /**
700 * Returns the first node, the unique node p for which:
701 * p.prev == null && p.next != p
702 * The returned node may or may not be logically deleted.
703 * Guarantees that head is set to the returned node.
704 */
705 Node<E> first() {
706 restartFromHead:
707 for (;;)
708 for (Node<E> h = head, p = h, q;;) {
709 if ((q = p.prev) != null &&
710 (q = (p = q).prev) != null)
711 // Check for head updates every other hop.
712 // If p == q, we are sure to follow head instead.
713 p = (h != (h = head)) ? h : q;
714 else if (p == h
715 // It is possible that p is PREV_TERMINATOR,
716 // but if so, the CAS is guaranteed to fail.
717 || casHead(h, p))
718 return p;
719 else
720 continue restartFromHead;
721 }
722 }
723
724 /**
725 * Returns the last node, the unique node p for which:
726 * p.next == null && p.prev != p
727 * The returned node may or may not be logically deleted.
728 * Guarantees that tail is set to the returned node.
729 */
730 Node<E> last() {
731 restartFromTail:
732 for (;;)
733 for (Node<E> t = tail, p = t, q;;) {
734 if ((q = p.next) != null &&
735 (q = (p = q).next) != null)
736 // Check for tail updates every other hop.
737 // If p == q, we are sure to follow tail instead.
738 p = (t != (t = tail)) ? t : q;
739 else if (p == t
740 // It is possible that p is NEXT_TERMINATOR,
741 // but if so, the CAS is guaranteed to fail.
742 || casTail(t, p))
743 return p;
744 else
745 continue restartFromTail;
746 }
747 }
748
749 // Minor convenience utilities
750
751 /**
752 * Throws NullPointerException if argument is null.
753 *
754 * @param v the element
755 */
756 private static void checkNotNull(Object v) {
757 if (v == null)
758 throw new NullPointerException();
759 }
760
761 /**
762 * Returns element unless it is null, in which case throws
763 * NoSuchElementException.
764 *
765 * @param v the element
766 * @return the element
767 */
768 private E screenNullResult(E v) {
769 if (v == null)
770 throw new NoSuchElementException();
771 return v;
772 }
773
774 /**
775 * Creates an array list and fills it with elements of this list.
776 * Used by toArray.
777 *
778 * @return the arrayList
779 */
780 private ArrayList<E> toArrayList() {
781 ArrayList<E> list = new ArrayList<E>();
782 for (Node<E> p = first(); p != null; p = succ(p)) {
783 E item = p.item;
784 if (item != null)
785 list.add(item);
786 }
787 return list;
788 }
789
790 /**
791 * Constructs an empty deque.
792 */
793 public ConcurrentLinkedDeque() {
794 head = tail = new Node<E>(null);
795 }
796
797 /**
798 * Constructs a deque initially containing the elements of
799 * the given collection, added in traversal order of the
800 * collection's iterator.
801 *
802 * @param c the collection of elements to initially contain
803 * @throws NullPointerException if the specified collection or any
804 * of its elements are null
805 */
806 public ConcurrentLinkedDeque(Collection<? extends E> c) {
807 // Copy c into a private chain of Nodes
808 Node<E> h = null, t = null;
809 for (E e : c) {
810 checkNotNull(e);
811 Node<E> newNode = new Node<E>(e);
812 if (h == null)
813 h = t = newNode;
814 else {
815 t.lazySetNext(newNode);
816 newNode.lazySetPrev(t);
817 t = newNode;
818 }
819 }
820 initHeadTail(h, t);
821 }
822
823 /**
824 * Initializes head and tail, ensuring invariants hold.
825 */
826 private void initHeadTail(Node<E> h, Node<E> t) {
827 if (h == t) {
828 if (h == null)
829 h = t = new Node<E>(null);
830 else {
831 // Avoid edge case of a single Node with non-null item.
832 Node<E> newNode = new Node<E>(null);
833 t.lazySetNext(newNode);
834 newNode.lazySetPrev(t);
835 t = newNode;
836 }
837 }
838 head = h;
839 tail = t;
840 }
841
842 /**
843 * Inserts the specified element at the front of this deque.
844 * As the deque is unbounded, this method will never throw
845 * {@link IllegalStateException}.
846 *
847 * @throws NullPointerException if the specified element is null
848 */
849 public void addFirst(E e) {
850 linkFirst(e);
851 }
852
853 /**
854 * Inserts the specified element at the end of this deque.
855 * As the deque is unbounded, this method will never throw
856 * {@link IllegalStateException}.
857 *
858 * <p>This method is equivalent to {@link #add}.
859 *
860 * @throws NullPointerException if the specified element is null
861 */
862 public void addLast(E e) {
863 linkLast(e);
864 }
865
866 /**
867 * Inserts the specified element at the front of this deque.
868 * As the deque is unbounded, this method will never return {@code false}.
869 *
870 * @return {@code true} (as specified by {@link Deque#offerFirst})
871 * @throws NullPointerException if the specified element is null
872 */
873 public boolean offerFirst(E e) {
874 linkFirst(e);
875 return true;
876 }
877
878 /**
879 * Inserts the specified element at the end of this deque.
880 * As the deque is unbounded, this method will never return {@code false}.
881 *
882 * <p>This method is equivalent to {@link #add}.
883 *
884 * @return {@code true} (as specified by {@link Deque#offerLast})
885 * @throws NullPointerException if the specified element is null
886 */
887 public boolean offerLast(E e) {
888 linkLast(e);
889 return true;
890 }
891
892 public E peekFirst() {
893 for (Node<E> p = first(); p != null; p = succ(p)) {
894 E item = p.item;
895 if (item != null)
896 return item;
897 }
898 return null;
899 }
900
901 public E peekLast() {
902 for (Node<E> p = last(); p != null; p = pred(p)) {
903 E item = p.item;
904 if (item != null)
905 return item;
906 }
907 return null;
908 }
909
910 /**
911 * @throws NoSuchElementException {@inheritDoc}
912 */
913 public E getFirst() {
914 return screenNullResult(peekFirst());
915 }
916
917 /**
918 * @throws NoSuchElementException {@inheritDoc}
919 */
920 public E getLast() {
921 return screenNullResult(peekLast());
922 }
923
924 public E pollFirst() {
925 for (Node<E> p = first(); p != null; p = succ(p)) {
926 E item = p.item;
927 if (item != null && p.casItem(item, null)) {
928 unlink(p);
929 return item;
930 }
931 }
932 return null;
933 }
934
935 public E pollLast() {
936 for (Node<E> p = last(); p != null; p = pred(p)) {
937 E item = p.item;
938 if (item != null && p.casItem(item, null)) {
939 unlink(p);
940 return item;
941 }
942 }
943 return null;
944 }
945
946 /**
947 * @throws NoSuchElementException {@inheritDoc}
948 */
949 public E removeFirst() {
950 return screenNullResult(pollFirst());
951 }
952
953 /**
954 * @throws NoSuchElementException {@inheritDoc}
955 */
956 public E removeLast() {
957 return screenNullResult(pollLast());
958 }
959
960 // *** Queue and stack methods ***
961
962 /**
963 * Inserts the specified element at the tail of this deque.
964 * As the deque is unbounded, this method will never return {@code false}.
965 *
966 * @return {@code true} (as specified by {@link Queue#offer})
967 * @throws NullPointerException if the specified element is null
968 */
969 public boolean offer(E e) {
970 return offerLast(e);
971 }
972
973 /**
974 * Inserts the specified element at the tail of this deque.
975 * As the deque is unbounded, this method will never throw
976 * {@link IllegalStateException} or return {@code false}.
977 *
978 * @return {@code true} (as specified by {@link Collection#add})
979 * @throws NullPointerException if the specified element is null
980 */
981 public boolean add(E e) {
982 return offerLast(e);
983 }
984
985 public E poll() { return pollFirst(); }
986 public E remove() { return removeFirst(); }
987 public E peek() { return peekFirst(); }
988 public E element() { return getFirst(); }
989 public void push(E e) { addFirst(e); }
990 public E pop() { return removeFirst(); }
991
992 /**
993 * Removes the first element {@code e} such that
994 * {@code o.equals(e)}, if such an element exists in this deque.
995 * If the deque does not contain the element, it is unchanged.
996 *
997 * @param o element to be removed from this deque, if present
998 * @return {@code true} if the deque contained the specified element
999 * @throws NullPointerException if the specified element is null
1000 */
1001 public boolean removeFirstOccurrence(Object o) {
1002 checkNotNull(o);
1003 for (Node<E> p = first(); p != null; p = succ(p)) {
1004 E item = p.item;
1005 if (item != null && o.equals(item) && p.casItem(item, null)) {
1006 unlink(p);
1007 return true;
1008 }
1009 }
1010 return false;
1011 }
1012
1013 /**
1014 * Removes the last element {@code e} such that
1015 * {@code o.equals(e)}, if such an element exists in this deque.
1016 * If the deque does not contain the element, it is unchanged.
1017 *
1018 * @param o element to be removed from this deque, if present
1019 * @return {@code true} if the deque contained the specified element
1020 * @throws NullPointerException if the specified element is null
1021 */
1022 public boolean removeLastOccurrence(Object o) {
1023 checkNotNull(o);
1024 for (Node<E> p = last(); p != null; p = pred(p)) {
1025 E item = p.item;
1026 if (item != null && o.equals(item) && p.casItem(item, null)) {
1027 unlink(p);
1028 return true;
1029 }
1030 }
1031 return false;
1032 }
1033
1034 /**
1035 * Returns {@code true} if this deque contains at least one
1036 * element {@code e} such that {@code o.equals(e)}.
1037 *
1038 * @param o element whose presence in this deque is to be tested
1039 * @return {@code true} if this deque contains the specified element
1040 */
1041 public boolean contains(Object o) {
1042 if (o == null) return false;
1043 for (Node<E> p = first(); p != null; p = succ(p)) {
1044 E item = p.item;
1045 if (item != null && o.equals(item))
1046 return true;
1047 }
1048 return false;
1049 }
1050
1051 /**
1052 * Returns {@code true} if this collection contains no elements.
1053 *
1054 * @return {@code true} if this collection contains no elements
1055 */
1056 public boolean isEmpty() {
1057 return peekFirst() == null;
1058 }
1059
1060 /**
1061 * Returns the number of elements in this deque. If this deque
1062 * contains more than {@code Integer.MAX_VALUE} elements, it
1063 * returns {@code Integer.MAX_VALUE}.
1064 *
1065 * <p>Beware that, unlike in most collections, this method is
1066 * <em>NOT</em> a constant-time operation. Because of the
1067 * asynchronous nature of these deques, determining the current
1068 * number of elements requires traversing them all to count them.
1069 * Additionally, it is possible for the size to change during
1070 * execution of this method, in which case the returned result
1071 * will be inaccurate. Thus, this method is typically not very
1072 * useful in concurrent applications.
1073 *
1074 * @return the number of elements in this deque
1075 */
1076 public int size() {
1077 int count = 0;
1078 for (Node<E> p = first(); p != null; p = succ(p))
1079 if (p.item != null)
1080 // Collection.size() spec says to max out
1081 if (++count == Integer.MAX_VALUE)
1082 break;
1083 return count;
1084 }
1085
1086 /**
1087 * Removes the first element {@code e} such that
1088 * {@code o.equals(e)}, if such an element exists in this deque.
1089 * If the deque does not contain the element, it is unchanged.
1090 *
1091 * @param o element to be removed from this deque, if present
1092 * @return {@code true} if the deque contained the specified element
1093 * @throws NullPointerException if the specified element is null
1094 */
1095 public boolean remove(Object o) {
1096 return removeFirstOccurrence(o);
1097 }
1098
1099 /**
1100 * Appends all of the elements in the specified collection to the end of
1101 * this deque, in the order that they are returned by the specified
1102 * collection's iterator. Attempts to {@code addAll} of a deque to
1103 * itself result in {@code IllegalArgumentException}.
1104 *
1105 * @param c the elements to be inserted into this deque
1106 * @return {@code true} if this deque changed as a result of the call
1107 * @throws NullPointerException if the specified collection or any
1108 * of its elements are null
1109 * @throws IllegalArgumentException if the collection is this deque
1110 */
1111 public boolean addAll(Collection<? extends E> c) {
1112 if (c == this)
1113 // As historically specified in AbstractQueue#addAll
1114 throw new IllegalArgumentException();
1115
1116 // Copy c into a private chain of Nodes
1117 Node<E> beginningOfTheEnd = null, last = null;
1118 for (E e : c) {
1119 checkNotNull(e);
1120 Node<E> newNode = new Node<E>(e);
1121 if (beginningOfTheEnd == null)
1122 beginningOfTheEnd = last = newNode;
1123 else {
1124 last.lazySetNext(newNode);
1125 newNode.lazySetPrev(last);
1126 last = newNode;
1127 }
1128 }
1129 if (beginningOfTheEnd == null)
1130 return false;
1131
1132 // Atomically append the chain at the tail of this collection
1133 restartFromTail:
1134 for (;;)
1135 for (Node<E> t = tail, p = t, q;;) {
1136 if ((q = p.next) != null &&
1137 (q = (p = q).next) != null)
1138 // Check for tail updates every other hop.
1139 // If p == q, we are sure to follow tail instead.
1140 p = (t != (t = tail)) ? t : q;
1141 else if (p.prev == p) // NEXT_TERMINATOR
1142 continue restartFromTail;
1143 else {
1144 // p is last node
1145 beginningOfTheEnd.lazySetPrev(p); // CAS piggyback
1146 if (p.casNext(null, beginningOfTheEnd)) {
1147 // Successful CAS is the linearization point
1148 // for all elements to be added to this deque.
1149 if (!casTail(t, last)) {
1150 // Try a little harder to update tail,
1151 // since we may be adding many elements.
1152 t = tail;
1153 if (last.next == null)
1154 casTail(t, last);
1155 }
1156 return true;
1157 }
1158 // Lost CAS race to another thread; re-read next
1159 }
1160 }
1161 }
1162
1163 /**
1164 * Removes all of the elements from this deque.
1165 */
1166 public void clear() {
1167 while (pollFirst() != null)
1168 ;
1169 }
1170
1171 /**
1172 * Returns an array containing all of the elements in this deque, in
1173 * proper sequence (from first to last element).
1174 *
1175 * <p>The returned array will be "safe" in that no references to it are
1176 * maintained by this deque. (In other words, this method must allocate
1177 * a new array). The caller is thus free to modify the returned array.
1178 *
1179 * <p>This method acts as bridge between array-based and collection-based
1180 * APIs.
1181 *
1182 * @return an array containing all of the elements in this deque
1183 */
1184 public Object[] toArray() {
1185 return toArrayList().toArray();
1186 }
1187
1188 /**
1189 * Returns an array containing all of the elements in this deque,
1190 * in proper sequence (from first to last element); the runtime
1191 * type of the returned array is that of the specified array. If
1192 * the deque fits in the specified array, it is returned therein.
1193 * Otherwise, a new array is allocated with the runtime type of
1194 * the specified array and the size of this deque.
1195 *
1196 * <p>If this deque fits in the specified array with room to spare
1197 * (i.e., the array has more elements than this deque), the element in
1198 * the array immediately following the end of the deque is set to
1199 * {@code null}.
1200 *
1201 * <p>Like the {@link #toArray()} method, this method acts as
1202 * bridge between array-based and collection-based APIs. Further,
1203 * this method allows precise control over the runtime type of the
1204 * output array, and may, under certain circumstances, be used to
1205 * save allocation costs.
1206 *
1207 * <p>Suppose {@code x} is a deque known to contain only strings.
1208 * The following code can be used to dump the deque into a newly
1209 * allocated array of {@code String}:
1210 *
1211 * <pre>
1212 * String[] y = x.toArray(new String[0]);</pre>
1213 *
1214 * Note that {@code toArray(new Object[0])} is identical in function to
1215 * {@code toArray()}.
1216 *
1217 * @param a the array into which the elements of the deque are to
1218 * be stored, if it is big enough; otherwise, a new array of the
1219 * same runtime type is allocated for this purpose
1220 * @return an array containing all of the elements in this deque
1221 * @throws ArrayStoreException if the runtime type of the specified array
1222 * is not a supertype of the runtime type of every element in
1223 * this deque
1224 * @throws NullPointerException if the specified array is null
1225 */
1226 public <T> T[] toArray(T[] a) {
1227 return toArrayList().toArray(a);
1228 }
1229
1230 /**
1231 * Returns an iterator over the elements in this deque in proper sequence.
1232 * The elements will be returned in order from first (head) to last (tail).
1233 *
1234 * <p>The returned {@code Iterator} is a "weakly consistent" iterator that
1235 * will never throw {@link java.util.ConcurrentModificationException
1236 * ConcurrentModificationException},
1237 * and guarantees to traverse elements as they existed upon
1238 * construction of the iterator, and may (but is not guaranteed to)
1239 * reflect any modifications subsequent to construction.
1240 *
1241 * @return an iterator over the elements in this deque in proper sequence
1242 */
1243 public Iterator<E> iterator() {
1244 return new Itr();
1245 }
1246
1247 /**
1248 * Returns an iterator over the elements in this deque in reverse
1249 * sequential order. The elements will be returned in order from
1250 * last (tail) to first (head).
1251 *
1252 * <p>The returned {@code Iterator} is a "weakly consistent" iterator that
1253 * will never throw {@link java.util.ConcurrentModificationException
1254 * ConcurrentModificationException},
1255 * and guarantees to traverse elements as they existed upon
1256 * construction of the iterator, and may (but is not guaranteed to)
1257 * reflect any modifications subsequent to construction.
1258 *
1259 * @return an iterator over the elements in this deque in reverse order
1260 */
1261 public Iterator<E> descendingIterator() {
1262 return new DescendingItr();
1263 }
1264
1265 private abstract class AbstractItr implements Iterator<E> {
1266 /**
1267 * Next node to return item for.
1268 */
1269 private Node<E> nextNode;
1270
1271 /**
1272 * nextItem holds on to item fields because once we claim
1273 * that an element exists in hasNext(), we must return it in
1274 * the following next() call even if it was in the process of
1275 * being removed when hasNext() was called.
1276 */
1277 private E nextItem;
1278
1279 /**
1280 * Node returned by most recent call to next. Needed by remove.
1281 * Reset to null if this element is deleted by a call to remove.
1282 */
1283 private Node<E> lastRet;
1284
1285 abstract Node<E> startNode();
1286 abstract Node<E> nextNode(Node<E> p);
1287
1288 AbstractItr() {
1289 advance();
1290 }
1291
1292 /**
1293 * Sets nextNode and nextItem to next valid node, or to null
1294 * if no such.
1295 */
1296 private void advance() {
1297 lastRet = nextNode;
1298
1299 Node<E> p = (nextNode == null) ? startNode() : nextNode(nextNode);
1300 for (;; p = nextNode(p)) {
1301 if (p == null) {
1302 // p might be active end or TERMINATOR node; both are OK
1303 nextNode = null;
1304 nextItem = null;
1305 break;
1306 }
1307 E item = p.item;
1308 if (item != null) {
1309 nextNode = p;
1310 nextItem = item;
1311 break;
1312 }
1313 }
1314 }
1315
1316 public boolean hasNext() {
1317 return nextItem != null;
1318 }
1319
1320 public E next() {
1321 E item = nextItem;
1322 if (item == null) throw new NoSuchElementException();
1323 advance();
1324 return item;
1325 }
1326
1327 public void remove() {
1328 Node<E> l = lastRet;
1329 if (l == null) throw new IllegalStateException();
1330 l.item = null;
1331 unlink(l);
1332 lastRet = null;
1333 }
1334 }
1335
1336 /** Forward iterator */
1337 private class Itr extends AbstractItr {
1338 Node<E> startNode() { return first(); }
1339 Node<E> nextNode(Node<E> p) { return succ(p); }
1340 }
1341
1342 /** Descending iterator */
1343 private class DescendingItr extends AbstractItr {
1344 Node<E> startNode() { return last(); }
1345 Node<E> nextNode(Node<E> p) { return pred(p); }
1346 }
1347
1348 /**
1349 * Saves the state to a stream (that is, serializes it).
1350 *
1351 * @serialData All of the elements (each an {@code E}) in
1352 * the proper order, followed by a null
1353 * @param s the stream
1354 */
1355 private void writeObject(java.io.ObjectOutputStream s)
1356 throws java.io.IOException {
1357
1358 // Write out any hidden stuff
1359 s.defaultWriteObject();
1360
1361 // Write out all elements in the proper order.
1362 for (Node<E> p = first(); p != null; p = succ(p)) {
1363 E item = p.item;
1364 if (item != null)
1365 s.writeObject(item);
1366 }
1367
1368 // Use trailing null as sentinel
1369 s.writeObject(null);
1370 }
1371
1372 /**
1373 * Reconstitutes the instance from a stream (that is, deserializes it).
1374 * @param s the stream
1375 */
1376 private void readObject(java.io.ObjectInputStream s)
1377 throws java.io.IOException, ClassNotFoundException {
1378 s.defaultReadObject();
1379
1380 // Read in elements until trailing null sentinel found
1381 Node<E> h = null, t = null;
1382 Object item;
1383 while ((item = s.readObject()) != null) {
1384 @SuppressWarnings("unchecked")
1385 Node<E> newNode = new Node<E>((E) item);
1386 if (h == null)
1387 h = t = newNode;
1388 else {
1389 t.lazySetNext(newNode);
1390 newNode.lazySetPrev(t);
1391 t = newNode;
1392 }
1393 }
1394 initHeadTail(h, t);
1395 }
1396
1397 // Unsafe mechanics
1398
1399 private static final sun.misc.Unsafe UNSAFE =
1400 sun.misc.Unsafe.getUnsafe();
1401 private static final long headOffset =
1402 objectFieldOffset(UNSAFE, "head", ConcurrentLinkedDeque.class);
1403 private static final long tailOffset =
1404 objectFieldOffset(UNSAFE, "tail", ConcurrentLinkedDeque.class);
1405
1406 private boolean casHead(Node<E> cmp, Node<E> val) {
1407 return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val);
1408 }
1409
1410 private boolean casTail(Node<E> cmp, Node<E> val) {
1411 return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val);
1412 }
1413
1414 static long objectFieldOffset(sun.misc.Unsafe UNSAFE,
1415 String field, Class<?> klazz) {
1416 try {
1417 return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
1418 } catch (NoSuchFieldException e) {
1419 // Convert Exception to corresponding Error
1420 NoSuchFieldError error = new NoSuchFieldError(field);
1421 error.initCause(e);
1422 throw error;
1423 }
1424 }
1425 }