author | dl |
Fri, 03 Mar 2017 10:45:38 -0800 | |
changeset 44039 | 058585425bb7 |
parent 43521 | 60e247b8d9a4 |
child 44743 | f0bbd698c486 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
6672 | 31 |
* Written by Doug Lea and Martin Buchholz with assistance from members of |
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 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
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 | 51 |
|
52 |
/** |
|
53 |
* An unbounded thread-safe {@linkplain Queue queue} based on linked nodes. |
|
54 |
* This queue orders elements FIFO (first-in-first-out). |
|
55 |
* The <em>head</em> of the queue is that element that has been on the |
|
56 |
* queue the longest time. |
|
57 |
* The <em>tail</em> of the queue is that element that has been on the |
|
58 |
* queue the shortest time. New elements |
|
59 |
* are inserted at the tail of the queue, and the queue retrieval |
|
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 | 62 |
* many threads will share access to a common collection. |
6672 | 63 |
* Like most other concurrent collection implementations, this class |
64 |
* does not permit the use of {@code null} elements. |
|
2 | 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 | 70 |
* Algorithms</a> by Maged M. Michael and Michael L. Scott. |
71 |
* |
|
6672 | 72 |
* <p>Iterators are <i>weakly consistent</i>, returning elements |
73 |
* reflecting the state of the queue at some point at or since the |
|
74 |
* creation of the iterator. They do <em>not</em> throw {@link |
|
7518 | 75 |
* java.util.ConcurrentModificationException}, and may proceed concurrently |
76 |
* with other operations. Elements contained in the queue since the creation |
|
6672 | 77 |
* of the iterator will be returned exactly once. |
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 | 80 |
* is <em>NOT</em> a constant-time operation. Because of the |
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 | 84 |
* |
85 |
* <p>Bulk operations that add, remove, or examine multiple elements, |
|
86 |
* such as {@link #addAll}, {@link #removeIf} or {@link #forEach}, |
|
87 |
* are <em>not</em> guaranteed to be performed atomically. |
|
88 |
* For example, a {@code forEach} traversal concurrent with an {@code |
|
89 |
* addAll} operation might observe only some of the added elements. |
|
2 | 90 |
* |
6672 | 91 |
* <p>This class and its iterator implement all of the <em>optional</em> |
92 |
* methods of the {@link Queue} and {@link Iterator} interfaces. |
|
2 | 93 |
* |
94 |
* <p>Memory consistency effects: As with other concurrent |
|
95 |
* collections, actions in a thread prior to placing an object into a |
|
96 |
* {@code ConcurrentLinkedQueue} |
|
97 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
|
98 |
* actions subsequent to the access or removal of that element from |
|
99 |
* the {@code ConcurrentLinkedQueue} in another thread. |
|
100 |
* |
|
101 |
* <p>This class is a member of the |
|
102 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
103 |
* Java Collections Framework</a>. |
|
104 |
* |
|
105 |
* @since 1.5 |
|
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 | 108 |
*/ |
109 |
public class ConcurrentLinkedQueue<E> extends AbstractQueue<E> |
|
110 |
implements Queue<E>, java.io.Serializable { |
|
111 |
private static final long serialVersionUID = 196745693267521676L; |
|
112 |
||
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 | 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 | 121 |
* collected systems, there is no possibility of ABA problems due |
122 |
* to recycled nodes, so there is no need to use "counted |
|
123 |
* pointers" or related techniques seen in versions used in |
|
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 | 155 |
* (fewer CASes). As with LinkedTransferQueue (see the internal |
156 |
* documentation for that class), we use a slack threshold of two; |
|
157 |
* that is, we update head/tail when the current pointer appears |
|
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 | 182 |
*/ |
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 | 185 |
volatile E item; |
186 |
volatile Node<E> next; |
|
43521 | 187 |
|
188 |
/** |
|
189 |
* Constructs a node holding item. Uses relaxed write because |
|
190 |
* item can only be seen after piggy-backing publication via CAS. |
|
191 |
*/ |
|
192 |
Node(E item) { |
|
193 |
ITEM.set(this, item); |
|
194 |
} |
|
195 |
||
196 |
/** Constructs a dead dummy node. */ |
|
197 |
Node() {} |
|
2 | 198 |
|
43521 | 199 |
void appendRelaxed(Node<E> next) { |
200 |
// assert next != null; |
|
201 |
// assert this.next == null; |
|
202 |
NEXT.set(this, next); |
|
203 |
} |
|
204 |
||
205 |
boolean casItem(E cmp, E val) { |
|
206 |
// assert item == cmp || item == null; |
|
207 |
// assert cmp != null; |
|
208 |
// assert val == null; |
|
209 |
return ITEM.compareAndSet(this, cmp, val); |
|
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 | 226 |
|
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 | 237 |
* - tail.next may or may not be self-linked. |
2 | 238 |
*/ |
6672 | 239 |
private transient volatile Node<E> tail; |
2 | 240 |
|
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 | 243 |
*/ |
6672 | 244 |
public ConcurrentLinkedQueue() { |
43521 | 245 |
head = tail = new Node<E>(); |
6672 | 246 |
} |
2 | 247 |
|
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 | 250 |
* initially containing the elements of the given collection, |
251 |
* added in traversal order of the collection's iterator. |
|
6672 | 252 |
* |
2 | 253 |
* @param c the collection of elements to initially contain |
254 |
* @throws NullPointerException if the specified collection or any |
|
255 |
* of its elements are null |
|
256 |
*/ |
|
257 |
public ConcurrentLinkedQueue(Collection<? extends E> c) { |
|
6672 | 258 |
Node<E> h = null, t = null; |
259 |
for (E e : c) { |
|
43521 | 260 |
Node<E> newNode = new Node<E>(Objects.requireNonNull(e)); |
6672 | 261 |
if (h == null) |
262 |
h = t = newNode; |
|
43521 | 263 |
else |
264 |
t.appendRelaxed(t = newNode); |
|
6672 | 265 |
} |
266 |
if (h == null) |
|
43521 | 267 |
h = t = new Node<E>(); |
6672 | 268 |
head = h; |
269 |
tail = t; |
|
2 | 270 |
} |
271 |
||
272 |
// Have to override just to update the javadoc |
|
273 |
||
274 |
/** |
|
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 | 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 | 280 |
* @throws NullPointerException if the specified element is null |
281 |
*/ |
|
282 |
public boolean add(E e) { |
|
283 |
return offer(e); |
|
284 |
} |
|
285 |
||
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 | 302 |
if (p == (p = p.next)) |
303 |
p = head; |
|
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 | 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 | 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 | 325 |
* Collapse dead nodes between pred and q. |
326 |
* @param pred the last known live node, or null if none |
|
327 |
* @param c the first dead node |
|
328 |
* @param p the last dead node |
|
329 |
* @param q p.next: the next live node, or null if at end |
|
330 |
* @return either old pred or p if pred dead or CAS failed |
|
331 |
*/ |
|
332 |
private Node<E> skipDeadNodes(Node<E> pred, Node<E> c, Node<E> p, Node<E> q) { |
|
333 |
// assert pred != c; |
|
334 |
// assert p != q; |
|
335 |
// assert c.item == null; |
|
336 |
// assert p.item == null; |
|
337 |
if (q == null) { |
|
338 |
// Never unlink trailing node. |
|
339 |
if (c == p) return pred; |
|
340 |
q = p; |
|
341 |
} |
|
342 |
return (tryCasSuccessor(pred, c, q) |
|
343 |
&& (pred == null || ITEM.get(pred) != null)) |
|
344 |
? pred : p; |
|
345 |
} |
|
346 |
||
347 |
/** |
|
2 | 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 | 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 | 352 |
* @throws NullPointerException if the specified element is null |
353 |
*/ |
|
354 |
public boolean offer(E e) { |
|
43521 | 355 |
final Node<E> newNode = new Node<E>(Objects.requireNonNull(e)); |
6672 | 356 |
|
357 |
for (Node<E> t = tail, p = t;;) { |
|
358 |
Node<E> q = p.next; |
|
359 |
if (q == null) { |
|
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 | 362 |
// Successful CAS is the linearization point |
363 |
// for e to become an element of this queue, |
|
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 | 368 |
} |
6672 | 369 |
// Lost CAS race to another thread; re-read next |
2 | 370 |
} |
6672 | 371 |
else if (p == q) |
372 |
// We have fallen off list. If tail is unchanged, it |
|
373 |
// will also be off-list, in which case we need to |
|
374 |
// jump to head, from which all live nodes are always |
|
375 |
// reachable. Else the new tail is a better bet. |
|
376 |
p = (t != (t = tail)) ? t : head; |
|
377 |
else |
|
378 |
// Check for tail updates after two hops. |
|
379 |
p = (p != t && t != (t = tail)) ? t : q; |
|
2 | 380 |
} |
381 |
} |
|
382 |
||
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 | 387 |
if ((item = p.item) != null && p.casItem(item, null)) { |
6672 | 388 |
// Successful CAS is the linearization point |
389 |
// for item to be removed from this queue. |
|
390 |
if (p != h) // hop two nodes at a time |
|
391 |
updateHead(h, ((q = p.next) != null) ? q : p); |
|
392 |
return item; |
|
2 | 393 |
} |
6672 | 394 |
else if ((q = p.next) == null) { |
395 |
updateHead(h, p); |
|
396 |
return null; |
|
397 |
} |
|
398 |
else if (p == q) |
|
399 |
continue restartFromHead; |
|
2 | 400 |
} |
401 |
} |
|
402 |
} |
|
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 | 410 |
updateHead(h, p); |
411 |
return item; |
|
412 |
} |
|
413 |
else if (p == q) |
|
414 |
continue restartFromHead; |
|
2 | 415 |
} |
416 |
} |
|
417 |
} |
|
418 |
||
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 | 426 |
*/ |
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 | 430 |
boolean hasItem = (p.item != null); |
431 |
if (hasItem || (q = p.next) == null) { |
|
432 |
updateHead(h, p); |
|
433 |
return hasItem ? p : null; |
|
434 |
} |
|
435 |
else if (p == q) |
|
436 |
continue restartFromHead; |
|
2 | 437 |
} |
438 |
} |
|
439 |
} |
|
440 |
||
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 | 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 | 445 |
*/ |
446 |
public boolean isEmpty() { |
|
447 |
return first() == null; |
|
448 |
} |
|
449 |
||
450 |
/** |
|
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 | 454 |
* |
455 |
* <p>Beware that, unlike in most collections, this method is |
|
456 |
* <em>NOT</em> a constant-time operation. Because of the |
|
457 |
* asynchronous nature of these queues, determining the current |
|
458 |
* number of elements requires an O(n) traversal. |
|
6672 | 459 |
* Additionally, if elements are added or removed during execution |
460 |
* of this method, the returned result may be inaccurate. Thus, |
|
461 |
* this method is typically not very useful in concurrent |
|
462 |
* applications. |
|
2 | 463 |
* |
464 |
* @return the number of elements in this queue |
|
465 |
*/ |
|
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 | 478 |
} |
479 |
||
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 | 484 |
* |
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 | 487 |
*/ |
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 | 491 |
for (Node<E> p = head, pred = null; p != null; ) { |
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 | 494 |
if ((item = p.item) != null) { |
495 |
if (o.equals(item)) |
|
496 |
return true; |
|
497 |
pred = p; p = q; continue; |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
498 |
} |
43521 | 499 |
for (Node<E> c = p;; q = p.next) { |
500 |
if (q == null || q.item != null) { |
|
501 |
pred = skipDeadNodes(pred, c, p, q); p = q; break; |
|
502 |
} |
|
503 |
if (p == (p = q)) continue restartFromHead; |
|
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 | 507 |
} |
508 |
} |
|
509 |
||
510 |
/** |
|
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 | 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 | 516 |
* (or equivalently, if this queue changed as a result of the call). |
517 |
* |
|
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 | 520 |
*/ |
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 | 524 |
for (Node<E> p = head, pred = null; p != null; ) { |
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 | 527 |
if ((item = p.item) != null) { |
528 |
if (o.equals(item) && p.casItem(item, null)) { |
|
529 |
skipDeadNodes(pred, p, p, q); |
|
530 |
return true; |
|
531 |
} |
|
532 |
pred = p; p = q; continue; |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
533 |
} |
43521 | 534 |
for (Node<E> c = p;; q = p.next) { |
535 |
if (q == null || q.item != null) { |
|
536 |
pred = skipDeadNodes(pred, c, p, q); p = q; break; |
|
537 |
} |
|
538 |
if (p == (p = q)) continue restartFromHead; |
|
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 | 542 |
} |
543 |
} |
|
544 |
||
545 |
/** |
|
6672 | 546 |
* Appends all of the elements in the specified collection to the end of |
547 |
* this queue, in the order that they are returned by the specified |
|
548 |
* collection's iterator. Attempts to {@code addAll} of a queue to |
|
549 |
* itself result in {@code IllegalArgumentException}. |
|
550 |
* |
|
551 |
* @param c the elements to be inserted into this queue |
|
552 |
* @return {@code true} if this queue changed as a result of the call |
|
553 |
* @throws NullPointerException if the specified collection or any |
|
554 |
* of its elements are null |
|
555 |
* @throws IllegalArgumentException if the collection is this queue |
|
556 |
*/ |
|
557 |
public boolean addAll(Collection<? extends E> c) { |
|
558 |
if (c == this) |
|
559 |
// As historically specified in AbstractQueue#addAll |
|
560 |
throw new IllegalArgumentException(); |
|
561 |
||
562 |
// Copy c into a private chain of Nodes |
|
563 |
Node<E> beginningOfTheEnd = null, last = null; |
|
564 |
for (E e : c) { |
|
43521 | 565 |
Node<E> newNode = new Node<E>(Objects.requireNonNull(e)); |
6672 | 566 |
if (beginningOfTheEnd == null) |
567 |
beginningOfTheEnd = last = newNode; |
|
43521 | 568 |
else |
569 |
last.appendRelaxed(last = newNode); |
|
6672 | 570 |
} |
571 |
if (beginningOfTheEnd == null) |
|
572 |
return false; |
|
573 |
||
574 |
// Atomically append the chain at the tail of this collection |
|
575 |
for (Node<E> t = tail, p = t;;) { |
|
576 |
Node<E> q = p.next; |
|
577 |
if (q == null) { |
|
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 | 580 |
// Successful CAS is the linearization point |
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 | 583 |
// Try a little harder to update tail, |
584 |
// since we may be adding many elements. |
|
585 |
t = tail; |
|
586 |
if (last.next == null) |
|
40734
48879ea67e2a
8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents:
39725
diff
changeset
|
587 |
TAIL.weakCompareAndSet(this, t, last); |
6672 | 588 |
} |
589 |
return true; |
|
590 |
} |
|
591 |
// Lost CAS race to another thread; re-read next |
|
592 |
} |
|
593 |
else if (p == q) |
|
594 |
// We have fallen off list. If tail is unchanged, it |
|
595 |
// will also be off-list, in which case we need to |
|
596 |
// jump to head, from which all live nodes are always |
|
597 |
// reachable. Else the new tail is a better bet. |
|
598 |
p = (t != (t = tail)) ? t : head; |
|
599 |
else |
|
600 |
// Check for tail updates after two hops. |
|
601 |
p = (p != t && t != (t = tail)) ? t : q; |
|
602 |
} |
|
603 |
} |
|
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 | 661 |
/** |
2 | 662 |
* Returns an array containing all of the elements in this queue, in |
663 |
* proper sequence. |
|
664 |
* |
|
665 |
* <p>The returned array will be "safe" in that no references to it are |
|
666 |
* maintained by this queue. (In other words, this method must allocate |
|
667 |
* a new array). The caller is thus free to modify the returned array. |
|
668 |
* |
|
669 |
* <p>This method acts as bridge between array-based and collection-based |
|
670 |
* APIs. |
|
671 |
* |
|
672 |
* @return an array containing all of the elements in this queue |
|
673 |
*/ |
|
674 |
public Object[] toArray() { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
675 |
return toArrayInternal(null); |
2 | 676 |
} |
677 |
||
678 |
/** |
|
679 |
* Returns an array containing all of the elements in this queue, in |
|
680 |
* proper sequence; the runtime type of the returned array is that of |
|
681 |
* the specified array. If the queue fits in the specified array, it |
|
682 |
* is returned therein. Otherwise, a new array is allocated with the |
|
683 |
* runtime type of the specified array and the size of this queue. |
|
684 |
* |
|
685 |
* <p>If this queue fits in the specified array with room to spare |
|
686 |
* (i.e., the array has more elements than this queue), the element in |
|
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 | 689 |
* |
690 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
691 |
* array-based and collection-based APIs. Further, this method allows |
|
692 |
* precise control over the runtime type of the output array, and may, |
|
693 |
* under certain circumstances, be used to save allocation costs. |
|
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 | 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 | 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 | 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 | 703 |
* |
704 |
* @param a the array into which the elements of the queue are to |
|
705 |
* be stored, if it is big enough; otherwise, a new array of the |
|
706 |
* same runtime type is allocated for this purpose |
|
707 |
* @return an array containing all of the elements in this queue |
|
708 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
709 |
* is not a supertype of the runtime type of every element in |
|
710 |
* this queue |
|
711 |
* @throws NullPointerException if the specified array is null |
|
712 |
*/ |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
713 |
@SuppressWarnings("unchecked") |
2 | 714 |
public <T> T[] toArray(T[] a) { |
43521 | 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 | 717 |
} |
718 |
||
719 |
/** |
|
720 |
* Returns an iterator over the elements in this queue in proper sequence. |
|
6672 | 721 |
* The elements will be returned in order from first (head) to last (tail). |
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 | 725 |
* |
726 |
* @return an iterator over the elements in this queue in proper sequence |
|
727 |
*/ |
|
728 |
public Iterator<E> iterator() { |
|
729 |
return new Itr(); |
|
730 |
} |
|
731 |
||
732 |
private class Itr implements Iterator<E> { |
|
733 |
/** |
|
734 |
* Next node to return item for. |
|
735 |
*/ |
|
736 |
private Node<E> nextNode; |
|
737 |
||
738 |
/** |
|
739 |
* nextItem holds on to item fields because once we claim |
|
740 |
* that an element exists in hasNext(), we must return it in |
|
741 |
* the following next() call even if it was in the process of |
|
742 |
* being removed when hasNext() was called. |
|
743 |
*/ |
|
744 |
private E nextItem; |
|
745 |
||
746 |
/** |
|
747 |
* Node of the last returned item, to support remove. |
|
748 |
*/ |
|
749 |
private Node<E> lastRet; |
|
750 |
||
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 | 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 | 768 |
} |
769 |
} |
|
770 |
||
771 |
public boolean hasNext() { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
772 |
return nextItem != null; |
2 | 773 |
} |
774 |
||
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 | 793 |
} |
794 |
||
43521 | 795 |
// Default implementation of forEachRemaining is "good enough". |
796 |
||
2 | 797 |
public void remove() { |
798 |
Node<E> l = lastRet; |
|
799 |
if (l == null) throw new IllegalStateException(); |
|
800 |
// rely on a future traversal to relink. |
|
6672 | 801 |
l.item = null; |
2 | 802 |
lastRet = null; |
803 |
} |
|
804 |
} |
|
805 |
||
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 | 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 | 812 |
* the proper order, followed by a null |
813 |
*/ |
|
814 |
private void writeObject(java.io.ObjectOutputStream s) |
|
815 |
throws java.io.IOException { |
|
816 |
||
817 |
// Write out any hidden stuff |
|
818 |
s.defaultWriteObject(); |
|
819 |
||
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 | 824 |
s.writeObject(item); |
825 |
} |
|
826 |
||
827 |
// Use trailing null as sentinel |
|
828 |
s.writeObject(null); |
|
829 |
} |
|
830 |
||
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 | 837 |
*/ |
838 |
private void readObject(java.io.ObjectInputStream s) |
|
839 |
throws java.io.IOException, ClassNotFoundException { |
|
840 |
s.defaultReadObject(); |
|
6672 | 841 |
|
842 |
// Read in elements until trailing null sentinel found |
|
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 | 846 |
Node<E> newNode = new Node<E>((E) item); |
6672 | 847 |
if (h == null) |
848 |
h = t = newNode; |
|
43521 | 849 |
else |
850 |
t.appendRelaxed(t = newNode); |
|
2 | 851 |
} |
6672 | 852 |
if (h == null) |
43521 | 853 |
h = t = new Node<E>(); |
6672 | 854 |
head = h; |
855 |
tail = t; |
|
856 |
} |
|
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 | 866 |
Node<E> p, q; |
867 |
if ((p = current()) == null || (q = p.next) == null) |
|
868 |
return null; |
|
869 |
int i = 0, n = batch = Math.min(batch + 1, MAX_BATCH); |
|
870 |
Object[] a = null; |
|
871 |
do { |
|
872 |
final E e; |
|
873 |
if ((e = p.item) != null) { |
|
874 |
if (a == null) |
|
875 |
a = new Object[n]; |
|
876 |
a[i++] = e; |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
877 |
} |
43521 | 878 |
if (p == (p = q)) |
879 |
p = first(); |
|
880 |
} while (p != null && (q = p.next) != null && i < n); |
|
881 |
setCurrent(p); |
|
882 |
return (i == 0) ? null : |
|
883 |
Spliterators.spliterator(a, 0, i, (Spliterator.ORDERED | |
|
884 |
Spliterator.NONNULL | |
|
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 | 889 |
Objects.requireNonNull(action); |
890 |
final Node<E> p; |
|
891 |
if ((p = current()) != null) { |
|
892 |
current = null; |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
893 |
exhausted = true; |
43521 | 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 | 899 |
Objects.requireNonNull(action); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
900 |
Node<E> p; |
43521 | 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 | 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 | 917 |
private void setCurrent(Node<E> p) { |
918 |
if ((current = p) == null) |
|
919 |
exhausted = true; |
|
920 |
} |
|
921 |
||
922 |
private Node<E> current() { |
|
923 |
Node<E> p; |
|
924 |
if ((p = current) == null && !exhausted) |
|
925 |
setCurrent(p = first()); |
|
926 |
return p; |
|
927 |
} |
|
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 | 932 |
return (Spliterator.ORDERED | |
933 |
Spliterator.NONNULL | |
|
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 | 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 | 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 | 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 | 1014 |
if ((c != p && !tryCasSuccessor(pred, c, c = p)) |
1015 |
|| pAlive) { |
|
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 | 1029 |
* Runs action on each element found during a traversal starting at p. |
1030 |
* If p is null, the action is not run. |
|
1031 |
*/ |
|
1032 |
void forEachFrom(Consumer<? super E> action, Node<E> p) { |
|
1033 |
for (Node<E> pred = null; p != null; ) { |
|
1034 |
Node<E> q = p.next; |
|
1035 |
final E item; |
|
1036 |
if ((item = p.item) != null) { |
|
1037 |
action.accept(item); |
|
1038 |
pred = p; p = q; continue; |
|
1039 |
} |
|
1040 |
for (Node<E> c = p;; q = p.next) { |
|
1041 |
if (q == null || q.item != null) { |
|
1042 |
pred = skipDeadNodes(pred, c, p, q); p = q; break; |
|
1043 |
} |
|
1044 |
if (p == (p = q)) { pred = null; p = head; break; } |
|
1045 |
} |
|
1046 |
} |
|
1047 |
} |
|
1048 |
||
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 | 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 | 1060 |
static final VarHandle ITEM; |
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 | 1075 |
} |