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