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 |
47 |
– |
* |
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 |
|
/* |
54 |
– |
* This is still a work in progress... |
55 |
– |
* |
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 |
+ |
private static final long serialVersionUID = -3375979862319811754L; |
127 |
|
} |
128 |
|
|
129 |
|
/** |
135 |
|
// enough padding for 64bytes with 4byte refs |
136 |
|
Object p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pa, pb, pc, pd, pe; |
137 |
|
PaddedAtomicReference(T r) { super(r); } |
138 |
+ |
private static final long serialVersionUID = 8170090609809740854L; |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
|
/** head of the queue */ |
143 |
|
private transient final PaddedAtomicReference<QNode> head; |
144 |
+ |
|
145 |
|
/** tail of the queue */ |
146 |
|
private transient final PaddedAtomicReference<QNode> tail; |
147 |
|
|
158 |
|
*/ |
159 |
|
private boolean advanceHead(QNode h, QNode nh) { |
160 |
|
if (h == head.get() && head.compareAndSet(h, nh)) { |
161 |
< |
h.next = h; // forget old next |
161 |
> |
h.clearNext(); // forget old next |
162 |
|
return true; |
163 |
|
} |
164 |
|
return false; |
166 |
|
|
167 |
|
/** |
168 |
|
* Puts or takes an item. Used for most queue operations (except |
169 |
< |
* poll() and tryTransfer()) |
169 |
> |
* poll() and tryTransfer()). See the similar code in |
170 |
> |
* SynchronousQueue for detailed explanation. |
171 |
> |
* |
172 |
|
* @param e the item or if null, signifies that this is a take |
173 |
|
* @param mode the wait mode: NOWAIT, TIMEOUT, WAIT |
174 |
|
* @param nanos timeout in nanosecs, used only if mode is TIMEOUT |
205 |
|
Object x = first.get(); |
206 |
|
if (x != first && first.compareAndSet(x, e)) { |
207 |
|
LockSupport.unpark(first.waiter); |
208 |
< |
return isData? e : x; |
208 |
> |
return isData ? e : x; |
209 |
|
} |
210 |
|
} |
211 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
|
* Version of xfer for poll() and tryTransfer, which |
218 |
< |
* simplifies control paths both here and in xfer |
218 |
> |
* simplifies control paths both here and in xfer. |
219 |
|
*/ |
220 |
|
private Object fulfill(Object e) { |
221 |
|
boolean isData = (e != null); |
243 |
|
Object x = first.get(); |
244 |
|
if (x != first && first.compareAndSet(x, e)) { |
245 |
|
LockSupport.unpark(first.waiter); |
246 |
< |
return isData? e : x; |
246 |
> |
return isData ? e : x; |
247 |
|
} |
248 |
|
} |
249 |
|
} |
266 |
|
if (mode == NOWAIT) |
267 |
|
return null; |
268 |
|
|
269 |
< |
long lastTime = (mode == TIMEOUT)? System.nanoTime() : 0; |
269 |
> |
long lastTime = (mode == TIMEOUT) ? System.nanoTime() : 0; |
270 |
|
Thread w = Thread.currentThread(); |
271 |
|
int spins = -1; // set to desired spin count below |
272 |
|
for (;;) { |
275 |
|
Object x = s.get(); |
276 |
|
if (x != e) { // Node was matched or cancelled |
277 |
|
advanceHead(pred, s); // unlink if head |
278 |
< |
if (x == s) // was cancelled |
279 |
< |
return clean(pred, s); |
278 |
> |
if (x == s) { // was cancelled |
279 |
> |
clean(pred, s); |
280 |
> |
return null; |
281 |
> |
} |
282 |
|
else if (x != null) { |
283 |
|
s.set(s); // avoid garbage retention |
284 |
|
return x; |
286 |
|
else |
287 |
|
return e; |
288 |
|
} |
278 |
– |
|
289 |
|
if (mode == TIMEOUT) { |
290 |
|
long now = System.nanoTime(); |
291 |
|
nanos -= now - lastTime; |
298 |
|
if (spins < 0) { |
299 |
|
QNode h = head.get(); // only spin if at head |
300 |
|
spins = ((h != null && h.next == s) ? |
301 |
< |
(mode == TIMEOUT? |
301 |
> |
((mode == TIMEOUT) ? |
302 |
|
maxTimedSpins : maxUntimedSpins) : 0); |
303 |
|
} |
304 |
|
if (spins > 0) |
306 |
|
else if (s.waiter == null) |
307 |
|
s.waiter = w; |
308 |
|
else if (mode != TIMEOUT) { |
309 |
< |
// LockSupport.park(this); |
300 |
< |
LockSupport.park(); // allows run on java5 |
309 |
> |
LockSupport.park(this); |
310 |
|
s.waiter = null; |
311 |
|
spins = -1; |
312 |
|
} |
313 |
|
else if (nanos > spinForTimeoutThreshold) { |
314 |
< |
// LockSupport.parkNanos(this, nanos); |
306 |
< |
LockSupport.parkNanos(nanos); |
314 |
> |
LockSupport.parkNanos(this, nanos); |
315 |
|
s.waiter = null; |
316 |
|
spins = -1; |
317 |
|
} |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
+ |
* Returns validated tail for use in cleaning methods. |
323 |
+ |
*/ |
324 |
+ |
private QNode getValidatedTail() { |
325 |
+ |
for (;;) { |
326 |
+ |
QNode h = head.get(); |
327 |
+ |
QNode first = h.next; |
328 |
+ |
if (first != null && first.next == first) { // help advance |
329 |
+ |
advanceHead(h, first); |
330 |
+ |
continue; |
331 |
+ |
} |
332 |
+ |
QNode t = tail.get(); |
333 |
+ |
QNode last = t.next; |
334 |
+ |
if (t == tail.get()) { |
335 |
+ |
if (last != null) |
336 |
+ |
tail.compareAndSet(t, last); // help advance |
337 |
+ |
else |
338 |
+ |
return t; |
339 |
+ |
} |
340 |
+ |
} |
341 |
+ |
} |
342 |
+ |
|
343 |
+ |
/** |
344 |
|
* Gets rid of cancelled node s with original predecessor pred. |
345 |
< |
* @return null (to simplify use by callers) |
345 |
> |
* |
346 |
> |
* @param pred predecessor of cancelled node |
347 |
> |
* @param s the cancelled node |
348 |
|
*/ |
349 |
< |
private Object clean(QNode pred, QNode s) { |
349 |
> |
private void clean(QNode pred, QNode s) { |
350 |
|
Thread w = s.waiter; |
351 |
|
if (w != null) { // Wake up thread |
352 |
|
s.waiter = null; |
354 |
|
LockSupport.unpark(w); |
355 |
|
} |
356 |
|
|
357 |
< |
for (;;) { |
358 |
< |
if (pred.next != s) // already cleaned |
359 |
< |
return null; |
360 |
< |
QNode h = head.get(); |
361 |
< |
QNode hn = h.next; // Absorb cancelled first node as head |
362 |
< |
if (hn != null && hn.next == hn) { |
363 |
< |
advanceHead(h, hn); |
364 |
< |
continue; |
365 |
< |
} |
366 |
< |
QNode t = tail.get(); // Ensure consistent read for tail |
367 |
< |
if (t == h) |
368 |
< |
return null; |
369 |
< |
QNode tn = t.next; |
370 |
< |
if (t != tail.get()) |
371 |
< |
continue; |
372 |
< |
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; |
357 |
> |
if (pred == null) |
358 |
> |
return; |
359 |
> |
|
360 |
> |
/* |
361 |
> |
* At any given time, exactly one node on list cannot be |
362 |
> |
* deleted -- the last inserted node. To accommodate this, if |
363 |
> |
* we cannot delete s, we save its predecessor as "cleanMe", |
364 |
> |
* processing the previously saved version first. At least one |
365 |
> |
* of node s or the node previously saved can always be |
366 |
> |
* processed, so this always terminates. |
367 |
> |
*/ |
368 |
> |
while (pred.next == s) { |
369 |
> |
QNode oldpred = reclean(); // First, help get rid of cleanMe |
370 |
> |
QNode t = getValidatedTail(); |
371 |
> |
if (s != t) { // If not tail, try to unsplice |
372 |
> |
QNode sn = s.next; // s.next == s means s already off list |
373 |
|
if (sn == s || pred.casNext(s, sn)) |
374 |
< |
return null; |
374 |
> |
break; |
375 |
|
} |
376 |
< |
QNode dp = cleanMe.get(); |
377 |
< |
if (dp != null) { // Try unlinking previous cancelled node |
378 |
< |
QNode d = dp.next; |
379 |
< |
QNode dn; |
380 |
< |
if (d == null || // d is gone or |
381 |
< |
d == dp || // d is off list or |
382 |
< |
d.get() != d || // d not cancelled or |
383 |
< |
(d != t && // d not tail and |
384 |
< |
(dn = d.next) != null && // has successor |
385 |
< |
dn != d && // that is on list |
386 |
< |
dp.casNext(d, dn))) // d unspliced |
387 |
< |
cleanMe.compareAndSet(dp, null); |
388 |
< |
if (dp == pred) |
389 |
< |
return null; // s is already saved node |
376 |
> |
else if (oldpred == pred || // Already saved |
377 |
> |
(oldpred == null && cleanMe.compareAndSet(null, pred))) |
378 |
> |
break; // Postpone cleaning |
379 |
> |
} |
380 |
> |
} |
381 |
> |
|
382 |
> |
/** |
383 |
> |
* Tries to unsplice the cancelled node held in cleanMe that was |
384 |
> |
* previously uncleanable because it was at tail. |
385 |
> |
* |
386 |
> |
* @return current cleanMe node (or null) |
387 |
> |
*/ |
388 |
> |
private QNode reclean() { |
389 |
> |
/* |
390 |
> |
* cleanMe is, or at one time was, predecessor of cancelled |
391 |
> |
* node s that was the tail so could not be unspliced. If s |
392 |
> |
* is no longer the tail, try to unsplice if necessary and |
393 |
> |
* make cleanMe slot available. This differs from similar |
394 |
> |
* code in clean() because we must check that pred still |
395 |
> |
* points to a cancelled node that must be unspliced -- if |
396 |
> |
* not, we can (must) clear cleanMe without unsplicing. |
397 |
> |
* This can loop only due to contention on casNext or |
398 |
> |
* clearing cleanMe. |
399 |
> |
*/ |
400 |
> |
QNode pred; |
401 |
> |
while ((pred = cleanMe.get()) != null) { |
402 |
> |
QNode t = getValidatedTail(); |
403 |
> |
QNode s = pred.next; |
404 |
> |
if (s != t) { |
405 |
> |
QNode sn; |
406 |
> |
if (s == null || s == pred || s.get() != s || |
407 |
> |
(sn = s.next) == s || pred.casNext(s, sn)) |
408 |
> |
cleanMe.compareAndSet(pred, null); |
409 |
|
} |
410 |
< |
else if (cleanMe.compareAndSet(null, pred)) |
411 |
< |
return null; // Postpone cleaning s |
410 |
> |
else // s is still tail; cannot clean |
411 |
> |
break; |
412 |
|
} |
413 |
+ |
return pred; |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
< |
* Creates an initially empty <tt>LinkedTransferQueue</tt>. |
417 |
> |
* Creates an initially empty {@code LinkedTransferQueue}. |
418 |
|
*/ |
419 |
|
public LinkedTransferQueue() { |
420 |
|
QNode dummy = new QNode(null, false); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
< |
* Creates a <tt>LinkedTransferQueue</tt> |
427 |
> |
* Creates a {@code LinkedTransferQueue} |
428 |
|
* initially containing the elements of the given collection, |
429 |
|
* added in traversal order of the collection's iterator. |
430 |
+ |
* |
431 |
|
* @param c the collection of elements to initially contain |
432 |
|
* @throws NullPointerException if the specified collection or any |
433 |
|
* of its elements are null |
457 |
|
return true; |
458 |
|
} |
459 |
|
|
460 |
+ |
public boolean add(E e) { |
461 |
+ |
if (e == null) throw new NullPointerException(); |
462 |
+ |
xfer(e, NOWAIT, 0); |
463 |
+ |
return true; |
464 |
+ |
} |
465 |
+ |
|
466 |
|
public void transfer(E e) throws InterruptedException { |
467 |
|
if (e == null) throw new NullPointerException(); |
468 |
|
if (xfer(e, WAIT, 0) == null) { |
489 |
|
public E take() throws InterruptedException { |
490 |
|
Object e = xfer(null, WAIT, 0); |
491 |
|
if (e != null) |
492 |
< |
return (E)e; |
492 |
> |
return (E) e; |
493 |
|
Thread.interrupted(); |
494 |
|
throw new InterruptedException(); |
495 |
|
} |
497 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
498 |
|
Object e = xfer(null, TIMEOUT, unit.toNanos(timeout)); |
499 |
|
if (e != null || !Thread.interrupted()) |
500 |
< |
return (E)e; |
500 |
> |
return (E) e; |
501 |
|
throw new InterruptedException(); |
502 |
|
} |
503 |
|
|
504 |
|
public E poll() { |
505 |
< |
return (E)fulfill(null); |
505 |
> |
return (E) fulfill(null); |
506 |
|
} |
507 |
|
|
508 |
|
public int drainTo(Collection<? super E> c) { |
536 |
|
// Traversal-based methods |
537 |
|
|
538 |
|
/** |
539 |
< |
* Return head after performing any outstanding helping steps |
539 |
> |
* Returns head after performing any outstanding helping steps. |
540 |
|
*/ |
541 |
|
private QNode traversalHead() { |
542 |
|
for (;;) { |
559 |
|
return h; |
560 |
|
} |
561 |
|
} |
562 |
+ |
reclean(); |
563 |
|
} |
564 |
|
} |
565 |
|
|
576 |
|
* if subsequently removed. |
577 |
|
*/ |
578 |
|
class Itr implements Iterator<E> { |
579 |
< |
QNode nextNode; // Next node to return next |
580 |
< |
QNode currentNode; // last returned node, for remove() |
581 |
< |
QNode prevNode; // predecessor of last returned node |
582 |
< |
E nextItem; // Cache of next item, once commited to in next |
579 |
> |
QNode next; // node to return next |
580 |
> |
QNode pnext; // predecessor of next |
581 |
> |
QNode snext; // successor of next |
582 |
> |
QNode curr; // last returned node, for remove() |
583 |
> |
QNode pcurr; // predecessor of curr, for remove() |
584 |
> |
E nextItem; // Cache of next item, once committed to in next |
585 |
|
|
586 |
|
Itr() { |
587 |
< |
nextNode = traversalHead(); |
531 |
< |
advance(); |
587 |
> |
findNext(); |
588 |
|
} |
589 |
|
|
590 |
< |
E advance() { |
591 |
< |
prevNode = currentNode; |
592 |
< |
currentNode = nextNode; |
593 |
< |
E x = nextItem; |
538 |
< |
|
539 |
< |
QNode p = nextNode.next; |
590 |
> |
/** |
591 |
> |
* Ensures next points to next valid node, or null if none. |
592 |
> |
*/ |
593 |
> |
void findNext() { |
594 |
|
for (;;) { |
595 |
< |
if (p == null || !p.isData) { |
596 |
< |
nextNode = null; |
597 |
< |
nextItem = null; |
598 |
< |
return x; |
595 |
> |
QNode pred = pnext; |
596 |
> |
QNode q = next; |
597 |
> |
if (pred == null || pred == q) { |
598 |
> |
pred = traversalHead(); |
599 |
> |
q = pred.next; |
600 |
|
} |
601 |
< |
Object item = p.get(); |
602 |
< |
if (item != p && item != null) { |
603 |
< |
nextNode = p; |
604 |
< |
nextItem = (E)item; |
605 |
< |
return x; |
601 |
> |
if (q == null || !q.isData) { |
602 |
> |
next = null; |
603 |
> |
return; |
604 |
> |
} |
605 |
> |
Object x = q.get(); |
606 |
> |
QNode s = q.next; |
607 |
> |
if (x != null && q != x && q != s) { |
608 |
> |
nextItem = (E) x; |
609 |
> |
snext = s; |
610 |
> |
pnext = pred; |
611 |
> |
next = q; |
612 |
> |
return; |
613 |
|
} |
614 |
< |
prevNode = p; |
615 |
< |
p = p.next; |
614 |
> |
pnext = q; |
615 |
> |
next = s; |
616 |
|
} |
617 |
|
} |
618 |
|
|
619 |
|
public boolean hasNext() { |
620 |
< |
return nextNode != null; |
620 |
> |
return next != null; |
621 |
|
} |
622 |
|
|
623 |
|
public E next() { |
624 |
< |
if (nextNode == null) throw new NoSuchElementException(); |
625 |
< |
return advance(); |
624 |
> |
if (next == null) throw new NoSuchElementException(); |
625 |
> |
pcurr = pnext; |
626 |
> |
curr = next; |
627 |
> |
pnext = next; |
628 |
> |
next = snext; |
629 |
> |
E x = nextItem; |
630 |
> |
findNext(); |
631 |
> |
return x; |
632 |
|
} |
633 |
|
|
634 |
|
public void remove() { |
635 |
< |
QNode p = currentNode; |
636 |
< |
QNode prev = prevNode; |
569 |
< |
if (prev == null || p == null) |
635 |
> |
QNode p = curr; |
636 |
> |
if (p == null) |
637 |
|
throw new IllegalStateException(); |
638 |
|
Object x = p.get(); |
639 |
|
if (x != null && x != p && p.compareAndSet(x, p)) |
640 |
< |
clean(prev, p); |
640 |
> |
clean(pcurr, p); |
641 |
|
} |
642 |
|
} |
643 |
|
|
652 |
|
if (!p.isData) |
653 |
|
return null; |
654 |
|
if (x != null) |
655 |
< |
return (E)x; |
655 |
> |
return (E) x; |
656 |
|
} |
657 |
|
} |
658 |
|
} |
687 |
|
|
688 |
|
/** |
689 |
|
* Returns the number of elements in this queue. If this queue |
690 |
< |
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
691 |
< |
* <tt>Integer.MAX_VALUE</tt>. |
690 |
> |
* contains more than {@code Integer.MAX_VALUE} elements, returns |
691 |
> |
* {@code Integer.MAX_VALUE}. |
692 |
|
* |
693 |
|
* <p>Beware that, unlike in most collections, this method is |
694 |
|
* <em>NOT</em> a constant-time operation. Because of the |
726 |
|
return Integer.MAX_VALUE; |
727 |
|
} |
728 |
|
|
729 |
+ |
public boolean remove(Object o) { |
730 |
+ |
if (o == null) |
731 |
+ |
return false; |
732 |
+ |
for (;;) { |
733 |
+ |
QNode pred = traversalHead(); |
734 |
+ |
for (;;) { |
735 |
+ |
QNode q = pred.next; |
736 |
+ |
if (q == null || !q.isData) |
737 |
+ |
return false; |
738 |
+ |
if (q == pred) // restart |
739 |
+ |
break; |
740 |
+ |
Object x = q.get(); |
741 |
+ |
if (x != null && x != q && o.equals(x) && |
742 |
+ |
q.compareAndSet(x, q)) { |
743 |
+ |
clean(pred, q); |
744 |
+ |
return true; |
745 |
+ |
} |
746 |
+ |
pred = q; |
747 |
+ |
} |
748 |
+ |
} |
749 |
+ |
} |
750 |
+ |
|
751 |
|
/** |
752 |
|
* Save the state to a stream (that is, serialize it). |
753 |
|
* |
754 |
< |
* @serialData All of the elements (each an <tt>E</tt>) in |
754 |
> |
* @serialData All of the elements (each an {@code E}) in |
755 |
|
* the proper order, followed by a null |
756 |
|
* @param s the stream |
757 |
|
*/ |
758 |
|
private void writeObject(java.io.ObjectOutputStream s) |
759 |
|
throws java.io.IOException { |
760 |
|
s.defaultWriteObject(); |
761 |
< |
for (Iterator<E> it = iterator(); it.hasNext(); ) |
762 |
< |
s.writeObject(it.next()); |
761 |
> |
for (E e : this) |
762 |
> |
s.writeObject(e); |
763 |
|
// Use trailing null as sentinel |
764 |
|
s.writeObject(null); |
765 |
|
} |
767 |
|
/** |
768 |
|
* Reconstitute the Queue instance from a stream (that is, |
769 |
|
* deserialize it). |
770 |
+ |
* |
771 |
|
* @param s the stream |
772 |
|
*/ |
773 |
|
private void readObject(java.io.ObjectInputStream s) |
775 |
|
s.defaultReadObject(); |
776 |
|
resetHeadAndTail(); |
777 |
|
for (;;) { |
778 |
< |
E item = (E)s.readObject(); |
778 |
> |
E item = (E) s.readObject(); |
779 |
|
if (item == null) |
780 |
|
break; |
781 |
|
else |
785 |
|
|
786 |
|
|
787 |
|
// Support for resetting head/tail while deserializing |
788 |
+ |
private void resetHeadAndTail() { |
789 |
+ |
QNode dummy = new QNode(null, false); |
790 |
+ |
UNSAFE.putObjectVolatile(this, headOffset, |
791 |
+ |
new PaddedAtomicReference<QNode>(dummy)); |
792 |
+ |
UNSAFE.putObjectVolatile(this, tailOffset, |
793 |
+ |
new PaddedAtomicReference<QNode>(dummy)); |
794 |
+ |
UNSAFE.putObjectVolatile(this, cleanMeOffset, |
795 |
+ |
new PaddedAtomicReference<QNode>(null)); |
796 |
+ |
} |
797 |
|
|
798 |
|
// Temporary Unsafe mechanics for preliminary release |
799 |
< |
private static final Unsafe _unsafe; |
799 |
> |
private static Unsafe getUnsafe() throws Throwable { |
800 |
> |
try { |
801 |
> |
return Unsafe.getUnsafe(); |
802 |
> |
} catch (SecurityException se) { |
803 |
> |
try { |
804 |
> |
return java.security.AccessController.doPrivileged |
805 |
> |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
806 |
> |
public Unsafe run() throws Exception { |
807 |
> |
return getUnsafePrivileged(); |
808 |
> |
}}); |
809 |
> |
} catch (java.security.PrivilegedActionException e) { |
810 |
> |
throw e.getCause(); |
811 |
> |
} |
812 |
> |
} |
813 |
> |
} |
814 |
> |
|
815 |
> |
private static Unsafe getUnsafePrivileged() |
816 |
> |
throws NoSuchFieldException, IllegalAccessException { |
817 |
> |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
818 |
> |
f.setAccessible(true); |
819 |
> |
return (Unsafe) f.get(null); |
820 |
> |
} |
821 |
> |
|
822 |
> |
private static long fieldOffset(String fieldName) |
823 |
> |
throws NoSuchFieldException { |
824 |
> |
return UNSAFE.objectFieldOffset |
825 |
> |
(LinkedTransferQueue.class.getDeclaredField(fieldName)); |
826 |
> |
} |
827 |
> |
|
828 |
> |
private static final Unsafe UNSAFE; |
829 |
|
private static final long headOffset; |
830 |
|
private static final long tailOffset; |
831 |
|
private static final long cleanMeOffset; |
832 |
|
static { |
833 |
|
try { |
834 |
< |
if (LinkedTransferQueue.class.getClassLoader() != null) { |
835 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
836 |
< |
f.setAccessible(true); |
837 |
< |
_unsafe = (Unsafe)f.get(null); |
838 |
< |
} |
711 |
< |
else |
712 |
< |
_unsafe = Unsafe.getUnsafe(); |
713 |
< |
headOffset = _unsafe.objectFieldOffset |
714 |
< |
(LinkedTransferQueue.class.getDeclaredField("head")); |
715 |
< |
tailOffset = _unsafe.objectFieldOffset |
716 |
< |
(LinkedTransferQueue.class.getDeclaredField("tail")); |
717 |
< |
cleanMeOffset = _unsafe.objectFieldOffset |
718 |
< |
(LinkedTransferQueue.class.getDeclaredField("cleanMe")); |
719 |
< |
} catch (Exception e) { |
834 |
> |
UNSAFE = getUnsafe(); |
835 |
> |
headOffset = fieldOffset("head"); |
836 |
> |
tailOffset = fieldOffset("tail"); |
837 |
> |
cleanMeOffset = fieldOffset("cleanMe"); |
838 |
> |
} catch (Throwable e) { |
839 |
|
throw new RuntimeException("Could not initialize intrinsics", e); |
840 |
|
} |
841 |
|
} |
842 |
|
|
724 |
– |
private void resetHeadAndTail() { |
725 |
– |
QNode dummy = new QNode(null, false); |
726 |
– |
_unsafe.putObjectVolatile(this, headOffset, dummy); |
727 |
– |
_unsafe.putObjectVolatile(this, tailOffset, dummy); |
728 |
– |
_unsafe.putObjectVolatile(this, cleanMeOffset, |
729 |
– |
new PaddedAtomicReference<QNode>(null)); |
730 |
– |
|
731 |
– |
} |
732 |
– |
|
843 |
|
} |