10 |
|
import java.util.concurrent.atomic.*; |
11 |
|
import java.util.*; |
12 |
|
import java.io.*; |
13 |
+ |
import sun.misc.Unsafe; |
14 |
+ |
import java.lang.reflect.*; |
15 |
|
|
16 |
|
/** |
17 |
|
* An unbounded {@linkplain TransferQueue} based on linked nodes. |
21 |
|
* producer. The <em>tail</em> of the queue is that element that has |
22 |
|
* been on the queue the shortest time for some producer. |
23 |
|
* |
24 |
< |
* <p>Beware that, unlike in most collections, the <tt>size</tt> |
24 |
> |
* <p>Beware that, unlike in most collections, the {@code size} |
25 |
|
* method is <em>NOT</em> a constant-time operation. Because of the |
26 |
|
* asynchronous nature of these queues, determining the current number |
27 |
|
* of elements requires a traversal of the elements. |
44 |
|
* @since 1.7 |
45 |
|
* @author Doug Lea |
46 |
|
* @param <E> the type of elements held in this collection |
45 |
– |
* |
47 |
|
*/ |
48 |
|
public class LinkedTransferQueue<E> extends AbstractQueue<E> |
49 |
|
implements TransferQueue<E>, java.io.Serializable { |
50 |
|
private static final long serialVersionUID = -3223113410248163686L; |
51 |
|
|
52 |
|
/* |
52 |
– |
* This is still a work in progress... |
53 |
– |
* |
53 |
|
* This class extends the approach used in FIFO-mode |
54 |
|
* SynchronousQueues. See the internal documentation, as well as |
55 |
|
* the PPoPP 2006 paper "Scalable Synchronous Queues" by Scherer, |
56 |
|
* Lea & Scott |
57 |
|
* (http://www.cs.rice.edu/~wns1/papers/2006-PPoPP-SQ.pdf) |
58 |
|
* |
59 |
< |
* The main extension is to provide different Wait modes |
60 |
< |
* for the main "xfer" method that puts or takes items. |
61 |
< |
* These don't impact the basic dual-queue logic, but instead |
62 |
< |
* control whether or how threads block upon insertion |
63 |
< |
* of request or data nodes into the dual queue. |
59 |
> |
* The main extension is to provide different Wait modes for the |
60 |
> |
* main "xfer" method that puts or takes items. These don't |
61 |
> |
* impact the basic dual-queue logic, but instead control whether |
62 |
> |
* or how threads block upon insertion of request or data nodes |
63 |
> |
* into the dual queue. It also uses slightly different |
64 |
> |
* conventions for tracking whether nodes are off-list or |
65 |
> |
* cancelled. |
66 |
|
*/ |
67 |
|
|
68 |
|
// Wait modes for xfer method |
80 |
|
* seems not to vary with number of CPUs (beyond 2) so is just |
81 |
|
* a constant. |
82 |
|
*/ |
83 |
< |
static final int maxTimedSpins = (NCPUS < 2)? 0 : 32; |
83 |
> |
static final int maxTimedSpins = (NCPUS < 2) ? 0 : 32; |
84 |
|
|
85 |
|
/** |
86 |
|
* The number of times to spin before blocking in untimed waits. |
96 |
|
static final long spinForTimeoutThreshold = 1000L; |
97 |
|
|
98 |
|
/** |
99 |
< |
* Node class for LinkedTransferQueue. Opportunistically subclasses from |
100 |
< |
* AtomicReference to represent item. Uses Object, not E, to allow |
101 |
< |
* setting item to "this" after use, to avoid garbage |
102 |
< |
* retention. Similarly, setting the next field to this is used as |
103 |
< |
* sentinel that node is off list. |
99 |
> |
* Node class for LinkedTransferQueue. Opportunistically |
100 |
> |
* subclasses from AtomicReference to represent item. Uses Object, |
101 |
> |
* not E, to allow setting item to "this" after use, to avoid |
102 |
> |
* garbage retention. Similarly, setting the next field to this is |
103 |
> |
* used as sentinel that node is off list. |
104 |
|
*/ |
105 |
|
static final class QNode extends AtomicReference<Object> { |
106 |
|
volatile QNode next; |
115 |
|
nextUpdater = AtomicReferenceFieldUpdater.newUpdater |
116 |
|
(QNode.class, QNode.class, "next"); |
117 |
|
|
118 |
< |
boolean casNext(QNode cmp, QNode val) { |
118 |
> |
final boolean casNext(QNode cmp, QNode val) { |
119 |
|
return nextUpdater.compareAndSet(this, cmp, val); |
120 |
|
} |
121 |
+ |
|
122 |
+ |
final void clearNext() { |
123 |
+ |
nextUpdater.lazySet(this, this); |
124 |
+ |
} |
125 |
+ |
|
126 |
|
} |
127 |
|
|
128 |
|
/** |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
< |
private final QNode dummy = new QNode(null, false); |
141 |
< |
private final PaddedAtomicReference<QNode> head = |
142 |
< |
new PaddedAtomicReference<QNode>(dummy); |
143 |
< |
private final PaddedAtomicReference<QNode> tail = |
138 |
< |
new PaddedAtomicReference<QNode>(dummy); |
140 |
> |
/** head of the queue */ |
141 |
> |
private transient final PaddedAtomicReference<QNode> head; |
142 |
> |
/** tail of the queue */ |
143 |
> |
private transient final PaddedAtomicReference<QNode> tail; |
144 |
|
|
145 |
|
/** |
146 |
|
* Reference to a cancelled node that might not yet have been |
147 |
|
* unlinked from queue because it was the last inserted node |
148 |
|
* when it cancelled. |
149 |
|
*/ |
150 |
< |
private final PaddedAtomicReference<QNode> cleanMe = |
146 |
< |
new PaddedAtomicReference<QNode>(null); |
150 |
> |
private transient final PaddedAtomicReference<QNode> cleanMe; |
151 |
|
|
152 |
|
/** |
153 |
|
* Tries to cas nh as new head; if successful, unlink |
155 |
|
*/ |
156 |
|
private boolean advanceHead(QNode h, QNode nh) { |
157 |
|
if (h == head.get() && head.compareAndSet(h, nh)) { |
158 |
< |
h.next = h; // forget old next |
158 |
> |
h.clearNext(); // forget old next |
159 |
|
return true; |
160 |
|
} |
161 |
|
return false; |
163 |
|
|
164 |
|
/** |
165 |
|
* Puts or takes an item. Used for most queue operations (except |
166 |
< |
* poll() and tryTransfer()) |
166 |
> |
* poll() and tryTransfer()). See the similar code in |
167 |
> |
* SynchronousQueue for detailed explanation. |
168 |
> |
* |
169 |
|
* @param e the item or if null, signifies that this is a take |
170 |
|
* @param mode the wait mode: NOWAIT, TIMEOUT, WAIT |
171 |
|
* @param nanos timeout in nanosecs, used only if mode is TIMEOUT |
202 |
|
Object x = first.get(); |
203 |
|
if (x != first && first.compareAndSet(x, e)) { |
204 |
|
LockSupport.unpark(first.waiter); |
205 |
< |
return isData? e : x; |
205 |
> |
return isData ? e : x; |
206 |
|
} |
207 |
|
} |
208 |
|
} |
212 |
|
|
213 |
|
/** |
214 |
|
* Version of xfer for poll() and tryTransfer, which |
215 |
< |
* simplifies control paths both here and in xfer |
215 |
> |
* simplifies control paths both here and in xfer. |
216 |
|
*/ |
217 |
|
private Object fulfill(Object e) { |
218 |
|
boolean isData = (e != null); |
240 |
|
Object x = first.get(); |
241 |
|
if (x != first && first.compareAndSet(x, e)) { |
242 |
|
LockSupport.unpark(first.waiter); |
243 |
< |
return isData? e : x; |
243 |
> |
return isData ? e : x; |
244 |
|
} |
245 |
|
} |
246 |
|
} |
263 |
|
if (mode == NOWAIT) |
264 |
|
return null; |
265 |
|
|
266 |
< |
long lastTime = (mode == TIMEOUT)? System.nanoTime() : 0; |
266 |
> |
long lastTime = (mode == TIMEOUT) ? System.nanoTime() : 0; |
267 |
|
Thread w = Thread.currentThread(); |
268 |
|
int spins = -1; // set to desired spin count below |
269 |
|
for (;;) { |
272 |
|
Object x = s.get(); |
273 |
|
if (x != e) { // Node was matched or cancelled |
274 |
|
advanceHead(pred, s); // unlink if head |
275 |
< |
if (x == s) // was cancelled |
276 |
< |
return clean(pred, s); |
275 |
> |
if (x == s) { // was cancelled |
276 |
> |
clean(pred, s); |
277 |
> |
return null; |
278 |
> |
} |
279 |
|
else if (x != null) { |
280 |
|
s.set(s); // avoid garbage retention |
281 |
|
return x; |
283 |
|
else |
284 |
|
return e; |
285 |
|
} |
278 |
– |
|
286 |
|
if (mode == TIMEOUT) { |
287 |
|
long now = System.nanoTime(); |
288 |
|
nanos -= now - lastTime; |
295 |
|
if (spins < 0) { |
296 |
|
QNode h = head.get(); // only spin if at head |
297 |
|
spins = ((h != null && h.next == s) ? |
298 |
< |
(mode == TIMEOUT? |
298 |
> |
((mode == TIMEOUT) ? |
299 |
|
maxTimedSpins : maxUntimedSpins) : 0); |
300 |
|
} |
301 |
|
if (spins > 0) |
303 |
|
else if (s.waiter == null) |
304 |
|
s.waiter = w; |
305 |
|
else if (mode != TIMEOUT) { |
306 |
< |
// LockSupport.park(this); |
300 |
< |
LockSupport.park(); // allows run on java5 |
306 |
> |
LockSupport.park(this); |
307 |
|
s.waiter = null; |
308 |
|
spins = -1; |
309 |
|
} |
310 |
|
else if (nanos > spinForTimeoutThreshold) { |
311 |
< |
// LockSupport.parkNanos(this, nanos); |
306 |
< |
LockSupport.parkNanos(nanos); |
311 |
> |
LockSupport.parkNanos(this, nanos); |
312 |
|
s.waiter = null; |
313 |
|
spins = -1; |
314 |
|
} |
316 |
|
} |
317 |
|
|
318 |
|
/** |
319 |
+ |
* Returns validated tail for use in cleaning methods. |
320 |
+ |
*/ |
321 |
+ |
private QNode getValidatedTail() { |
322 |
+ |
for (;;) { |
323 |
+ |
QNode h = head.get(); |
324 |
+ |
QNode first = h.next; |
325 |
+ |
if (first != null && first.next == first) { // help advance |
326 |
+ |
advanceHead(h, first); |
327 |
+ |
continue; |
328 |
+ |
} |
329 |
+ |
QNode t = tail.get(); |
330 |
+ |
QNode last = t.next; |
331 |
+ |
if (t == tail.get()) { |
332 |
+ |
if (last != null) |
333 |
+ |
tail.compareAndSet(t, last); // help advance |
334 |
+ |
else |
335 |
+ |
return t; |
336 |
+ |
} |
337 |
+ |
} |
338 |
+ |
} |
339 |
+ |
|
340 |
+ |
/** |
341 |
|
* Gets rid of cancelled node s with original predecessor pred. |
342 |
< |
* @return null (to simplify use by callers) |
342 |
> |
* |
343 |
> |
* @param pred predecessor of cancelled node |
344 |
> |
* @param s the cancelled node |
345 |
|
*/ |
346 |
< |
private Object clean(QNode pred, QNode s) { |
346 |
> |
private void clean(QNode pred, QNode s) { |
347 |
|
Thread w = s.waiter; |
348 |
|
if (w != null) { // Wake up thread |
349 |
|
s.waiter = null; |
351 |
|
LockSupport.unpark(w); |
352 |
|
} |
353 |
|
|
354 |
< |
for (;;) { |
355 |
< |
if (pred.next != s) // already cleaned |
356 |
< |
return null; |
357 |
< |
QNode h = head.get(); |
358 |
< |
QNode hn = h.next; // Absorb cancelled first node as head |
359 |
< |
if (hn != null && hn.next == hn) { |
360 |
< |
advanceHead(h, hn); |
361 |
< |
continue; |
362 |
< |
} |
363 |
< |
QNode t = tail.get(); // Ensure consistent read for tail |
364 |
< |
if (t == h) |
365 |
< |
return null; |
366 |
< |
QNode tn = t.next; |
367 |
< |
if (t != tail.get()) |
368 |
< |
continue; |
369 |
< |
if (tn != null) { // Help advance tail |
341 |
< |
tail.compareAndSet(t, tn); |
342 |
< |
continue; |
343 |
< |
} |
344 |
< |
if (s != t) { // If not tail, try to unsplice |
345 |
< |
QNode sn = s.next; |
354 |
> |
if (pred == null) |
355 |
> |
return; |
356 |
> |
|
357 |
> |
/* |
358 |
> |
* At any given time, exactly one node on list cannot be |
359 |
> |
* deleted -- the last inserted node. To accommodate this, if |
360 |
> |
* we cannot delete s, we save its predecessor as "cleanMe", |
361 |
> |
* processing the previously saved version first. At least one |
362 |
> |
* of node s or the node previously saved can always be |
363 |
> |
* processed, so this always terminates. |
364 |
> |
*/ |
365 |
> |
while (pred.next == s) { |
366 |
> |
QNode oldpred = reclean(); // First, help get rid of cleanMe |
367 |
> |
QNode t = getValidatedTail(); |
368 |
> |
if (s != t) { // If not tail, try to unsplice |
369 |
> |
QNode sn = s.next; // s.next == s means s already off list |
370 |
|
if (sn == s || pred.casNext(s, sn)) |
371 |
< |
return null; |
371 |
> |
break; |
372 |
|
} |
373 |
< |
QNode dp = cleanMe.get(); |
374 |
< |
if (dp != null) { // Try unlinking previous cancelled node |
375 |
< |
QNode d = dp.next; |
376 |
< |
QNode dn; |
377 |
< |
if (d == null || // d is gone or |
378 |
< |
d == dp || // d is off list or |
379 |
< |
d.get() != d || // d not cancelled or |
380 |
< |
(d != t && // d not tail and |
381 |
< |
(dn = d.next) != null && // has successor |
382 |
< |
dn != d && // that is on list |
383 |
< |
dp.casNext(d, dn))) // d unspliced |
384 |
< |
cleanMe.compareAndSet(dp, null); |
385 |
< |
if (dp == pred) |
386 |
< |
return null; // s is already saved node |
373 |
> |
else if (oldpred == pred || // Already saved |
374 |
> |
(oldpred == null && cleanMe.compareAndSet(null, pred))) |
375 |
> |
break; // Postpone cleaning |
376 |
> |
} |
377 |
> |
} |
378 |
> |
|
379 |
> |
/** |
380 |
> |
* Tries to unsplice the cancelled node held in cleanMe that was |
381 |
> |
* previously uncleanable because it was at tail. |
382 |
> |
* |
383 |
> |
* @return current cleanMe node (or null) |
384 |
> |
*/ |
385 |
> |
private QNode reclean() { |
386 |
> |
/* |
387 |
> |
* cleanMe is, or at one time was, predecessor of cancelled |
388 |
> |
* node s that was the tail so could not be unspliced. If s |
389 |
> |
* is no longer the tail, try to unsplice if necessary and |
390 |
> |
* make cleanMe slot available. This differs from similar |
391 |
> |
* code in clean() because we must check that pred still |
392 |
> |
* points to a cancelled node that must be unspliced -- if |
393 |
> |
* not, we can (must) clear cleanMe without unsplicing. |
394 |
> |
* This can loop only due to contention on casNext or |
395 |
> |
* clearing cleanMe. |
396 |
> |
*/ |
397 |
> |
QNode pred; |
398 |
> |
while ((pred = cleanMe.get()) != null) { |
399 |
> |
QNode t = getValidatedTail(); |
400 |
> |
QNode s = pred.next; |
401 |
> |
if (s != t) { |
402 |
> |
QNode sn; |
403 |
> |
if (s == null || s == pred || s.get() != s || |
404 |
> |
(sn = s.next) == s || pred.casNext(s, sn)) |
405 |
> |
cleanMe.compareAndSet(pred, null); |
406 |
|
} |
407 |
< |
else if (cleanMe.compareAndSet(null, pred)) |
408 |
< |
return null; // Postpone cleaning s |
407 |
> |
else // s is still tail; cannot clean |
408 |
> |
break; |
409 |
|
} |
410 |
+ |
return pred; |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
< |
* Creates an initially empty <tt>LinkedTransferQueue</tt>. |
414 |
> |
* Creates an initially empty {@code LinkedTransferQueue}. |
415 |
|
*/ |
416 |
|
public LinkedTransferQueue() { |
417 |
+ |
QNode dummy = new QNode(null, false); |
418 |
+ |
head = new PaddedAtomicReference<QNode>(dummy); |
419 |
+ |
tail = new PaddedAtomicReference<QNode>(dummy); |
420 |
+ |
cleanMe = new PaddedAtomicReference<QNode>(null); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
< |
* Creates a <tt>LinkedTransferQueue</tt> |
424 |
> |
* Creates a {@code LinkedTransferQueue} |
425 |
|
* initially containing the elements of the given collection, |
426 |
|
* added in traversal order of the collection's iterator. |
427 |
+ |
* |
428 |
|
* @param c the collection of elements to initially contain |
429 |
|
* @throws NullPointerException if the specified collection or any |
430 |
|
* of its elements are null |
431 |
|
*/ |
432 |
|
public LinkedTransferQueue(Collection<? extends E> c) { |
433 |
+ |
this(); |
434 |
|
addAll(c); |
435 |
|
} |
436 |
|
|
454 |
|
return true; |
455 |
|
} |
456 |
|
|
457 |
+ |
public boolean add(E e) { |
458 |
+ |
if (e == null) throw new NullPointerException(); |
459 |
+ |
xfer(e, NOWAIT, 0); |
460 |
+ |
return true; |
461 |
+ |
} |
462 |
+ |
|
463 |
|
public void transfer(E e) throws InterruptedException { |
464 |
|
if (e == null) throw new NullPointerException(); |
465 |
|
if (xfer(e, WAIT, 0) == null) { |
466 |
< |
Thread.interrupted(); |
466 |
> |
Thread.interrupted(); |
467 |
|
throw new InterruptedException(); |
468 |
< |
} |
468 |
> |
} |
469 |
|
} |
470 |
|
|
471 |
|
public boolean tryTransfer(E e, long timeout, TimeUnit unit) |
486 |
|
public E take() throws InterruptedException { |
487 |
|
Object e = xfer(null, WAIT, 0); |
488 |
|
if (e != null) |
489 |
< |
return (E)e; |
490 |
< |
Thread.interrupted(); |
489 |
> |
return (E) e; |
490 |
> |
Thread.interrupted(); |
491 |
|
throw new InterruptedException(); |
492 |
|
} |
493 |
|
|
494 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
495 |
|
Object e = xfer(null, TIMEOUT, unit.toNanos(timeout)); |
496 |
|
if (e != null || !Thread.interrupted()) |
497 |
< |
return (E)e; |
497 |
> |
return (E) e; |
498 |
|
throw new InterruptedException(); |
499 |
|
} |
500 |
|
|
501 |
|
public E poll() { |
502 |
< |
return (E)fulfill(null); |
502 |
> |
return (E) fulfill(null); |
503 |
|
} |
504 |
|
|
505 |
|
public int drainTo(Collection<? super E> c) { |
533 |
|
// Traversal-based methods |
534 |
|
|
535 |
|
/** |
536 |
< |
* Return head after performing any outstanding helping steps |
536 |
> |
* Returns head after performing any outstanding helping steps. |
537 |
|
*/ |
538 |
|
private QNode traversalHead() { |
539 |
|
for (;;) { |
556 |
|
return h; |
557 |
|
} |
558 |
|
} |
559 |
+ |
reclean(); |
560 |
|
} |
561 |
|
} |
562 |
|
|
573 |
|
* if subsequently removed. |
574 |
|
*/ |
575 |
|
class Itr implements Iterator<E> { |
576 |
< |
QNode nextNode; // Next node to return next |
577 |
< |
QNode currentNode; // last returned node, for remove() |
578 |
< |
QNode prevNode; // predecessor of last returned node |
579 |
< |
E nextItem; // Cache of next item, once commited to in next |
576 |
> |
QNode next; // node to return next |
577 |
> |
QNode pnext; // predecessor of next |
578 |
> |
QNode snext; // successor of next |
579 |
> |
QNode curr; // last returned node, for remove() |
580 |
> |
QNode pcurr; // predecessor of curr, for remove() |
581 |
> |
E nextItem; // Cache of next item, once committed to in next |
582 |
|
|
583 |
|
Itr() { |
584 |
< |
nextNode = traversalHead(); |
526 |
< |
advance(); |
584 |
> |
findNext(); |
585 |
|
} |
586 |
|
|
587 |
< |
E advance() { |
588 |
< |
prevNode = currentNode; |
589 |
< |
currentNode = nextNode; |
590 |
< |
E x = nextItem; |
533 |
< |
|
534 |
< |
QNode p = nextNode.next; |
587 |
> |
/** |
588 |
> |
* Ensures next points to next valid node, or null if none. |
589 |
> |
*/ |
590 |
> |
void findNext() { |
591 |
|
for (;;) { |
592 |
< |
if (p == null || !p.isData) { |
593 |
< |
nextNode = null; |
594 |
< |
nextItem = null; |
595 |
< |
return x; |
592 |
> |
QNode pred = pnext; |
593 |
> |
QNode q = next; |
594 |
> |
if (pred == null || pred == q) { |
595 |
> |
pred = traversalHead(); |
596 |
> |
q = pred.next; |
597 |
|
} |
598 |
< |
Object item = p.get(); |
599 |
< |
if (item != p && item != null) { |
600 |
< |
nextNode = p; |
601 |
< |
nextItem = (E)item; |
602 |
< |
return x; |
598 |
> |
if (q == null || !q.isData) { |
599 |
> |
next = null; |
600 |
> |
return; |
601 |
> |
} |
602 |
> |
Object x = q.get(); |
603 |
> |
QNode s = q.next; |
604 |
> |
if (x != null && q != x && q != s) { |
605 |
> |
nextItem = (E) x; |
606 |
> |
snext = s; |
607 |
> |
pnext = pred; |
608 |
> |
next = q; |
609 |
> |
return; |
610 |
|
} |
611 |
< |
prevNode = p; |
612 |
< |
p = p.next; |
611 |
> |
pnext = q; |
612 |
> |
next = s; |
613 |
|
} |
614 |
|
} |
615 |
|
|
616 |
|
public boolean hasNext() { |
617 |
< |
return nextNode != null; |
617 |
> |
return next != null; |
618 |
|
} |
619 |
|
|
620 |
|
public E next() { |
621 |
< |
if (nextNode == null) throw new NoSuchElementException(); |
622 |
< |
return advance(); |
621 |
> |
if (next == null) throw new NoSuchElementException(); |
622 |
> |
pcurr = pnext; |
623 |
> |
curr = next; |
624 |
> |
pnext = next; |
625 |
> |
next = snext; |
626 |
> |
E x = nextItem; |
627 |
> |
findNext(); |
628 |
> |
return x; |
629 |
|
} |
630 |
|
|
631 |
|
public void remove() { |
632 |
< |
QNode p = currentNode; |
633 |
< |
QNode prev = prevNode; |
564 |
< |
if (prev == null || p == null) |
632 |
> |
QNode p = curr; |
633 |
> |
if (p == null) |
634 |
|
throw new IllegalStateException(); |
635 |
|
Object x = p.get(); |
636 |
|
if (x != null && x != p && p.compareAndSet(x, p)) |
637 |
< |
clean(prev, p); |
637 |
> |
clean(pcurr, p); |
638 |
|
} |
639 |
|
} |
640 |
|
|
649 |
|
if (!p.isData) |
650 |
|
return null; |
651 |
|
if (x != null) |
652 |
< |
return (E)x; |
652 |
> |
return (E) x; |
653 |
|
} |
654 |
|
} |
655 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
|
* Returns the number of elements in this queue. If this queue |
687 |
< |
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
688 |
< |
* <tt>Integer.MAX_VALUE</tt>. |
687 |
> |
* contains more than {@code Integer.MAX_VALUE} elements, returns |
688 |
> |
* {@code Integer.MAX_VALUE}. |
689 |
|
* |
690 |
|
* <p>Beware that, unlike in most collections, this method is |
691 |
|
* <em>NOT</em> a constant-time operation. Because of the |
723 |
|
return Integer.MAX_VALUE; |
724 |
|
} |
725 |
|
|
726 |
+ |
public boolean remove(Object o) { |
727 |
+ |
if (o == null) |
728 |
+ |
return false; |
729 |
+ |
for (;;) { |
730 |
+ |
QNode pred = traversalHead(); |
731 |
+ |
for (;;) { |
732 |
+ |
QNode q = pred.next; |
733 |
+ |
if (q == null || !q.isData) |
734 |
+ |
return false; |
735 |
+ |
if (q == pred) // restart |
736 |
+ |
break; |
737 |
+ |
Object x = q.get(); |
738 |
+ |
if (x != null && x != q && o.equals(x) && |
739 |
+ |
q.compareAndSet(x, q)) { |
740 |
+ |
clean(pred, q); |
741 |
+ |
return true; |
742 |
+ |
} |
743 |
+ |
pred = q; |
744 |
+ |
} |
745 |
+ |
} |
746 |
+ |
} |
747 |
+ |
|
748 |
|
/** |
749 |
|
* Save the state to a stream (that is, serialize it). |
750 |
|
* |
751 |
< |
* @serialData All of the elements (each an <tt>E</tt>) in |
751 |
> |
* @serialData All of the elements (each an {@code E}) in |
752 |
|
* the proper order, followed by a null |
753 |
|
* @param s the stream |
754 |
|
*/ |
755 |
|
private void writeObject(java.io.ObjectOutputStream s) |
756 |
|
throws java.io.IOException { |
757 |
|
s.defaultWriteObject(); |
758 |
< |
for (Iterator<E> it = iterator(); it.hasNext(); ) |
759 |
< |
s.writeObject(it.next()); |
758 |
> |
for (E e : this) |
759 |
> |
s.writeObject(e); |
760 |
|
// Use trailing null as sentinel |
761 |
|
s.writeObject(null); |
762 |
|
} |
764 |
|
/** |
765 |
|
* Reconstitute the Queue instance from a stream (that is, |
766 |
|
* deserialize it). |
767 |
+ |
* |
768 |
|
* @param s the stream |
769 |
|
*/ |
770 |
|
private void readObject(java.io.ObjectInputStream s) |
771 |
|
throws java.io.IOException, ClassNotFoundException { |
772 |
|
s.defaultReadObject(); |
773 |
+ |
resetHeadAndTail(); |
774 |
|
for (;;) { |
775 |
< |
E item = (E)s.readObject(); |
775 |
> |
E item = (E) s.readObject(); |
776 |
|
if (item == null) |
777 |
|
break; |
778 |
|
else |
779 |
|
offer(item); |
780 |
|
} |
781 |
|
} |
782 |
+ |
|
783 |
+ |
|
784 |
+ |
// Support for resetting head/tail while deserializing |
785 |
+ |
private void resetHeadAndTail() { |
786 |
+ |
QNode dummy = new QNode(null, false); |
787 |
+ |
UNSAFE.putObjectVolatile(this, headOffset, |
788 |
+ |
new PaddedAtomicReference<QNode>(dummy)); |
789 |
+ |
UNSAFE.putObjectVolatile(this, tailOffset, |
790 |
+ |
new PaddedAtomicReference<QNode>(dummy)); |
791 |
+ |
UNSAFE.putObjectVolatile(this, cleanMeOffset, |
792 |
+ |
new PaddedAtomicReference<QNode>(null)); |
793 |
+ |
} |
794 |
+ |
|
795 |
+ |
// Temporary Unsafe mechanics for preliminary release |
796 |
+ |
private static Unsafe getUnsafe() throws Throwable { |
797 |
+ |
try { |
798 |
+ |
return Unsafe.getUnsafe(); |
799 |
+ |
} catch (SecurityException se) { |
800 |
+ |
try { |
801 |
+ |
return java.security.AccessController.doPrivileged |
802 |
+ |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
803 |
+ |
public Unsafe run() throws Exception { |
804 |
+ |
return getUnsafePrivileged(); |
805 |
+ |
}}); |
806 |
+ |
} catch (java.security.PrivilegedActionException e) { |
807 |
+ |
throw e.getCause(); |
808 |
+ |
} |
809 |
+ |
} |
810 |
+ |
} |
811 |
+ |
|
812 |
+ |
private static Unsafe getUnsafePrivileged() |
813 |
+ |
throws NoSuchFieldException, IllegalAccessException { |
814 |
+ |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
815 |
+ |
f.setAccessible(true); |
816 |
+ |
return (Unsafe) f.get(null); |
817 |
+ |
} |
818 |
+ |
|
819 |
+ |
private static long fieldOffset(String fieldName) |
820 |
+ |
throws NoSuchFieldException { |
821 |
+ |
return UNSAFE.objectFieldOffset |
822 |
+ |
(LinkedTransferQueue.class.getDeclaredField(fieldName)); |
823 |
+ |
} |
824 |
+ |
|
825 |
+ |
private static final Unsafe UNSAFE; |
826 |
+ |
private static final long headOffset; |
827 |
+ |
private static final long tailOffset; |
828 |
+ |
private static final long cleanMeOffset; |
829 |
+ |
static { |
830 |
+ |
try { |
831 |
+ |
UNSAFE = getUnsafe(); |
832 |
+ |
headOffset = fieldOffset("head"); |
833 |
+ |
tailOffset = fieldOffset("tail"); |
834 |
+ |
cleanMeOffset = fieldOffset("cleanMe"); |
835 |
+ |
} catch (Throwable e) { |
836 |
+ |
throw new RuntimeException("Could not initialize intrinsics", e); |
837 |
+ |
} |
838 |
+ |
} |
839 |
+ |
|
840 |
|
} |