jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 19428 jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java@83f87aff7b07
child 32991 b27c76b82713
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@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;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    44
import java.util.Spliterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    45
import java.util.Spliterators;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    46
import java.util.function.Consumer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * An unbounded thread-safe {@linkplain Queue queue} based on linked nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This queue orders elements FIFO (first-in-first-out).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * The <em>head</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * queue the longest time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The <em>tail</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * queue the shortest time. New elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * are inserted at the tail of the queue, and the queue retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * 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
    57
 * A {@code ConcurrentLinkedQueue} is an appropriate choice when
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * many threads will share access to a common collection.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    59
 * Like most other concurrent collection implementations, this class
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    60
 * does not permit the use of {@code null} elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    62
 * <p>This implementation employs an efficient <em>non-blocking</em>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * algorithm based on one described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * href="http://www.cs.rochester.edu/u/michael/PODC96.html"> Simple,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Fast, and Practical Non-Blocking and Blocking Concurrent Queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Algorithms</a> by Maged M. Michael and Michael L. Scott.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    68
 * <p>Iterators are <i>weakly consistent</i>, returning elements
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    69
 * reflecting the state of the queue at some point at or since the
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    70
 * 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
    71
 * java.util.ConcurrentModificationException}, and may proceed concurrently
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 6672
diff changeset
    72
 * with other operations.  Elements contained in the queue since the creation
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    73
 * of the iterator will be returned exactly once.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    74
 *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    75
 * <p>Beware that, unlike in most collections, the {@code size} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * is <em>NOT</em> a constant-time operation. Because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * asynchronous nature of these queues, determining the current number
9515
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    78
 * 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
    79
 * inaccurate results if this collection is modified during traversal.
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    80
 * Additionally, the bulk operations {@code addAll},
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    81
 * {@code removeAll}, {@code retainAll}, {@code containsAll},
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    82
 * {@code equals}, and {@code toArray} are <em>not</em> guaranteed
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    83
 * to be performed atomically. For example, an iterator operating
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    84
 * concurrently with an {@code addAll} operation might view only some
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    85
 * of the added elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    87
 * <p>This class and its iterator implement all of the <em>optional</em>
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    88
 * methods of the {@link Queue} and {@link Iterator} interfaces.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>Memory consistency effects: As with other concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * collections, actions in a thread prior to placing an object into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * {@code ConcurrentLinkedQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * actions subsequent to the access or removal of that element from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * the {@code ConcurrentLinkedQueue} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        implements Queue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final long serialVersionUID = 196745693267521676L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /*
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   110
     * 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
   111
     * 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
   112
     * 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
   113
     * explanation, read the paper.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   115
     * 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
   116
     * this implementation relies on the fact that in garbage
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * collected systems, there is no possibility of ABA problems due
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * to recycled nodes, so there is no need to use "counted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * pointers" or related techniques seen in versions used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * non-GC'ed settings.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   121
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   122
     * The fundamental invariants are:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   123
     * - 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
   124
     *   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
   125
     *   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
   126
     *   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
   127
     *   head as well.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   128
     * - 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
   129
     *   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
   130
     *   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
   131
     *   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
   132
     *   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
   133
     *   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
   134
     *   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
   135
     *   poll() that has lost its time slice.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   136
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   137
     * 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
   138
     * 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
   139
     * - 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
   140
     * - 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
   141
     *   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
   142
     *   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
   143
     * 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
   144
     * 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
   145
     * 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
   146
     * 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
   147
     * 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
   148
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   149
     * 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
   150
     * update them every time one could is a significant optimization
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   151
     * (fewer CASes). As with LinkedTransferQueue (see the internal
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   152
     * documentation for that class), we use a slack threshold of two;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   153
     * that is, we update head/tail when the current pointer appears
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   154
     * 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
   155
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   156
     * 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
   157
     * 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
   158
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   159
     * 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
   160
     * 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
   161
     * 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
   162
     * 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
   163
     * 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
   164
     * 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
   165
     * this is merely an optimization.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   166
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   167
     * When constructing a Node (before enqueuing it) we avoid paying
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   168
     * for a volatile write to item by using Unsafe.putObject instead
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   169
     * 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
   170
     * "one-and-a-half" CASes.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   171
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   172
     * 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
   173
     * 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
   174
     * 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
   175
     * 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
   176
     * 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
   177
     * optimization.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static class Node<E> {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   181
        volatile E item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   182
        volatile Node<E> next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   184
        /**
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   185
         * Constructs a new node.  Uses relaxed write because item can
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   186
         * only be seen after publication via casNext.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   187
         */
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   188
        Node(E item) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   189
            UNSAFE.putObject(this, itemOffset, item);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        boolean casItem(E cmp, E val) {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   193
            return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   196
        void lazySetNext(Node<E> val) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   197
            UNSAFE.putOrderedObject(this, nextOffset, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        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
   201
            return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   204
        // Unsafe mechanics
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   206
        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
   207
        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
   208
        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
   209
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   210
        static {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   211
            try {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   212
                UNSAFE = sun.misc.Unsafe.getUnsafe();
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   213
                Class<?> k = Node.class;
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   214
                itemOffset = 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("item"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   216
                nextOffset = UNSAFE.objectFieldOffset
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   217
                    (k.getDeclaredField("next"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   218
            } catch (Exception e) {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   219
                throw new Error(e);
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   220
            }
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   221
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   224
    /**
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   225
     * 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
   226
     * can be reached in O(1) time.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   227
     * Invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   228
     * - 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
   229
     * - head != null
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   230
     * - (tmp = head).next != tmp || tmp != head
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   231
     * Non-invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   232
     * - 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
   233
     * - 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
   234
     *   to not be reachable from head!
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   235
     */
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   236
    private transient volatile Node<E> head;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   239
     * 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
   240
     * 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
   241
     * Invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   242
     * - 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
   243
     * - tail != null
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   244
     * Non-invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   245
     * - 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
   246
     * - 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
   247
     *   to not be reachable from head!
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   248
     * - tail.next may or may not be self-pointing to tail.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   250
    private transient volatile Node<E> tail;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   253
     * Creates a {@code ConcurrentLinkedQueue} that is initially empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   255
    public ConcurrentLinkedQueue() {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   256
        head = tail = new Node<E>(null);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   257
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   260
     * Creates a {@code ConcurrentLinkedQueue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * initially containing the elements of the given collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * added in traversal order of the collection's iterator.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   263
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param c the collection of elements to initially contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public ConcurrentLinkedQueue(Collection<? extends E> c) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   269
        Node<E> h = null, t = null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   270
        for (E e : c) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   271
            checkNotNull(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   272
            Node<E> newNode = new Node<E>(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   273
            if (h == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   274
                h = t = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   275
            else {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   276
                t.lazySetNext(newNode);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   277
                t = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   278
            }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   279
        }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   280
        if (h == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   281
            h = t = new Node<E>(null);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   282
        head = h;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   283
        tail = t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    // Have to override just to update the javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * 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
   290
     * 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
   291
     * {@link IllegalStateException} or return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   293
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   301
     * Tries to CAS head to p. If successful, repoint old head to itself
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   302
     * as sentinel for succ(), below.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   303
     */
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   304
    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
   305
        if (h != p && casHead(h, p))
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   306
            h.lazySetNext(h);
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
    /**
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   310
     * 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
   311
     * 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
   312
     * 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
   313
     */
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   314
    final Node<E> succ(Node<E> p) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   315
        Node<E> next = p.next;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   316
        return (p == next) ? head : next;
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
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   319
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * 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
   321
     * As the queue is unbounded, this method will never return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   323
     * @return {@code true} (as specified by {@link Queue#offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public boolean offer(E e) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   327
        checkNotNull(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   328
        final Node<E> newNode = new Node<E>(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   329
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   330
        for (Node<E> t = tail, p = t;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   331
            Node<E> q = p.next;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   332
            if (q == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   333
                // p is last node
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   334
                if (p.casNext(null, newNode)) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   335
                    // Successful CAS is the linearization point
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   336
                    // for e to become an element of this queue,
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   337
                    // and for newNode to become "live".
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   338
                    if (p != t) // hop two nodes at a time
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   339
                        casTail(t, newNode);  // Failure is OK.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   340
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   342
                // Lost CAS race to another thread; re-read next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   344
            else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   345
                // We have fallen off list.  If tail is unchanged, it
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   346
                // will also be off-list, in which case we need to
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   347
                // jump to head, from which all live nodes are always
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   348
                // reachable.  Else the new tail is a better bet.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   349
                p = (t != (t = tail)) ? t : head;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   350
            else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   351
                // Check for tail updates after two hops.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   352
                p = (p != t && t != (t = tail)) ? t : q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public E poll() {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   357
        restartFromHead:
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   358
        for (;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   359
            for (Node<E> h = head, p = h, q;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   360
                E item = p.item;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   361
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   362
                if (item != null && p.casItem(item, null)) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   363
                    // Successful CAS is the linearization point
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   364
                    // for item to be removed from this queue.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   365
                    if (p != h) // hop two nodes at a time
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   366
                        updateHead(h, ((q = p.next) != null) ? q : p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   367
                    return item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   369
                else if ((q = p.next) == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   370
                    updateHead(h, p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   371
                    return null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   372
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   373
                else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   374
                    continue restartFromHead;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   375
                else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   376
                    p = q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   381
    public E peek() {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   382
        restartFromHead:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        for (;;) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   384
            for (Node<E> h = head, p = h, q;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   385
                E item = p.item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   386
                if (item != null || (q = p.next) == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   387
                    updateHead(h, p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   388
                    return item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   389
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   390
                else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   391
                    continue restartFromHead;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   392
                else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   393
                    p = q;
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   399
     * 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
   400
     * 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
   401
     * 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
   402
     * 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
   403
     * 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
   404
     * of losing a race to a concurrent poll().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    Node<E> first() {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   407
        restartFromHead:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        for (;;) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   409
            for (Node<E> h = head, p = h, q;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   410
                boolean hasItem = (p.item != null);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   411
                if (hasItem || (q = p.next) == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   412
                    updateHead(h, p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   413
                    return hasItem ? p : null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   414
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   415
                else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   416
                    continue restartFromHead;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   417
                else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   418
                    p = q;
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   424
     * Returns {@code true} if this queue contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   426
     * @return {@code true} if this queue contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return first() == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * 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
   434
     * 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
   435
     * {@code Integer.MAX_VALUE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <p>Beware that, unlike in most collections, this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * <em>NOT</em> a constant-time operation. Because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * asynchronous nature of these queues, determining the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * number of elements requires an O(n) traversal.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   441
     * Additionally, if elements are added or removed during execution
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   442
     * of this method, the returned result may be inaccurate.  Thus,
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   443
     * this method is typically not very useful in concurrent
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   444
     * applications.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @return the number of elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        int count = 0;
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   450
        for (Node<E> p = first(); p != null; p = succ(p))
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   451
            if (p.item != null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   452
                // Collection.size() spec says to max out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                if (++count == Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   459
     * 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
   460
     * 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
   461
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @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
   464
     * @return {@code true} if this queue contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (o == null) return false;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   468
        for (Node<E> p = first(); p != null; p = succ(p)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   469
            E item = p.item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   470
            if (item != null && o.equals(item))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * 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
   478
     * 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
   479
     * that {@code o.equals(e)}, if this queue contains one or more such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * elements.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   481
     * Returns {@code true} if this queue contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @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
   485
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (o == null) return false;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   489
        Node<E> pred = null;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   490
        for (Node<E> p = first(); p != null; p = succ(p)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   491
            E item = p.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (item != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                o.equals(item) &&
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   494
                p.casItem(item, null)) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   495
                Node<E> next = succ(p);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   496
                if (pred != null && next != null)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   497
                    pred.casNext(p, next);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                return true;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   499
            }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   500
            pred = p;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   506
     * Appends all of the elements in the specified collection to the end of
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   507
     * this queue, in the order that they are returned by the specified
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   508
     * collection's iterator.  Attempts to {@code addAll} of a queue to
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   509
     * itself result in {@code IllegalArgumentException}.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   510
     *
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   511
     * @param c the elements to be inserted into this queue
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   512
     * @return {@code true} if this queue changed as a result of the call
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   513
     * @throws NullPointerException if the specified collection or any
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   514
     *         of its elements are null
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   515
     * @throws IllegalArgumentException if the collection is this queue
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   516
     */
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   517
    public boolean addAll(Collection<? extends E> c) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   518
        if (c == this)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   519
            // As historically specified in AbstractQueue#addAll
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   520
            throw new IllegalArgumentException();
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   521
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   522
        // Copy c into a private chain of Nodes
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   523
        Node<E> beginningOfTheEnd = null, last = null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   524
        for (E e : c) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   525
            checkNotNull(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   526
            Node<E> newNode = new Node<E>(e);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   527
            if (beginningOfTheEnd == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   528
                beginningOfTheEnd = last = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   529
            else {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   530
                last.lazySetNext(newNode);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   531
                last = newNode;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   532
            }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   533
        }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   534
        if (beginningOfTheEnd == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   535
            return false;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   536
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   537
        // Atomically append the chain at the tail of this collection
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   538
        for (Node<E> t = tail, p = t;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   539
            Node<E> q = p.next;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   540
            if (q == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   541
                // p is last node
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   542
                if (p.casNext(null, beginningOfTheEnd)) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   543
                    // Successful CAS is the linearization point
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   544
                    // for all elements to be added to this queue.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   545
                    if (!casTail(t, last)) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   546
                        // Try a little harder to update tail,
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   547
                        // since we may be adding many elements.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   548
                        t = tail;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   549
                        if (last.next == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   550
                            casTail(t, last);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   551
                    }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   552
                    return true;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   553
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   554
                // Lost CAS race to another thread; re-read next
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   555
            }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   556
            else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   557
                // We have fallen off list.  If tail is unchanged, it
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   558
                // will also be off-list, in which case we need to
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   559
                // jump to head, from which all live nodes are always
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   560
                // reachable.  Else the new tail is a better bet.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   561
                p = (t != (t = tail)) ? t : head;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   562
            else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   563
                // Check for tail updates after two hops.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   564
                p = (p != t && t != (t = tail)) ? t : q;
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
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   568
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        // Use ArrayList to deal with resizing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        ArrayList<E> al = new ArrayList<E>();
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   584
        for (Node<E> p = first(); p != null; p = succ(p)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   585
            E item = p.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            if (item != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                al.add(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return al.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * the specified array.  If the queue fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * runtime type of the specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * 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
   602
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   609
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * 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
   611
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   613
     *  <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
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
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   657
     * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   658
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @return an iterator over the elements in this queue in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         * Next node to return item for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        private Node<E> nextNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * nextItem holds on to item fields because once we claim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * that an element exists in hasNext(), we must return it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         * the following next() call even if it was in the process of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * being removed when hasNext() was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        private E nextItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         * Node of the last returned item, to support remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        private Node<E> lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        Itr() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * Moves to next valid node and returns item to return for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         * next(), or null if no such.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        private E advance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            lastRet = nextNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            E x = nextItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   697
            Node<E> pred, p;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   698
            if (nextNode == null) {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   699
                p = first();
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   700
                pred = null;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   701
            } else {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   702
                pred = nextNode;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   703
                p = succ(nextNode);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   704
            }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   705
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    nextNode = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    nextItem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   712
                E item = p.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                if (item != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    nextNode = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    nextItem = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    return x;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   717
                } else {
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   718
                    // skip over nulls
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   719
                    Node<E> next = succ(p);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   720
                    if (pred != null && next != null)
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   721
                        pred.casNext(p, next);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   722
                    p = next;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   723
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            return nextNode != null;
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 E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            if (nextNode == null) throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            return advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            Node<E> l = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (l == null) throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            // rely on a future traversal to relink.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   740
            l.item = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            lastRet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   746
     * Saves this queue to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   748
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   749
     * @throws java.io.IOException if an I/O error occurs
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   750
     * @serialData All of the elements (each an {@code E}) in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * the proper order, followed by a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        // Write out any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        // 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
   760
        for (Node<E> p = first(); p != null; p = succ(p)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   761
            Object item = p.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            if (item != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                s.writeObject(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        // Use trailing null as sentinel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   771
     * Reconstitutes this queue from a stream (that is, deserializes it).
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   772
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   773
     * @throws ClassNotFoundException if the class of a serialized object
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   774
     *         could not be found
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   775
     * @throws java.io.IOException if an I/O error occurs
2
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
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   800
    /** A customized variant of Spliterators.IteratorSpliterator */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   801
    static final class CLQSpliterator<E> implements Spliterator<E> {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   802
        static final int MAX_BATCH = 1 << 25;  // max batch array size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   803
        final ConcurrentLinkedQueue<E> queue;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   804
        Node<E> current;    // current node; null until initialized
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   805
        int batch;          // batch size for splits
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   806
        boolean exhausted;  // true when no more nodes
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   807
        CLQSpliterator(ConcurrentLinkedQueue<E> queue) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   808
            this.queue = queue;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   809
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   810
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   811
        public Spliterator<E> trySplit() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   812
            Node<E> p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   813
            final ConcurrentLinkedQueue<E> q = this.queue;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   814
            int b = batch;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   815
            int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   816
            if (!exhausted &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   817
                ((p = current) != null || (p = q.first()) != null) &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   818
                p.next != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   819
                Object[] a = new Object[n];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   820
                int i = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   821
                do {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   822
                    if ((a[i] = p.item) != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   823
                        ++i;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   824
                    if (p == (p = p.next))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   825
                        p = q.first();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   826
                } while (p != null && i < n);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   827
                if ((current = p) == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   828
                    exhausted = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   829
                if (i > 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   830
                    batch = i;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   831
                    return Spliterators.spliterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   832
                        (a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   833
                         Spliterator.CONCURRENT);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   834
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   835
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   836
            return null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   837
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   838
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   839
        public void forEachRemaining(Consumer<? super E> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   840
            Node<E> p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   841
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   842
            final ConcurrentLinkedQueue<E> q = this.queue;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   843
            if (!exhausted &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   844
                ((p = current) != null || (p = q.first()) != null)) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   845
                exhausted = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   846
                do {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   847
                    E e = p.item;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   848
                    if (p == (p = p.next))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   849
                        p = q.first();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   850
                    if (e != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   851
                        action.accept(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   852
                } while (p != null);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   853
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   854
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   855
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   856
        public boolean tryAdvance(Consumer<? super E> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   857
            Node<E> p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   858
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   859
            final ConcurrentLinkedQueue<E> q = this.queue;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   860
            if (!exhausted &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   861
                ((p = current) != null || (p = q.first()) != null)) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   862
                E e;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   863
                do {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   864
                    e = p.item;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   865
                    if (p == (p = p.next))
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   866
                        p = q.first();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   867
                } while (e == null && p != null);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   868
                if ((current = p) == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   869
                    exhausted = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   870
                if (e != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   871
                    action.accept(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   872
                    return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   873
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   874
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   875
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   876
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   877
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   878
        public long estimateSize() { return Long.MAX_VALUE; }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   879
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   880
        public int characteristics() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   881
            return Spliterator.ORDERED | Spliterator.NONNULL |
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   882
                Spliterator.CONCURRENT;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   883
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   884
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   885
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   886
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   887
     * Returns a {@link Spliterator} over the elements in this queue.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   888
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   889
     * <p>The returned spliterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   890
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   891
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   892
     * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   893
     * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   894
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   895
     * @implNote
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   896
     * The {@code Spliterator} implements {@code trySplit} to permit limited
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   897
     * parallelism.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   898
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   899
     * @return a {@code Spliterator} over the elements in this queue
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   900
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   901
     */
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   902
    @Override
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   903
    public Spliterator<E> spliterator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   904
        return new CLQSpliterator<E>(this);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   905
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   906
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   907
    /**
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   908
     * Throws NullPointerException if argument is null.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   909
     *
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   910
     * @param v the element
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   911
     */
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   912
    private static void checkNotNull(Object v) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   913
        if (v == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   914
            throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   917
    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
   918
        return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   919
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   920
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   921
    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
   922
        return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val);
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   923
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   924
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   925
    // Unsafe mechanics
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   926
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   927
    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
   928
    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
   929
    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
   930
    static {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   931
        try {
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   932
            UNSAFE = sun.misc.Unsafe.getUnsafe();
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   933
            Class<?> k = ConcurrentLinkedQueue.class;
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   934
            headOffset = UNSAFE.objectFieldOffset
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   935
                (k.getDeclaredField("head"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   936
            tailOffset = UNSAFE.objectFieldOffset
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   937
                (k.getDeclaredField("tail"));
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   938
        } catch (Exception e) {
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   939
            throw new Error(e);
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   940
        }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   941
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
}