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