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