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