jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java
author smarks
Tue, 06 Dec 2011 10:14:02 -0800
changeset 11139 db0c2ff5e1ea
parent 9515 04056ec0f477
child 11279 d9dab5ec5044
permissions -rw-r--r--
7116997: fix warnings in java.util.PropertyPermission Reviewed-by: smarks Contributed-by: Brandon Passanisi <brandon.passanisi@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    31
 * Written by Doug Lea and Martin Buchholz with assistance from members of
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
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/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    38
import java.util.AbstractQueue;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    39
import java.util.ArrayList;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    40
import java.util.Collection;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    41
import java.util.Iterator;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    42
import java.util.NoSuchElementException;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    43
import java.util.Queue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * An unbounded thread-safe {@linkplain Queue queue} based on linked nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This queue orders elements FIFO (first-in-first-out).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The <em>head</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * queue the longest time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The <em>tail</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * queue the shortest time. New elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * are inserted at the tail of the queue, and the queue retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * operations obtain elements at the head of the queue.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    54
 * A {@code ConcurrentLinkedQueue} is an appropriate choice when
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * many threads will share access to a common collection.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    56
 * Like most other concurrent collection implementations, this class
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    57
 * does not permit the use of {@code null} elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>This implementation employs an efficient &quot;wait-free&quot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * algorithm based on one described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * href="http://www.cs.rochester.edu/u/michael/PODC96.html"> Simple,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Fast, and Practical Non-Blocking and Blocking Concurrent Queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Algorithms</a> by Maged M. Michael and Michael L. Scott.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    65
 * <p>Iterators are <i>weakly consistent</i>, returning elements
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    66
 * reflecting the state of the queue at some point at or since the
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    67
 * creation of the iterator.  They do <em>not</em> throw {@link
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 6672
diff changeset
    68
 * java.util.ConcurrentModificationException}, and may proceed concurrently
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 6672
diff changeset
    69
 * with other operations.  Elements contained in the queue since the creation
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    70
 * of the iterator will be returned exactly once.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    71
 *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    72
 * <p>Beware that, unlike in most collections, the {@code size} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * is <em>NOT</em> a constant-time operation. Because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * asynchronous nature of these queues, determining the current number
9515
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    75
 * 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
    76
 * inaccurate results if this collection is modified during traversal.
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    77
 * Additionally, the bulk operations {@code addAll},
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    78
 * {@code removeAll}, {@code retainAll}, {@code containsAll},
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    79
 * {@code equals}, and {@code toArray} are <em>not</em> guaranteed
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    80
 * to be performed atomically. For example, an iterator operating
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    81
 * concurrently with an {@code addAll} operation might view only some
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    82
 * of the added elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    84
 * <p>This class and its iterator implement all of the <em>optional</em>
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    85
 * methods of the {@link Queue} and {@link Iterator} interfaces.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>Memory consistency effects: As with other concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * collections, actions in a thread prior to placing an object into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * {@code ConcurrentLinkedQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * actions subsequent to the access or removal of that element from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * the {@code ConcurrentLinkedQueue} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        implements Queue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final long serialVersionUID = 196745693267521676L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /*
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   108
     * This is a modification of the Michael & Scott algorithm,
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   109
     * adapted for a garbage-collected environment, with support for
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   110
     * interior node deletion (to support remove(Object)).  For
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   111
     * explanation, read the paper.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   113
     * Note that like most non-blocking algorithms in this package,
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   114
     * this implementation relies on the fact that in garbage
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * collected systems, there is no possibility of ABA problems due
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * to recycled nodes, so there is no need to use "counted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * pointers" or related techniques seen in versions used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * non-GC'ed settings.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   119
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   120
     * The fundamental invariants are:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   121
     * - There is exactly one (last) Node with a null next reference,
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   122
     *   which is CASed when enqueueing.  This last Node can be
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   123
     *   reached in O(1) time from tail, but tail is merely an
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   124
     *   optimization - it can always be reached in O(N) time from
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   125
     *   head as well.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   126
     * - The elements contained in the queue are the non-null items in
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   127
     *   Nodes that are reachable from head.  CASing the item
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   128
     *   reference of a Node to null atomically removes it from the
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   129
     *   queue.  Reachability of all elements from head must remain
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   130
     *   true even in the case of concurrent modifications that cause
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   131
     *   head to advance.  A dequeued Node may remain in use
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   132
     *   indefinitely due to creation of an Iterator or simply a
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   133
     *   poll() that has lost its time slice.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   134
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   135
     * The above might appear to imply that all Nodes are GC-reachable
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   136
     * from a predecessor dequeued Node.  That would cause two problems:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   137
     * - allow a rogue Iterator to cause unbounded memory retention
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   138
     * - cause cross-generational linking of old Nodes to new Nodes if
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   139
     *   a Node was tenured while live, which generational GCs have a
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   140
     *   hard time dealing with, causing repeated major collections.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   141
     * However, only non-deleted Nodes need to be reachable from
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   142
     * dequeued Nodes, and reachability does not necessarily have to
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   143
     * be of the kind understood by the GC.  We use the trick of
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   144
     * linking a Node that has just been dequeued to itself.  Such a
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   145
     * self-link implicitly means to advance to head.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   146
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   147
     * Both head and tail are permitted to lag.  In fact, failing to
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   148
     * update them every time one could is a significant optimization
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   149
     * (fewer CASes). As with LinkedTransferQueue (see the internal
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   150
     * documentation for that class), we use a slack threshold of two;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   151
     * that is, we update head/tail when the current pointer appears
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   152
     * to be two or more steps away from the first/last node.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   153
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   154
     * Since head and tail are updated concurrently and independently,
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   155
     * it is possible for tail to lag behind head (why not)?
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   156
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   157
     * CASing a Node's item reference to null atomically removes the
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   158
     * element from the queue.  Iterators skip over Nodes with null
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   159
     * items.  Prior implementations of this class had a race between
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   160
     * poll() and remove(Object) where the same element would appear
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   161
     * to be successfully removed by two concurrent operations.  The
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   162
     * method remove(Object) also lazily unlinks deleted Nodes, but
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   163
     * this is merely an optimization.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   164
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   165
     * When constructing a Node (before enqueuing it) we avoid paying
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   166
     * for a volatile write to item by using Unsafe.putObject instead
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   167
     * of a normal write.  This allows the cost of enqueue to be
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   168
     * "one-and-a-half" CASes.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   169
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   170
     * Both head and tail may or may not point to a Node with a
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   171
     * non-null item.  If the queue is empty, all items must of course
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   172
     * be null.  Upon creation, both head and tail refer to a dummy
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   173
     * Node with null item.  Both head and tail are only updated using
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   174
     * CAS, so they never regress, although again this is merely an
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   175
     * optimization.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static class Node<E> {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   179
        volatile E item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   180
        volatile Node<E> next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   182
        /**
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   183
         * Constructs a new node.  Uses relaxed write because item can
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   184
         * only be seen after publication via casNext.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   185
         */
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   186
        Node(E item) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   187
            UNSAFE.putObject(this, itemOffset, item);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        boolean casItem(E cmp, E val) {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   191
            return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   194
        void lazySetNext(Node<E> val) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   195
            UNSAFE.putOrderedObject(this, nextOffset, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        boolean casNext(Node<E> cmp, Node<E> val) {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   199
            return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   202
        // Unsafe mechanics
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   204
        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
   205
        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
   206
        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
   207
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   208
        static {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   209
            try {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   210
                UNSAFE = sun.misc.Unsafe.getUnsafe();
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   211
                Class k = Node.class;
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   212
                itemOffset = UNSAFE.objectFieldOffset
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   213
                    (k.getDeclaredField("item"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   214
                nextOffset = UNSAFE.objectFieldOffset
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   215
                    (k.getDeclaredField("next"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   216
            } catch (Exception e) {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   217
                throw new Error(e);
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   218
            }
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   219
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   222
    /**
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   223
     * A node from which the first live (non-deleted) node (if any)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   224
     * can be reached in O(1) time.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   225
     * Invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   226
     * - all live nodes are reachable from head via succ()
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   227
     * - head != null
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   228
     * - (tmp = head).next != tmp || tmp != head
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   229
     * Non-invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   230
     * - head.item may or may not be null.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   231
     * - it is permitted for tail to lag behind head, that is, for tail
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   232
     *   to not be reachable from head!
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   233
     */
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   234
    private transient volatile Node<E> head;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   237
     * A node from which the last node on list (that is, the unique
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   238
     * node with node.next == null) can be reached in O(1) time.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   239
     * Invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   240
     * - the last node is always reachable from tail via succ()
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   241
     * - tail != null
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   242
     * Non-invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   243
     * - tail.item may or may not be null.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   244
     * - it is permitted for tail to lag behind head, that is, for tail
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   245
     *   to not be reachable from head!
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   246
     * - tail.next may or may not be self-pointing to tail.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   248
    private transient volatile Node<E> tail;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   252
     * Creates a {@code ConcurrentLinkedQueue} that is initially empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   254
    public ConcurrentLinkedQueue() {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   255
        head = tail = new Node<E>(null);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   256
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   259
     * Creates a {@code ConcurrentLinkedQueue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * initially containing the elements of the given collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * added in traversal order of the collection's iterator.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   262
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param c the collection of elements to initially contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public ConcurrentLinkedQueue(Collection<? extends E> c) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   268
        Node<E> h = null, t = null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   269
        for (E e : c) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   270
            checkNotNull(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   271
            Node<E> newNode = new Node<E>(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   272
            if (h == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   273
                h = t = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   274
            else {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   275
                t.lazySetNext(newNode);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   276
                t = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   277
            }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   278
        }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   279
        if (h == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   280
            h = t = new Node<E>(null);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   281
        head = h;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   282
        tail = t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    // Have to override just to update the javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Inserts the specified element at the tail of this queue.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7518
diff changeset
   289
     * As the queue is unbounded, this method will never throw
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7518
diff changeset
   290
     * {@link IllegalStateException} or return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   292
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   300
     * Try to CAS head to p. If successful, repoint old head to itself
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   301
     * as sentinel for succ(), below.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   302
     */
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   303
    final void updateHead(Node<E> h, Node<E> p) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   304
        if (h != p && casHead(h, p))
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   305
            h.lazySetNext(h);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   306
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   307
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   308
    /**
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   309
     * Returns the successor of p, or the head node if p.next has been
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   310
     * linked to self, which will only be true if traversing with a
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   311
     * stale pointer that is now off the list.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   312
     */
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   313
    final Node<E> succ(Node<E> p) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   314
        Node<E> next = p.next;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   315
        return (p == next) ? head : next;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   316
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   317
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   318
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Inserts the specified element at the tail of this queue.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7518
diff changeset
   320
     * As the queue is unbounded, this method will never return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   322
     * @return {@code true} (as specified by {@link Queue#offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public boolean offer(E e) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   326
        checkNotNull(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   327
        final Node<E> newNode = new Node<E>(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   328
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   329
        for (Node<E> t = tail, p = t;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   330
            Node<E> q = p.next;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   331
            if (q == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   332
                // p is last node
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   333
                if (p.casNext(null, newNode)) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   334
                    // Successful CAS is the linearization point
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   335
                    // for e to become an element of this queue,
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   336
                    // and for newNode to become "live".
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   337
                    if (p != t) // hop two nodes at a time
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   338
                        casTail(t, newNode);  // Failure is OK.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   339
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   341
                // Lost CAS race to another thread; re-read next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   343
            else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   344
                // We have fallen off list.  If tail is unchanged, it
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   345
                // will also be off-list, in which case we need to
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   346
                // jump to head, from which all live nodes are always
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   347
                // reachable.  Else the new tail is a better bet.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   348
                p = (t != (t = tail)) ? t : head;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   349
            else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   350
                // Check for tail updates after two hops.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   351
                p = (p != t && t != (t = tail)) ? t : q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public E poll() {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   356
        restartFromHead:
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   357
        for (;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   358
            for (Node<E> h = head, p = h, q;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   359
                E item = p.item;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   360
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   361
                if (item != null && p.casItem(item, null)) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   362
                    // Successful CAS is the linearization point
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   363
                    // for item to be removed from this queue.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   364
                    if (p != h) // hop two nodes at a time
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   365
                        updateHead(h, ((q = p.next) != null) ? q : p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   366
                    return item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   368
                else if ((q = p.next) == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   369
                    updateHead(h, p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   370
                    return null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   371
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   372
                else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   373
                    continue restartFromHead;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   374
                else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   375
                    p = q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   380
    public E peek() {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   381
        restartFromHead:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        for (;;) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   383
            for (Node<E> h = head, p = h, q;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   384
                E item = p.item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   385
                if (item != null || (q = p.next) == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   386
                    updateHead(h, p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   387
                    return item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   388
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   389
                else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   390
                    continue restartFromHead;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   391
                else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   392
                    p = q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   398
     * Returns the first live (non-deleted) node on list, or null if none.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   399
     * This is yet another variant of poll/peek; here returning the
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   400
     * first node, not element.  We could make peek() a wrapper around
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   401
     * first(), but that would cost an extra volatile read of item,
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   402
     * and the need to add a retry loop to deal with the possibility
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   403
     * of losing a race to a concurrent poll().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    Node<E> first() {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   406
        restartFromHead:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        for (;;) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   408
            for (Node<E> h = head, p = h, q;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   409
                boolean hasItem = (p.item != null);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   410
                if (hasItem || (q = p.next) == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   411
                    updateHead(h, p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   412
                    return hasItem ? p : null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   413
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   414
                else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   415
                    continue restartFromHead;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   416
                else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   417
                    p = q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   423
     * Returns {@code true} if this queue contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   425
     * @return {@code true} if this queue contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return first() == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Returns the number of elements in this queue.  If this queue
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   433
     * contains more than {@code Integer.MAX_VALUE} elements, returns
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   434
     * {@code Integer.MAX_VALUE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <p>Beware that, unlike in most collections, this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <em>NOT</em> a constant-time operation. Because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * asynchronous nature of these queues, determining the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * number of elements requires an O(n) traversal.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   440
     * Additionally, if elements are added or removed during execution
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   441
     * of this method, the returned result may be inaccurate.  Thus,
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   442
     * this method is typically not very useful in concurrent
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   443
     * applications.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @return the number of elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        int count = 0;
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   449
        for (Node<E> p = first(); p != null; p = succ(p))
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   450
            if (p.item != null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   451
                // Collection.size() spec says to max out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (++count == Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   458
     * Returns {@code true} if this queue contains the specified element.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   459
     * More formally, returns {@code true} if and only if this queue contains
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   460
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param o object to be checked for containment in this queue
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   463
     * @return {@code true} if this queue contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (o == null) return false;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   467
        for (Node<E> p = first(); p != null; p = succ(p)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   468
            E item = p.item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   469
            if (item != null && o.equals(item))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Removes a single instance of the specified element from this queue,
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   477
     * if it is present.  More formally, removes an element {@code e} such
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   478
     * that {@code o.equals(e)}, if this queue contains one or more such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * elements.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   480
     * Returns {@code true} if this queue contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @param o element to be removed from this queue, if present
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   484
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (o == null) return false;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   488
        Node<E> pred = null;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   489
        for (Node<E> p = first(); p != null; p = succ(p)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   490
            E item = p.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (item != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                o.equals(item) &&
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   493
                p.casItem(item, null)) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   494
                Node<E> next = succ(p);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   495
                if (pred != null && next != null)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   496
                    pred.casNext(p, next);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                return true;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   498
            }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   499
            pred = p;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   505
     * Appends all of the elements in the specified collection to the end of
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   506
     * this queue, in the order that they are returned by the specified
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   507
     * collection's iterator.  Attempts to {@code addAll} of a queue to
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   508
     * itself result in {@code IllegalArgumentException}.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   509
     *
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   510
     * @param c the elements to be inserted into this queue
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   511
     * @return {@code true} if this queue changed as a result of the call
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   512
     * @throws NullPointerException if the specified collection or any
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   513
     *         of its elements are null
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   514
     * @throws IllegalArgumentException if the collection is this queue
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   515
     */
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   516
    public boolean addAll(Collection<? extends E> c) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   517
        if (c == this)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   518
            // As historically specified in AbstractQueue#addAll
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   519
            throw new IllegalArgumentException();
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   520
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   521
        // Copy c into a private chain of Nodes
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   522
        Node<E> beginningOfTheEnd = null, last = null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   523
        for (E e : c) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   524
            checkNotNull(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   525
            Node<E> newNode = new Node<E>(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   526
            if (beginningOfTheEnd == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   527
                beginningOfTheEnd = last = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   528
            else {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   529
                last.lazySetNext(newNode);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   530
                last = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   531
            }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   532
        }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   533
        if (beginningOfTheEnd == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   534
            return false;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   535
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   536
        // Atomically append the chain at the tail of this collection
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   537
        for (Node<E> t = tail, p = t;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   538
            Node<E> q = p.next;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   539
            if (q == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   540
                // p is last node
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   541
                if (p.casNext(null, beginningOfTheEnd)) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   542
                    // Successful CAS is the linearization point
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   543
                    // for all elements to be added to this queue.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   544
                    if (!casTail(t, last)) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   545
                        // Try a little harder to update tail,
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   546
                        // since we may be adding many elements.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   547
                        t = tail;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   548
                        if (last.next == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   549
                            casTail(t, last);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   550
                    }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   551
                    return true;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   552
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   553
                // Lost CAS race to another thread; re-read next
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   554
            }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   555
            else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   556
                // We have fallen off list.  If tail is unchanged, it
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   557
                // will also be off-list, in which case we need to
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   558
                // jump to head, from which all live nodes are always
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   559
                // reachable.  Else the new tail is a better bet.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   560
                p = (t != (t = tail)) ? t : head;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   561
            else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   562
                // Check for tail updates after two hops.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   563
                p = (p != t && t != (t = tail)) ? t : q;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   564
        }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   565
    }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   566
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   567
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // Use ArrayList to deal with resizing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        ArrayList<E> al = new ArrayList<E>();
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   583
        for (Node<E> p = first(); p != null; p = succ(p)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   584
            E item = p.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            if (item != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                al.add(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return al.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * the specified array.  If the queue fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * runtime type of the specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * the array immediately following the end of the queue is set to
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   601
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   608
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * The following code can be used to dump the queue into a newly
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   610
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *     String[] y = x.toArray(new String[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   615
     * Note that {@code toArray(new Object[0])} is identical in function to
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   616
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   627
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        // try to use sent-in array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        Node<E> p;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   632
        for (p = first(); p != null && k < a.length; p = succ(p)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   633
            E item = p.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if (item != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                a[k++] = (T)item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            if (k < a.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                a[k] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // If won't fit, use ArrayList version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        ArrayList<E> al = new ArrayList<E>();
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   645
        for (Node<E> q = first(); q != null; q = succ(q)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   646
            E item = q.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            if (item != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                al.add(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return al.toArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Returns an iterator over the elements in this queue in proper sequence.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   655
     * The elements will be returned in order from first (head) to last (tail).
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   656
     *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 6672
diff changeset
   657
     * <p>The returned iterator is a "weakly consistent" iterator that
3419
3ae6dc68c20d 6866554: Misc. javadoc warnings
martin
parents: 3414
diff changeset
   658
     * will never throw {@link java.util.ConcurrentModificationException
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 6672
diff changeset
   659
     * ConcurrentModificationException}, and guarantees to traverse
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 6672
diff changeset
   660
     * elements as they existed upon construction of the iterator, and
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 6672
diff changeset
   661
     * may (but is not guaranteed to) reflect any modifications
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 6672
diff changeset
   662
     * subsequent to construction.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @return an iterator over the elements in this queue in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * Next node to return item for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        private Node<E> nextNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * nextItem holds on to item fields because once we claim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         * that an element exists in hasNext(), we must return it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         * the following next() call even if it was in the process of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         * being removed when hasNext() was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        private E nextItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         * Node of the last returned item, to support remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        private Node<E> lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        Itr() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         * Moves to next valid node and returns item to return for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
         * next(), or null if no such.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        private E advance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            lastRet = nextNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            E x = nextItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   701
            Node<E> pred, p;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   702
            if (nextNode == null) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   703
                p = first();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   704
                pred = null;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   705
            } else {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   706
                pred = nextNode;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   707
                p = succ(nextNode);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   708
            }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   709
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    nextNode = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    nextItem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   716
                E item = p.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                if (item != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    nextNode = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    nextItem = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    return x;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   721
                } else {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   722
                    // skip over nulls
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   723
                    Node<E> next = succ(p);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   724
                    if (pred != null && next != null)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   725
                        pred.casNext(p, next);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   726
                    p = next;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   727
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            return nextNode != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            if (nextNode == null) throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            return advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            Node<E> l = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            if (l == null) throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            // rely on a future traversal to relink.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   744
            l.item = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            lastRet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   750
     * Saves the state to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   752
     * @serialData All of the elements (each an {@code E}) in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * the proper order, followed by a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @param s the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        // Write out any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        // Write out all elements in the proper order.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   763
        for (Node<E> p = first(); p != null; p = succ(p)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   764
            Object item = p.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            if (item != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                s.writeObject(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        // Use trailing null as sentinel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    /**
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   774
     * Reconstitutes the instance from a stream (that is, deserializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @param s the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        s.defaultReadObject();
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   780
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   781
        // Read in elements until trailing null sentinel found
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   782
        Node<E> h = null, t = null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   783
        Object item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   784
        while ((item = s.readObject()) != null) {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   785
            @SuppressWarnings("unchecked")
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   786
            Node<E> newNode = new Node<E>((E) item);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   787
            if (h == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   788
                h = t = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   789
            else {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   790
                t.lazySetNext(newNode);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   791
                t = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   792
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   794
        if (h == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   795
            h = t = new Node<E>(null);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   796
        head = h;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   797
        tail = t;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   798
    }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   799
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   800
    /**
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   801
     * Throws NullPointerException if argument is null.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   802
     *
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   803
     * @param v the element
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   804
     */
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   805
    private static void checkNotNull(Object v) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   806
        if (v == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   807
            throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   810
    private boolean casTail(Node<E> cmp, Node<E> val) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   811
        return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   812
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   813
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   814
    private boolean casHead(Node<E> cmp, Node<E> val) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   815
        return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   816
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   817
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   818
    // Unsafe mechanics
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   819
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   820
    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
   821
    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
   822
    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
   823
    static {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   824
        try {
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   825
            UNSAFE = sun.misc.Unsafe.getUnsafe();
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   826
            Class k = ConcurrentLinkedQueue.class;
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   827
            headOffset = UNSAFE.objectFieldOffset
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   828
                (k.getDeclaredField("head"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   829
            tailOffset = UNSAFE.objectFieldOffset
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   830
                (k.getDeclaredField("tail"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   831
        } catch (Exception e) {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   832
            throw new Error(e);
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   833
        }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   834
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
}