author | dl |
Wed, 21 Dec 2016 14:26:52 -0800 | |
changeset 42927 | 1d31e540bfcb |
parent 40734 | 48879ea67e2a |
child 43521 | 60e247b8d9a4 |
permissions | -rw-r--r-- |
6672 | 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 |
|
6 |
* published by the Free Software Foundation. Oracle designates this |
|
7 |
* particular file as subject to the "Classpath" exception as provided |
|
8 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
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 |
* |
|
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/ |
6672 | 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; |
6672 | 40 |
import java.util.AbstractCollection; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
41 |
import java.util.Arrays; |
6672 | 42 |
import java.util.Collection; |
43 |
import java.util.Deque; |
|
44 |
import java.util.Iterator; |
|
45 |
import java.util.NoSuchElementException; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
46 |
import java.util.Objects; |
6672 | 47 |
import java.util.Queue; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
48 |
import java.util.Spliterator; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
49 |
import java.util.Spliterators; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
50 |
import java.util.function.Consumer; |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
51 |
import java.util.function.Predicate; |
6672 | 52 |
|
53 |
/** |
|
54 |
* An unbounded concurrent {@linkplain Deque deque} based on linked nodes. |
|
55 |
* Concurrent insertion, removal, and access operations execute safely |
|
56 |
* across multiple threads. |
|
57 |
* A {@code ConcurrentLinkedDeque} is an appropriate choice when |
|
58 |
* many threads will share access to a common collection. |
|
59 |
* Like most other concurrent collection implementations, this class |
|
60 |
* does not permit the use of {@code null} elements. |
|
61 |
* |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
62 |
* <p>Iterators and spliterators are |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
63 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
6672 | 64 |
* |
9515
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
65 |
* <p>Beware that, unlike in most collections, the {@code size} method |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
66 |
* is <em>NOT</em> a constant-time operation. Because of the |
6672 | 67 |
* asynchronous nature of these deques, determining the current number |
9515
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
68 |
* 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
|
69 |
* inaccurate results if this collection is modified during traversal. |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
70 |
* Additionally, the bulk operations {@code addAll}, |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
71 |
* {@code removeAll}, {@code retainAll}, {@code containsAll}, |
39040
a6f5ef42ecda
8159351: non-atomic "bulk" ops note in class javadoc for ConcurrentLinkedQueue, ConcurrentLinkedDeque, & LinkedTransferQueue should not include equals
dl
parents:
36936
diff
changeset
|
72 |
* and {@code toArray} are <em>not</em> guaranteed |
9515
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
73 |
* to be performed atomically. For example, an iterator operating |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
74 |
* concurrently with an {@code addAll} operation might view only some |
04056ec0f477
7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents:
9242
diff
changeset
|
75 |
* of the added elements. |
6672 | 76 |
* |
77 |
* <p>This class and its iterator implement all of the <em>optional</em> |
|
78 |
* methods of the {@link Deque} and {@link Iterator} interfaces. |
|
79 |
* |
|
80 |
* <p>Memory consistency effects: As with other concurrent collections, |
|
81 |
* actions in a thread prior to placing an object into a |
|
82 |
* {@code ConcurrentLinkedDeque} |
|
83 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
|
84 |
* actions subsequent to the access or removal of that element from |
|
85 |
* the {@code ConcurrentLinkedDeque} in another thread. |
|
86 |
* |
|
87 |
* <p>This class is a member of the |
|
88 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
89 |
* Java Collections Framework</a>. |
|
90 |
* |
|
91 |
* @since 1.7 |
|
92 |
* @author Doug Lea |
|
93 |
* @author Martin Buchholz |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
94 |
* @param <E> the type of elements held in this deque |
6672 | 95 |
*/ |
96 |
public class ConcurrentLinkedDeque<E> |
|
97 |
extends AbstractCollection<E> |
|
98 |
implements Deque<E>, java.io.Serializable { |
|
99 |
||
100 |
/* |
|
101 |
* This is an implementation of a concurrent lock-free deque |
|
102 |
* supporting interior removes but not interior insertions, as |
|
103 |
* required to support the entire Deque interface. |
|
104 |
* |
|
105 |
* We extend the techniques developed for ConcurrentLinkedQueue and |
|
106 |
* LinkedTransferQueue (see the internal docs for those classes). |
|
107 |
* Understanding the ConcurrentLinkedQueue implementation is a |
|
108 |
* prerequisite for understanding the implementation of this class. |
|
109 |
* |
|
110 |
* The data structure is a symmetrical doubly-linked "GC-robust" |
|
111 |
* linked list of nodes. We minimize the number of volatile writes |
|
112 |
* using two techniques: advancing multiple hops with a single CAS |
|
113 |
* and mixing volatile and non-volatile writes of the same memory |
|
114 |
* locations. |
|
115 |
* |
|
116 |
* A node contains the expected E ("item") and links to predecessor |
|
117 |
* ("prev") and successor ("next") nodes: |
|
118 |
* |
|
119 |
* class Node<E> { volatile Node<E> prev, next; volatile E item; } |
|
120 |
* |
|
121 |
* A node p is considered "live" if it contains a non-null item |
|
122 |
* (p.item != null). When an item is CASed to null, the item is |
|
123 |
* atomically logically deleted from the collection. |
|
124 |
* |
|
125 |
* At any time, there is precisely one "first" node with a null |
|
126 |
* prev reference that terminates any chain of prev references |
|
127 |
* starting at a live node. Similarly there is precisely one |
|
128 |
* "last" node terminating any chain of next references starting at |
|
129 |
* a live node. The "first" and "last" nodes may or may not be live. |
|
130 |
* The "first" and "last" nodes are always mutually reachable. |
|
131 |
* |
|
132 |
* A new element is added atomically by CASing the null prev or |
|
133 |
* next reference in the first or last node to a fresh node |
|
134 |
* containing the element. The element's node atomically becomes |
|
135 |
* "live" at that point. |
|
136 |
* |
|
137 |
* A node is considered "active" if it is a live node, or the |
|
138 |
* first or last node. Active nodes cannot be unlinked. |
|
139 |
* |
|
140 |
* A "self-link" is a next or prev reference that is the same node: |
|
141 |
* p.prev == p or p.next == p |
|
142 |
* Self-links are used in the node unlinking process. Active nodes |
|
143 |
* never have self-links. |
|
144 |
* |
|
145 |
* A node p is active if and only if: |
|
146 |
* |
|
147 |
* p.item != null || |
|
148 |
* (p.prev == null && p.next != p) || |
|
149 |
* (p.next == null && p.prev != p) |
|
150 |
* |
|
151 |
* The deque object has two node references, "head" and "tail". |
|
152 |
* The head and tail are only approximations to the first and last |
|
153 |
* nodes of the deque. The first node can always be found by |
|
154 |
* following prev pointers from head; likewise for tail. However, |
|
155 |
* it is permissible for head and tail to be referring to deleted |
|
156 |
* nodes that have been unlinked and so may not be reachable from |
|
157 |
* any live node. |
|
158 |
* |
|
159 |
* There are 3 stages of node deletion; |
|
160 |
* "logical deletion", "unlinking", and "gc-unlinking". |
|
161 |
* |
|
162 |
* 1. "logical deletion" by CASing item to null atomically removes |
|
163 |
* the element from the collection, and makes the containing node |
|
164 |
* eligible for unlinking. |
|
165 |
* |
|
166 |
* 2. "unlinking" makes a deleted node unreachable from active |
|
167 |
* nodes, and thus eventually reclaimable by GC. Unlinked nodes |
|
168 |
* may remain reachable indefinitely from an iterator. |
|
169 |
* |
|
170 |
* Physical node unlinking is merely an optimization (albeit a |
|
171 |
* critical one), and so can be performed at our convenience. At |
|
172 |
* any time, the set of live nodes maintained by prev and next |
|
173 |
* links are identical, that is, the live nodes found via next |
|
174 |
* links from the first node is equal to the elements found via |
|
175 |
* prev links from the last node. However, this is not true for |
|
176 |
* nodes that have already been logically deleted - such nodes may |
|
177 |
* be reachable in one direction only. |
|
178 |
* |
|
179 |
* 3. "gc-unlinking" takes unlinking further by making active |
|
180 |
* nodes unreachable from deleted nodes, making it easier for the |
|
181 |
* GC to reclaim future deleted nodes. This step makes the data |
|
182 |
* structure "gc-robust", as first described in detail by Boehm |
|
183 |
* (http://portal.acm.org/citation.cfm?doid=503272.503282). |
|
184 |
* |
|
185 |
* GC-unlinked nodes may remain reachable indefinitely from an |
|
186 |
* iterator, but unlike unlinked nodes, are never reachable from |
|
187 |
* head or tail. |
|
188 |
* |
|
189 |
* Making the data structure GC-robust will eliminate the risk of |
|
190 |
* unbounded memory retention with conservative GCs and is likely |
|
191 |
* to improve performance with generational GCs. |
|
192 |
* |
|
193 |
* When a node is dequeued at either end, e.g. via poll(), we would |
|
194 |
* like to break any references from the node to active nodes. We |
|
195 |
* develop further the use of self-links that was very effective in |
|
196 |
* other concurrent collection classes. The idea is to replace |
|
197 |
* prev and next pointers with special values that are interpreted |
|
198 |
* to mean off-the-list-at-one-end. These are approximations, but |
|
199 |
* good enough to preserve the properties we want in our |
|
200 |
* traversals, e.g. we guarantee that a traversal will never visit |
|
201 |
* the same element twice, but we don't guarantee whether a |
|
202 |
* traversal that runs out of elements will be able to see more |
|
203 |
* elements later after enqueues at that end. Doing gc-unlinking |
|
204 |
* safely is particularly tricky, since any node can be in use |
|
205 |
* indefinitely (for example by an iterator). We must ensure that |
|
206 |
* the nodes pointed at by head/tail never get gc-unlinked, since |
|
207 |
* head/tail are needed to get "back on track" by other nodes that |
|
208 |
* are gc-unlinked. gc-unlinking accounts for much of the |
|
209 |
* implementation complexity. |
|
210 |
* |
|
211 |
* Since neither unlinking nor gc-unlinking are necessary for |
|
212 |
* correctness, there are many implementation choices regarding |
|
213 |
* frequency (eagerness) of these operations. Since volatile |
|
214 |
* reads are likely to be much cheaper than CASes, saving CASes by |
|
215 |
* unlinking multiple adjacent nodes at a time may be a win. |
|
216 |
* gc-unlinking can be performed rarely and still be effective, |
|
217 |
* since it is most important that long chains of deleted nodes |
|
218 |
* are occasionally broken. |
|
219 |
* |
|
220 |
* The actual representation we use is that p.next == p means to |
|
221 |
* goto the first node (which in turn is reached by following prev |
|
222 |
* pointers from head), and p.next == null && p.prev == p means |
|
7518 | 223 |
* that the iteration is at an end and that p is a (static final) |
6672 | 224 |
* dummy node, NEXT_TERMINATOR, and not the last active node. |
225 |
* Finishing the iteration when encountering such a TERMINATOR is |
|
226 |
* good enough for read-only traversals, so such traversals can use |
|
227 |
* p.next == null as the termination condition. When we need to |
|
228 |
* find the last (active) node, for enqueueing a new node, we need |
|
229 |
* to check whether we have reached a TERMINATOR node; if so, |
|
230 |
* restart traversal from tail. |
|
231 |
* |
|
232 |
* The implementation is completely directionally symmetrical, |
|
233 |
* except that most public methods that iterate through the list |
|
234 |
* follow next pointers ("forward" direction). |
|
235 |
* |
|
236 |
* We believe (without full proof) that all single-element deque |
|
237 |
* operations (e.g., addFirst, peekLast, pollLast) are linearizable |
|
238 |
* (see Herlihy and Shavit's book). However, some combinations of |
|
239 |
* operations are known not to be linearizable. In particular, |
|
240 |
* when an addFirst(A) is racing with pollFirst() removing B, it is |
|
241 |
* possible for an observer iterating over the elements to observe |
|
242 |
* A B C and subsequently observe A C, even though no interior |
|
243 |
* removes are ever performed. Nevertheless, iterators behave |
|
244 |
* reasonably, providing the "weakly consistent" guarantees. |
|
245 |
* |
|
246 |
* Empirically, microbenchmarks suggest that this class adds about |
|
247 |
* 40% overhead relative to ConcurrentLinkedQueue, which feels as |
|
248 |
* good as we can hope for. |
|
249 |
*/ |
|
250 |
||
251 |
private static final long serialVersionUID = 876323262645176354L; |
|
252 |
||
253 |
/** |
|
254 |
* A node from which the first node on list (that is, the unique node p |
|
255 |
* with p.prev == null && p.next != p) can be reached in O(1) time. |
|
256 |
* Invariants: |
|
257 |
* - the first node is always O(1) reachable from head via prev links |
|
258 |
* - all live nodes are reachable from the first node via succ() |
|
259 |
* - head != null |
|
260 |
* - (tmp = head).next != tmp || tmp != head |
|
261 |
* - head is never gc-unlinked (but may be unlinked) |
|
262 |
* Non-invariants: |
|
263 |
* - head.item may or may not be null |
|
264 |
* - head may not be reachable from the first or last node, or from tail |
|
265 |
*/ |
|
266 |
private transient volatile Node<E> head; |
|
267 |
||
268 |
/** |
|
269 |
* A node from which the last node on list (that is, the unique node p |
|
270 |
* with p.next == null && p.prev != p) can be reached in O(1) time. |
|
271 |
* Invariants: |
|
272 |
* - the last node is always O(1) reachable from tail via next links |
|
273 |
* - all live nodes are reachable from the last node via pred() |
|
274 |
* - tail != null |
|
275 |
* - tail is never gc-unlinked (but may be unlinked) |
|
276 |
* Non-invariants: |
|
277 |
* - tail.item may or may not be null |
|
278 |
* - tail may not be reachable from the first or last node, or from head |
|
279 |
*/ |
|
280 |
private transient volatile Node<E> tail; |
|
281 |
||
7518 | 282 |
private static final Node<Object> PREV_TERMINATOR, NEXT_TERMINATOR; |
6672 | 283 |
|
284 |
@SuppressWarnings("unchecked") |
|
285 |
Node<E> prevTerminator() { |
|
286 |
return (Node<E>) PREV_TERMINATOR; |
|
287 |
} |
|
288 |
||
289 |
@SuppressWarnings("unchecked") |
|
290 |
Node<E> nextTerminator() { |
|
291 |
return (Node<E>) NEXT_TERMINATOR; |
|
292 |
} |
|
293 |
||
294 |
static final class Node<E> { |
|
295 |
volatile Node<E> prev; |
|
296 |
volatile E item; |
|
297 |
volatile Node<E> next; |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
298 |
} |
6672 | 299 |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
300 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
301 |
* Returns a new node holding item. Uses relaxed write because item |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
302 |
* can only be seen after piggy-backing publication via CAS. |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
303 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
304 |
static <E> Node<E> newNode(E item) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
305 |
Node<E> node = new Node<E>(); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
306 |
ITEM.set(node, item); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
307 |
return node; |
6672 | 308 |
} |
309 |
||
310 |
/** |
|
311 |
* Links e as first element. |
|
312 |
*/ |
|
313 |
private void linkFirst(E e) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
314 |
final Node<E> newNode = newNode(Objects.requireNonNull(e)); |
6672 | 315 |
|
316 |
restartFromHead: |
|
317 |
for (;;) |
|
318 |
for (Node<E> h = head, p = h, q;;) { |
|
319 |
if ((q = p.prev) != null && |
|
320 |
(q = (p = q).prev) != null) |
|
321 |
// Check for head updates every other hop. |
|
322 |
// If p == q, we are sure to follow head instead. |
|
323 |
p = (h != (h = head)) ? h : q; |
|
324 |
else if (p.next == p) // PREV_TERMINATOR |
|
325 |
continue restartFromHead; |
|
326 |
else { |
|
327 |
// p is first node |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
328 |
NEXT.set(newNode, p); // CAS piggyback |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
329 |
if (PREV.compareAndSet(p, null, newNode)) { |
6672 | 330 |
// Successful CAS is the linearization point |
331 |
// for e to become an element of this deque, |
|
332 |
// and for newNode to become "live". |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
333 |
if (p != h) // hop two nodes at a time; failure is OK |
40734
48879ea67e2a
8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents:
39725
diff
changeset
|
334 |
HEAD.weakCompareAndSet(this, h, newNode); |
6672 | 335 |
return; |
336 |
} |
|
337 |
// Lost CAS race to another thread; re-read prev |
|
338 |
} |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
/** |
|
343 |
* Links e as last element. |
|
344 |
*/ |
|
345 |
private void linkLast(E e) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
346 |
final Node<E> newNode = newNode(Objects.requireNonNull(e)); |
6672 | 347 |
|
348 |
restartFromTail: |
|
349 |
for (;;) |
|
350 |
for (Node<E> t = tail, p = t, q;;) { |
|
351 |
if ((q = p.next) != null && |
|
352 |
(q = (p = q).next) != null) |
|
353 |
// Check for tail updates every other hop. |
|
354 |
// If p == q, we are sure to follow tail instead. |
|
355 |
p = (t != (t = tail)) ? t : q; |
|
356 |
else if (p.prev == p) // NEXT_TERMINATOR |
|
357 |
continue restartFromTail; |
|
358 |
else { |
|
359 |
// p is last node |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
360 |
PREV.set(newNode, p); // CAS piggyback |
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 deque, |
|
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); |
6672 | 367 |
return; |
368 |
} |
|
369 |
// Lost CAS race to another thread; re-read next |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
||
7518 | 374 |
private static final int HOPS = 2; |
6672 | 375 |
|
376 |
/** |
|
377 |
* Unlinks non-null node x. |
|
378 |
*/ |
|
379 |
void unlink(Node<E> x) { |
|
380 |
// assert x != null; |
|
381 |
// assert x.item == null; |
|
382 |
// assert x != PREV_TERMINATOR; |
|
383 |
// assert x != NEXT_TERMINATOR; |
|
384 |
||
385 |
final Node<E> prev = x.prev; |
|
386 |
final Node<E> next = x.next; |
|
387 |
if (prev == null) { |
|
388 |
unlinkFirst(x, next); |
|
389 |
} else if (next == null) { |
|
390 |
unlinkLast(x, prev); |
|
391 |
} else { |
|
392 |
// Unlink interior node. |
|
393 |
// |
|
394 |
// This is the common case, since a series of polls at the |
|
395 |
// same end will be "interior" removes, except perhaps for |
|
396 |
// the first one, since end nodes cannot be unlinked. |
|
397 |
// |
|
398 |
// At any time, all active nodes are mutually reachable by |
|
399 |
// following a sequence of either next or prev pointers. |
|
400 |
// |
|
401 |
// Our strategy is to find the unique active predecessor |
|
402 |
// and successor of x. Try to fix up their links so that |
|
403 |
// they point to each other, leaving x unreachable from |
|
404 |
// active nodes. If successful, and if x has no live |
|
405 |
// predecessor/successor, we additionally try to gc-unlink, |
|
406 |
// leaving active nodes unreachable from x, by rechecking |
|
407 |
// that the status of predecessor and successor are |
|
408 |
// unchanged and ensuring that x is not reachable from |
|
409 |
// tail/head, before setting x's prev/next links to their |
|
410 |
// logical approximate replacements, self/TERMINATOR. |
|
411 |
Node<E> activePred, activeSucc; |
|
412 |
boolean isFirst, isLast; |
|
413 |
int hops = 1; |
|
414 |
||
415 |
// Find active predecessor |
|
416 |
for (Node<E> p = prev; ; ++hops) { |
|
417 |
if (p.item != null) { |
|
418 |
activePred = p; |
|
419 |
isFirst = false; |
|
420 |
break; |
|
421 |
} |
|
422 |
Node<E> q = p.prev; |
|
423 |
if (q == null) { |
|
424 |
if (p.next == p) |
|
425 |
return; |
|
426 |
activePred = p; |
|
427 |
isFirst = true; |
|
428 |
break; |
|
429 |
} |
|
430 |
else if (p == q) |
|
431 |
return; |
|
432 |
else |
|
433 |
p = q; |
|
434 |
} |
|
435 |
||
436 |
// Find active successor |
|
437 |
for (Node<E> p = next; ; ++hops) { |
|
438 |
if (p.item != null) { |
|
439 |
activeSucc = p; |
|
440 |
isLast = false; |
|
441 |
break; |
|
442 |
} |
|
443 |
Node<E> q = p.next; |
|
444 |
if (q == null) { |
|
445 |
if (p.prev == p) |
|
446 |
return; |
|
447 |
activeSucc = p; |
|
448 |
isLast = true; |
|
449 |
break; |
|
450 |
} |
|
451 |
else if (p == q) |
|
452 |
return; |
|
453 |
else |
|
454 |
p = q; |
|
455 |
} |
|
456 |
||
457 |
// TODO: better HOP heuristics |
|
458 |
if (hops < HOPS |
|
459 |
// always squeeze out interior deleted nodes |
|
460 |
&& (isFirst | isLast)) |
|
461 |
return; |
|
462 |
||
463 |
// Squeeze out deleted nodes between activePred and |
|
464 |
// activeSucc, including x. |
|
465 |
skipDeletedSuccessors(activePred); |
|
466 |
skipDeletedPredecessors(activeSucc); |
|
467 |
||
468 |
// Try to gc-unlink, if possible |
|
469 |
if ((isFirst | isLast) && |
|
470 |
||
471 |
// Recheck expected state of predecessor and successor |
|
472 |
(activePred.next == activeSucc) && |
|
473 |
(activeSucc.prev == activePred) && |
|
474 |
(isFirst ? activePred.prev == null : activePred.item != null) && |
|
475 |
(isLast ? activeSucc.next == null : activeSucc.item != null)) { |
|
476 |
||
477 |
updateHead(); // Ensure x is not reachable from head |
|
478 |
updateTail(); // Ensure x is not reachable from tail |
|
479 |
||
480 |
// Finally, actually gc-unlink |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
481 |
PREV.setRelease(x, isFirst ? prevTerminator() : x); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
482 |
NEXT.setRelease(x, isLast ? nextTerminator() : x); |
6672 | 483 |
} |
484 |
} |
|
485 |
} |
|
486 |
||
487 |
/** |
|
488 |
* Unlinks non-null first node. |
|
489 |
*/ |
|
490 |
private void unlinkFirst(Node<E> first, Node<E> next) { |
|
491 |
// assert first != null; |
|
492 |
// assert next != null; |
|
493 |
// assert first.item == null; |
|
494 |
for (Node<E> o = null, p = next, q;;) { |
|
495 |
if (p.item != null || (q = p.next) == null) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
496 |
if (o != null && p.prev != p && |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
497 |
NEXT.compareAndSet(first, next, p)) { |
6672 | 498 |
skipDeletedPredecessors(p); |
499 |
if (first.prev == null && |
|
500 |
(p.next == null || p.item != null) && |
|
501 |
p.prev == first) { |
|
502 |
||
503 |
updateHead(); // Ensure o is not reachable from head |
|
504 |
updateTail(); // Ensure o is not reachable from tail |
|
505 |
||
506 |
// Finally, actually gc-unlink |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
507 |
NEXT.setRelease(o, o); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
508 |
PREV.setRelease(o, prevTerminator()); |
6672 | 509 |
} |
510 |
} |
|
511 |
return; |
|
512 |
} |
|
513 |
else if (p == q) |
|
514 |
return; |
|
515 |
else { |
|
516 |
o = p; |
|
517 |
p = q; |
|
518 |
} |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Unlinks non-null last node. |
|
524 |
*/ |
|
525 |
private void unlinkLast(Node<E> last, Node<E> prev) { |
|
526 |
// assert last != null; |
|
527 |
// assert prev != null; |
|
528 |
// assert last.item == null; |
|
529 |
for (Node<E> o = null, p = prev, q;;) { |
|
530 |
if (p.item != null || (q = p.prev) == null) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
531 |
if (o != null && p.next != p && |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
532 |
PREV.compareAndSet(last, prev, p)) { |
6672 | 533 |
skipDeletedSuccessors(p); |
534 |
if (last.next == null && |
|
535 |
(p.prev == null || p.item != null) && |
|
536 |
p.next == last) { |
|
537 |
||
538 |
updateHead(); // Ensure o is not reachable from head |
|
539 |
updateTail(); // Ensure o is not reachable from tail |
|
540 |
||
541 |
// Finally, actually gc-unlink |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
542 |
PREV.setRelease(o, o); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
543 |
NEXT.setRelease(o, nextTerminator()); |
6672 | 544 |
} |
545 |
} |
|
546 |
return; |
|
547 |
} |
|
548 |
else if (p == q) |
|
549 |
return; |
|
550 |
else { |
|
551 |
o = p; |
|
552 |
p = q; |
|
553 |
} |
|
554 |
} |
|
555 |
} |
|
556 |
||
557 |
/** |
|
558 |
* Guarantees that any node which was unlinked before a call to |
|
559 |
* this method will be unreachable from head after it returns. |
|
560 |
* Does not guarantee to eliminate slack, only that head will |
|
561 |
* point to a node that was active while this method was running. |
|
562 |
*/ |
|
563 |
private final void updateHead() { |
|
564 |
// Either head already points to an active node, or we keep |
|
565 |
// trying to cas it to the first node until it does. |
|
566 |
Node<E> h, p, q; |
|
567 |
restartFromHead: |
|
568 |
while ((h = head).item == null && (p = h.prev) != null) { |
|
569 |
for (;;) { |
|
570 |
if ((q = p.prev) == null || |
|
571 |
(q = (p = q).prev) == null) { |
|
572 |
// It is possible that p is PREV_TERMINATOR, |
|
573 |
// but if so, the CAS is guaranteed to fail. |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
574 |
if (HEAD.compareAndSet(this, h, p)) |
6672 | 575 |
return; |
576 |
else |
|
577 |
continue restartFromHead; |
|
578 |
} |
|
579 |
else if (h != head) |
|
580 |
continue restartFromHead; |
|
581 |
else |
|
582 |
p = q; |
|
583 |
} |
|
584 |
} |
|
585 |
} |
|
586 |
||
587 |
/** |
|
588 |
* Guarantees that any node which was unlinked before a call to |
|
589 |
* this method will be unreachable from tail after it returns. |
|
590 |
* Does not guarantee to eliminate slack, only that tail will |
|
591 |
* point to a node that was active while this method was running. |
|
592 |
*/ |
|
593 |
private final void updateTail() { |
|
594 |
// Either tail already points to an active node, or we keep |
|
595 |
// trying to cas it to the last node until it does. |
|
596 |
Node<E> t, p, q; |
|
597 |
restartFromTail: |
|
598 |
while ((t = tail).item == null && (p = t.next) != null) { |
|
599 |
for (;;) { |
|
600 |
if ((q = p.next) == null || |
|
601 |
(q = (p = q).next) == null) { |
|
602 |
// It is possible that p is NEXT_TERMINATOR, |
|
603 |
// but if so, the CAS is guaranteed to fail. |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
604 |
if (TAIL.compareAndSet(this, t, p)) |
6672 | 605 |
return; |
606 |
else |
|
607 |
continue restartFromTail; |
|
608 |
} |
|
609 |
else if (t != tail) |
|
610 |
continue restartFromTail; |
|
611 |
else |
|
612 |
p = q; |
|
613 |
} |
|
614 |
} |
|
615 |
} |
|
616 |
||
617 |
private void skipDeletedPredecessors(Node<E> x) { |
|
618 |
whileActive: |
|
619 |
do { |
|
620 |
Node<E> prev = x.prev; |
|
621 |
// assert prev != null; |
|
622 |
// assert x != NEXT_TERMINATOR; |
|
623 |
// assert x != PREV_TERMINATOR; |
|
624 |
Node<E> p = prev; |
|
625 |
findActive: |
|
626 |
for (;;) { |
|
627 |
if (p.item != null) |
|
628 |
break findActive; |
|
629 |
Node<E> q = p.prev; |
|
630 |
if (q == null) { |
|
631 |
if (p.next == p) |
|
632 |
continue whileActive; |
|
633 |
break findActive; |
|
634 |
} |
|
635 |
else if (p == q) |
|
636 |
continue whileActive; |
|
637 |
else |
|
638 |
p = q; |
|
639 |
} |
|
640 |
||
641 |
// found active CAS target |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
642 |
if (prev == p || PREV.compareAndSet(x, prev, p)) |
6672 | 643 |
return; |
644 |
||
645 |
} while (x.item != null || x.next == null); |
|
646 |
} |
|
647 |
||
648 |
private void skipDeletedSuccessors(Node<E> x) { |
|
649 |
whileActive: |
|
650 |
do { |
|
651 |
Node<E> next = x.next; |
|
652 |
// assert next != null; |
|
653 |
// assert x != NEXT_TERMINATOR; |
|
654 |
// assert x != PREV_TERMINATOR; |
|
655 |
Node<E> p = next; |
|
656 |
findActive: |
|
657 |
for (;;) { |
|
658 |
if (p.item != null) |
|
659 |
break findActive; |
|
660 |
Node<E> q = p.next; |
|
661 |
if (q == null) { |
|
662 |
if (p.prev == p) |
|
663 |
continue whileActive; |
|
664 |
break findActive; |
|
665 |
} |
|
666 |
else if (p == q) |
|
667 |
continue whileActive; |
|
668 |
else |
|
669 |
p = q; |
|
670 |
} |
|
671 |
||
672 |
// found active CAS target |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
673 |
if (next == p || NEXT.compareAndSet(x, next, p)) |
6672 | 674 |
return; |
675 |
||
676 |
} while (x.item != null || x.prev == null); |
|
677 |
} |
|
678 |
||
679 |
/** |
|
680 |
* Returns the successor of p, or the first node if p.next has been |
|
681 |
* linked to self, which will only be true if traversing with a |
|
682 |
* stale pointer that is now off the list. |
|
683 |
*/ |
|
684 |
final Node<E> succ(Node<E> p) { |
|
685 |
// TODO: should we skip deleted nodes here? |
|
686 |
Node<E> q = p.next; |
|
687 |
return (p == q) ? first() : q; |
|
688 |
} |
|
689 |
||
690 |
/** |
|
691 |
* Returns the predecessor of p, or the last node if p.prev has been |
|
692 |
* linked to self, which will only be true if traversing with a |
|
693 |
* stale pointer that is now off the list. |
|
694 |
*/ |
|
695 |
final Node<E> pred(Node<E> p) { |
|
696 |
Node<E> q = p.prev; |
|
697 |
return (p == q) ? last() : q; |
|
698 |
} |
|
699 |
||
700 |
/** |
|
701 |
* Returns the first node, the unique node p for which: |
|
702 |
* p.prev == null && p.next != p |
|
703 |
* The returned node may or may not be logically deleted. |
|
704 |
* Guarantees that head is set to the returned node. |
|
705 |
*/ |
|
706 |
Node<E> first() { |
|
707 |
restartFromHead: |
|
708 |
for (;;) |
|
709 |
for (Node<E> h = head, p = h, q;;) { |
|
710 |
if ((q = p.prev) != null && |
|
711 |
(q = (p = q).prev) != null) |
|
712 |
// Check for head updates every other hop. |
|
713 |
// If p == q, we are sure to follow head instead. |
|
714 |
p = (h != (h = head)) ? h : q; |
|
715 |
else if (p == h |
|
716 |
// It is possible that p is PREV_TERMINATOR, |
|
717 |
// but if so, the CAS is guaranteed to fail. |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
718 |
|| HEAD.compareAndSet(this, h, p)) |
6672 | 719 |
return p; |
720 |
else |
|
721 |
continue restartFromHead; |
|
722 |
} |
|
723 |
} |
|
724 |
||
725 |
/** |
|
726 |
* Returns the last node, the unique node p for which: |
|
727 |
* p.next == null && p.prev != p |
|
728 |
* The returned node may or may not be logically deleted. |
|
729 |
* Guarantees that tail is set to the returned node. |
|
730 |
*/ |
|
731 |
Node<E> last() { |
|
732 |
restartFromTail: |
|
733 |
for (;;) |
|
734 |
for (Node<E> t = tail, p = t, q;;) { |
|
735 |
if ((q = p.next) != null && |
|
736 |
(q = (p = q).next) != null) |
|
737 |
// Check for tail updates every other hop. |
|
738 |
// If p == q, we are sure to follow tail instead. |
|
739 |
p = (t != (t = tail)) ? t : q; |
|
740 |
else if (p == t |
|
741 |
// It is possible that p is NEXT_TERMINATOR, |
|
742 |
// but if so, the CAS is guaranteed to fail. |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
743 |
|| TAIL.compareAndSet(this, t, p)) |
6672 | 744 |
return p; |
745 |
else |
|
746 |
continue restartFromTail; |
|
747 |
} |
|
748 |
} |
|
749 |
||
750 |
// Minor convenience utilities |
|
751 |
||
752 |
/** |
|
753 |
* Returns element unless it is null, in which case throws |
|
754 |
* NoSuchElementException. |
|
755 |
* |
|
756 |
* @param v the element |
|
757 |
* @return the element |
|
758 |
*/ |
|
759 |
private E screenNullResult(E v) { |
|
760 |
if (v == null) |
|
761 |
throw new NoSuchElementException(); |
|
762 |
return v; |
|
763 |
} |
|
764 |
||
765 |
/** |
|
766 |
* Constructs an empty deque. |
|
767 |
*/ |
|
768 |
public ConcurrentLinkedDeque() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
769 |
head = tail = new Node<E>(); |
6672 | 770 |
} |
771 |
||
772 |
/** |
|
773 |
* Constructs a deque initially containing the elements of |
|
774 |
* the given collection, added in traversal order of the |
|
775 |
* collection's iterator. |
|
776 |
* |
|
777 |
* @param c the collection of elements to initially contain |
|
778 |
* @throws NullPointerException if the specified collection or any |
|
779 |
* of its elements are null |
|
780 |
*/ |
|
781 |
public ConcurrentLinkedDeque(Collection<? extends E> c) { |
|
782 |
// Copy c into a private chain of Nodes |
|
783 |
Node<E> h = null, t = null; |
|
784 |
for (E e : c) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
785 |
Node<E> newNode = newNode(Objects.requireNonNull(e)); |
6672 | 786 |
if (h == null) |
787 |
h = t = newNode; |
|
788 |
else { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
789 |
NEXT.set(t, newNode); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
790 |
PREV.set(newNode, t); |
6672 | 791 |
t = newNode; |
792 |
} |
|
793 |
} |
|
794 |
initHeadTail(h, t); |
|
795 |
} |
|
796 |
||
797 |
/** |
|
798 |
* Initializes head and tail, ensuring invariants hold. |
|
799 |
*/ |
|
800 |
private void initHeadTail(Node<E> h, Node<E> t) { |
|
801 |
if (h == t) { |
|
802 |
if (h == null) |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
803 |
h = t = new Node<E>(); |
6672 | 804 |
else { |
805 |
// Avoid edge case of a single Node with non-null item. |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
806 |
Node<E> newNode = new Node<E>(); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
807 |
NEXT.set(t, newNode); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
808 |
PREV.set(newNode, t); |
6672 | 809 |
t = newNode; |
810 |
} |
|
811 |
} |
|
812 |
head = h; |
|
813 |
tail = t; |
|
814 |
} |
|
815 |
||
816 |
/** |
|
817 |
* Inserts the specified element at the front of this deque. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
818 |
* As the deque is unbounded, this method will never throw |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
819 |
* {@link IllegalStateException}. |
6672 | 820 |
* |
7518 | 821 |
* @throws NullPointerException if the specified element is null |
6672 | 822 |
*/ |
823 |
public void addFirst(E e) { |
|
824 |
linkFirst(e); |
|
825 |
} |
|
826 |
||
827 |
/** |
|
828 |
* Inserts the specified element at the end of this deque. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
829 |
* As the deque is unbounded, this method will never throw |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
830 |
* {@link IllegalStateException}. |
6672 | 831 |
* |
832 |
* <p>This method is equivalent to {@link #add}. |
|
833 |
* |
|
7518 | 834 |
* @throws NullPointerException if the specified element is null |
6672 | 835 |
*/ |
836 |
public void addLast(E e) { |
|
837 |
linkLast(e); |
|
838 |
} |
|
839 |
||
840 |
/** |
|
841 |
* Inserts the specified element at the front of this deque. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
842 |
* As the deque is unbounded, this method will never return {@code false}. |
6672 | 843 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
844 |
* @return {@code true} (as specified by {@link Deque#offerFirst}) |
7518 | 845 |
* @throws NullPointerException if the specified element is null |
6672 | 846 |
*/ |
847 |
public boolean offerFirst(E e) { |
|
848 |
linkFirst(e); |
|
849 |
return true; |
|
850 |
} |
|
851 |
||
852 |
/** |
|
853 |
* Inserts the specified element at the end of this deque. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
854 |
* As the deque is unbounded, this method will never return {@code false}. |
6672 | 855 |
* |
856 |
* <p>This method is equivalent to {@link #add}. |
|
857 |
* |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
858 |
* @return {@code true} (as specified by {@link Deque#offerLast}) |
7518 | 859 |
* @throws NullPointerException if the specified element is null |
6672 | 860 |
*/ |
861 |
public boolean offerLast(E e) { |
|
862 |
linkLast(e); |
|
863 |
return true; |
|
864 |
} |
|
865 |
||
866 |
public E peekFirst() { |
|
867 |
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
|
868 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
869 |
if ((item = p.item) != null) |
6672 | 870 |
return item; |
871 |
} |
|
872 |
return null; |
|
873 |
} |
|
874 |
||
875 |
public E peekLast() { |
|
876 |
for (Node<E> p = last(); p != null; p = pred(p)) { |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
877 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
878 |
if ((item = p.item) != null) |
6672 | 879 |
return item; |
880 |
} |
|
881 |
return null; |
|
882 |
} |
|
883 |
||
884 |
/** |
|
885 |
* @throws NoSuchElementException {@inheritDoc} |
|
886 |
*/ |
|
887 |
public E getFirst() { |
|
888 |
return screenNullResult(peekFirst()); |
|
889 |
} |
|
890 |
||
891 |
/** |
|
892 |
* @throws NoSuchElementException {@inheritDoc} |
|
893 |
*/ |
|
7518 | 894 |
public E getLast() { |
6672 | 895 |
return screenNullResult(peekLast()); |
896 |
} |
|
897 |
||
898 |
public E pollFirst() { |
|
899 |
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
|
900 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
901 |
if ((item = p.item) != null |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
902 |
&& ITEM.compareAndSet(p, item, null)) { |
6672 | 903 |
unlink(p); |
904 |
return item; |
|
905 |
} |
|
906 |
} |
|
907 |
return null; |
|
908 |
} |
|
909 |
||
910 |
public E pollLast() { |
|
911 |
for (Node<E> p = last(); p != null; p = pred(p)) { |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
912 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
913 |
if ((item = p.item) != null |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
914 |
&& ITEM.compareAndSet(p, item, null)) { |
6672 | 915 |
unlink(p); |
916 |
return item; |
|
917 |
} |
|
918 |
} |
|
919 |
return null; |
|
920 |
} |
|
921 |
||
922 |
/** |
|
923 |
* @throws NoSuchElementException {@inheritDoc} |
|
924 |
*/ |
|
925 |
public E removeFirst() { |
|
926 |
return screenNullResult(pollFirst()); |
|
927 |
} |
|
928 |
||
929 |
/** |
|
930 |
* @throws NoSuchElementException {@inheritDoc} |
|
931 |
*/ |
|
932 |
public E removeLast() { |
|
933 |
return screenNullResult(pollLast()); |
|
934 |
} |
|
935 |
||
936 |
// *** Queue and stack methods *** |
|
937 |
||
938 |
/** |
|
939 |
* Inserts the specified element at the tail of this deque. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
940 |
* As the deque is unbounded, this method will never return {@code false}. |
6672 | 941 |
* |
942 |
* @return {@code true} (as specified by {@link Queue#offer}) |
|
943 |
* @throws NullPointerException if the specified element is null |
|
944 |
*/ |
|
945 |
public boolean offer(E e) { |
|
946 |
return offerLast(e); |
|
947 |
} |
|
948 |
||
949 |
/** |
|
950 |
* Inserts the specified element at the tail of this deque. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
951 |
* As the deque is unbounded, this method will never throw |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
952 |
* {@link IllegalStateException} or return {@code false}. |
6672 | 953 |
* |
954 |
* @return {@code true} (as specified by {@link Collection#add}) |
|
955 |
* @throws NullPointerException if the specified element is null |
|
956 |
*/ |
|
957 |
public boolean add(E e) { |
|
958 |
return offerLast(e); |
|
959 |
} |
|
960 |
||
961 |
public E poll() { return pollFirst(); } |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
962 |
public E peek() { return peekFirst(); } |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
963 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
964 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
965 |
* @throws NoSuchElementException {@inheritDoc} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
966 |
*/ |
6672 | 967 |
public E remove() { return removeFirst(); } |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
968 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
969 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
970 |
* @throws NoSuchElementException {@inheritDoc} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
971 |
*/ |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
972 |
public E pop() { return removeFirst(); } |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
973 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
974 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
975 |
* @throws NoSuchElementException {@inheritDoc} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
976 |
*/ |
6672 | 977 |
public E element() { return getFirst(); } |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
978 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
979 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
980 |
* @throws NullPointerException {@inheritDoc} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
981 |
*/ |
6672 | 982 |
public void push(E e) { addFirst(e); } |
983 |
||
984 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
985 |
* Removes the first occurrence of the specified element from this deque. |
6672 | 986 |
* If the deque does not contain the element, it is unchanged. |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
987 |
* More formally, removes the first element {@code e} such that |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
988 |
* {@code o.equals(e)} (if such an element exists). |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
989 |
* Returns {@code true} if this deque contained the specified element |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
990 |
* (or equivalently, if this deque changed as a result of the call). |
6672 | 991 |
* |
992 |
* @param o element to be removed from this deque, if present |
|
993 |
* @return {@code true} if the deque contained the specified element |
|
7518 | 994 |
* @throws NullPointerException if the specified element is null |
6672 | 995 |
*/ |
996 |
public boolean removeFirstOccurrence(Object o) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
997 |
Objects.requireNonNull(o); |
6672 | 998 |
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
|
999 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1000 |
if ((item = p.item) != null |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1001 |
&& o.equals(item) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1002 |
&& ITEM.compareAndSet(p, item, null)) { |
6672 | 1003 |
unlink(p); |
1004 |
return true; |
|
1005 |
} |
|
1006 |
} |
|
1007 |
return false; |
|
1008 |
} |
|
1009 |
||
1010 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1011 |
* Removes the last occurrence of the specified element from this deque. |
6672 | 1012 |
* If the deque does not contain the element, it is unchanged. |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1013 |
* More formally, removes the last element {@code e} such that |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1014 |
* {@code o.equals(e)} (if such an element exists). |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1015 |
* Returns {@code true} if this deque contained the specified element |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1016 |
* (or equivalently, if this deque changed as a result of the call). |
6672 | 1017 |
* |
1018 |
* @param o element to be removed from this deque, if present |
|
1019 |
* @return {@code true} if the deque contained the specified element |
|
7518 | 1020 |
* @throws NullPointerException if the specified element is null |
6672 | 1021 |
*/ |
1022 |
public boolean removeLastOccurrence(Object o) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1023 |
Objects.requireNonNull(o); |
6672 | 1024 |
for (Node<E> p = last(); p != null; p = pred(p)) { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1025 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1026 |
if ((item = p.item) != null |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1027 |
&& o.equals(item) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1028 |
&& ITEM.compareAndSet(p, item, null)) { |
6672 | 1029 |
unlink(p); |
1030 |
return true; |
|
1031 |
} |
|
1032 |
} |
|
1033 |
return false; |
|
1034 |
} |
|
1035 |
||
1036 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1037 |
* Returns {@code true} if this deque contains the specified element. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1038 |
* More formally, returns {@code true} if and only if this deque contains |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1039 |
* at least one element {@code e} such that {@code o.equals(e)}. |
6672 | 1040 |
* |
1041 |
* @param o element whose presence in this deque is to be tested |
|
1042 |
* @return {@code true} if this deque contains the specified element |
|
1043 |
*/ |
|
1044 |
public boolean contains(Object o) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1045 |
if (o != null) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1046 |
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
|
1047 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1048 |
if ((item = p.item) != null && o.equals(item)) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1049 |
return true; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1050 |
} |
6672 | 1051 |
} |
1052 |
return false; |
|
1053 |
} |
|
1054 |
||
1055 |
/** |
|
1056 |
* Returns {@code true} if this collection contains no elements. |
|
1057 |
* |
|
1058 |
* @return {@code true} if this collection contains no elements |
|
1059 |
*/ |
|
1060 |
public boolean isEmpty() { |
|
1061 |
return peekFirst() == null; |
|
1062 |
} |
|
1063 |
||
1064 |
/** |
|
1065 |
* Returns the number of elements in this deque. If this deque |
|
1066 |
* contains more than {@code Integer.MAX_VALUE} elements, it |
|
1067 |
* returns {@code Integer.MAX_VALUE}. |
|
1068 |
* |
|
1069 |
* <p>Beware that, unlike in most collections, this method is |
|
1070 |
* <em>NOT</em> a constant-time operation. Because of the |
|
1071 |
* asynchronous nature of these deques, determining the current |
|
1072 |
* number of elements requires traversing them all to count them. |
|
1073 |
* Additionally, it is possible for the size to change during |
|
1074 |
* execution of this method, in which case the returned result |
|
1075 |
* will be inaccurate. Thus, this method is typically not very |
|
1076 |
* useful in concurrent applications. |
|
1077 |
* |
|
1078 |
* @return the number of elements in this deque |
|
1079 |
*/ |
|
1080 |
public int size() { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1081 |
restartFromHead: for (;;) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1082 |
int count = 0; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1083 |
for (Node<E> p = first(); p != null;) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1084 |
if (p.item != null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1085 |
if (++count == Integer.MAX_VALUE) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1086 |
break; // @see Collection.size() |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1087 |
if (p == (p = p.next)) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1088 |
continue restartFromHead; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1089 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1090 |
return count; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1091 |
} |
6672 | 1092 |
} |
1093 |
||
1094 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1095 |
* Removes the first occurrence of the specified element from this deque. |
6672 | 1096 |
* If the deque does not contain the element, it is unchanged. |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1097 |
* More formally, removes the first element {@code e} such that |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1098 |
* {@code o.equals(e)} (if such an element exists). |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1099 |
* Returns {@code true} if this deque contained the specified element |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1100 |
* (or equivalently, if this deque changed as a result of the call). |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1101 |
* |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1102 |
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. |
6672 | 1103 |
* |
1104 |
* @param o element to be removed from this deque, if present |
|
1105 |
* @return {@code true} if the deque contained the specified element |
|
7518 | 1106 |
* @throws NullPointerException if the specified element is null |
6672 | 1107 |
*/ |
1108 |
public boolean remove(Object o) { |
|
1109 |
return removeFirstOccurrence(o); |
|
1110 |
} |
|
1111 |
||
1112 |
/** |
|
1113 |
* Appends all of the elements in the specified collection to the end of |
|
1114 |
* this deque, in the order that they are returned by the specified |
|
1115 |
* collection's iterator. Attempts to {@code addAll} of a deque to |
|
1116 |
* itself result in {@code IllegalArgumentException}. |
|
1117 |
* |
|
1118 |
* @param c the elements to be inserted into this deque |
|
1119 |
* @return {@code true} if this deque changed as a result of the call |
|
1120 |
* @throws NullPointerException if the specified collection or any |
|
1121 |
* of its elements are null |
|
1122 |
* @throws IllegalArgumentException if the collection is this deque |
|
1123 |
*/ |
|
1124 |
public boolean addAll(Collection<? extends E> c) { |
|
1125 |
if (c == this) |
|
1126 |
// As historically specified in AbstractQueue#addAll |
|
1127 |
throw new IllegalArgumentException(); |
|
1128 |
||
1129 |
// Copy c into a private chain of Nodes |
|
1130 |
Node<E> beginningOfTheEnd = null, last = null; |
|
1131 |
for (E e : c) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1132 |
Node<E> newNode = newNode(Objects.requireNonNull(e)); |
6672 | 1133 |
if (beginningOfTheEnd == null) |
1134 |
beginningOfTheEnd = last = newNode; |
|
1135 |
else { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1136 |
NEXT.set(last, newNode); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1137 |
PREV.set(newNode, last); |
6672 | 1138 |
last = newNode; |
1139 |
} |
|
1140 |
} |
|
1141 |
if (beginningOfTheEnd == null) |
|
1142 |
return false; |
|
1143 |
||
1144 |
// Atomically append the chain at the tail of this collection |
|
1145 |
restartFromTail: |
|
1146 |
for (;;) |
|
1147 |
for (Node<E> t = tail, p = t, q;;) { |
|
1148 |
if ((q = p.next) != null && |
|
1149 |
(q = (p = q).next) != null) |
|
1150 |
// Check for tail updates every other hop. |
|
1151 |
// If p == q, we are sure to follow tail instead. |
|
1152 |
p = (t != (t = tail)) ? t : q; |
|
1153 |
else if (p.prev == p) // NEXT_TERMINATOR |
|
1154 |
continue restartFromTail; |
|
1155 |
else { |
|
1156 |
// p is last node |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1157 |
PREV.set(beginningOfTheEnd, p); // CAS piggyback |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1158 |
if (NEXT.compareAndSet(p, null, beginningOfTheEnd)) { |
6672 | 1159 |
// Successful CAS is the linearization point |
7518 | 1160 |
// for all elements to be added to this deque. |
40734
48879ea67e2a
8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents:
39725
diff
changeset
|
1161 |
if (!TAIL.weakCompareAndSet(this, t, last)) { |
6672 | 1162 |
// Try a little harder to update tail, |
1163 |
// since we may be adding many elements. |
|
1164 |
t = tail; |
|
1165 |
if (last.next == null) |
|
40734
48879ea67e2a
8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents:
39725
diff
changeset
|
1166 |
TAIL.weakCompareAndSet(this, t, last); |
6672 | 1167 |
} |
1168 |
return true; |
|
1169 |
} |
|
1170 |
// Lost CAS race to another thread; re-read next |
|
1171 |
} |
|
1172 |
} |
|
1173 |
} |
|
1174 |
||
1175 |
/** |
|
1176 |
* Removes all of the elements from this deque. |
|
1177 |
*/ |
|
1178 |
public void clear() { |
|
1179 |
while (pollFirst() != null) |
|
1180 |
; |
|
1181 |
} |
|
1182 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1183 |
public String toString() { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1184 |
String[] a = null; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1185 |
restartFromHead: for (;;) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1186 |
int charLength = 0; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1187 |
int size = 0; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1188 |
for (Node<E> p = first(); p != null;) { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1189 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1190 |
if ((item = p.item) != null) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1191 |
if (a == null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1192 |
a = new String[4]; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1193 |
else if (size == a.length) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1194 |
a = Arrays.copyOf(a, 2 * size); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1195 |
String s = item.toString(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1196 |
a[size++] = s; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1197 |
charLength += s.length(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1198 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1199 |
if (p == (p = p.next)) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1200 |
continue restartFromHead; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1201 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1202 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1203 |
if (size == 0) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1204 |
return "[]"; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1205 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1206 |
return Helpers.toString(a, size, charLength); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1207 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1208 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1209 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1210 |
private Object[] toArrayInternal(Object[] a) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1211 |
Object[] x = a; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1212 |
restartFromHead: for (;;) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1213 |
int size = 0; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1214 |
for (Node<E> p = first(); p != null;) { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1215 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1216 |
if ((item = p.item) != null) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1217 |
if (x == null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1218 |
x = new Object[4]; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1219 |
else if (size == x.length) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1220 |
x = Arrays.copyOf(x, 2 * (size + 4)); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1221 |
x[size++] = item; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1222 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1223 |
if (p == (p = p.next)) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1224 |
continue restartFromHead; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1225 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1226 |
if (x == null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1227 |
return new Object[0]; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1228 |
else if (a != null && size <= a.length) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1229 |
if (a != x) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1230 |
System.arraycopy(x, 0, a, 0, size); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1231 |
if (size < a.length) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1232 |
a[size] = null; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1233 |
return a; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1234 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1235 |
return (size == x.length) ? x : Arrays.copyOf(x, size); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1236 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1237 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1238 |
|
6672 | 1239 |
/** |
1240 |
* Returns an array containing all of the elements in this deque, in |
|
1241 |
* proper sequence (from first to last element). |
|
1242 |
* |
|
1243 |
* <p>The returned array will be "safe" in that no references to it are |
|
1244 |
* maintained by this deque. (In other words, this method must allocate |
|
1245 |
* a new array). The caller is thus free to modify the returned array. |
|
1246 |
* |
|
1247 |
* <p>This method acts as bridge between array-based and collection-based |
|
1248 |
* APIs. |
|
1249 |
* |
|
1250 |
* @return an array containing all of the elements in this deque |
|
1251 |
*/ |
|
1252 |
public Object[] toArray() { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1253 |
return toArrayInternal(null); |
6672 | 1254 |
} |
1255 |
||
1256 |
/** |
|
1257 |
* Returns an array containing all of the elements in this deque, |
|
1258 |
* in proper sequence (from first to last element); the runtime |
|
1259 |
* type of the returned array is that of the specified array. If |
|
1260 |
* the deque fits in the specified array, it is returned therein. |
|
1261 |
* Otherwise, a new array is allocated with the runtime type of |
|
1262 |
* the specified array and the size of this deque. |
|
1263 |
* |
|
1264 |
* <p>If this deque fits in the specified array with room to spare |
|
1265 |
* (i.e., the array has more elements than this deque), the element in |
|
1266 |
* the array immediately following the end of the deque is set to |
|
1267 |
* {@code null}. |
|
1268 |
* |
|
1269 |
* <p>Like the {@link #toArray()} method, this method acts as |
|
1270 |
* bridge between array-based and collection-based APIs. Further, |
|
1271 |
* this method allows precise control over the runtime type of the |
|
1272 |
* output array, and may, under certain circumstances, be used to |
|
1273 |
* save allocation costs. |
|
1274 |
* |
|
1275 |
* <p>Suppose {@code x} is a deque known to contain only strings. |
|
1276 |
* The following code can be used to dump the deque into a newly |
|
1277 |
* allocated array of {@code String}: |
|
1278 |
* |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1279 |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
6672 | 1280 |
* |
1281 |
* Note that {@code toArray(new Object[0])} is identical in function to |
|
1282 |
* {@code toArray()}. |
|
1283 |
* |
|
1284 |
* @param a the array into which the elements of the deque are to |
|
1285 |
* be stored, if it is big enough; otherwise, a new array of the |
|
1286 |
* same runtime type is allocated for this purpose |
|
1287 |
* @return an array containing all of the elements in this deque |
|
1288 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
1289 |
* is not a supertype of the runtime type of every element in |
|
1290 |
* this deque |
|
1291 |
* @throws NullPointerException if the specified array is null |
|
1292 |
*/ |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1293 |
@SuppressWarnings("unchecked") |
6672 | 1294 |
public <T> T[] toArray(T[] a) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1295 |
if (a == null) throw new NullPointerException(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1296 |
return (T[]) toArrayInternal(a); |
6672 | 1297 |
} |
1298 |
||
1299 |
/** |
|
1300 |
* Returns an iterator over the elements in this deque in proper sequence. |
|
1301 |
* The elements will be returned in order from first (head) to last (tail). |
|
1302 |
* |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1303 |
* <p>The returned iterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1304 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
6672 | 1305 |
* |
1306 |
* @return an iterator over the elements in this deque in proper sequence |
|
1307 |
*/ |
|
1308 |
public Iterator<E> iterator() { |
|
1309 |
return new Itr(); |
|
1310 |
} |
|
1311 |
||
1312 |
/** |
|
1313 |
* Returns an iterator over the elements in this deque in reverse |
|
1314 |
* sequential order. The elements will be returned in order from |
|
1315 |
* last (tail) to first (head). |
|
1316 |
* |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1317 |
* <p>The returned iterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1318 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
6672 | 1319 |
* |
1320 |
* @return an iterator over the elements in this deque in reverse order |
|
1321 |
*/ |
|
1322 |
public Iterator<E> descendingIterator() { |
|
1323 |
return new DescendingItr(); |
|
1324 |
} |
|
1325 |
||
1326 |
private abstract class AbstractItr implements Iterator<E> { |
|
1327 |
/** |
|
1328 |
* Next node to return item for. |
|
1329 |
*/ |
|
1330 |
private Node<E> nextNode; |
|
1331 |
||
1332 |
/** |
|
1333 |
* nextItem holds on to item fields because once we claim |
|
1334 |
* that an element exists in hasNext(), we must return it in |
|
1335 |
* the following next() call even if it was in the process of |
|
1336 |
* being removed when hasNext() was called. |
|
1337 |
*/ |
|
1338 |
private E nextItem; |
|
1339 |
||
1340 |
/** |
|
1341 |
* Node returned by most recent call to next. Needed by remove. |
|
1342 |
* Reset to null if this element is deleted by a call to remove. |
|
1343 |
*/ |
|
1344 |
private Node<E> lastRet; |
|
1345 |
||
1346 |
abstract Node<E> startNode(); |
|
1347 |
abstract Node<E> nextNode(Node<E> p); |
|
1348 |
||
1349 |
AbstractItr() { |
|
1350 |
advance(); |
|
1351 |
} |
|
1352 |
||
1353 |
/** |
|
1354 |
* Sets nextNode and nextItem to next valid node, or to null |
|
1355 |
* if no such. |
|
1356 |
*/ |
|
1357 |
private void advance() { |
|
1358 |
lastRet = nextNode; |
|
1359 |
||
1360 |
Node<E> p = (nextNode == null) ? startNode() : nextNode(nextNode); |
|
1361 |
for (;; p = nextNode(p)) { |
|
1362 |
if (p == null) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1363 |
// might be at active end or TERMINATOR node; both are OK |
6672 | 1364 |
nextNode = null; |
1365 |
nextItem = null; |
|
1366 |
break; |
|
1367 |
} |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1368 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1369 |
if ((item = p.item) != null) { |
6672 | 1370 |
nextNode = p; |
1371 |
nextItem = item; |
|
1372 |
break; |
|
1373 |
} |
|
1374 |
} |
|
1375 |
} |
|
1376 |
||
1377 |
public boolean hasNext() { |
|
1378 |
return nextItem != null; |
|
1379 |
} |
|
1380 |
||
1381 |
public E next() { |
|
1382 |
E item = nextItem; |
|
1383 |
if (item == null) throw new NoSuchElementException(); |
|
1384 |
advance(); |
|
1385 |
return item; |
|
1386 |
} |
|
1387 |
||
1388 |
public void remove() { |
|
1389 |
Node<E> l = lastRet; |
|
1390 |
if (l == null) throw new IllegalStateException(); |
|
1391 |
l.item = null; |
|
1392 |
unlink(l); |
|
1393 |
lastRet = null; |
|
1394 |
} |
|
1395 |
} |
|
1396 |
||
1397 |
/** Forward iterator */ |
|
1398 |
private class Itr extends AbstractItr { |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1399 |
Itr() {} // prevent access constructor creation |
6672 | 1400 |
Node<E> startNode() { return first(); } |
1401 |
Node<E> nextNode(Node<E> p) { return succ(p); } |
|
1402 |
} |
|
1403 |
||
1404 |
/** Descending iterator */ |
|
1405 |
private class DescendingItr extends AbstractItr { |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1406 |
DescendingItr() {} // prevent access constructor creation |
6672 | 1407 |
Node<E> startNode() { return last(); } |
1408 |
Node<E> nextNode(Node<E> p) { return pred(p); } |
|
1409 |
} |
|
1410 |
||
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1411 |
/** A customized variant of Spliterators.IteratorSpliterator */ |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1412 |
final class CLDSpliterator implements Spliterator<E> { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1413 |
static final int MAX_BATCH = 1 << 25; // max batch array size; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1414 |
Node<E> current; // current node; null until initialized |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1415 |
int batch; // batch size for splits |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1416 |
boolean exhausted; // true when no more nodes |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1417 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1418 |
public Spliterator<E> trySplit() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1419 |
Node<E> p; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1420 |
int b = batch; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1421 |
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
|
1422 |
if (!exhausted && |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1423 |
((p = current) != null || (p = first()) != null)) { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1424 |
if (p.item == null && p == (p = p.next)) |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1425 |
current = p = first(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1426 |
if (p != null && p.next != null) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1427 |
Object[] a = new Object[n]; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1428 |
int i = 0; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1429 |
do { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1430 |
if ((a[i] = p.item) != null) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1431 |
++i; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1432 |
if (p == (p = p.next)) |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1433 |
p = first(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1434 |
} while (p != null && i < n); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1435 |
if ((current = p) == null) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1436 |
exhausted = true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1437 |
if (i > 0) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1438 |
batch = i; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1439 |
return Spliterators.spliterator |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1440 |
(a, 0, i, (Spliterator.ORDERED | |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1441 |
Spliterator.NONNULL | |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1442 |
Spliterator.CONCURRENT)); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1443 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1444 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1445 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1446 |
return null; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1447 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1448 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1449 |
public void forEachRemaining(Consumer<? super E> action) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1450 |
Node<E> p; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1451 |
if (action == null) throw new NullPointerException(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1452 |
if (!exhausted && |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1453 |
((p = current) != null || (p = first()) != null)) { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1454 |
exhausted = true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1455 |
do { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1456 |
E e = p.item; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1457 |
if (p == (p = p.next)) |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1458 |
p = first(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1459 |
if (e != null) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1460 |
action.accept(e); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1461 |
} while (p != null); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1462 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1463 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1464 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1465 |
public boolean tryAdvance(Consumer<? super E> action) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1466 |
Node<E> p; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1467 |
if (action == null) throw new NullPointerException(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1468 |
if (!exhausted && |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1469 |
((p = current) != null || (p = first()) != null)) { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1470 |
E e; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1471 |
do { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1472 |
e = p.item; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1473 |
if (p == (p = p.next)) |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1474 |
p = first(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1475 |
} while (e == null && p != null); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1476 |
if ((current = p) == null) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1477 |
exhausted = true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1478 |
if (e != null) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1479 |
action.accept(e); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1480 |
return true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1481 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1482 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1483 |
return false; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1484 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1485 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1486 |
public long estimateSize() { return Long.MAX_VALUE; } |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1487 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1488 |
public int characteristics() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1489 |
return Spliterator.ORDERED | Spliterator.NONNULL | |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1490 |
Spliterator.CONCURRENT; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1491 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1492 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1493 |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1494 |
/** |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1495 |
* Returns a {@link Spliterator} over the elements in this deque. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1496 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1497 |
* <p>The returned spliterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1498 |
* <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
|
1499 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1500 |
* <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
|
1501 |
* {@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
|
1502 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1503 |
* @implNote |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1504 |
* 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
|
1505 |
* parallelism. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1506 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1507 |
* @return a {@code Spliterator} over the elements in this deque |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1508 |
* @since 1.8 |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1509 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1510 |
public Spliterator<E> spliterator() { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1511 |
return new CLDSpliterator(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1512 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1513 |
|
6672 | 1514 |
/** |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1515 |
* Saves this deque to a stream (that is, serializes it). |
6672 | 1516 |
* |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1517 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1518 |
* @throws java.io.IOException if an I/O error occurs |
6672 | 1519 |
* @serialData All of the elements (each an {@code E}) in |
1520 |
* the proper order, followed by a null |
|
1521 |
*/ |
|
1522 |
private void writeObject(java.io.ObjectOutputStream s) |
|
1523 |
throws java.io.IOException { |
|
1524 |
||
1525 |
// Write out any hidden stuff |
|
1526 |
s.defaultWriteObject(); |
|
1527 |
||
1528 |
// Write out all elements in the proper order. |
|
1529 |
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
|
1530 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1531 |
if ((item = p.item) != null) |
6672 | 1532 |
s.writeObject(item); |
1533 |
} |
|
1534 |
||
1535 |
// Use trailing null as sentinel |
|
1536 |
s.writeObject(null); |
|
1537 |
} |
|
1538 |
||
1539 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1540 |
* Reconstitutes this deque 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
|
1541 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1542 |
* @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
|
1543 |
* could not be found |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1544 |
* @throws java.io.IOException if an I/O error occurs |
6672 | 1545 |
*/ |
1546 |
private void readObject(java.io.ObjectInputStream s) |
|
1547 |
throws java.io.IOException, ClassNotFoundException { |
|
1548 |
s.defaultReadObject(); |
|
1549 |
||
1550 |
// Read in elements until trailing null sentinel found |
|
1551 |
Node<E> h = null, t = null; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1552 |
for (Object item; (item = s.readObject()) != null; ) { |
6672 | 1553 |
@SuppressWarnings("unchecked") |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1554 |
Node<E> newNode = newNode((E) item); |
6672 | 1555 |
if (h == null) |
1556 |
h = t = newNode; |
|
1557 |
else { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1558 |
NEXT.set(t, newNode); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1559 |
PREV.set(newNode, t); |
6672 | 1560 |
t = newNode; |
1561 |
} |
|
1562 |
} |
|
1563 |
initHeadTail(h, t); |
|
1564 |
} |
|
1565 |
||
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1566 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1567 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1568 |
*/ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1569 |
public boolean removeIf(Predicate<? super E> filter) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1570 |
Objects.requireNonNull(filter); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1571 |
return bulkRemove(filter); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1572 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1573 |
|
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1574 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1575 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1576 |
*/ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1577 |
public boolean removeAll(Collection<?> c) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1578 |
Objects.requireNonNull(c); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1579 |
return bulkRemove(e -> c.contains(e)); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1580 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1581 |
|
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1582 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1583 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1584 |
*/ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1585 |
public boolean retainAll(Collection<?> c) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1586 |
Objects.requireNonNull(c); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1587 |
return bulkRemove(e -> !c.contains(e)); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1588 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1589 |
|
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1590 |
/** Implementation of bulk remove methods. */ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1591 |
private boolean bulkRemove(Predicate<? super E> filter) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1592 |
boolean removed = false; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1593 |
for (Node<E> p = first(), succ; p != null; p = succ) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1594 |
succ = succ(p); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1595 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1596 |
if ((item = p.item) != null |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1597 |
&& filter.test(item) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1598 |
&& ITEM.compareAndSet(p, item, null)) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1599 |
unlink(p); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1600 |
removed = true; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1601 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1602 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1603 |
return removed; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1604 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1605 |
|
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1606 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1607 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1608 |
*/ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1609 |
public void forEach(Consumer<? super E> action) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1610 |
Objects.requireNonNull(action); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1611 |
E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1612 |
for (Node<E> p = first(); p != null; p = succ(p)) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1613 |
if ((item = p.item) != null) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1614 |
action.accept(item); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1615 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1616 |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1617 |
// VarHandle mechanics |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1618 |
private static final VarHandle HEAD; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1619 |
private static final VarHandle TAIL; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1620 |
private static final VarHandle PREV; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1621 |
private static final VarHandle NEXT; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1622 |
private static final VarHandle ITEM; |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1623 |
static { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1624 |
PREV_TERMINATOR = new Node<Object>(); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1625 |
PREV_TERMINATOR.next = PREV_TERMINATOR; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1626 |
NEXT_TERMINATOR = new Node<Object>(); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1627 |
NEXT_TERMINATOR.prev = NEXT_TERMINATOR; |
6672 | 1628 |
try { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1629 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1630 |
HEAD = l.findVarHandle(ConcurrentLinkedDeque.class, "head", |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1631 |
Node.class); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1632 |
TAIL = l.findVarHandle(ConcurrentLinkedDeque.class, "tail", |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1633 |
Node.class); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1634 |
PREV = l.findVarHandle(Node.class, "prev", Node.class); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1635 |
NEXT = l.findVarHandle(Node.class, "next", Node.class); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1636 |
ITEM = l.findVarHandle(Node.class, "item", Object.class); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1637 |
} catch (ReflectiveOperationException e) { |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1638 |
throw new Error(e); |
6672 | 1639 |
} |
1640 |
} |
|
1641 |
} |