author | dl |
Thu, 09 Nov 2017 16:10:46 -0800 | |
changeset 47729 | 1563167c9520 |
parent 47340 | 83f933b97787 |
child 49433 | b6671a111395 |
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. |
43521 | 70 |
* |
71 |
* <p>Bulk operations that add, remove, or examine multiple elements, |
|
72 |
* such as {@link #addAll}, {@link #removeIf} or {@link #forEach}, |
|
73 |
* are <em>not</em> guaranteed to be performed atomically. |
|
74 |
* For example, a {@code forEach} traversal concurrent with an {@code |
|
75 |
* addAll} operation might observe only some 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 |
|
44743
f0bbd698c486
8177789: fix collections framework links to point to java.util package doc
smarks
parents:
43521
diff
changeset
|
88 |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
6672 | 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 |
|
47340 | 234 |
* follow next pointers, in the "forward" direction. |
6672 | 235 |
* |
47340 | 236 |
* We believe (without full proof) that all single-element Deque |
237 |
* operations that operate directly at the two ends of the Deque |
|
238 |
* (e.g., addFirst, peekLast, pollLast) are linearizable (see |
|
239 |
* Herlihy and Shavit's book). However, some combinations of |
|
6672 | 240 |
* operations are known not to be linearizable. In particular, |
47340 | 241 |
* when an addFirst(A) is racing with pollFirst() removing B, it |
242 |
* is possible for an observer iterating over the elements to |
|
243 |
* observe first [A B C] and then [A C], even though no interior |
|
6672 | 244 |
* removes are ever performed. Nevertheless, iterators behave |
245 |
* reasonably, providing the "weakly consistent" guarantees. |
|
246 |
* |
|
247 |
* Empirically, microbenchmarks suggest that this class adds about |
|
248 |
* 40% overhead relative to ConcurrentLinkedQueue, which feels as |
|
249 |
* good as we can hope for. |
|
250 |
*/ |
|
251 |
||
252 |
private static final long serialVersionUID = 876323262645176354L; |
|
253 |
||
254 |
/** |
|
255 |
* A node from which the first node on list (that is, the unique node p |
|
256 |
* with p.prev == null && p.next != p) can be reached in O(1) time. |
|
257 |
* Invariants: |
|
258 |
* - the first node is always O(1) reachable from head via prev links |
|
259 |
* - all live nodes are reachable from the first node via succ() |
|
260 |
* - head != null |
|
261 |
* - (tmp = head).next != tmp || tmp != head |
|
262 |
* - head is never gc-unlinked (but may be unlinked) |
|
263 |
* Non-invariants: |
|
264 |
* - head.item may or may not be null |
|
265 |
* - head may not be reachable from the first or last node, or from tail |
|
266 |
*/ |
|
267 |
private transient volatile Node<E> head; |
|
268 |
||
269 |
/** |
|
270 |
* A node from which the last node on list (that is, the unique node p |
|
271 |
* with p.next == null && p.prev != p) can be reached in O(1) time. |
|
272 |
* Invariants: |
|
273 |
* - the last node is always O(1) reachable from tail via next links |
|
274 |
* - all live nodes are reachable from the last node via pred() |
|
275 |
* - tail != null |
|
276 |
* - tail is never gc-unlinked (but may be unlinked) |
|
277 |
* Non-invariants: |
|
278 |
* - tail.item may or may not be null |
|
279 |
* - tail may not be reachable from the first or last node, or from head |
|
280 |
*/ |
|
281 |
private transient volatile Node<E> tail; |
|
282 |
||
7518 | 283 |
private static final Node<Object> PREV_TERMINATOR, NEXT_TERMINATOR; |
6672 | 284 |
|
285 |
@SuppressWarnings("unchecked") |
|
286 |
Node<E> prevTerminator() { |
|
287 |
return (Node<E>) PREV_TERMINATOR; |
|
288 |
} |
|
289 |
||
290 |
@SuppressWarnings("unchecked") |
|
291 |
Node<E> nextTerminator() { |
|
292 |
return (Node<E>) NEXT_TERMINATOR; |
|
293 |
} |
|
294 |
||
295 |
static final class Node<E> { |
|
296 |
volatile Node<E> prev; |
|
297 |
volatile E item; |
|
298 |
volatile Node<E> next; |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
299 |
} |
6672 | 300 |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
301 |
/** |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
302 |
* 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
|
303 |
* 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
|
304 |
*/ |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
305 |
static <E> Node<E> newNode(E item) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
306 |
Node<E> node = new Node<E>(); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
307 |
ITEM.set(node, item); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
308 |
return node; |
6672 | 309 |
} |
310 |
||
311 |
/** |
|
312 |
* Links e as first element. |
|
313 |
*/ |
|
314 |
private void linkFirst(E e) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
315 |
final Node<E> newNode = newNode(Objects.requireNonNull(e)); |
6672 | 316 |
|
317 |
restartFromHead: |
|
318 |
for (;;) |
|
319 |
for (Node<E> h = head, p = h, q;;) { |
|
320 |
if ((q = p.prev) != null && |
|
321 |
(q = (p = q).prev) != null) |
|
322 |
// Check for head updates every other hop. |
|
323 |
// If p == q, we are sure to follow head instead. |
|
324 |
p = (h != (h = head)) ? h : q; |
|
325 |
else if (p.next == p) // PREV_TERMINATOR |
|
326 |
continue restartFromHead; |
|
327 |
else { |
|
328 |
// p is first node |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
329 |
NEXT.set(newNode, p); // CAS piggyback |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
330 |
if (PREV.compareAndSet(p, null, newNode)) { |
6672 | 331 |
// Successful CAS is the linearization point |
332 |
// for e to become an element of this deque, |
|
333 |
// and for newNode to become "live". |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
334 |
if (p != h) // hop two nodes at a time; failure is OK |
40734
48879ea67e2a
8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents:
39725
diff
changeset
|
335 |
HEAD.weakCompareAndSet(this, h, newNode); |
6672 | 336 |
return; |
337 |
} |
|
338 |
// Lost CAS race to another thread; re-read prev |
|
339 |
} |
|
340 |
} |
|
341 |
} |
|
342 |
||
343 |
/** |
|
344 |
* Links e as last element. |
|
345 |
*/ |
|
346 |
private void linkLast(E e) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
347 |
final Node<E> newNode = newNode(Objects.requireNonNull(e)); |
6672 | 348 |
|
349 |
restartFromTail: |
|
350 |
for (;;) |
|
351 |
for (Node<E> t = tail, p = t, q;;) { |
|
352 |
if ((q = p.next) != null && |
|
353 |
(q = (p = q).next) != null) |
|
354 |
// Check for tail updates every other hop. |
|
355 |
// If p == q, we are sure to follow tail instead. |
|
356 |
p = (t != (t = tail)) ? t : q; |
|
357 |
else if (p.prev == p) // NEXT_TERMINATOR |
|
358 |
continue restartFromTail; |
|
359 |
else { |
|
360 |
// p is last node |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
361 |
PREV.set(newNode, p); // CAS piggyback |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
362 |
if (NEXT.compareAndSet(p, null, newNode)) { |
6672 | 363 |
// Successful CAS is the linearization point |
364 |
// for e to become an element of this deque, |
|
365 |
// and for newNode to become "live". |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
366 |
if (p != t) // hop two nodes at a time; failure is OK |
40734
48879ea67e2a
8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents:
39725
diff
changeset
|
367 |
TAIL.weakCompareAndSet(this, t, newNode); |
6672 | 368 |
return; |
369 |
} |
|
370 |
// Lost CAS race to another thread; re-read next |
|
371 |
} |
|
372 |
} |
|
373 |
} |
|
374 |
||
7518 | 375 |
private static final int HOPS = 2; |
6672 | 376 |
|
377 |
/** |
|
378 |
* Unlinks non-null node x. |
|
379 |
*/ |
|
380 |
void unlink(Node<E> x) { |
|
381 |
// assert x != null; |
|
382 |
// assert x.item == null; |
|
383 |
// assert x != PREV_TERMINATOR; |
|
384 |
// assert x != NEXT_TERMINATOR; |
|
385 |
||
386 |
final Node<E> prev = x.prev; |
|
387 |
final Node<E> next = x.next; |
|
388 |
if (prev == null) { |
|
389 |
unlinkFirst(x, next); |
|
390 |
} else if (next == null) { |
|
391 |
unlinkLast(x, prev); |
|
392 |
} else { |
|
393 |
// Unlink interior node. |
|
394 |
// |
|
395 |
// This is the common case, since a series of polls at the |
|
396 |
// same end will be "interior" removes, except perhaps for |
|
397 |
// the first one, since end nodes cannot be unlinked. |
|
398 |
// |
|
399 |
// At any time, all active nodes are mutually reachable by |
|
400 |
// following a sequence of either next or prev pointers. |
|
401 |
// |
|
402 |
// Our strategy is to find the unique active predecessor |
|
403 |
// and successor of x. Try to fix up their links so that |
|
404 |
// they point to each other, leaving x unreachable from |
|
405 |
// active nodes. If successful, and if x has no live |
|
406 |
// predecessor/successor, we additionally try to gc-unlink, |
|
407 |
// leaving active nodes unreachable from x, by rechecking |
|
408 |
// that the status of predecessor and successor are |
|
409 |
// unchanged and ensuring that x is not reachable from |
|
410 |
// tail/head, before setting x's prev/next links to their |
|
411 |
// logical approximate replacements, self/TERMINATOR. |
|
412 |
Node<E> activePred, activeSucc; |
|
413 |
boolean isFirst, isLast; |
|
414 |
int hops = 1; |
|
415 |
||
416 |
// Find active predecessor |
|
417 |
for (Node<E> p = prev; ; ++hops) { |
|
418 |
if (p.item != null) { |
|
419 |
activePred = p; |
|
420 |
isFirst = false; |
|
421 |
break; |
|
422 |
} |
|
423 |
Node<E> q = p.prev; |
|
424 |
if (q == null) { |
|
425 |
if (p.next == p) |
|
426 |
return; |
|
427 |
activePred = p; |
|
428 |
isFirst = true; |
|
429 |
break; |
|
430 |
} |
|
431 |
else if (p == q) |
|
432 |
return; |
|
433 |
else |
|
434 |
p = q; |
|
435 |
} |
|
436 |
||
437 |
// Find active successor |
|
438 |
for (Node<E> p = next; ; ++hops) { |
|
439 |
if (p.item != null) { |
|
440 |
activeSucc = p; |
|
441 |
isLast = false; |
|
442 |
break; |
|
443 |
} |
|
444 |
Node<E> q = p.next; |
|
445 |
if (q == null) { |
|
446 |
if (p.prev == p) |
|
447 |
return; |
|
448 |
activeSucc = p; |
|
449 |
isLast = true; |
|
450 |
break; |
|
451 |
} |
|
452 |
else if (p == q) |
|
453 |
return; |
|
454 |
else |
|
455 |
p = q; |
|
456 |
} |
|
457 |
||
458 |
// TODO: better HOP heuristics |
|
459 |
if (hops < HOPS |
|
460 |
// always squeeze out interior deleted nodes |
|
461 |
&& (isFirst | isLast)) |
|
462 |
return; |
|
463 |
||
464 |
// Squeeze out deleted nodes between activePred and |
|
465 |
// activeSucc, including x. |
|
466 |
skipDeletedSuccessors(activePred); |
|
467 |
skipDeletedPredecessors(activeSucc); |
|
468 |
||
469 |
// Try to gc-unlink, if possible |
|
470 |
if ((isFirst | isLast) && |
|
471 |
||
472 |
// Recheck expected state of predecessor and successor |
|
473 |
(activePred.next == activeSucc) && |
|
474 |
(activeSucc.prev == activePred) && |
|
475 |
(isFirst ? activePred.prev == null : activePred.item != null) && |
|
476 |
(isLast ? activeSucc.next == null : activeSucc.item != null)) { |
|
477 |
||
478 |
updateHead(); // Ensure x is not reachable from head |
|
479 |
updateTail(); // Ensure x is not reachable from tail |
|
480 |
||
481 |
// Finally, actually gc-unlink |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
482 |
PREV.setRelease(x, isFirst ? prevTerminator() : x); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
483 |
NEXT.setRelease(x, isLast ? nextTerminator() : x); |
6672 | 484 |
} |
485 |
} |
|
486 |
} |
|
487 |
||
488 |
/** |
|
489 |
* Unlinks non-null first node. |
|
490 |
*/ |
|
491 |
private void unlinkFirst(Node<E> first, Node<E> next) { |
|
492 |
// assert first != null; |
|
493 |
// assert next != null; |
|
494 |
// assert first.item == null; |
|
495 |
for (Node<E> o = null, p = next, q;;) { |
|
496 |
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
|
497 |
if (o != null && p.prev != p && |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
498 |
NEXT.compareAndSet(first, next, p)) { |
6672 | 499 |
skipDeletedPredecessors(p); |
500 |
if (first.prev == null && |
|
501 |
(p.next == null || p.item != null) && |
|
502 |
p.prev == first) { |
|
503 |
||
504 |
updateHead(); // Ensure o is not reachable from head |
|
505 |
updateTail(); // Ensure o is not reachable from tail |
|
506 |
||
507 |
// Finally, actually gc-unlink |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
508 |
NEXT.setRelease(o, o); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
509 |
PREV.setRelease(o, prevTerminator()); |
6672 | 510 |
} |
511 |
} |
|
512 |
return; |
|
513 |
} |
|
514 |
else if (p == q) |
|
515 |
return; |
|
516 |
else { |
|
517 |
o = p; |
|
518 |
p = q; |
|
519 |
} |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
/** |
|
524 |
* Unlinks non-null last node. |
|
525 |
*/ |
|
526 |
private void unlinkLast(Node<E> last, Node<E> prev) { |
|
527 |
// assert last != null; |
|
528 |
// assert prev != null; |
|
529 |
// assert last.item == null; |
|
530 |
for (Node<E> o = null, p = prev, q;;) { |
|
531 |
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
|
532 |
if (o != null && p.next != p && |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
533 |
PREV.compareAndSet(last, prev, p)) { |
6672 | 534 |
skipDeletedSuccessors(p); |
535 |
if (last.next == null && |
|
536 |
(p.prev == null || p.item != null) && |
|
537 |
p.next == last) { |
|
538 |
||
539 |
updateHead(); // Ensure o is not reachable from head |
|
540 |
updateTail(); // Ensure o is not reachable from tail |
|
541 |
||
542 |
// Finally, actually gc-unlink |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
543 |
PREV.setRelease(o, o); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
544 |
NEXT.setRelease(o, nextTerminator()); |
6672 | 545 |
} |
546 |
} |
|
547 |
return; |
|
548 |
} |
|
549 |
else if (p == q) |
|
550 |
return; |
|
551 |
else { |
|
552 |
o = p; |
|
553 |
p = q; |
|
554 |
} |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
/** |
|
559 |
* Guarantees that any node which was unlinked before a call to |
|
560 |
* this method will be unreachable from head after it returns. |
|
561 |
* Does not guarantee to eliminate slack, only that head will |
|
562 |
* point to a node that was active while this method was running. |
|
563 |
*/ |
|
564 |
private final void updateHead() { |
|
565 |
// Either head already points to an active node, or we keep |
|
566 |
// trying to cas it to the first node until it does. |
|
567 |
Node<E> h, p, q; |
|
568 |
restartFromHead: |
|
569 |
while ((h = head).item == null && (p = h.prev) != null) { |
|
570 |
for (;;) { |
|
571 |
if ((q = p.prev) == null || |
|
572 |
(q = (p = q).prev) == null) { |
|
573 |
// It is possible that p is PREV_TERMINATOR, |
|
574 |
// 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
|
575 |
if (HEAD.compareAndSet(this, h, p)) |
6672 | 576 |
return; |
577 |
else |
|
578 |
continue restartFromHead; |
|
579 |
} |
|
580 |
else if (h != head) |
|
581 |
continue restartFromHead; |
|
582 |
else |
|
583 |
p = q; |
|
584 |
} |
|
585 |
} |
|
586 |
} |
|
587 |
||
588 |
/** |
|
589 |
* Guarantees that any node which was unlinked before a call to |
|
590 |
* this method will be unreachable from tail after it returns. |
|
591 |
* Does not guarantee to eliminate slack, only that tail will |
|
592 |
* point to a node that was active while this method was running. |
|
593 |
*/ |
|
594 |
private final void updateTail() { |
|
595 |
// Either tail already points to an active node, or we keep |
|
596 |
// trying to cas it to the last node until it does. |
|
597 |
Node<E> t, p, q; |
|
598 |
restartFromTail: |
|
599 |
while ((t = tail).item == null && (p = t.next) != null) { |
|
600 |
for (;;) { |
|
601 |
if ((q = p.next) == null || |
|
602 |
(q = (p = q).next) == null) { |
|
603 |
// It is possible that p is NEXT_TERMINATOR, |
|
604 |
// 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
|
605 |
if (TAIL.compareAndSet(this, t, p)) |
6672 | 606 |
return; |
607 |
else |
|
608 |
continue restartFromTail; |
|
609 |
} |
|
610 |
else if (t != tail) |
|
611 |
continue restartFromTail; |
|
612 |
else |
|
613 |
p = q; |
|
614 |
} |
|
615 |
} |
|
616 |
} |
|
617 |
||
618 |
private void skipDeletedPredecessors(Node<E> x) { |
|
619 |
whileActive: |
|
620 |
do { |
|
621 |
Node<E> prev = x.prev; |
|
622 |
// assert prev != null; |
|
623 |
// assert x != NEXT_TERMINATOR; |
|
624 |
// assert x != PREV_TERMINATOR; |
|
625 |
Node<E> p = prev; |
|
626 |
findActive: |
|
627 |
for (;;) { |
|
628 |
if (p.item != null) |
|
629 |
break findActive; |
|
630 |
Node<E> q = p.prev; |
|
631 |
if (q == null) { |
|
632 |
if (p.next == p) |
|
633 |
continue whileActive; |
|
634 |
break findActive; |
|
635 |
} |
|
636 |
else if (p == q) |
|
637 |
continue whileActive; |
|
638 |
else |
|
639 |
p = q; |
|
640 |
} |
|
641 |
||
642 |
// found active CAS target |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
643 |
if (prev == p || PREV.compareAndSet(x, prev, p)) |
6672 | 644 |
return; |
645 |
||
646 |
} while (x.item != null || x.next == null); |
|
647 |
} |
|
648 |
||
649 |
private void skipDeletedSuccessors(Node<E> x) { |
|
650 |
whileActive: |
|
651 |
do { |
|
652 |
Node<E> next = x.next; |
|
653 |
// assert next != null; |
|
654 |
// assert x != NEXT_TERMINATOR; |
|
655 |
// assert x != PREV_TERMINATOR; |
|
656 |
Node<E> p = next; |
|
657 |
findActive: |
|
658 |
for (;;) { |
|
659 |
if (p.item != null) |
|
660 |
break findActive; |
|
661 |
Node<E> q = p.next; |
|
662 |
if (q == null) { |
|
663 |
if (p.prev == p) |
|
664 |
continue whileActive; |
|
665 |
break findActive; |
|
666 |
} |
|
667 |
else if (p == q) |
|
668 |
continue whileActive; |
|
669 |
else |
|
670 |
p = q; |
|
671 |
} |
|
672 |
||
673 |
// found active CAS target |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
674 |
if (next == p || NEXT.compareAndSet(x, next, p)) |
6672 | 675 |
return; |
676 |
||
677 |
} while (x.item != null || x.prev == null); |
|
678 |
} |
|
679 |
||
680 |
/** |
|
681 |
* Returns the successor of p, or the first node if p.next has been |
|
682 |
* linked to self, which will only be true if traversing with a |
|
683 |
* stale pointer that is now off the list. |
|
684 |
*/ |
|
685 |
final Node<E> succ(Node<E> p) { |
|
686 |
// TODO: should we skip deleted nodes here? |
|
43521 | 687 |
if (p == (p = p.next)) |
688 |
p = first(); |
|
689 |
return p; |
|
6672 | 690 |
} |
691 |
||
692 |
/** |
|
693 |
* Returns the predecessor of p, or the last node if p.prev has been |
|
694 |
* linked to self, which will only be true if traversing with a |
|
695 |
* stale pointer that is now off the list. |
|
696 |
*/ |
|
697 |
final Node<E> pred(Node<E> p) { |
|
47729
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
698 |
if (p == (p = p.prev)) |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
699 |
p = last(); |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
700 |
return p; |
6672 | 701 |
} |
702 |
||
703 |
/** |
|
704 |
* Returns the first node, the unique node p for which: |
|
705 |
* p.prev == null && p.next != p |
|
706 |
* The returned node may or may not be logically deleted. |
|
707 |
* Guarantees that head is set to the returned node. |
|
708 |
*/ |
|
709 |
Node<E> first() { |
|
710 |
restartFromHead: |
|
711 |
for (;;) |
|
712 |
for (Node<E> h = head, p = h, q;;) { |
|
713 |
if ((q = p.prev) != null && |
|
714 |
(q = (p = q).prev) != null) |
|
715 |
// Check for head updates every other hop. |
|
716 |
// If p == q, we are sure to follow head instead. |
|
717 |
p = (h != (h = head)) ? h : q; |
|
718 |
else if (p == h |
|
719 |
// It is possible that p is PREV_TERMINATOR, |
|
720 |
// 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
|
721 |
|| HEAD.compareAndSet(this, h, p)) |
6672 | 722 |
return p; |
723 |
else |
|
724 |
continue restartFromHead; |
|
725 |
} |
|
726 |
} |
|
727 |
||
728 |
/** |
|
729 |
* Returns the last node, the unique node p for which: |
|
730 |
* p.next == null && p.prev != p |
|
731 |
* The returned node may or may not be logically deleted. |
|
732 |
* Guarantees that tail is set to the returned node. |
|
733 |
*/ |
|
734 |
Node<E> last() { |
|
735 |
restartFromTail: |
|
736 |
for (;;) |
|
737 |
for (Node<E> t = tail, p = t, q;;) { |
|
738 |
if ((q = p.next) != null && |
|
739 |
(q = (p = q).next) != null) |
|
740 |
// Check for tail updates every other hop. |
|
741 |
// If p == q, we are sure to follow tail instead. |
|
742 |
p = (t != (t = tail)) ? t : q; |
|
743 |
else if (p == t |
|
744 |
// It is possible that p is NEXT_TERMINATOR, |
|
745 |
// 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
|
746 |
|| TAIL.compareAndSet(this, t, p)) |
6672 | 747 |
return p; |
748 |
else |
|
749 |
continue restartFromTail; |
|
750 |
} |
|
751 |
} |
|
752 |
||
753 |
// Minor convenience utilities |
|
754 |
||
755 |
/** |
|
756 |
* Returns element unless it is null, in which case throws |
|
757 |
* NoSuchElementException. |
|
758 |
* |
|
759 |
* @param v the element |
|
760 |
* @return the element |
|
761 |
*/ |
|
762 |
private E screenNullResult(E v) { |
|
763 |
if (v == null) |
|
764 |
throw new NoSuchElementException(); |
|
765 |
return v; |
|
766 |
} |
|
767 |
||
768 |
/** |
|
769 |
* Constructs an empty deque. |
|
770 |
*/ |
|
771 |
public ConcurrentLinkedDeque() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
772 |
head = tail = new Node<E>(); |
6672 | 773 |
} |
774 |
||
775 |
/** |
|
776 |
* Constructs a deque initially containing the elements of |
|
777 |
* the given collection, added in traversal order of the |
|
778 |
* collection's iterator. |
|
779 |
* |
|
780 |
* @param c the collection of elements to initially contain |
|
781 |
* @throws NullPointerException if the specified collection or any |
|
782 |
* of its elements are null |
|
783 |
*/ |
|
784 |
public ConcurrentLinkedDeque(Collection<? extends E> c) { |
|
785 |
// Copy c into a private chain of Nodes |
|
786 |
Node<E> h = null, t = null; |
|
787 |
for (E e : c) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
788 |
Node<E> newNode = newNode(Objects.requireNonNull(e)); |
6672 | 789 |
if (h == null) |
790 |
h = t = newNode; |
|
791 |
else { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
792 |
NEXT.set(t, newNode); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
793 |
PREV.set(newNode, t); |
6672 | 794 |
t = newNode; |
795 |
} |
|
796 |
} |
|
797 |
initHeadTail(h, t); |
|
798 |
} |
|
799 |
||
800 |
/** |
|
801 |
* Initializes head and tail, ensuring invariants hold. |
|
802 |
*/ |
|
803 |
private void initHeadTail(Node<E> h, Node<E> t) { |
|
804 |
if (h == t) { |
|
805 |
if (h == null) |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
806 |
h = t = new Node<E>(); |
6672 | 807 |
else { |
808 |
// 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
|
809 |
Node<E> newNode = new Node<E>(); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
810 |
NEXT.set(t, newNode); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
811 |
PREV.set(newNode, t); |
6672 | 812 |
t = newNode; |
813 |
} |
|
814 |
} |
|
815 |
head = h; |
|
816 |
tail = t; |
|
817 |
} |
|
818 |
||
819 |
/** |
|
820 |
* 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
|
821 |
* 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
|
822 |
* {@link IllegalStateException}. |
6672 | 823 |
* |
7518 | 824 |
* @throws NullPointerException if the specified element is null |
6672 | 825 |
*/ |
826 |
public void addFirst(E e) { |
|
827 |
linkFirst(e); |
|
828 |
} |
|
829 |
||
830 |
/** |
|
831 |
* 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
|
832 |
* 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
|
833 |
* {@link IllegalStateException}. |
6672 | 834 |
* |
835 |
* <p>This method is equivalent to {@link #add}. |
|
836 |
* |
|
7518 | 837 |
* @throws NullPointerException if the specified element is null |
6672 | 838 |
*/ |
839 |
public void addLast(E e) { |
|
840 |
linkLast(e); |
|
841 |
} |
|
842 |
||
843 |
/** |
|
844 |
* 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
|
845 |
* As the deque is unbounded, this method will never return {@code false}. |
6672 | 846 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
847 |
* @return {@code true} (as specified by {@link Deque#offerFirst}) |
7518 | 848 |
* @throws NullPointerException if the specified element is null |
6672 | 849 |
*/ |
850 |
public boolean offerFirst(E e) { |
|
851 |
linkFirst(e); |
|
852 |
return true; |
|
853 |
} |
|
854 |
||
855 |
/** |
|
856 |
* 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
|
857 |
* As the deque is unbounded, this method will never return {@code false}. |
6672 | 858 |
* |
859 |
* <p>This method is equivalent to {@link #add}. |
|
860 |
* |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
861 |
* @return {@code true} (as specified by {@link Deque#offerLast}) |
7518 | 862 |
* @throws NullPointerException if the specified element is null |
6672 | 863 |
*/ |
864 |
public boolean offerLast(E e) { |
|
865 |
linkLast(e); |
|
866 |
return true; |
|
867 |
} |
|
868 |
||
869 |
public E peekFirst() { |
|
47340 | 870 |
restart: for (;;) { |
47729
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
871 |
E item; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
872 |
Node<E> first = first(), p = first; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
873 |
while ((item = p.item) == null) { |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
874 |
if (p == (p = p.next)) continue restart; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
875 |
if (p == null) |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
876 |
break; |
47340 | 877 |
} |
47729
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
878 |
// recheck for linearizability |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
879 |
if (first.prev != null) continue restart; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
880 |
return item; |
6672 | 881 |
} |
882 |
} |
|
883 |
||
884 |
public E peekLast() { |
|
47340 | 885 |
restart: for (;;) { |
47729
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
886 |
E item; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
887 |
Node<E> last = last(), p = last; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
888 |
while ((item = p.item) == null) { |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
889 |
if (p == (p = p.prev)) continue restart; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
890 |
if (p == null) |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
891 |
break; |
47340 | 892 |
} |
47729
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
893 |
// recheck for linearizability |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
894 |
if (last.next != null) continue restart; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
895 |
return item; |
6672 | 896 |
} |
897 |
} |
|
898 |
||
899 |
/** |
|
900 |
* @throws NoSuchElementException {@inheritDoc} |
|
901 |
*/ |
|
902 |
public E getFirst() { |
|
903 |
return screenNullResult(peekFirst()); |
|
904 |
} |
|
905 |
||
906 |
/** |
|
907 |
* @throws NoSuchElementException {@inheritDoc} |
|
908 |
*/ |
|
7518 | 909 |
public E getLast() { |
6672 | 910 |
return screenNullResult(peekLast()); |
911 |
} |
|
912 |
||
913 |
public E pollFirst() { |
|
47340 | 914 |
restart: for (;;) { |
915 |
for (Node<E> first = first(), p = first;;) { |
|
916 |
final E item; |
|
917 |
if ((item = p.item) != null) { |
|
918 |
// recheck for linearizability |
|
919 |
if (first.prev != null) continue restart; |
|
920 |
if (ITEM.compareAndSet(p, item, null)) { |
|
921 |
unlink(p); |
|
922 |
return item; |
|
923 |
} |
|
924 |
} |
|
47729
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
925 |
if (p == (p = p.next)) continue restart; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
926 |
if (p == null) { |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
927 |
if (first.prev != null) continue restart; |
47340 | 928 |
return null; |
47729
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
929 |
} |
6672 | 930 |
} |
931 |
} |
|
932 |
} |
|
933 |
||
934 |
public E pollLast() { |
|
47340 | 935 |
restart: for (;;) { |
936 |
for (Node<E> last = last(), p = last;;) { |
|
937 |
final E item; |
|
938 |
if ((item = p.item) != null) { |
|
939 |
// recheck for linearizability |
|
940 |
if (last.next != null) continue restart; |
|
941 |
if (ITEM.compareAndSet(p, item, null)) { |
|
942 |
unlink(p); |
|
943 |
return item; |
|
944 |
} |
|
945 |
} |
|
47729
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
946 |
if (p == (p = p.prev)) continue restart; |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
947 |
if (p == null) { |
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
948 |
if (last.next != null) continue restart; |
47340 | 949 |
return null; |
47729
1563167c9520
8189387: ConcurrentLinkedDeque linearizability continued ...
dl
parents:
47340
diff
changeset
|
950 |
} |
6672 | 951 |
} |
952 |
} |
|
953 |
} |
|
954 |
||
955 |
/** |
|
956 |
* @throws NoSuchElementException {@inheritDoc} |
|
957 |
*/ |
|
958 |
public E removeFirst() { |
|
959 |
return screenNullResult(pollFirst()); |
|
960 |
} |
|
961 |
||
962 |
/** |
|
963 |
* @throws NoSuchElementException {@inheritDoc} |
|
964 |
*/ |
|
965 |
public E removeLast() { |
|
966 |
return screenNullResult(pollLast()); |
|
967 |
} |
|
968 |
||
969 |
// *** Queue and stack methods *** |
|
970 |
||
971 |
/** |
|
972 |
* 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
|
973 |
* As the deque is unbounded, this method will never return {@code false}. |
6672 | 974 |
* |
975 |
* @return {@code true} (as specified by {@link Queue#offer}) |
|
976 |
* @throws NullPointerException if the specified element is null |
|
977 |
*/ |
|
978 |
public boolean offer(E e) { |
|
979 |
return offerLast(e); |
|
980 |
} |
|
981 |
||
982 |
/** |
|
983 |
* 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
|
984 |
* 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
|
985 |
* {@link IllegalStateException} or return {@code false}. |
6672 | 986 |
* |
987 |
* @return {@code true} (as specified by {@link Collection#add}) |
|
988 |
* @throws NullPointerException if the specified element is null |
|
989 |
*/ |
|
990 |
public boolean add(E e) { |
|
991 |
return offerLast(e); |
|
992 |
} |
|
993 |
||
994 |
public E poll() { return pollFirst(); } |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
995 |
public E peek() { return peekFirst(); } |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
996 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
997 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
998 |
* @throws NoSuchElementException {@inheritDoc} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
999 |
*/ |
6672 | 1000 |
public E remove() { return removeFirst(); } |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1001 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1002 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1003 |
* @throws NoSuchElementException {@inheritDoc} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1004 |
*/ |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1005 |
public E pop() { return removeFirst(); } |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1006 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1007 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1008 |
* @throws NoSuchElementException {@inheritDoc} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1009 |
*/ |
6672 | 1010 |
public E element() { return getFirst(); } |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1011 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1012 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1013 |
* @throws NullPointerException {@inheritDoc} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1014 |
*/ |
6672 | 1015 |
public void push(E e) { addFirst(e); } |
1016 |
||
1017 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1018 |
* Removes the first occurrence of the specified element from this deque. |
6672 | 1019 |
* 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
|
1020 |
* More formally, removes the first element {@code e} such that |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1021 |
* {@code o.equals(e)} (if such an element exists). |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1022 |
* Returns {@code true} if this deque contained the specified element |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1023 |
* (or equivalently, if this deque changed as a result of the call). |
6672 | 1024 |
* |
1025 |
* @param o element to be removed from this deque, if present |
|
1026 |
* @return {@code true} if the deque contained the specified element |
|
7518 | 1027 |
* @throws NullPointerException if the specified element is null |
6672 | 1028 |
*/ |
1029 |
public boolean removeFirstOccurrence(Object o) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1030 |
Objects.requireNonNull(o); |
6672 | 1031 |
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
|
1032 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1033 |
if ((item = p.item) != null |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1034 |
&& o.equals(item) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1035 |
&& ITEM.compareAndSet(p, item, null)) { |
6672 | 1036 |
unlink(p); |
1037 |
return true; |
|
1038 |
} |
|
1039 |
} |
|
1040 |
return false; |
|
1041 |
} |
|
1042 |
||
1043 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1044 |
* Removes the last occurrence of the specified element from this deque. |
6672 | 1045 |
* 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
|
1046 |
* More formally, removes the last element {@code e} such that |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1047 |
* {@code o.equals(e)} (if such an element exists). |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1048 |
* Returns {@code true} if this deque contained the specified element |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1049 |
* (or equivalently, if this deque changed as a result of the call). |
6672 | 1050 |
* |
1051 |
* @param o element to be removed from this deque, if present |
|
1052 |
* @return {@code true} if the deque contained the specified element |
|
7518 | 1053 |
* @throws NullPointerException if the specified element is null |
6672 | 1054 |
*/ |
1055 |
public boolean removeLastOccurrence(Object o) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1056 |
Objects.requireNonNull(o); |
6672 | 1057 |
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
|
1058 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1059 |
if ((item = p.item) != null |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1060 |
&& o.equals(item) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1061 |
&& ITEM.compareAndSet(p, item, null)) { |
6672 | 1062 |
unlink(p); |
1063 |
return true; |
|
1064 |
} |
|
1065 |
} |
|
1066 |
return false; |
|
1067 |
} |
|
1068 |
||
1069 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1070 |
* Returns {@code true} if this deque contains the specified element. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1071 |
* 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
|
1072 |
* at least one element {@code e} such that {@code o.equals(e)}. |
6672 | 1073 |
* |
1074 |
* @param o element whose presence in this deque is to be tested |
|
1075 |
* @return {@code true} if this deque contains the specified element |
|
1076 |
*/ |
|
1077 |
public boolean contains(Object o) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1078 |
if (o != null) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1079 |
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
|
1080 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1081 |
if ((item = p.item) != null && o.equals(item)) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1082 |
return true; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1083 |
} |
6672 | 1084 |
} |
1085 |
return false; |
|
1086 |
} |
|
1087 |
||
1088 |
/** |
|
1089 |
* Returns {@code true} if this collection contains no elements. |
|
1090 |
* |
|
1091 |
* @return {@code true} if this collection contains no elements |
|
1092 |
*/ |
|
1093 |
public boolean isEmpty() { |
|
1094 |
return peekFirst() == null; |
|
1095 |
} |
|
1096 |
||
1097 |
/** |
|
1098 |
* Returns the number of elements in this deque. If this deque |
|
1099 |
* contains more than {@code Integer.MAX_VALUE} elements, it |
|
1100 |
* returns {@code Integer.MAX_VALUE}. |
|
1101 |
* |
|
1102 |
* <p>Beware that, unlike in most collections, this method is |
|
1103 |
* <em>NOT</em> a constant-time operation. Because of the |
|
1104 |
* asynchronous nature of these deques, determining the current |
|
1105 |
* number of elements requires traversing them all to count them. |
|
1106 |
* Additionally, it is possible for the size to change during |
|
1107 |
* execution of this method, in which case the returned result |
|
1108 |
* will be inaccurate. Thus, this method is typically not very |
|
1109 |
* useful in concurrent applications. |
|
1110 |
* |
|
1111 |
* @return the number of elements in this deque |
|
1112 |
*/ |
|
1113 |
public int size() { |
|
47340 | 1114 |
restart: for (;;) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1115 |
int count = 0; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1116 |
for (Node<E> p = first(); p != null;) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1117 |
if (p.item != null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1118 |
if (++count == Integer.MAX_VALUE) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1119 |
break; // @see Collection.size() |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1120 |
if (p == (p = p.next)) |
47340 | 1121 |
continue restart; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1122 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1123 |
return count; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1124 |
} |
6672 | 1125 |
} |
1126 |
||
1127 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1128 |
* Removes the first occurrence of the specified element from this deque. |
6672 | 1129 |
* 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
|
1130 |
* More formally, removes the first element {@code e} such that |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1131 |
* {@code o.equals(e)} (if such an element exists). |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1132 |
* Returns {@code true} if this deque contained the specified element |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1133 |
* (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
|
1134 |
* |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1135 |
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. |
6672 | 1136 |
* |
1137 |
* @param o element to be removed from this deque, if present |
|
1138 |
* @return {@code true} if the deque contained the specified element |
|
7518 | 1139 |
* @throws NullPointerException if the specified element is null |
6672 | 1140 |
*/ |
1141 |
public boolean remove(Object o) { |
|
1142 |
return removeFirstOccurrence(o); |
|
1143 |
} |
|
1144 |
||
1145 |
/** |
|
1146 |
* Appends all of the elements in the specified collection to the end of |
|
1147 |
* this deque, in the order that they are returned by the specified |
|
1148 |
* collection's iterator. Attempts to {@code addAll} of a deque to |
|
1149 |
* itself result in {@code IllegalArgumentException}. |
|
1150 |
* |
|
1151 |
* @param c the elements to be inserted into this deque |
|
1152 |
* @return {@code true} if this deque changed as a result of the call |
|
1153 |
* @throws NullPointerException if the specified collection or any |
|
1154 |
* of its elements are null |
|
1155 |
* @throws IllegalArgumentException if the collection is this deque |
|
1156 |
*/ |
|
1157 |
public boolean addAll(Collection<? extends E> c) { |
|
1158 |
if (c == this) |
|
1159 |
// As historically specified in AbstractQueue#addAll |
|
1160 |
throw new IllegalArgumentException(); |
|
1161 |
||
1162 |
// Copy c into a private chain of Nodes |
|
1163 |
Node<E> beginningOfTheEnd = null, last = null; |
|
1164 |
for (E e : c) { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1165 |
Node<E> newNode = newNode(Objects.requireNonNull(e)); |
6672 | 1166 |
if (beginningOfTheEnd == null) |
1167 |
beginningOfTheEnd = last = newNode; |
|
1168 |
else { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1169 |
NEXT.set(last, newNode); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1170 |
PREV.set(newNode, last); |
6672 | 1171 |
last = newNode; |
1172 |
} |
|
1173 |
} |
|
1174 |
if (beginningOfTheEnd == null) |
|
1175 |
return false; |
|
1176 |
||
1177 |
// Atomically append the chain at the tail of this collection |
|
1178 |
restartFromTail: |
|
1179 |
for (;;) |
|
1180 |
for (Node<E> t = tail, p = t, q;;) { |
|
1181 |
if ((q = p.next) != null && |
|
1182 |
(q = (p = q).next) != null) |
|
1183 |
// Check for tail updates every other hop. |
|
1184 |
// If p == q, we are sure to follow tail instead. |
|
1185 |
p = (t != (t = tail)) ? t : q; |
|
1186 |
else if (p.prev == p) // NEXT_TERMINATOR |
|
1187 |
continue restartFromTail; |
|
1188 |
else { |
|
1189 |
// p is last node |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1190 |
PREV.set(beginningOfTheEnd, p); // CAS piggyback |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1191 |
if (NEXT.compareAndSet(p, null, beginningOfTheEnd)) { |
6672 | 1192 |
// Successful CAS is the linearization point |
7518 | 1193 |
// for all elements to be added to this deque. |
40734
48879ea67e2a
8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents:
39725
diff
changeset
|
1194 |
if (!TAIL.weakCompareAndSet(this, t, last)) { |
6672 | 1195 |
// Try a little harder to update tail, |
1196 |
// since we may be adding many elements. |
|
1197 |
t = tail; |
|
1198 |
if (last.next == null) |
|
40734
48879ea67e2a
8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents:
39725
diff
changeset
|
1199 |
TAIL.weakCompareAndSet(this, t, last); |
6672 | 1200 |
} |
1201 |
return true; |
|
1202 |
} |
|
1203 |
// Lost CAS race to another thread; re-read next |
|
1204 |
} |
|
1205 |
} |
|
1206 |
} |
|
1207 |
||
1208 |
/** |
|
1209 |
* Removes all of the elements from this deque. |
|
1210 |
*/ |
|
1211 |
public void clear() { |
|
1212 |
while (pollFirst() != null) |
|
1213 |
; |
|
1214 |
} |
|
1215 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1216 |
public String toString() { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1217 |
String[] a = null; |
47340 | 1218 |
restart: for (;;) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1219 |
int charLength = 0; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1220 |
int size = 0; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1221 |
for (Node<E> p = first(); p != null;) { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1222 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1223 |
if ((item = p.item) != null) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1224 |
if (a == null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1225 |
a = new String[4]; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1226 |
else if (size == a.length) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1227 |
a = Arrays.copyOf(a, 2 * size); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1228 |
String s = item.toString(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1229 |
a[size++] = s; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1230 |
charLength += s.length(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1231 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1232 |
if (p == (p = p.next)) |
47340 | 1233 |
continue restart; |
32991
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 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1236 |
if (size == 0) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1237 |
return "[]"; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1238 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1239 |
return Helpers.toString(a, size, charLength); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1240 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1241 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1242 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1243 |
private Object[] toArrayInternal(Object[] a) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1244 |
Object[] x = a; |
47340 | 1245 |
restart: for (;;) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1246 |
int size = 0; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1247 |
for (Node<E> p = first(); p != null;) { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1248 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1249 |
if ((item = p.item) != null) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1250 |
if (x == null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1251 |
x = new Object[4]; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1252 |
else if (size == x.length) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1253 |
x = Arrays.copyOf(x, 2 * (size + 4)); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1254 |
x[size++] = item; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1255 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1256 |
if (p == (p = p.next)) |
47340 | 1257 |
continue restart; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1258 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1259 |
if (x == null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1260 |
return new Object[0]; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1261 |
else if (a != null && size <= a.length) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1262 |
if (a != x) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1263 |
System.arraycopy(x, 0, a, 0, size); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1264 |
if (size < a.length) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1265 |
a[size] = null; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1266 |
return a; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1267 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1268 |
return (size == x.length) ? x : Arrays.copyOf(x, size); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1269 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1270 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1271 |
|
6672 | 1272 |
/** |
1273 |
* Returns an array containing all of the elements in this deque, in |
|
1274 |
* proper sequence (from first to last element). |
|
1275 |
* |
|
1276 |
* <p>The returned array will be "safe" in that no references to it are |
|
1277 |
* maintained by this deque. (In other words, this method must allocate |
|
1278 |
* a new array). The caller is thus free to modify the returned array. |
|
1279 |
* |
|
1280 |
* <p>This method acts as bridge between array-based and collection-based |
|
1281 |
* APIs. |
|
1282 |
* |
|
1283 |
* @return an array containing all of the elements in this deque |
|
1284 |
*/ |
|
1285 |
public Object[] toArray() { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1286 |
return toArrayInternal(null); |
6672 | 1287 |
} |
1288 |
||
1289 |
/** |
|
1290 |
* Returns an array containing all of the elements in this deque, |
|
1291 |
* in proper sequence (from first to last element); the runtime |
|
1292 |
* type of the returned array is that of the specified array. If |
|
1293 |
* the deque fits in the specified array, it is returned therein. |
|
1294 |
* Otherwise, a new array is allocated with the runtime type of |
|
1295 |
* the specified array and the size of this deque. |
|
1296 |
* |
|
1297 |
* <p>If this deque fits in the specified array with room to spare |
|
1298 |
* (i.e., the array has more elements than this deque), the element in |
|
1299 |
* the array immediately following the end of the deque is set to |
|
1300 |
* {@code null}. |
|
1301 |
* |
|
1302 |
* <p>Like the {@link #toArray()} method, this method acts as |
|
1303 |
* bridge between array-based and collection-based APIs. Further, |
|
1304 |
* this method allows precise control over the runtime type of the |
|
1305 |
* output array, and may, under certain circumstances, be used to |
|
1306 |
* save allocation costs. |
|
1307 |
* |
|
1308 |
* <p>Suppose {@code x} is a deque known to contain only strings. |
|
1309 |
* The following code can be used to dump the deque into a newly |
|
1310 |
* allocated array of {@code String}: |
|
1311 |
* |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1312 |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
6672 | 1313 |
* |
1314 |
* Note that {@code toArray(new Object[0])} is identical in function to |
|
1315 |
* {@code toArray()}. |
|
1316 |
* |
|
1317 |
* @param a the array into which the elements of the deque are to |
|
1318 |
* be stored, if it is big enough; otherwise, a new array of the |
|
1319 |
* same runtime type is allocated for this purpose |
|
1320 |
* @return an array containing all of the elements in this deque |
|
1321 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
1322 |
* is not a supertype of the runtime type of every element in |
|
1323 |
* this deque |
|
1324 |
* @throws NullPointerException if the specified array is null |
|
1325 |
*/ |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1326 |
@SuppressWarnings("unchecked") |
6672 | 1327 |
public <T> T[] toArray(T[] a) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1328 |
if (a == null) throw new NullPointerException(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1329 |
return (T[]) toArrayInternal(a); |
6672 | 1330 |
} |
1331 |
||
1332 |
/** |
|
1333 |
* Returns an iterator over the elements in this deque in proper sequence. |
|
1334 |
* The elements will be returned in order from first (head) to last (tail). |
|
1335 |
* |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1336 |
* <p>The returned iterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1337 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
6672 | 1338 |
* |
1339 |
* @return an iterator over the elements in this deque in proper sequence |
|
1340 |
*/ |
|
1341 |
public Iterator<E> iterator() { |
|
1342 |
return new Itr(); |
|
1343 |
} |
|
1344 |
||
1345 |
/** |
|
1346 |
* Returns an iterator over the elements in this deque in reverse |
|
1347 |
* sequential order. The elements will be returned in order from |
|
1348 |
* last (tail) to first (head). |
|
1349 |
* |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1350 |
* <p>The returned iterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1351 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
6672 | 1352 |
* |
1353 |
* @return an iterator over the elements in this deque in reverse order |
|
1354 |
*/ |
|
1355 |
public Iterator<E> descendingIterator() { |
|
1356 |
return new DescendingItr(); |
|
1357 |
} |
|
1358 |
||
1359 |
private abstract class AbstractItr implements Iterator<E> { |
|
1360 |
/** |
|
1361 |
* Next node to return item for. |
|
1362 |
*/ |
|
1363 |
private Node<E> nextNode; |
|
1364 |
||
1365 |
/** |
|
1366 |
* nextItem holds on to item fields because once we claim |
|
1367 |
* that an element exists in hasNext(), we must return it in |
|
1368 |
* the following next() call even if it was in the process of |
|
1369 |
* being removed when hasNext() was called. |
|
1370 |
*/ |
|
1371 |
private E nextItem; |
|
1372 |
||
1373 |
/** |
|
1374 |
* Node returned by most recent call to next. Needed by remove. |
|
1375 |
* Reset to null if this element is deleted by a call to remove. |
|
1376 |
*/ |
|
1377 |
private Node<E> lastRet; |
|
1378 |
||
1379 |
abstract Node<E> startNode(); |
|
1380 |
abstract Node<E> nextNode(Node<E> p); |
|
1381 |
||
1382 |
AbstractItr() { |
|
1383 |
advance(); |
|
1384 |
} |
|
1385 |
||
1386 |
/** |
|
1387 |
* Sets nextNode and nextItem to next valid node, or to null |
|
1388 |
* if no such. |
|
1389 |
*/ |
|
1390 |
private void advance() { |
|
1391 |
lastRet = nextNode; |
|
1392 |
||
1393 |
Node<E> p = (nextNode == null) ? startNode() : nextNode(nextNode); |
|
1394 |
for (;; p = nextNode(p)) { |
|
1395 |
if (p == null) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1396 |
// might be at active end or TERMINATOR node; both are OK |
6672 | 1397 |
nextNode = null; |
1398 |
nextItem = null; |
|
1399 |
break; |
|
1400 |
} |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1401 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1402 |
if ((item = p.item) != null) { |
6672 | 1403 |
nextNode = p; |
1404 |
nextItem = item; |
|
1405 |
break; |
|
1406 |
} |
|
1407 |
} |
|
1408 |
} |
|
1409 |
||
1410 |
public boolean hasNext() { |
|
1411 |
return nextItem != null; |
|
1412 |
} |
|
1413 |
||
1414 |
public E next() { |
|
1415 |
E item = nextItem; |
|
1416 |
if (item == null) throw new NoSuchElementException(); |
|
1417 |
advance(); |
|
1418 |
return item; |
|
1419 |
} |
|
1420 |
||
1421 |
public void remove() { |
|
1422 |
Node<E> l = lastRet; |
|
1423 |
if (l == null) throw new IllegalStateException(); |
|
1424 |
l.item = null; |
|
1425 |
unlink(l); |
|
1426 |
lastRet = null; |
|
1427 |
} |
|
1428 |
} |
|
1429 |
||
1430 |
/** Forward iterator */ |
|
1431 |
private class Itr extends AbstractItr { |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1432 |
Itr() {} // prevent access constructor creation |
6672 | 1433 |
Node<E> startNode() { return first(); } |
1434 |
Node<E> nextNode(Node<E> p) { return succ(p); } |
|
1435 |
} |
|
1436 |
||
1437 |
/** Descending iterator */ |
|
1438 |
private class DescendingItr extends AbstractItr { |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1439 |
DescendingItr() {} // prevent access constructor creation |
6672 | 1440 |
Node<E> startNode() { return last(); } |
1441 |
Node<E> nextNode(Node<E> p) { return pred(p); } |
|
1442 |
} |
|
1443 |
||
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1444 |
/** A customized variant of Spliterators.IteratorSpliterator */ |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1445 |
final class CLDSpliterator implements Spliterator<E> { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1446 |
static final int MAX_BATCH = 1 << 25; // max batch array size; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1447 |
Node<E> current; // current node; null until initialized |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1448 |
int batch; // batch size for splits |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1449 |
boolean exhausted; // true when no more nodes |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1450 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1451 |
public Spliterator<E> trySplit() { |
43521 | 1452 |
Node<E> p, q; |
1453 |
if ((p = current()) == null || (q = p.next) == null) |
|
1454 |
return null; |
|
1455 |
int i = 0, n = batch = Math.min(batch + 1, MAX_BATCH); |
|
1456 |
Object[] a = null; |
|
1457 |
do { |
|
1458 |
final E e; |
|
1459 |
if ((e = p.item) != null) { |
|
1460 |
if (a == null) |
|
1461 |
a = new Object[n]; |
|
1462 |
a[i++] = e; |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1463 |
} |
43521 | 1464 |
if (p == (p = q)) |
1465 |
p = first(); |
|
1466 |
} while (p != null && (q = p.next) != null && i < n); |
|
1467 |
setCurrent(p); |
|
1468 |
return (i == 0) ? null : |
|
1469 |
Spliterators.spliterator(a, 0, i, (Spliterator.ORDERED | |
|
1470 |
Spliterator.NONNULL | |
|
1471 |
Spliterator.CONCURRENT)); |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1472 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1473 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1474 |
public void forEachRemaining(Consumer<? super E> action) { |
43521 | 1475 |
Objects.requireNonNull(action); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1476 |
Node<E> p; |
43521 | 1477 |
if ((p = current()) != null) { |
1478 |
current = null; |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1479 |
exhausted = true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1480 |
do { |
43521 | 1481 |
final E e; |
1482 |
if ((e = p.item) != null) |
|
1483 |
action.accept(e); |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1484 |
if (p == (p = p.next)) |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1485 |
p = first(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1486 |
} while (p != null); |
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 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1489 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1490 |
public boolean tryAdvance(Consumer<? super E> action) { |
43521 | 1491 |
Objects.requireNonNull(action); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1492 |
Node<E> p; |
43521 | 1493 |
if ((p = current()) != null) { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1494 |
E e; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1495 |
do { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1496 |
e = p.item; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1497 |
if (p == (p = p.next)) |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1498 |
p = first(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1499 |
} while (e == null && p != null); |
43521 | 1500 |
setCurrent(p); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1501 |
if (e != null) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1502 |
action.accept(e); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1503 |
return true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1504 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1505 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1506 |
return false; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1507 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1508 |
|
43521 | 1509 |
private void setCurrent(Node<E> p) { |
1510 |
if ((current = p) == null) |
|
1511 |
exhausted = true; |
|
1512 |
} |
|
1513 |
||
1514 |
private Node<E> current() { |
|
1515 |
Node<E> p; |
|
1516 |
if ((p = current) == null && !exhausted) |
|
1517 |
setCurrent(p = first()); |
|
1518 |
return p; |
|
1519 |
} |
|
1520 |
||
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1521 |
public long estimateSize() { return Long.MAX_VALUE; } |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1522 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1523 |
public int characteristics() { |
43521 | 1524 |
return (Spliterator.ORDERED | |
1525 |
Spliterator.NONNULL | |
|
1526 |
Spliterator.CONCURRENT); |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1527 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1528 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1529 |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1530 |
/** |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1531 |
* 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
|
1532 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1533 |
* <p>The returned spliterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1534 |
* <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
|
1535 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1536 |
* <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
|
1537 |
* {@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
|
1538 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1539 |
* @implNote |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1540 |
* 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
|
1541 |
* parallelism. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1542 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1543 |
* @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
|
1544 |
* @since 1.8 |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1545 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1546 |
public Spliterator<E> spliterator() { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1547 |
return new CLDSpliterator(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1548 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
1549 |
|
6672 | 1550 |
/** |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1551 |
* Saves this deque to a stream (that is, serializes it). |
6672 | 1552 |
* |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1553 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1554 |
* @throws java.io.IOException if an I/O error occurs |
6672 | 1555 |
* @serialData All of the elements (each an {@code E}) in |
1556 |
* the proper order, followed by a null |
|
1557 |
*/ |
|
1558 |
private void writeObject(java.io.ObjectOutputStream s) |
|
1559 |
throws java.io.IOException { |
|
1560 |
||
1561 |
// Write out any hidden stuff |
|
1562 |
s.defaultWriteObject(); |
|
1563 |
||
1564 |
// Write out all elements in the proper order. |
|
1565 |
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
|
1566 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1567 |
if ((item = p.item) != null) |
6672 | 1568 |
s.writeObject(item); |
1569 |
} |
|
1570 |
||
1571 |
// Use trailing null as sentinel |
|
1572 |
s.writeObject(null); |
|
1573 |
} |
|
1574 |
||
1575 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1576 |
* 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
|
1577 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1578 |
* @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
|
1579 |
* could not be found |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1580 |
* @throws java.io.IOException if an I/O error occurs |
6672 | 1581 |
*/ |
1582 |
private void readObject(java.io.ObjectInputStream s) |
|
1583 |
throws java.io.IOException, ClassNotFoundException { |
|
1584 |
s.defaultReadObject(); |
|
1585 |
||
1586 |
// Read in elements until trailing null sentinel found |
|
1587 |
Node<E> h = null, t = null; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1588 |
for (Object item; (item = s.readObject()) != null; ) { |
6672 | 1589 |
@SuppressWarnings("unchecked") |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1590 |
Node<E> newNode = newNode((E) item); |
6672 | 1591 |
if (h == null) |
1592 |
h = t = newNode; |
|
1593 |
else { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1594 |
NEXT.set(t, newNode); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1595 |
PREV.set(newNode, t); |
6672 | 1596 |
t = newNode; |
1597 |
} |
|
1598 |
} |
|
1599 |
initHeadTail(h, t); |
|
1600 |
} |
|
1601 |
||
42927
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 |
* @throws NullPointerException {@inheritDoc} |
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 |
public boolean removeIf(Predicate<? super E> filter) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1606 |
Objects.requireNonNull(filter); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1607 |
return bulkRemove(filter); |
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 |
|
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1610 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1611 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1612 |
*/ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1613 |
public boolean removeAll(Collection<?> c) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1614 |
Objects.requireNonNull(c); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1615 |
return bulkRemove(e -> c.contains(e)); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1616 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1617 |
|
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1618 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1619 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1620 |
*/ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1621 |
public boolean retainAll(Collection<?> c) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1622 |
Objects.requireNonNull(c); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1623 |
return bulkRemove(e -> !c.contains(e)); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1624 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1625 |
|
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1626 |
/** Implementation of bulk remove methods. */ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1627 |
private boolean bulkRemove(Predicate<? super E> filter) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1628 |
boolean removed = false; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1629 |
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
|
1630 |
succ = succ(p); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1631 |
final E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1632 |
if ((item = p.item) != null |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1633 |
&& filter.test(item) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1634 |
&& ITEM.compareAndSet(p, item, null)) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1635 |
unlink(p); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1636 |
removed = true; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1637 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1638 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1639 |
return removed; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1640 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1641 |
|
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1642 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1643 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1644 |
*/ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1645 |
public void forEach(Consumer<? super E> action) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1646 |
Objects.requireNonNull(action); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1647 |
E item; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1648 |
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
|
1649 |
if ((item = p.item) != null) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1650 |
action.accept(item); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1651 |
} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
40734
diff
changeset
|
1652 |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1653 |
// VarHandle mechanics |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1654 |
private static final VarHandle HEAD; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1655 |
private static final VarHandle TAIL; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1656 |
private static final VarHandle PREV; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1657 |
private static final VarHandle NEXT; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1658 |
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
|
1659 |
static { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1660 |
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
|
1661 |
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
|
1662 |
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
|
1663 |
NEXT_TERMINATOR.prev = NEXT_TERMINATOR; |
6672 | 1664 |
try { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1665 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1666 |
HEAD = l.findVarHandle(ConcurrentLinkedDeque.class, "head", |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1667 |
Node.class); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1668 |
TAIL = l.findVarHandle(ConcurrentLinkedDeque.class, "tail", |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1669 |
Node.class); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1670 |
PREV = l.findVarHandle(Node.class, "prev", Node.class); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1671 |
NEXT = l.findVarHandle(Node.class, "next", Node.class); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
39040
diff
changeset
|
1672 |
ITEM = l.findVarHandle(Node.class, "item", Object.class); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1673 |
} catch (ReflectiveOperationException e) { |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1674 |
throw new Error(e); |
6672 | 1675 |
} |
1676 |
} |
|
1677 |
} |