1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * Written by Doug Lea and Martin Buchholz with assistance from members of 32 * JCP JSR-166 Expert Group and released to the public domain, as explained 33 * at http://creativecommons.org/publicdomain/zero/1.0/ 34 */ 35 36 package java.util.concurrent; 37 38 import java.lang.invoke.MethodHandles; 39 import java.lang.invoke.VarHandle; 40 import java.util.AbstractCollection; 41 import java.util.Arrays; 42 import java.util.Collection; 43 import java.util.Deque; 44 import java.util.Iterator; 45 import java.util.NoSuchElementException; 46 import java.util.Objects; 47 import java.util.Queue; 48 import java.util.Spliterator; 49 import java.util.Spliterators; 50 import java.util.function.Consumer; 51 import java.util.function.Predicate; 52 53 /** 54 * An unbounded concurrent {@linkplain Deque deque} based on linked nodes. 55 * Concurrent insertion, removal, and access operations execute safely 56 * across multiple threads. 57 * A {@code ConcurrentLinkedDeque} is an appropriate choice when 58 * many threads will share access to a common collection. 59 * Like most other concurrent collection implementations, this class 60 * does not permit the use of {@code null} elements. 61 * 62 * <p>Iterators and spliterators are 63 * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. 64 * 65 * <p>Beware that, unlike in most collections, the {@code size} method 66 * is <em>NOT</em> a constant-time operation. Because of the 67 * asynchronous nature of these deques, determining the current number 68 * of elements requires a traversal of the elements, and so may report 69 * inaccurate results if this collection is modified during traversal. 70 * 71 * <p>Bulk operations that add, remove, or examine multiple elements, 72 * such as {@link #addAll}, {@link #removeIf} or {@link #forEach}, 73 * are <em>not</em> guaranteed to be performed atomically. 74 * For example, a {@code forEach} traversal concurrent with an {@code 75 * addAll} operation might observe only some of the added elements. 76 * 77 * <p>This class and its iterator implement all of the <em>optional</em> 78 * methods of the {@link Deque} and {@link Iterator} interfaces. 79 * 80 * <p>Memory consistency effects: As with other concurrent collections, 81 * actions in a thread prior to placing an object into a 82 * {@code ConcurrentLinkedDeque} 83 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> 84 * actions subsequent to the access or removal of that element from 85 * the {@code ConcurrentLinkedDeque} in another thread. 86 * 87 * <p>This class is a member of the 88 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> 89 * Java Collections Framework</a>. 90 * 91 * @since 1.7 92 * @author Doug Lea 93 * @author Martin Buchholz 94 * @param <E> the type of elements held in this deque 95 */ 96 public class ConcurrentLinkedDeque<E> 97 extends AbstractCollection<E> 98 implements Deque<E>, java.io.Serializable { 99 100 /* 101 * This is an implementation of a concurrent lock-free deque 102 * supporting interior removes but not interior insertions, as 103 * required to support the entire Deque interface. 104 * 105 * We extend the techniques developed for ConcurrentLinkedQueue and 106 * LinkedTransferQueue (see the internal docs for those classes). 107 * Understanding the ConcurrentLinkedQueue implementation is a 108 * prerequisite for understanding the implementation of this class. 109 * 110 * The data structure is a symmetrical doubly-linked "GC-robust" 111 * linked list of nodes. We minimize the number of volatile writes 112 * using two techniques: advancing multiple hops with a single CAS 113 * and mixing volatile and non-volatile writes of the same memory 114 * locations. 115 * 116 * A node contains the expected E ("item") and links to predecessor 117 * ("prev") and successor ("next") nodes: 118 * 119 * class Node<E> { volatile Node<E> prev, next; volatile E item; } 120 * 121 * A node p is considered "live" if it contains a non-null item 122 * (p.item != null). When an item is CASed to null, the item is 123 * atomically logically deleted from the collection. 124 * 125 * At any time, there is precisely one "first" node with a null 126 * prev reference that terminates any chain of prev references 127 * starting at a live node. Similarly there is precisely one 128 * "last" node terminating any chain of next references starting at 129 * a live node. The "first" and "last" nodes may or may not be live. 130 * The "first" and "last" nodes are always mutually reachable. 131 * 132 * A new element is added atomically by CASing the null prev or 133 * next reference in the first or last node to a fresh node 134 * containing the element. The element's node atomically becomes 135 * "live" at that point. 136 * 137 * A node is considered "active" if it is a live node, or the 138 * first or last node. Active nodes cannot be unlinked. 139 * 140 * A "self-link" is a next or prev reference that is the same node: 141 * p.prev == p or p.next == p 142 * Self-links are used in the node unlinking process. Active nodes 143 * never have self-links. 144 * 145 * A node p is active if and only if: 146 * 147 * p.item != null || 148 * (p.prev == null && p.next != p) || 149 * (p.next == null && p.prev != p) 150 * 151 * The deque object has two node references, "head" and "tail". 152 * The head and tail are only approximations to the first and last 153 * nodes of the deque. The first node can always be found by 154 * following prev pointers from head; likewise for tail. However, 155 * it is permissible for head and tail to be referring to deleted 156 * nodes that have been unlinked and so may not be reachable from 157 * any live node. 158 * 159 * There are 3 stages of node deletion; 160 * "logical deletion", "unlinking", and "gc-unlinking". 161 * 162 * 1. "logical deletion" by CASing item to null atomically removes 163 * the element from the collection, and makes the containing node 164 * eligible for unlinking. 165 * 166 * 2. "unlinking" makes a deleted node unreachable from active 167 * nodes, and thus eventually reclaimable by GC. Unlinked nodes 168 * may remain reachable indefinitely from an iterator. 169 * 170 * Physical node unlinking is merely an optimization (albeit a 171 * critical one), and so can be performed at our convenience. At 172 * any time, the set of live nodes maintained by prev and next 173 * links are identical, that is, the live nodes found via next 174 * links from the first node is equal to the elements found via 175 * prev links from the last node. However, this is not true for 176 * nodes that have already been logically deleted - such nodes may 177 * be reachable in one direction only. 178 * 179 * 3. "gc-unlinking" takes unlinking further by making active 180 * nodes unreachable from deleted nodes, making it easier for the 181 * GC to reclaim future deleted nodes. This step makes the data 182 * structure "gc-robust", as first described in detail by Boehm 183 * (http://portal.acm.org/citation.cfm?doid=503272.503282). 184 * 185 * GC-unlinked nodes may remain reachable indefinitely from an 186 * iterator, but unlike unlinked nodes, are never reachable from 187 * head or tail. 188 * 189 * Making the data structure GC-robust will eliminate the risk of 190 * unbounded memory retention with conservative GCs and is likely 191 * to improve performance with generational GCs. 192 * 193 * When a node is dequeued at either end, e.g. via poll(), we would 194 * like to break any references from the node to active nodes. We 195 * develop further the use of self-links that was very effective in 196 * other concurrent collection classes. The idea is to replace 197 * prev and next pointers with special values that are interpreted 198 * to mean off-the-list-at-one-end. These are approximations, but 199 * good enough to preserve the properties we want in our 200 * traversals, e.g. we guarantee that a traversal will never visit 201 * the same element twice, but we don't guarantee whether a 202 * traversal that runs out of elements will be able to see more 203 * elements later after enqueues at that end. Doing gc-unlinking 204 * safely is particularly tricky, since any node can be in use 205 * indefinitely (for example by an iterator). We must ensure that 206 * the nodes pointed at by head/tail never get gc-unlinked, since 207 * head/tail are needed to get "back on track" by other nodes that 208 * are gc-unlinked. gc-unlinking accounts for much of the 209 * implementation complexity. 210 * 211 * Since neither unlinking nor gc-unlinking are necessary for 212 * correctness, there are many implementation choices regarding 213 * frequency (eagerness) of these operations. Since volatile 214 * reads are likely to be much cheaper than CASes, saving CASes by 215 * unlinking multiple adjacent nodes at a time may be a win. 216 * gc-unlinking can be performed rarely and still be effective, 217 * since it is most important that long chains of deleted nodes 218 * are occasionally broken. 219 * 220 * The actual representation we use is that p.next == p means to 221 * goto the first node (which in turn is reached by following prev 222 * pointers from head), and p.next == null && p.prev == p means 223 * that the iteration is at an end and that p is a (static final) 224 * dummy node, NEXT_TERMINATOR, and not the last active node. 225 * Finishing the iteration when encountering such a TERMINATOR is 226 * good enough for read-only traversals, so such traversals can use 227 * p.next == null as the termination condition. When we need to 228 * find the last (active) node, for enqueueing a new node, we need 229 * to check whether we have reached a TERMINATOR node; if so, 230 * restart traversal from tail. 231 * 232 * The implementation is completely directionally symmetrical, 233 * except that most public methods that iterate through the list 234 * follow next pointers, in the "forward" direction. 235 * 236 * We believe (without full proof) that all single-element Deque 237 * operations that operate directly at the two ends of the Deque 238 * (e.g., addFirst, peekLast, pollLast) are linearizable (see 239 * Herlihy and Shavit's book). However, some combinations of 240 * operations are known not to be linearizable. In particular, 241 * when an addFirst(A) is racing with pollFirst() removing B, it 242 * is possible for an observer iterating over the elements to 243 * observe first [A B C] and then [A C], even though no interior 244 * removes are ever performed. Nevertheless, iterators behave 245 * reasonably, providing the "weakly consistent" guarantees. 246 * 247 * Empirically, microbenchmarks suggest that this class adds about 248 * 40% overhead relative to ConcurrentLinkedQueue, which feels as 249 * good as we can hope for. 250 */ 251 252 private static final long serialVersionUID = 876323262645176354L; 253 254 /** 255 * A node from which the first node on list (that is, the unique node p 256 * with p.prev == null && p.next != p) can be reached in O(1) time. 257 * Invariants: 258 * - the first node is always O(1) reachable from head via prev links 259 * - all live nodes are reachable from the first node via succ() 260 * - head != null 261 * - (tmp = head).next != tmp || tmp != head 262 * - head is never gc-unlinked (but may be unlinked) 263 * Non-invariants: 264 * - head.item may or may not be null 265 * - head may not be reachable from the first or last node, or from tail 266 */ 267 private transient volatile Node<E> head; 268 269 /** 270 * A node from which the last node on list (that is, the unique node p 271 * with p.next == null && p.prev != p) can be reached in O(1) time. 272 * Invariants: 273 * - the last node is always O(1) reachable from tail via next links 274 * - all live nodes are reachable from the last node via pred() 275 * - tail != null 276 * - tail is never gc-unlinked (but may be unlinked) 277 * Non-invariants: 278 * - tail.item may or may not be null 279 * - tail may not be reachable from the first or last node, or from head 280 */ 281 private transient volatile Node<E> tail; 282 283 private static final Node<Object> PREV_TERMINATOR, NEXT_TERMINATOR; 284 285 @SuppressWarnings("unchecked") prevTerminator()286 Node<E> prevTerminator() { 287 return (Node<E>) PREV_TERMINATOR; 288 } 289 290 @SuppressWarnings("unchecked") nextTerminator()291 Node<E> nextTerminator() { 292 return (Node<E>) NEXT_TERMINATOR; 293 } 294 295 static final class Node<E> { 296 volatile Node<E> prev; 297 volatile E item; 298 volatile Node<E> next; 299 } 300 301 /** 302 * Returns a new node holding item. Uses relaxed write because item 303 * can only be seen after piggy-backing publication via CAS. 304 */ newNode(E item)305 static <E> Node<E> newNode(E item) { 306 Node<E> node = new Node<E>(); 307 ITEM.set(node, item); 308 return node; 309 } 310 311 /** 312 * Links e as first element. 313 */ linkFirst(E e)314 private void linkFirst(E e) { 315 final Node<E> newNode = newNode(Objects.requireNonNull(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 NEXT.set(newNode, p); // CAS piggyback 330 if (PREV.compareAndSet(p, 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; failure is OK 335 HEAD.weakCompareAndSet(this, h, newNode); 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 */ linkLast(E e)346 private void linkLast(E e) { 347 final Node<E> newNode = newNode(Objects.requireNonNull(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 PREV.set(newNode, p); // CAS piggyback 362 if (NEXT.compareAndSet(p, 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; failure is OK 367 TAIL.weakCompareAndSet(this, t, newNode); 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 */ unlink(Node<E> x)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 PREV.setRelease(x, isFirst ? prevTerminator() : x); 483 NEXT.setRelease(x, isLast ? nextTerminator() : x); 484 } 485 } 486 } 487 488 /** 489 * Unlinks non-null first node. 490 */ unlinkFirst(Node<E> first, Node<E> next)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 && 498 NEXT.compareAndSet(first, 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 NEXT.setRelease(o, o); 509 PREV.setRelease(o, 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 */ unlinkLast(Node<E> last, Node<E> prev)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 && 533 PREV.compareAndSet(last, prev, p)) { 534 skipDeletedSuccessors(p); 535 if (last.next == null && 536 (p.prev == null || p.item != null) && 537 p.next == last) { 538 539 updateHead(); // Ensure o is not reachable from head 540 updateTail(); // Ensure o is not reachable from tail 541 542 // Finally, actually gc-unlink 543 PREV.setRelease(o, o); 544 NEXT.setRelease(o, nextTerminator()); 545 } 546 } 547 return; 548 } 549 else if (p == q) 550 return; 551 else { 552 o = p; 553 p = q; 554 } 555 } 556 } 557 558 /** 559 * Guarantees that any node which was unlinked before a call to 560 * this method will be unreachable from head after it returns. 561 * Does not guarantee to eliminate slack, only that head will 562 * point to a node that was active while this method was running. 563 */ updateHead()564 private final void updateHead() { 565 // Either head already points to an active node, or we keep 566 // trying to cas it to the first node until it does. 567 Node<E> h, p, q; 568 restartFromHead: 569 while ((h = head).item == null && (p = h.prev) != null) { 570 for (;;) { 571 if ((q = p.prev) == null || 572 (q = (p = q).prev) == null) { 573 // It is possible that p is PREV_TERMINATOR, 574 // but if so, the CAS is guaranteed to fail. 575 if (HEAD.compareAndSet(this, h, p)) 576 return; 577 else 578 continue restartFromHead; 579 } 580 else if (h != head) 581 continue restartFromHead; 582 else 583 p = q; 584 } 585 } 586 } 587 588 /** 589 * Guarantees that any node which was unlinked before a call to 590 * this method will be unreachable from tail after it returns. 591 * Does not guarantee to eliminate slack, only that tail will 592 * point to a node that was active while this method was running. 593 */ updateTail()594 private final void updateTail() { 595 // Either tail already points to an active node, or we keep 596 // trying to cas it to the last node until it does. 597 Node<E> t, p, q; 598 restartFromTail: 599 while ((t = tail).item == null && (p = t.next) != null) { 600 for (;;) { 601 if ((q = p.next) == null || 602 (q = (p = q).next) == null) { 603 // It is possible that p is NEXT_TERMINATOR, 604 // but if so, the CAS is guaranteed to fail. 605 if (TAIL.compareAndSet(this, t, p)) 606 return; 607 else 608 continue restartFromTail; 609 } 610 else if (t != tail) 611 continue restartFromTail; 612 else 613 p = q; 614 } 615 } 616 } 617 skipDeletedPredecessors(Node<E> x)618 private void skipDeletedPredecessors(Node<E> x) { 619 whileActive: 620 do { 621 Node<E> prev = x.prev; 622 // assert prev != null; 623 // assert x != NEXT_TERMINATOR; 624 // assert x != PREV_TERMINATOR; 625 Node<E> p = prev; 626 findActive: 627 for (;;) { 628 if (p.item != null) 629 break findActive; 630 Node<E> q = p.prev; 631 if (q == null) { 632 if (p.next == p) 633 continue whileActive; 634 break findActive; 635 } 636 else if (p == q) 637 continue whileActive; 638 else 639 p = q; 640 } 641 642 // found active CAS target 643 if (prev == p || PREV.compareAndSet(x, prev, p)) 644 return; 645 646 } while (x.item != null || x.next == null); 647 } 648 skipDeletedSuccessors(Node<E> x)649 private void skipDeletedSuccessors(Node<E> x) { 650 whileActive: 651 do { 652 Node<E> next = x.next; 653 // assert next != null; 654 // assert x != NEXT_TERMINATOR; 655 // assert x != PREV_TERMINATOR; 656 Node<E> p = next; 657 findActive: 658 for (;;) { 659 if (p.item != null) 660 break findActive; 661 Node<E> q = p.next; 662 if (q == null) { 663 if (p.prev == p) 664 continue whileActive; 665 break findActive; 666 } 667 else if (p == q) 668 continue whileActive; 669 else 670 p = q; 671 } 672 673 // found active CAS target 674 if (next == p || NEXT.compareAndSet(x, next, p)) 675 return; 676 677 } while (x.item != null || x.prev == null); 678 } 679 680 /** 681 * Returns the successor of p, or the first node if p.next has been 682 * linked to self, which will only be true if traversing with a 683 * stale pointer that is now off the list. 684 */ succ(Node<E> p)685 final Node<E> succ(Node<E> p) { 686 // TODO: should we skip deleted nodes here? 687 if (p == (p = p.next)) 688 p = first(); 689 return p; 690 } 691 692 /** 693 * Returns the predecessor of p, or the last node if p.prev 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 */ pred(Node<E> p)697 final Node<E> pred(Node<E> p) { 698 if (p == (p = p.prev)) 699 p = last(); 700 return p; 701 } 702 703 /** 704 * Returns the first node, the unique node p for which: 705 * p.prev == null && p.next != p 706 * The returned node may or may not be logically deleted. 707 * Guarantees that head is set to the returned node. 708 */ first()709 Node<E> first() { 710 restartFromHead: 711 for (;;) 712 for (Node<E> h = head, p = h, q;;) { 713 if ((q = p.prev) != null && 714 (q = (p = q).prev) != null) 715 // Check for head updates every other hop. 716 // If p == q, we are sure to follow head instead. 717 p = (h != (h = head)) ? h : q; 718 else if (p == h 719 // It is possible that p is PREV_TERMINATOR, 720 // but if so, the CAS is guaranteed to fail. 721 || HEAD.compareAndSet(this, h, p)) 722 return p; 723 else 724 continue restartFromHead; 725 } 726 } 727 728 /** 729 * Returns the last node, the unique node p for which: 730 * p.next == null && p.prev != p 731 * The returned node may or may not be logically deleted. 732 * Guarantees that tail is set to the returned node. 733 */ last()734 Node<E> last() { 735 restartFromTail: 736 for (;;) 737 for (Node<E> t = tail, p = t, q;;) { 738 if ((q = p.next) != null && 739 (q = (p = q).next) != null) 740 // Check for tail updates every other hop. 741 // If p == q, we are sure to follow tail instead. 742 p = (t != (t = tail)) ? t : q; 743 else if (p == t 744 // It is possible that p is NEXT_TERMINATOR, 745 // but if so, the CAS is guaranteed to fail. 746 || TAIL.compareAndSet(this, t, p)) 747 return p; 748 else 749 continue restartFromTail; 750 } 751 } 752 753 // Minor convenience utilities 754 755 /** 756 * Returns element unless it is null, in which case throws 757 * NoSuchElementException. 758 * 759 * @param v the element 760 * @return the element 761 */ screenNullResult(E v)762 private E screenNullResult(E v) { 763 if (v == null) 764 throw new NoSuchElementException(); 765 return v; 766 } 767 768 /** 769 * Constructs an empty deque. 770 */ ConcurrentLinkedDeque()771 public ConcurrentLinkedDeque() { 772 head = tail = new Node<E>(); 773 } 774 775 /** 776 * Constructs a deque initially containing the elements of 777 * the given collection, added in traversal order of the 778 * collection's iterator. 779 * 780 * @param c the collection of elements to initially contain 781 * @throws NullPointerException if the specified collection or any 782 * of its elements are null 783 */ ConcurrentLinkedDeque(Collection<? extends E> c)784 public ConcurrentLinkedDeque(Collection<? extends E> c) { 785 // Copy c into a private chain of Nodes 786 Node<E> h = null, t = null; 787 for (E e : c) { 788 Node<E> newNode = newNode(Objects.requireNonNull(e)); 789 if (h == null) 790 h = t = newNode; 791 else { 792 NEXT.set(t, newNode); 793 PREV.set(newNode, t); 794 t = newNode; 795 } 796 } 797 initHeadTail(h, t); 798 } 799 800 /** 801 * Initializes head and tail, ensuring invariants hold. 802 */ initHeadTail(Node<E> h, Node<E> t)803 private void initHeadTail(Node<E> h, Node<E> t) { 804 if (h == t) { 805 if (h == null) 806 h = t = new Node<E>(); 807 else { 808 // Avoid edge case of a single Node with non-null item. 809 Node<E> newNode = new Node<E>(); 810 NEXT.set(t, newNode); 811 PREV.set(newNode, t); 812 t = newNode; 813 } 814 } 815 head = h; 816 tail = t; 817 } 818 819 /** 820 * Inserts the specified element at the front of this deque. 821 * As the deque is unbounded, this method will never throw 822 * {@link IllegalStateException}. 823 * 824 * @throws NullPointerException if the specified element is null 825 */ addFirst(E e)826 public void addFirst(E e) { 827 linkFirst(e); 828 } 829 830 /** 831 * Inserts the specified element at the end of this deque. 832 * As the deque is unbounded, this method will never throw 833 * {@link IllegalStateException}. 834 * 835 * <p>This method is equivalent to {@link #add}. 836 * 837 * @throws NullPointerException if the specified element is null 838 */ addLast(E e)839 public void addLast(E e) { 840 linkLast(e); 841 } 842 843 /** 844 * Inserts the specified element at the front of this deque. 845 * As the deque is unbounded, this method will never return {@code false}. 846 * 847 * @return {@code true} (as specified by {@link Deque#offerFirst}) 848 * @throws NullPointerException if the specified element is null 849 */ offerFirst(E e)850 public boolean offerFirst(E e) { 851 linkFirst(e); 852 return true; 853 } 854 855 /** 856 * Inserts the specified element at the end of this deque. 857 * As the deque is unbounded, this method will never return {@code false}. 858 * 859 * <p>This method is equivalent to {@link #add}. 860 * 861 * @return {@code true} (as specified by {@link Deque#offerLast}) 862 * @throws NullPointerException if the specified element is null 863 */ offerLast(E e)864 public boolean offerLast(E e) { 865 linkLast(e); 866 return true; 867 } 868 peekFirst()869 public E peekFirst() { 870 restart: for (;;) { 871 E item; 872 Node<E> first = first(), p = first; 873 while ((item = p.item) == null) { 874 if (p == (p = p.next)) continue restart; 875 if (p == null) 876 break; 877 } 878 // recheck for linearizability 879 if (first.prev != null) continue restart; 880 return item; 881 } 882 } 883 peekLast()884 public E peekLast() { 885 restart: for (;;) { 886 E item; 887 Node<E> last = last(), p = last; 888 while ((item = p.item) == null) { 889 if (p == (p = p.prev)) continue restart; 890 if (p == null) 891 break; 892 } 893 // recheck for linearizability 894 if (last.next != null) continue restart; 895 return item; 896 } 897 } 898 899 /** 900 * @throws NoSuchElementException {@inheritDoc} 901 */ getFirst()902 public E getFirst() { 903 return screenNullResult(peekFirst()); 904 } 905 906 /** 907 * @throws NoSuchElementException {@inheritDoc} 908 */ getLast()909 public E getLast() { 910 return screenNullResult(peekLast()); 911 } 912 pollFirst()913 public E pollFirst() { 914 restart: for (;;) { 915 for (Node<E> first = first(), p = first;;) { 916 final E item; 917 if ((item = p.item) != null) { 918 // recheck for linearizability 919 if (first.prev != null) continue restart; 920 if (ITEM.compareAndSet(p, item, null)) { 921 unlink(p); 922 return item; 923 } 924 } 925 if (p == (p = p.next)) continue restart; 926 if (p == null) { 927 if (first.prev != null) continue restart; 928 return null; 929 } 930 } 931 } 932 } 933 pollLast()934 public E pollLast() { 935 restart: for (;;) { 936 for (Node<E> last = last(), p = last;;) { 937 final E item; 938 if ((item = p.item) != null) { 939 // recheck for linearizability 940 if (last.next != null) continue restart; 941 if (ITEM.compareAndSet(p, item, null)) { 942 unlink(p); 943 return item; 944 } 945 } 946 if (p == (p = p.prev)) continue restart; 947 if (p == null) { 948 if (last.next != null) continue restart; 949 return null; 950 } 951 } 952 } 953 } 954 955 /** 956 * @throws NoSuchElementException {@inheritDoc} 957 */ removeFirst()958 public E removeFirst() { 959 return screenNullResult(pollFirst()); 960 } 961 962 /** 963 * @throws NoSuchElementException {@inheritDoc} 964 */ removeLast()965 public E removeLast() { 966 return screenNullResult(pollLast()); 967 } 968 969 // *** Queue and stack methods *** 970 971 /** 972 * Inserts the specified element at the tail of this deque. 973 * As the deque is unbounded, this method will never return {@code false}. 974 * 975 * @return {@code true} (as specified by {@link Queue#offer}) 976 * @throws NullPointerException if the specified element is null 977 */ offer(E e)978 public boolean offer(E e) { 979 return offerLast(e); 980 } 981 982 /** 983 * Inserts the specified element at the tail of this deque. 984 * As the deque is unbounded, this method will never throw 985 * {@link IllegalStateException} or return {@code false}. 986 * 987 * @return {@code true} (as specified by {@link Collection#add}) 988 * @throws NullPointerException if the specified element is null 989 */ add(E e)990 public boolean add(E e) { 991 return offerLast(e); 992 } 993 poll()994 public E poll() { return pollFirst(); } peek()995 public E peek() { return peekFirst(); } 996 997 /** 998 * @throws NoSuchElementException {@inheritDoc} 999 */ remove()1000 public E remove() { return removeFirst(); } 1001 1002 /** 1003 * @throws NoSuchElementException {@inheritDoc} 1004 */ pop()1005 public E pop() { return removeFirst(); } 1006 1007 /** 1008 * @throws NoSuchElementException {@inheritDoc} 1009 */ element()1010 public E element() { return getFirst(); } 1011 1012 /** 1013 * @throws NullPointerException {@inheritDoc} 1014 */ push(E e)1015 public void push(E e) { addFirst(e); } 1016 1017 /** 1018 * Removes the first occurrence of the specified element from this deque. 1019 * If the deque does not contain the element, it is unchanged. 1020 * More formally, removes the first element {@code e} such that 1021 * {@code o.equals(e)} (if such an element exists). 1022 * Returns {@code true} if this deque contained the specified element 1023 * (or equivalently, if this deque changed as a result of the call). 1024 * 1025 * @param o element to be removed from this deque, if present 1026 * @return {@code true} if the deque contained the specified element 1027 * @throws NullPointerException if the specified element is null 1028 */ removeFirstOccurrence(Object o)1029 public boolean removeFirstOccurrence(Object o) { 1030 Objects.requireNonNull(o); 1031 for (Node<E> p = first(); p != null; p = succ(p)) { 1032 final E item; 1033 if ((item = p.item) != null 1034 && o.equals(item) 1035 && ITEM.compareAndSet(p, item, null)) { 1036 unlink(p); 1037 return true; 1038 } 1039 } 1040 return false; 1041 } 1042 1043 /** 1044 * Removes the last occurrence of the specified element from this deque. 1045 * If the deque does not contain the element, it is unchanged. 1046 * More formally, removes the last element {@code e} such that 1047 * {@code o.equals(e)} (if such an element exists). 1048 * Returns {@code true} if this deque contained the specified element 1049 * (or equivalently, if this deque changed as a result of the call). 1050 * 1051 * @param o element to be removed from this deque, if present 1052 * @return {@code true} if the deque contained the specified element 1053 * @throws NullPointerException if the specified element is null 1054 */ removeLastOccurrence(Object o)1055 public boolean removeLastOccurrence(Object o) { 1056 Objects.requireNonNull(o); 1057 for (Node<E> p = last(); p != null; p = pred(p)) { 1058 final E item; 1059 if ((item = p.item) != null 1060 && o.equals(item) 1061 && ITEM.compareAndSet(p, item, null)) { 1062 unlink(p); 1063 return true; 1064 } 1065 } 1066 return false; 1067 } 1068 1069 /** 1070 * Returns {@code true} if this deque contains the specified element. 1071 * More formally, returns {@code true} if and only if this deque contains 1072 * at least one element {@code e} such that {@code o.equals(e)}. 1073 * 1074 * @param o element whose presence in this deque is to be tested 1075 * @return {@code true} if this deque contains the specified element 1076 */ contains(Object o)1077 public boolean contains(Object o) { 1078 if (o != null) { 1079 for (Node<E> p = first(); p != null; p = succ(p)) { 1080 final E item; 1081 if ((item = p.item) != null && o.equals(item)) 1082 return true; 1083 } 1084 } 1085 return false; 1086 } 1087 1088 /** 1089 * Returns {@code true} if this collection contains no elements. 1090 * 1091 * @return {@code true} if this collection contains no elements 1092 */ isEmpty()1093 public boolean isEmpty() { 1094 return peekFirst() == null; 1095 } 1096 1097 /** 1098 * Returns the number of elements in this deque. If this deque 1099 * contains more than {@code Integer.MAX_VALUE} elements, it 1100 * returns {@code Integer.MAX_VALUE}. 1101 * 1102 * <p>Beware that, unlike in most collections, this method is 1103 * <em>NOT</em> a constant-time operation. Because of the 1104 * asynchronous nature of these deques, determining the current 1105 * number of elements requires traversing them all to count them. 1106 * Additionally, it is possible for the size to change during 1107 * execution of this method, in which case the returned result 1108 * will be inaccurate. Thus, this method is typically not very 1109 * useful in concurrent applications. 1110 * 1111 * @return the number of elements in this deque 1112 */ size()1113 public int size() { 1114 restart: for (;;) { 1115 int count = 0; 1116 for (Node<E> p = first(); p != null;) { 1117 if (p.item != null) 1118 if (++count == Integer.MAX_VALUE) 1119 break; // @see Collection.size() 1120 if (p == (p = p.next)) 1121 continue restart; 1122 } 1123 return count; 1124 } 1125 } 1126 1127 /** 1128 * Removes the first occurrence of the specified element from this deque. 1129 * If the deque does not contain the element, it is unchanged. 1130 * More formally, removes the first element {@code e} such that 1131 * {@code o.equals(e)} (if such an element exists). 1132 * Returns {@code true} if this deque contained the specified element 1133 * (or equivalently, if this deque changed as a result of the call). 1134 * 1135 * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. 1136 * 1137 * @param o element to be removed from this deque, if present 1138 * @return {@code true} if the deque contained the specified element 1139 * @throws NullPointerException if the specified element is null 1140 */ remove(Object o)1141 public boolean remove(Object o) { 1142 return removeFirstOccurrence(o); 1143 } 1144 1145 /** 1146 * Appends all of the elements in the specified collection to the end of 1147 * this deque, in the order that they are returned by the specified 1148 * collection's iterator. Attempts to {@code addAll} of a deque to 1149 * itself result in {@code IllegalArgumentException}. 1150 * 1151 * @param c the elements to be inserted into this deque 1152 * @return {@code true} if this deque changed as a result of the call 1153 * @throws NullPointerException if the specified collection or any 1154 * of its elements are null 1155 * @throws IllegalArgumentException if the collection is this deque 1156 */ addAll(Collection<? extends E> c)1157 public boolean addAll(Collection<? extends E> c) { 1158 if (c == this) 1159 // As historically specified in AbstractQueue#addAll 1160 throw new IllegalArgumentException(); 1161 1162 // Copy c into a private chain of Nodes 1163 Node<E> beginningOfTheEnd = null, last = null; 1164 for (E e : c) { 1165 Node<E> newNode = newNode(Objects.requireNonNull(e)); 1166 if (beginningOfTheEnd == null) 1167 beginningOfTheEnd = last = newNode; 1168 else { 1169 NEXT.set(last, newNode); 1170 PREV.set(newNode, last); 1171 last = newNode; 1172 } 1173 } 1174 if (beginningOfTheEnd == null) 1175 return false; 1176 1177 // Atomically append the chain at the tail of this collection 1178 restartFromTail: 1179 for (;;) 1180 for (Node<E> t = tail, p = t, q;;) { 1181 if ((q = p.next) != null && 1182 (q = (p = q).next) != null) 1183 // Check for tail updates every other hop. 1184 // If p == q, we are sure to follow tail instead. 1185 p = (t != (t = tail)) ? t : q; 1186 else if (p.prev == p) // NEXT_TERMINATOR 1187 continue restartFromTail; 1188 else { 1189 // p is last node 1190 PREV.set(beginningOfTheEnd, p); // CAS piggyback 1191 if (NEXT.compareAndSet(p, null, beginningOfTheEnd)) { 1192 // Successful CAS is the linearization point 1193 // for all elements to be added to this deque. 1194 if (!TAIL.weakCompareAndSet(this, t, last)) { 1195 // Try a little harder to update tail, 1196 // since we may be adding many elements. 1197 t = tail; 1198 if (last.next == null) 1199 TAIL.weakCompareAndSet(this, t, last); 1200 } 1201 return true; 1202 } 1203 // Lost CAS race to another thread; re-read next 1204 } 1205 } 1206 } 1207 1208 /** 1209 * Removes all of the elements from this deque. 1210 */ clear()1211 public void clear() { 1212 while (pollFirst() != null) 1213 ; 1214 } 1215 toString()1216 public String toString() { 1217 String[] a = null; 1218 restart: for (;;) { 1219 int charLength = 0; 1220 int size = 0; 1221 for (Node<E> p = first(); p != null;) { 1222 final E item; 1223 if ((item = p.item) != null) { 1224 if (a == null) 1225 a = new String[4]; 1226 else if (size == a.length) 1227 a = Arrays.copyOf(a, 2 * size); 1228 String s = item.toString(); 1229 a[size++] = s; 1230 charLength += s.length(); 1231 } 1232 if (p == (p = p.next)) 1233 continue restart; 1234 } 1235 1236 if (size == 0) 1237 return "[]"; 1238 1239 return Helpers.toString(a, size, charLength); 1240 } 1241 } 1242 toArrayInternal(Object[] a)1243 private Object[] toArrayInternal(Object[] a) { 1244 Object[] x = a; 1245 restart: for (;;) { 1246 int size = 0; 1247 for (Node<E> p = first(); p != null;) { 1248 final E item; 1249 if ((item = p.item) != null) { 1250 if (x == null) 1251 x = new Object[4]; 1252 else if (size == x.length) 1253 x = Arrays.copyOf(x, 2 * (size + 4)); 1254 x[size++] = item; 1255 } 1256 if (p == (p = p.next)) 1257 continue restart; 1258 } 1259 if (x == null) 1260 return new Object[0]; 1261 else if (a != null && size <= a.length) { 1262 if (a != x) 1263 System.arraycopy(x, 0, a, 0, size); 1264 if (size < a.length) 1265 a[size] = null; 1266 return a; 1267 } 1268 return (size == x.length) ? x : Arrays.copyOf(x, size); 1269 } 1270 } 1271 1272 /** 1273 * Returns an array containing all of the elements in this deque, in 1274 * proper sequence (from first to last element). 1275 * 1276 * <p>The returned array will be "safe" in that no references to it are 1277 * maintained by this deque. (In other words, this method must allocate 1278 * a new array). The caller is thus free to modify the returned array. 1279 * 1280 * <p>This method acts as bridge between array-based and collection-based 1281 * APIs. 1282 * 1283 * @return an array containing all of the elements in this deque 1284 */ toArray()1285 public Object[] toArray() { 1286 return toArrayInternal(null); 1287 } 1288 1289 /** 1290 * Returns an array containing all of the elements in this deque, 1291 * in proper sequence (from first to last element); the runtime 1292 * type of the returned array is that of the specified array. If 1293 * the deque fits in the specified array, it is returned therein. 1294 * Otherwise, a new array is allocated with the runtime type of 1295 * the specified array and the size of this deque. 1296 * 1297 * <p>If this deque fits in the specified array with room to spare 1298 * (i.e., the array has more elements than this deque), the element in 1299 * the array immediately following the end of the deque is set to 1300 * {@code null}. 1301 * 1302 * <p>Like the {@link #toArray()} method, this method acts as 1303 * bridge between array-based and collection-based APIs. Further, 1304 * this method allows precise control over the runtime type of the 1305 * output array, and may, under certain circumstances, be used to 1306 * save allocation costs. 1307 * 1308 * <p>Suppose {@code x} is a deque known to contain only strings. 1309 * The following code can be used to dump the deque into a newly 1310 * allocated array of {@code String}: 1311 * 1312 * <pre> {@code String[] y = x.toArray(new String[0]);}</pre> 1313 * 1314 * Note that {@code toArray(new Object[0])} is identical in function to 1315 * {@code toArray()}. 1316 * 1317 * @param a the array into which the elements of the deque are to 1318 * be stored, if it is big enough; otherwise, a new array of the 1319 * same runtime type is allocated for this purpose 1320 * @return an array containing all of the elements in this deque 1321 * @throws ArrayStoreException if the runtime type of the specified array 1322 * is not a supertype of the runtime type of every element in 1323 * this deque 1324 * @throws NullPointerException if the specified array is null 1325 */ 1326 @SuppressWarnings("unchecked") toArray(T[] a)1327 public <T> T[] toArray(T[] a) { 1328 if (a == null) throw new NullPointerException(); 1329 return (T[]) toArrayInternal(a); 1330 } 1331 1332 /** 1333 * Returns an iterator over the elements in this deque in proper sequence. 1334 * The elements will be returned in order from first (head) to last (tail). 1335 * 1336 * <p>The returned iterator is 1337 * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. 1338 * 1339 * @return an iterator over the elements in this deque in proper sequence 1340 */ iterator()1341 public Iterator<E> iterator() { 1342 return new Itr(); 1343 } 1344 1345 /** 1346 * Returns an iterator over the elements in this deque in reverse 1347 * sequential order. The elements will be returned in order from 1348 * last (tail) to first (head). 1349 * 1350 * <p>The returned iterator is 1351 * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. 1352 * 1353 * @return an iterator over the elements in this deque in reverse order 1354 */ descendingIterator()1355 public Iterator<E> descendingIterator() { 1356 return new DescendingItr(); 1357 } 1358 1359 private abstract class AbstractItr implements Iterator<E> { 1360 /** 1361 * Next node to return item for. 1362 */ 1363 private Node<E> nextNode; 1364 1365 /** 1366 * nextItem holds on to item fields because once we claim 1367 * that an element exists in hasNext(), we must return it in 1368 * the following next() call even if it was in the process of 1369 * being removed when hasNext() was called. 1370 */ 1371 private E nextItem; 1372 1373 /** 1374 * Node returned by most recent call to next. Needed by remove. 1375 * Reset to null if this element is deleted by a call to remove. 1376 */ 1377 private Node<E> lastRet; 1378 startNode()1379 abstract Node<E> startNode(); nextNode(Node<E> p)1380 abstract Node<E> nextNode(Node<E> p); 1381 AbstractItr()1382 AbstractItr() { 1383 advance(); 1384 } 1385 1386 /** 1387 * Sets nextNode and nextItem to next valid node, or to null 1388 * if no such. 1389 */ advance()1390 private void advance() { 1391 lastRet = nextNode; 1392 1393 Node<E> p = (nextNode == null) ? startNode() : nextNode(nextNode); 1394 for (;; p = nextNode(p)) { 1395 if (p == null) { 1396 // might be at active end or TERMINATOR node; both are OK 1397 nextNode = null; 1398 nextItem = null; 1399 break; 1400 } 1401 final E item; 1402 if ((item = p.item) != null) { 1403 nextNode = p; 1404 nextItem = item; 1405 break; 1406 } 1407 } 1408 } 1409 hasNext()1410 public boolean hasNext() { 1411 return nextItem != null; 1412 } 1413 next()1414 public E next() { 1415 E item = nextItem; 1416 if (item == null) throw new NoSuchElementException(); 1417 advance(); 1418 return item; 1419 } 1420 remove()1421 public void remove() { 1422 Node<E> l = lastRet; 1423 if (l == null) throw new IllegalStateException(); 1424 l.item = null; 1425 unlink(l); 1426 lastRet = null; 1427 } 1428 } 1429 1430 /** Forward iterator */ 1431 private class Itr extends AbstractItr { Itr()1432 Itr() {} // prevent access constructor creation startNode()1433 Node<E> startNode() { return first(); } nextNode(Node<E> p)1434 Node<E> nextNode(Node<E> p) { return succ(p); } 1435 } 1436 1437 /** Descending iterator */ 1438 private class DescendingItr extends AbstractItr { DescendingItr()1439 DescendingItr() {} // prevent access constructor creation startNode()1440 Node<E> startNode() { return last(); } nextNode(Node<E> p)1441 Node<E> nextNode(Node<E> p) { return pred(p); } 1442 } 1443 1444 /** A customized variant of Spliterators.IteratorSpliterator */ 1445 final class CLDSpliterator implements Spliterator<E> { 1446 static final int MAX_BATCH = 1 << 25; // max batch array size; 1447 Node<E> current; // current node; null until initialized 1448 int batch; // batch size for splits 1449 boolean exhausted; // true when no more nodes 1450 trySplit()1451 public Spliterator<E> trySplit() { 1452 Node<E> p, q; 1453 if ((p = current()) == null || (q = p.next) == null) 1454 return null; 1455 int i = 0, n = batch = Math.min(batch + 1, MAX_BATCH); 1456 Object[] a = null; 1457 do { 1458 final E e; 1459 if ((e = p.item) != null) { 1460 if (a == null) 1461 a = new Object[n]; 1462 a[i++] = e; 1463 } 1464 if (p == (p = q)) 1465 p = first(); 1466 } while (p != null && (q = p.next) != null && i < n); 1467 setCurrent(p); 1468 return (i == 0) ? null : 1469 Spliterators.spliterator(a, 0, i, (Spliterator.ORDERED | 1470 Spliterator.NONNULL | 1471 Spliterator.CONCURRENT)); 1472 } 1473 forEachRemaining(Consumer<? super E> action)1474 public void forEachRemaining(Consumer<? super E> action) { 1475 Objects.requireNonNull(action); 1476 Node<E> p; 1477 if ((p = current()) != null) { 1478 current = null; 1479 exhausted = true; 1480 do { 1481 final E e; 1482 if ((e = p.item) != null) 1483 action.accept(e); 1484 if (p == (p = p.next)) 1485 p = first(); 1486 } while (p != null); 1487 } 1488 } 1489 tryAdvance(Consumer<? super E> action)1490 public boolean tryAdvance(Consumer<? super E> action) { 1491 Objects.requireNonNull(action); 1492 Node<E> p; 1493 if ((p = current()) != null) { 1494 E e; 1495 do { 1496 e = p.item; 1497 if (p == (p = p.next)) 1498 p = first(); 1499 } while (e == null && p != null); 1500 setCurrent(p); 1501 if (e != null) { 1502 action.accept(e); 1503 return true; 1504 } 1505 } 1506 return false; 1507 } 1508 setCurrent(Node<E> p)1509 private void setCurrent(Node<E> p) { 1510 if ((current = p) == null) 1511 exhausted = true; 1512 } 1513 current()1514 private Node<E> current() { 1515 Node<E> p; 1516 if ((p = current) == null && !exhausted) 1517 setCurrent(p = first()); 1518 return p; 1519 } 1520 estimateSize()1521 public long estimateSize() { return Long.MAX_VALUE; } 1522 characteristics()1523 public int characteristics() { 1524 return (Spliterator.ORDERED | 1525 Spliterator.NONNULL | 1526 Spliterator.CONCURRENT); 1527 } 1528 } 1529 1530 /** 1531 * Returns a {@link Spliterator} over the elements in this deque. 1532 * 1533 * <p>The returned spliterator is 1534 * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. 1535 * 1536 * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT}, 1537 * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}. 1538 * 1539 * @implNote 1540 * The {@code Spliterator} implements {@code trySplit} to permit limited 1541 * parallelism. 1542 * 1543 * @return a {@code Spliterator} over the elements in this deque 1544 * @since 1.8 1545 */ spliterator()1546 public Spliterator<E> spliterator() { 1547 return new CLDSpliterator(); 1548 } 1549 1550 /** 1551 * Saves this deque to a stream (that is, serializes it). 1552 * 1553 * @param s the stream 1554 * @throws java.io.IOException if an I/O error occurs 1555 * @serialData All of the elements (each an {@code E}) in 1556 * the proper order, followed by a null 1557 */ writeObject(java.io.ObjectOutputStream s)1558 private void writeObject(java.io.ObjectOutputStream s) 1559 throws java.io.IOException { 1560 1561 // Write out any hidden stuff 1562 s.defaultWriteObject(); 1563 1564 // Write out all elements in the proper order. 1565 for (Node<E> p = first(); p != null; p = succ(p)) { 1566 final E item; 1567 if ((item = p.item) != null) 1568 s.writeObject(item); 1569 } 1570 1571 // Use trailing null as sentinel 1572 s.writeObject(null); 1573 } 1574 1575 /** 1576 * Reconstitutes this deque from a stream (that is, deserializes it). 1577 * @param s the stream 1578 * @throws ClassNotFoundException if the class of a serialized object 1579 * could not be found 1580 * @throws java.io.IOException if an I/O error occurs 1581 */ readObject(java.io.ObjectInputStream s)1582 private void readObject(java.io.ObjectInputStream s) 1583 throws java.io.IOException, ClassNotFoundException { 1584 s.defaultReadObject(); 1585 1586 // Read in elements until trailing null sentinel found 1587 Node<E> h = null, t = null; 1588 for (Object item; (item = s.readObject()) != null; ) { 1589 @SuppressWarnings("unchecked") 1590 Node<E> newNode = newNode((E) item); 1591 if (h == null) 1592 h = t = newNode; 1593 else { 1594 NEXT.set(t, newNode); 1595 PREV.set(newNode, t); 1596 t = newNode; 1597 } 1598 } 1599 initHeadTail(h, t); 1600 } 1601 1602 /** 1603 * @throws NullPointerException {@inheritDoc} 1604 */ removeIf(Predicate<? super E> filter)1605 public boolean removeIf(Predicate<? super E> filter) { 1606 Objects.requireNonNull(filter); 1607 return bulkRemove(filter); 1608 } 1609 1610 /** 1611 * @throws NullPointerException {@inheritDoc} 1612 */ removeAll(Collection<?> c)1613 public boolean removeAll(Collection<?> c) { 1614 Objects.requireNonNull(c); 1615 return bulkRemove(e -> c.contains(e)); 1616 } 1617 1618 /** 1619 * @throws NullPointerException {@inheritDoc} 1620 */ retainAll(Collection<?> c)1621 public boolean retainAll(Collection<?> c) { 1622 Objects.requireNonNull(c); 1623 return bulkRemove(e -> !c.contains(e)); 1624 } 1625 1626 /** Implementation of bulk remove methods. */ bulkRemove(Predicate<? super E> filter)1627 private boolean bulkRemove(Predicate<? super E> filter) { 1628 boolean removed = false; 1629 for (Node<E> p = first(), succ; p != null; p = succ) { 1630 succ = succ(p); 1631 final E item; 1632 if ((item = p.item) != null 1633 && filter.test(item) 1634 && ITEM.compareAndSet(p, item, null)) { 1635 unlink(p); 1636 removed = true; 1637 } 1638 } 1639 return removed; 1640 } 1641 1642 /** 1643 * @throws NullPointerException {@inheritDoc} 1644 */ forEach(Consumer<? super E> action)1645 public void forEach(Consumer<? super E> action) { 1646 Objects.requireNonNull(action); 1647 E item; 1648 for (Node<E> p = first(); p != null; p = succ(p)) 1649 if ((item = p.item) != null) 1650 action.accept(item); 1651 } 1652 1653 // VarHandle mechanics 1654 private static final VarHandle HEAD; 1655 private static final VarHandle TAIL; 1656 private static final VarHandle PREV; 1657 private static final VarHandle NEXT; 1658 private static final VarHandle ITEM; 1659 static { 1660 PREV_TERMINATOR = new Node<Object>(); 1661 PREV_TERMINATOR.next = PREV_TERMINATOR; 1662 NEXT_TERMINATOR = new Node<Object>(); 1663 NEXT_TERMINATOR.prev = NEXT_TERMINATOR; 1664 try { 1665 MethodHandles.Lookup l = MethodHandles.lookup(); 1666 HEAD = l.findVarHandle(ConcurrentLinkedDeque.class, "head", 1667 Node.class); 1668 TAIL = l.findVarHandle(ConcurrentLinkedDeque.class, "tail", 1669 Node.class); 1670 PREV = l.findVarHandle(Node.class, "prev", Node.class); 1671 NEXT = l.findVarHandle(Node.class, "next", Node.class); 1672 ITEM = l.findVarHandle(Node.class, "item", Object.class); 1673 } catch (ReflectiveOperationException e) { 1674 throw new ExceptionInInitializerError(e); 1675 } 1676 } 1677 } 1678