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