jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java
author dl
Fri, 03 Mar 2017 10:45:38 -0800
changeset 44039 058585425bb7
parent 43521 60e247b8d9a4
child 44743 f0bbd698c486
permissions -rw-r--r--
8173909: Miscellaneous changes imported from jsr166 CVS 2017-03 Reviewed-by: martin, psandoz
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
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
    38
import java.lang.invoke.MethodHandles;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
    39
import java.lang.invoke.VarHandle;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    40
import java.util.AbstractQueue;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    41
import java.util.Arrays;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    42
import java.util.Collection;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    43
import java.util.Iterator;
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    44
import java.util.NoSuchElementException;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    45
import java.util.Objects;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    46
import java.util.Queue;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    47
import java.util.Spliterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    48
import java.util.Spliterators;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    49
import java.util.function.Consumer;
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
    50
import java.util.function.Predicate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * An unbounded thread-safe {@linkplain Queue queue} based on linked nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This queue orders elements FIFO (first-in-first-out).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The <em>head</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * queue the longest time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The <em>tail</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * queue the shortest time. New elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * are inserted at the tail of the queue, and the queue retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * 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
    61
 * A {@code ConcurrentLinkedQueue} is an appropriate choice when
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * many threads will share access to a common collection.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    63
 * Like most other concurrent collection implementations, this class
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    64
 * does not permit the use of {@code null} elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    66
 * <p>This implementation employs an efficient <em>non-blocking</em>
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    67
 * algorithm based on one described in
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    68
 * <a href="http://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdf">
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    69
 * Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Algorithms</a> by Maged M. Michael and Michael L. Scott.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    72
 * <p>Iterators are <i>weakly consistent</i>, returning elements
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    73
 * reflecting the state of the queue at some point at or since the
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    74
 * 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
    75
 * java.util.ConcurrentModificationException}, and may proceed concurrently
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 6672
diff changeset
    76
 * with other operations.  Elements contained in the queue since the creation
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    77
 * of the iterator will be returned exactly once.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    78
 *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
    79
 * <p>Beware that, unlike in most collections, the {@code size} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * is <em>NOT</em> a constant-time operation. Because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * asynchronous nature of these queues, determining the current number
9515
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    82
 * 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
    83
 * inaccurate results if this collection is modified during traversal.
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    84
 *
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    85
 * <p>Bulk operations that add, remove, or examine multiple elements,
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    86
 * such as {@link #addAll}, {@link #removeIf} or {@link #forEach},
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    87
 * are <em>not</em> guaranteed to be performed atomically.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    88
 * For example, a {@code forEach} traversal concurrent with an {@code
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    89
 * addAll} operation might observe only some of the added elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    91
 * <p>This class and its iterator implement all of the <em>optional</em>
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    92
 * methods of the {@link Queue} and {@link Iterator} interfaces.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>Memory consistency effects: As with other concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * collections, actions in a thread prior to placing an object into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * {@code ConcurrentLinkedQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * actions subsequent to the access or removal of that element from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * the {@code ConcurrentLinkedQueue} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @author Doug Lea
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   107
 * @param <E> the type of elements held in this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        implements Queue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final long serialVersionUID = 196745693267521676L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /*
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   114
     * 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
   115
     * adapted for a garbage-collected environment, with support for
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   116
     * interior node deletion (to support e.g. remove(Object)).  For
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   117
     * explanation, read the paper.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   119
     * 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
   120
     * this implementation relies on the fact that in garbage
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * collected systems, there is no possibility of ABA problems due
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * to recycled nodes, so there is no need to use "counted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * pointers" or related techniques seen in versions used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * non-GC'ed settings.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   125
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   126
     * The fundamental invariants are:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   127
     * - 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
   128
     *   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
   129
     *   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
   130
     *   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
   131
     *   head as well.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   132
     * - 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
   133
     *   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
   134
     *   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
   135
     *   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
   136
     *   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
   137
     *   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
   138
     *   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
   139
     *   poll() that has lost its time slice.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   140
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   141
     * 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
   142
     * 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
   143
     * - 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
   144
     * - 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
   145
     *   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
   146
     *   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
   147
     * 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
   148
     * 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
   149
     * 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
   150
     * 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
   151
     * 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
   152
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   153
     * 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
   154
     * update them every time one could is a significant optimization
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   155
     * (fewer CASes). As with LinkedTransferQueue (see the internal
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   156
     * documentation for that class), we use a slack threshold of two;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   157
     * that is, we update head/tail when the current pointer appears
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   158
     * 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
   159
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   160
     * 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
   161
     * 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
   162
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   163
     * CASing a Node's item reference to null atomically removes the
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   164
     * element from the queue, leaving a "dead" node that should later
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   165
     * be unlinked (but unlinking is merely an optimization).
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   166
     * Interior element removal methods (other than Iterator.remove())
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   167
     * keep track of the predecessor node during traversal so that the
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   168
     * node can be CAS-unlinked.  Some traversal methods try to unlink
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   169
     * any deleted nodes encountered during traversal.  See comments
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   170
     * in bulkRemove.
3414
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
     * When constructing a Node (before enqueuing it) we avoid paying
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
   173
     * for a volatile write to item.  This allows the cost of enqueue
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
   174
     * to be "one-and-a-half" CASes.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   175
     *
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   176
     * 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
   177
     * 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
   178
     * 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
   179
     * 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
   180
     * 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
   181
     * optimization.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
   184
    static final class Node<E> {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   185
        volatile E item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   186
        volatile Node<E> next;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   187
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   188
        /**
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   189
         * Constructs a node holding item.  Uses relaxed write because
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   190
         * item can only be seen after piggy-backing publication via CAS.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   191
         */
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   192
        Node(E item) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   193
            ITEM.set(this, item);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   194
        }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   195
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   196
        /** Constructs a dead dummy node. */
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   197
        Node() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   199
        void appendRelaxed(Node<E> next) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   200
            // assert next != null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   201
            // assert this.next == null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   202
            NEXT.set(this, next);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   203
        }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   204
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   205
        boolean casItem(E cmp, E val) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   206
            // assert item == cmp || item == null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   207
            // assert cmp != null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   208
            // assert val == null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   209
            return ITEM.compareAndSet(this, cmp, val);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   210
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   211
    }
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
   212
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   213
    /**
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   214
     * 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
   215
     * can be reached in O(1) time.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   216
     * Invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   217
     * - 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
   218
     * - head != null
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   219
     * - (tmp = head).next != tmp || tmp != head
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   220
     * Non-invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   221
     * - 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
   222
     * - 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
   223
     *   to not be reachable from head!
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   224
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   225
    transient volatile Node<E> head;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   228
     * 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
   229
     * 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
   230
     * Invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   231
     * - 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
   232
     * - tail != null
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   233
     * Non-invariants:
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   234
     * - 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
   235
     * - 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
   236
     *   to not be reachable from head!
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   237
     * - tail.next may or may not be self-linked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   239
    private transient volatile Node<E> tail;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   242
     * Creates a {@code ConcurrentLinkedQueue} that is initially empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   244
    public ConcurrentLinkedQueue() {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   245
        head = tail = new Node<E>();
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   246
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   249
     * Creates a {@code ConcurrentLinkedQueue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * initially containing the elements of the given collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * added in traversal order of the collection's iterator.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   252
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param c the collection of elements to initially contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public ConcurrentLinkedQueue(Collection<? extends E> c) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   258
        Node<E> h = null, t = null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   259
        for (E e : c) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   260
            Node<E> newNode = new Node<E>(Objects.requireNonNull(e));
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   261
            if (h == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   262
                h = t = newNode;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   263
            else
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   264
                t.appendRelaxed(t = newNode);
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   265
        }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   266
        if (h == null)
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   267
            h = t = new Node<E>();
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   268
        head = h;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   269
        tail = t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    // Have to override just to update the javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * 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
   276
     * 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
   277
     * {@link IllegalStateException} or return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   279
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   287
     * 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
   288
     * as sentinel for succ(), below.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   289
     */
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   290
    final void updateHead(Node<E> h, Node<E> p) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   291
        // assert h != null && p != null && (h == p || h.item == null);
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
   292
        if (h != p && HEAD.compareAndSet(this, h, p))
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
   293
            NEXT.setRelease(h, h);
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   294
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   295
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   296
    /**
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   297
     * 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
   298
     * 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
   299
     * 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
   300
     */
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   301
    final Node<E> succ(Node<E> p) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   302
        if (p == (p = p.next))
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   303
            p = head;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   304
        return p;
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   305
    }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   306
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   307
    /**
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   308
     * Tries to CAS pred.next (or head, if pred is null) from c to p.
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   309
     * Caller must ensure that we're not unlinking the trailing node.
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   310
     */
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   311
    private boolean tryCasSuccessor(Node<E> pred, Node<E> c, Node<E> p) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   312
        // assert p != null;
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   313
        // assert c.item == null;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   314
        // assert c != p;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   315
        if (pred != null)
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   316
            return NEXT.compareAndSet(pred, c, p);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   317
        if (HEAD.compareAndSet(this, c, p)) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   318
            NEXT.setRelease(c, c);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   319
            return true;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   320
        }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   321
        return false;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   322
    }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   323
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   324
    /**
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   325
     * Collapse dead nodes between pred and q.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   326
     * @param pred the last known live node, or null if none
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   327
     * @param c the first dead node
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   328
     * @param p the last dead node
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   329
     * @param q p.next: the next live node, or null if at end
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   330
     * @return either old pred or p if pred dead or CAS failed
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   331
     */
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   332
    private Node<E> skipDeadNodes(Node<E> pred, Node<E> c, Node<E> p, Node<E> q) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   333
        // assert pred != c;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   334
        // assert p != q;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   335
        // assert c.item == null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   336
        // assert p.item == null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   337
        if (q == null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   338
            // Never unlink trailing node.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   339
            if (c == p) return pred;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   340
            q = p;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   341
        }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   342
        return (tryCasSuccessor(pred, c, q)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   343
                && (pred == null || ITEM.get(pred) != null))
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   344
            ? pred : p;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   345
    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   346
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   347
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * 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
   349
     * As the queue is unbounded, this method will never return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   351
     * @return {@code true} (as specified by {@link Queue#offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public boolean offer(E e) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   355
        final Node<E> newNode = new Node<E>(Objects.requireNonNull(e));
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   356
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   357
        for (Node<E> t = tail, p = t;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   358
            Node<E> q = p.next;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   359
            if (q == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   360
                // p is last node
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
   361
                if (NEXT.compareAndSet(p, null, newNode)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   362
                    // Successful CAS is the linearization point
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   363
                    // for e to become an element of this queue,
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   364
                    // and for newNode to become "live".
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
   365
                    if (p != t) // hop two nodes at a time; failure is OK
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39725
diff changeset
   366
                        TAIL.weakCompareAndSet(this, t, newNode);
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   367
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   369
                // Lost CAS race to another thread; re-read next
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   371
            else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   372
                // We have fallen off list.  If tail is unchanged, it
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   373
                // will also be off-list, in which case we need to
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   374
                // jump to head, from which all live nodes are always
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   375
                // reachable.  Else the new tail is a better bet.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   376
                p = (t != (t = tail)) ? t : head;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   377
            else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   378
                // Check for tail updates after two hops.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   379
                p = (p != t && t != (t = tail)) ? t : q;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public E poll() {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   384
        restartFromHead: for (;;) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   385
            for (Node<E> h = head, p = h, q;; p = q) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   386
                final E item;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   387
                if ((item = p.item) != null && p.casItem(item, null)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   388
                    // Successful CAS is the linearization point
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   389
                    // for item to be removed from this queue.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   390
                    if (p != h) // hop two nodes at a time
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   391
                        updateHead(h, ((q = p.next) != null) ? q : p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   392
                    return item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   394
                else if ((q = p.next) == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   395
                    updateHead(h, p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   396
                    return null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   397
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   398
                else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   399
                    continue restartFromHead;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   404
    public E peek() {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   405
        restartFromHead: for (;;) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   406
            for (Node<E> h = head, p = h, q;; p = q) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   407
                final E item;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   408
                if ((item = p.item) != null
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   409
                    || (q = p.next) == null) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   410
                    updateHead(h, p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   411
                    return item;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   412
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   413
                else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   414
                    continue restartFromHead;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   420
     * 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
   421
     * 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
   422
     * 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
   423
     * 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
   424
     * 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
   425
     * of losing a race to a concurrent poll().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    Node<E> first() {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   428
        restartFromHead: for (;;) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   429
            for (Node<E> h = head, p = h, q;; p = q) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   430
                boolean hasItem = (p.item != null);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   431
                if (hasItem || (q = p.next) == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   432
                    updateHead(h, p);
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   433
                    return hasItem ? p : null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   434
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   435
                else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   436
                    continue restartFromHead;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   442
     * Returns {@code true} if this queue contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   444
     * @return {@code true} if this queue contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return first() == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * 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
   452
     * 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
   453
     * {@code Integer.MAX_VALUE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * <p>Beware that, unlike in most collections, this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <em>NOT</em> a constant-time operation. Because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * asynchronous nature of these queues, determining the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * number of elements requires an O(n) traversal.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   459
     * Additionally, if elements are added or removed during execution
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   460
     * of this method, the returned result may be inaccurate.  Thus,
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   461
     * this method is typically not very useful in concurrent
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   462
     * applications.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @return the number of elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public int size() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   467
        restartFromHead: for (;;) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   468
            int count = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   469
            for (Node<E> p = first(); p != null;) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   470
                if (p.item != null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   471
                    if (++count == Integer.MAX_VALUE)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   472
                        break;  // @see Collection.size()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   473
                if (p == (p = p.next))
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   474
                    continue restartFromHead;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   475
            }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   476
            return count;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   477
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
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 contains the specified element.
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   482
     * 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
   483
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @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
   486
     * @return {@code true} if this queue contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public boolean contains(Object o) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   489
        if (o == null) return false;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   490
        restartFromHead: for (;;) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   491
            for (Node<E> p = head, pred = null; p != null; ) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   492
                Node<E> q = p.next;
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   493
                final E item;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   494
                if ((item = p.item) != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   495
                    if (o.equals(item))
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   496
                        return true;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   497
                    pred = p; p = q; continue;
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   498
                }
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   499
                for (Node<E> c = p;; q = p.next) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   500
                    if (q == null || q.item != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   501
                        pred = skipDeadNodes(pred, c, p, q); p = q; break;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   502
                    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   503
                    if (p == (p = q)) continue restartFromHead;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   504
                }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   505
            }
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   506
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * 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
   512
     * 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
   513
     * that {@code o.equals(e)}, if this queue contains one or more such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * elements.
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   515
     * Returns {@code true} if this queue contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @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
   519
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public boolean remove(Object o) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   522
        if (o == null) return false;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   523
        restartFromHead: for (;;) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   524
            for (Node<E> p = head, pred = null; p != null; ) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   525
                Node<E> q = p.next;
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   526
                final E item;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   527
                if ((item = p.item) != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   528
                    if (o.equals(item) && p.casItem(item, null)) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   529
                        skipDeadNodes(pred, p, p, q);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   530
                        return true;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   531
                    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   532
                    pred = p; p = q; continue;
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   533
                }
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   534
                for (Node<E> c = p;; q = p.next) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   535
                    if (q == null || q.item != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   536
                        pred = skipDeadNodes(pred, c, p, q); p = q; break;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   537
                    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   538
                    if (p == (p = q)) continue restartFromHead;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   539
                }
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   540
            }
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   541
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   546
     * Appends all of the elements in the specified collection to the end of
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   547
     * this queue, in the order that they are returned by the specified
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   548
     * collection's iterator.  Attempts to {@code addAll} of a queue to
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   549
     * itself result in {@code IllegalArgumentException}.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   550
     *
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   551
     * @param c the elements to be inserted into this queue
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   552
     * @return {@code true} if this queue changed as a result of the call
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   553
     * @throws NullPointerException if the specified collection or any
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   554
     *         of its elements are null
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   555
     * @throws IllegalArgumentException if the collection is this queue
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   556
     */
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   557
    public boolean addAll(Collection<? extends E> c) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   558
        if (c == this)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   559
            // As historically specified in AbstractQueue#addAll
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   560
            throw new IllegalArgumentException();
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   561
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   562
        // Copy c into a private chain of Nodes
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   563
        Node<E> beginningOfTheEnd = null, last = null;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   564
        for (E e : c) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   565
            Node<E> newNode = new Node<E>(Objects.requireNonNull(e));
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   566
            if (beginningOfTheEnd == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   567
                beginningOfTheEnd = last = newNode;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   568
            else
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   569
                last.appendRelaxed(last = newNode);
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   570
        }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   571
        if (beginningOfTheEnd == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   572
            return false;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   573
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   574
        // Atomically append the chain at the tail of this collection
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   575
        for (Node<E> t = tail, p = t;;) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   576
            Node<E> q = p.next;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   577
            if (q == null) {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   578
                // p is last node
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
   579
                if (NEXT.compareAndSet(p, null, beginningOfTheEnd)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   580
                    // Successful CAS is the linearization point
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   581
                    // for all elements to be added to this queue.
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39725
diff changeset
   582
                    if (!TAIL.weakCompareAndSet(this, t, last)) {
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   583
                        // Try a little harder to update tail,
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   584
                        // since we may be adding many elements.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   585
                        t = tail;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   586
                        if (last.next == null)
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39725
diff changeset
   587
                            TAIL.weakCompareAndSet(this, t, last);
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   588
                    }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   589
                    return true;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   590
                }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   591
                // Lost CAS race to another thread; re-read next
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   592
            }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   593
            else if (p == q)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   594
                // We have fallen off list.  If tail is unchanged, it
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   595
                // will also be off-list, in which case we need to
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   596
                // jump to head, from which all live nodes are always
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   597
                // reachable.  Else the new tail is a better bet.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   598
                p = (t != (t = tail)) ? t : head;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   599
            else
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   600
                // Check for tail updates after two hops.
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   601
                p = (p != t && t != (t = tail)) ? t : q;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   602
        }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   603
    }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   604
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   605
    public String toString() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   606
        String[] a = null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   607
        restartFromHead: for (;;) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   608
            int charLength = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   609
            int size = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   610
            for (Node<E> p = first(); p != null;) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   611
                final E item;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   612
                if ((item = p.item) != null) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   613
                    if (a == null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   614
                        a = new String[4];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   615
                    else if (size == a.length)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   616
                        a = Arrays.copyOf(a, 2 * size);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   617
                    String s = item.toString();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   618
                    a[size++] = s;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   619
                    charLength += s.length();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   620
                }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   621
                if (p == (p = p.next))
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   622
                    continue restartFromHead;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   623
            }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   624
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   625
            if (size == 0)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   626
                return "[]";
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   627
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   628
            return Helpers.toString(a, size, charLength);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   629
        }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   630
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   631
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   632
    private Object[] toArrayInternal(Object[] a) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   633
        Object[] x = a;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   634
        restartFromHead: for (;;) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   635
            int size = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   636
            for (Node<E> p = first(); p != null;) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   637
                final E item;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   638
                if ((item = p.item) != null) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   639
                    if (x == null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   640
                        x = new Object[4];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   641
                    else if (size == x.length)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   642
                        x = Arrays.copyOf(x, 2 * (size + 4));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   643
                    x[size++] = item;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   644
                }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   645
                if (p == (p = p.next))
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   646
                    continue restartFromHead;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   647
            }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   648
            if (x == null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   649
                return new Object[0];
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   650
            else if (a != null && size <= a.length) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   651
                if (a != x)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   652
                    System.arraycopy(x, 0, a, 0, size);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   653
                if (size < a.length)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   654
                    a[size] = null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   655
                return a;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   656
            }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   657
            return (size == x.length) ? x : Arrays.copyOf(x, size);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   658
        }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   659
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   660
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   661
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public Object[] toArray() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   675
        return toArrayInternal(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * the specified array.  If the queue fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * runtime type of the specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * 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
   688
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   695
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * 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
   697
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   699
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   701
     * 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
   702
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   713
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    public <T> T[] toArray(T[] a) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   715
        Objects.requireNonNull(a);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   716
        return (T[]) toArrayInternal(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * Returns an iterator over the elements in this queue in proper sequence.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   721
     * The elements will be returned in order from first (head) to last (tail).
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   722
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   723
     * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   724
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @return an iterator over the elements in this queue in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         * Next node to return item for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        private Node<E> nextNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
         * nextItem holds on to item fields because once we claim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
         * that an element exists in hasNext(), we must return it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         * the following next() call even if it was in the process of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         * being removed when hasNext() was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        private E nextItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
         * Node of the last returned item, to support remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        private Node<E> lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        Itr() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   752
            restartFromHead: for (;;) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   753
                Node<E> h, p, q;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   754
                for (p = h = head;; p = q) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   755
                    final E item;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   756
                    if ((item = p.item) != null) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   757
                        nextNode = p;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   758
                        nextItem = item;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   759
                        break;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   760
                    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   761
                    else if ((q = p.next) == null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   762
                        break;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   763
                    else if (p == q)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   764
                        continue restartFromHead;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   766
                updateHead(h, p);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   767
                return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        public boolean hasNext() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   772
            return nextItem != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        public E next() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   776
            final Node<E> pred = nextNode;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   777
            if (pred == null) throw new NoSuchElementException();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   778
            // assert nextItem != null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   779
            lastRet = pred;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   780
            E item = null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   781
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   782
            for (Node<E> p = succ(pred), q;; p = q) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   783
                if (p == null || (item = p.item) != null) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   784
                    nextNode = p;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   785
                    E x = nextItem;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   786
                    nextItem = item;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   787
                    return x;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   788
                }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   789
                // unlink deleted nodes
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   790
                if ((q = succ(p)) != null)
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
   791
                    NEXT.compareAndSet(pred, p, q);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   792
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   795
        // Default implementation of forEachRemaining is "good enough".
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   796
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            Node<E> l = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if (l == null) throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // rely on a future traversal to relink.
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   801
            l.item = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            lastRet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   807
     * Saves this queue to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   809
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   810
     * @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
   811
     * @serialData All of the elements (each an {@code E}) in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * the proper order, followed by a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        // Write out any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        // 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
   821
        for (Node<E> p = first(); p != null; p = succ(p)) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   822
            final E item;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   823
            if ((item = p.item) != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                s.writeObject(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        // Use trailing null as sentinel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   832
     * 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
   833
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   834
     * @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
   835
     *         could not be found
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   836
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        s.defaultReadObject();
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   841
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   842
        // Read in elements until trailing null sentinel found
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   843
        Node<E> h = null, t = null;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   844
        for (Object item; (item = s.readObject()) != null; ) {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
   845
            @SuppressWarnings("unchecked")
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   846
            Node<E> newNode = new Node<E>((E) item);
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   847
            if (h == null)
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   848
                h = t = newNode;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   849
            else
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   850
                t.appendRelaxed(t = newNode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   852
        if (h == null)
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   853
            h = t = new Node<E>();
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   854
        head = h;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   855
        tail = t;
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   856
    }
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   857
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   858
    /** A customized variant of Spliterators.IteratorSpliterator */
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   859
    final class CLQSpliterator implements Spliterator<E> {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   860
        static final int MAX_BATCH = 1 << 25;  // max batch array size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   861
        Node<E> current;    // current node; null until initialized
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   862
        int batch;          // batch size for splits
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   863
        boolean exhausted;  // true when no more nodes
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   864
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   865
        public Spliterator<E> trySplit() {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   866
            Node<E> p, q;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   867
            if ((p = current()) == null || (q = p.next) == null)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   868
                return null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   869
            int i = 0, n = batch = Math.min(batch + 1, MAX_BATCH);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   870
            Object[] a = null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   871
            do {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   872
                final E e;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   873
                if ((e = p.item) != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   874
                    if (a == null)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   875
                        a = new Object[n];
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   876
                    a[i++] = e;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   877
                }
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   878
                if (p == (p = q))
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   879
                    p = first();
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   880
            } while (p != null && (q = p.next) != null && i < n);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   881
            setCurrent(p);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   882
            return (i == 0) ? null :
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   883
                Spliterators.spliterator(a, 0, i, (Spliterator.ORDERED |
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   884
                                                   Spliterator.NONNULL |
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   885
                                                   Spliterator.CONCURRENT));
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   886
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   887
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   888
        public void forEachRemaining(Consumer<? super E> action) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   889
            Objects.requireNonNull(action);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   890
            final Node<E> p;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   891
            if ((p = current()) != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   892
                current = null;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   893
                exhausted = true;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   894
                forEachFrom(action, p);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   895
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   896
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   897
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   898
        public boolean tryAdvance(Consumer<? super E> action) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   899
            Objects.requireNonNull(action);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   900
            Node<E> p;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   901
            if ((p = current()) != null) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   902
                E e;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   903
                do {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   904
                    e = p.item;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   905
                    if (p == (p = p.next))
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   906
                        p = first();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   907
                } while (e == null && p != null);
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   908
                setCurrent(p);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   909
                if (e != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   910
                    action.accept(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   911
                    return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   912
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   913
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   914
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   915
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   916
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   917
        private void setCurrent(Node<E> p) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   918
            if ((current = p) == null)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   919
                exhausted = true;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   920
        }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   921
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   922
        private Node<E> current() {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   923
            Node<E> p;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   924
            if ((p = current) == null && !exhausted)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   925
                setCurrent(p = first());
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   926
            return p;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   927
        }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   928
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   929
        public long estimateSize() { return Long.MAX_VALUE; }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   930
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   931
        public int characteristics() {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   932
            return (Spliterator.ORDERED |
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   933
                    Spliterator.NONNULL |
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   934
                    Spliterator.CONCURRENT);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   935
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   936
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   937
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   938
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   939
     * 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
   940
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   941
     * <p>The returned spliterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   942
     * <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
   943
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   944
     * <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
   945
     * {@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
   946
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   947
     * @implNote
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   948
     * 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
   949
     * parallelism.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   950
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   951
     * @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
   952
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   953
     */
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   954
    @Override
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   955
    public Spliterator<E> spliterator() {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   956
        return new CLQSpliterator();
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   957
    }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   958
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   959
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   960
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   961
     */
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   962
    public boolean removeIf(Predicate<? super E> filter) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   963
        Objects.requireNonNull(filter);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   964
        return bulkRemove(filter);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   965
    }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   966
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   967
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   968
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   969
     */
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   970
    public boolean removeAll(Collection<?> c) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   971
        Objects.requireNonNull(c);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   972
        return bulkRemove(e -> c.contains(e));
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   973
    }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   974
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   975
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   976
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   977
     */
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   978
    public boolean retainAll(Collection<?> c) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   979
        Objects.requireNonNull(c);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   980
        return bulkRemove(e -> !c.contains(e));
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   981
    }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   982
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   983
    public void clear() {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   984
        bulkRemove(e -> true);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   985
    }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   986
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   987
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   988
     * Tolerate this many consecutive dead nodes before CAS-collapsing.
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   989
     * Amortized cost of clear() is (1 + 1/MAX_HOPS) CASes per element.
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   990
     */
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   991
    private static final int MAX_HOPS = 8;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   992
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   993
    /** Implementation of bulk remove methods. */
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   994
    private boolean bulkRemove(Predicate<? super E> filter) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   995
        boolean removed = false;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   996
        restartFromHead: for (;;) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   997
            int hops = MAX_HOPS;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   998
            // c will be CASed to collapse intervening dead nodes between
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
   999
            // pred (or head if null) and p.
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1000
            for (Node<E> p = head, c = p, pred = null, q; p != null; p = q) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1001
                q = p.next;
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1002
                final E item; boolean pAlive;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1003
                if (pAlive = ((item = p.item) != null)) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1004
                    if (filter.test(item)) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1005
                        if (p.casItem(item, null))
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1006
                            removed = true;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1007
                        pAlive = false;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1008
                    }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1009
                }
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1010
                if (pAlive || q == null || --hops == 0) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1011
                    // p might already be self-linked here, but if so:
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1012
                    // - CASing head will surely fail
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1013
                    // - CASing pred's next will be useless but harmless.
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1014
                    if ((c != p && !tryCasSuccessor(pred, c, c = p))
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1015
                        || pAlive) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1016
                        // if CAS failed or alive, abandon old pred
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1017
                        hops = MAX_HOPS;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1018
                        pred = p;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1019
                        c = q;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1020
                    }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1021
                } else if (p == q)
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1022
                    continue restartFromHead;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1023
            }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1024
            return removed;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1025
        }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1026
    }
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1027
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1028
    /**
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1029
     * Runs action on each element found during a traversal starting at p.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1030
     * If p is null, the action is not run.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1031
     */
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1032
    void forEachFrom(Consumer<? super E> action, Node<E> p) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1033
        for (Node<E> pred = null; p != null; ) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1034
            Node<E> q = p.next;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1035
            final E item;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1036
            if ((item = p.item) != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1037
                action.accept(item);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1038
                pred = p; p = q; continue;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1039
            }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1040
            for (Node<E> c = p;; q = p.next) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1041
                if (q == null || q.item != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1042
                    pred = skipDeadNodes(pred, c, p, q); p = q; break;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1043
                }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1044
                if (p == (p = q)) { pred = null; p = head; break; }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1045
            }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1046
        }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1047
    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1048
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1049
    /**
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1050
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1051
     */
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1052
    public void forEach(Consumer<? super E> action) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40734
diff changeset
  1053
        Objects.requireNonNull(action);
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1054
        forEachFrom(action, head);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1055
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1056
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1057
    // VarHandle mechanics
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1058
    private static final VarHandle HEAD;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1059
    private static final VarHandle TAIL;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1060
    static final VarHandle ITEM;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1061
    static final VarHandle NEXT;
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1062
    static {
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
  1063
        try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1064
            MethodHandles.Lookup l = MethodHandles.lookup();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1065
            HEAD = l.findVarHandle(ConcurrentLinkedQueue.class, "head",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1066
                                   Node.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1067
            TAIL = l.findVarHandle(ConcurrentLinkedQueue.class, "tail",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1068
                                   Node.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1069
            ITEM = l.findVarHandle(Node.class, "item", Object.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39040
diff changeset
  1070
            NEXT = l.findVarHandle(Node.class, "next", Node.class);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1071
        } catch (ReflectiveOperationException e) {
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1072
            throw new Error(e);
3414
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
  1073
        }
cdf768813b4d 6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents: 2
diff changeset
  1074
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
}