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