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