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