5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
8 |
+ |
|
9 |
|
import java.util.concurrent.*; |
10 |
< |
import java.util.concurrent.locks.*; |
11 |
< |
import java.util.concurrent.atomic.*; |
12 |
< |
import java.util.*; |
13 |
< |
import java.io.*; |
14 |
< |
import sun.misc.Unsafe; |
15 |
< |
import java.lang.reflect.*; |
10 |
> |
|
11 |
> |
import java.util.AbstractQueue; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.concurrent.locks.LockSupport; |
16 |
> |
import java.util.concurrent.atomic.AtomicReference; |
17 |
|
|
18 |
|
/** |
19 |
|
* An unbounded {@linkplain TransferQueue} based on linked nodes. |
23 |
|
* producer. The <em>tail</em> of the queue is that element that has |
24 |
|
* been on the queue the shortest time for some producer. |
25 |
|
* |
26 |
< |
* <p>Beware that, unlike in most collections, the <tt>size</tt> |
26 |
> |
* <p>Beware that, unlike in most collections, the {@code size} |
27 |
|
* method is <em>NOT</em> a constant-time operation. Because of the |
28 |
|
* asynchronous nature of these queues, determining the current number |
29 |
|
* of elements requires a traversal of the elements. |
46 |
|
* @since 1.7 |
47 |
|
* @author Doug Lea |
48 |
|
* @param <E> the type of elements held in this collection |
47 |
– |
* |
49 |
|
*/ |
50 |
|
public class LinkedTransferQueue<E> extends AbstractQueue<E> |
51 |
|
implements TransferQueue<E>, java.io.Serializable { |
82 |
|
* seems not to vary with number of CPUs (beyond 2) so is just |
83 |
|
* a constant. |
84 |
|
*/ |
85 |
< |
static final int maxTimedSpins = (NCPUS < 2)? 0 : 32; |
85 |
> |
static final int maxTimedSpins = (NCPUS < 2) ? 0 : 32; |
86 |
|
|
87 |
|
/** |
88 |
|
* The number of times to spin before blocking in untimed waits. |
104 |
|
* garbage retention. Similarly, setting the next field to this is |
105 |
|
* used as sentinel that node is off list. |
106 |
|
*/ |
107 |
< |
static final class QNode extends AtomicReference<Object> { |
108 |
< |
volatile QNode next; |
107 |
> |
static final class Node<E> extends AtomicReference<Object> { |
108 |
> |
volatile Node<E> next; |
109 |
|
volatile Thread waiter; // to control park/unpark |
110 |
|
final boolean isData; |
111 |
< |
QNode(Object item, boolean isData) { |
111 |
> |
|
112 |
> |
Node(E item, boolean isData) { |
113 |
|
super(item); |
114 |
|
this.isData = isData; |
115 |
|
} |
116 |
|
|
117 |
< |
static final AtomicReferenceFieldUpdater<QNode, QNode> |
118 |
< |
nextUpdater = AtomicReferenceFieldUpdater.newUpdater |
119 |
< |
(QNode.class, QNode.class, "next"); |
117 |
> |
final boolean casNext(Node<E> cmp, Node<E> val) { |
118 |
> |
return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); |
119 |
> |
} |
120 |
> |
|
121 |
> |
final void clearNext() { |
122 |
> |
UNSAFE.putOrderedObject(this, nextOffset, this); |
123 |
> |
} |
124 |
> |
|
125 |
> |
// Unsafe mechanics |
126 |
|
|
127 |
< |
boolean casNext(QNode cmp, QNode val) { |
128 |
< |
return nextUpdater.compareAndSet(this, cmp, val); |
127 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
128 |
> |
private static final long nextOffset = |
129 |
> |
objectFieldOffset(UNSAFE, "next", Node.class); |
130 |
> |
|
131 |
> |
/** |
132 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
133 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
134 |
> |
* into a jdk. |
135 |
> |
* |
136 |
> |
* @return a sun.misc.Unsafe |
137 |
> |
*/ |
138 |
> |
private static sun.misc.Unsafe getUnsafe() { |
139 |
> |
try { |
140 |
> |
return sun.misc.Unsafe.getUnsafe(); |
141 |
> |
} catch (SecurityException se) { |
142 |
> |
try { |
143 |
> |
return java.security.AccessController.doPrivileged |
144 |
> |
(new java.security |
145 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
146 |
> |
public sun.misc.Unsafe run() throws Exception { |
147 |
> |
java.lang.reflect.Field f = sun.misc |
148 |
> |
.Unsafe.class.getDeclaredField("theUnsafe"); |
149 |
> |
f.setAccessible(true); |
150 |
> |
return (sun.misc.Unsafe) f.get(null); |
151 |
> |
}}); |
152 |
> |
} catch (java.security.PrivilegedActionException e) { |
153 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
154 |
> |
e.getCause()); |
155 |
> |
} |
156 |
> |
} |
157 |
|
} |
158 |
+ |
|
159 |
+ |
private static final long serialVersionUID = -3375979862319811754L; |
160 |
|
} |
161 |
|
|
162 |
|
/** |
168 |
|
// enough padding for 64bytes with 4byte refs |
169 |
|
Object p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pa, pb, pc, pd, pe; |
170 |
|
PaddedAtomicReference(T r) { super(r); } |
171 |
+ |
private static final long serialVersionUID = 8170090609809740854L; |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
/** head of the queue */ |
176 |
< |
private transient final PaddedAtomicReference<QNode> head; |
176 |
> |
private transient final PaddedAtomicReference<Node<E>> head; |
177 |
> |
|
178 |
|
/** tail of the queue */ |
179 |
< |
private transient final PaddedAtomicReference<QNode> tail; |
179 |
> |
private transient final PaddedAtomicReference<Node<E>> tail; |
180 |
|
|
181 |
|
/** |
182 |
|
* Reference to a cancelled node that might not yet have been |
183 |
|
* unlinked from queue because it was the last inserted node |
184 |
|
* when it cancelled. |
185 |
|
*/ |
186 |
< |
private transient final PaddedAtomicReference<QNode> cleanMe; |
186 |
> |
private transient final PaddedAtomicReference<Node<E>> cleanMe; |
187 |
|
|
188 |
|
/** |
189 |
|
* Tries to cas nh as new head; if successful, unlink |
190 |
|
* old head's next node to avoid garbage retention. |
191 |
|
*/ |
192 |
< |
private boolean advanceHead(QNode h, QNode nh) { |
192 |
> |
private boolean advanceHead(Node<E> h, Node<E> nh) { |
193 |
|
if (h == head.get() && head.compareAndSet(h, nh)) { |
194 |
< |
h.next = h; // forget old next |
194 |
> |
h.clearNext(); // forget old next |
195 |
|
return true; |
196 |
|
} |
197 |
|
return false; |
201 |
|
* Puts or takes an item. Used for most queue operations (except |
202 |
|
* poll() and tryTransfer()). See the similar code in |
203 |
|
* SynchronousQueue for detailed explanation. |
204 |
+ |
* |
205 |
|
* @param e the item or if null, signifies that this is a take |
206 |
|
* @param mode the wait mode: NOWAIT, TIMEOUT, WAIT |
207 |
|
* @param nanos timeout in nanosecs, used only if mode is TIMEOUT |
208 |
|
* @return an item, or null on failure |
209 |
|
*/ |
210 |
< |
private Object xfer(Object e, int mode, long nanos) { |
210 |
> |
private E xfer(E e, int mode, long nanos) { |
211 |
|
boolean isData = (e != null); |
212 |
< |
QNode s = null; |
213 |
< |
final PaddedAtomicReference<QNode> head = this.head; |
214 |
< |
final PaddedAtomicReference<QNode> tail = this.tail; |
212 |
> |
Node<E> s = null; |
213 |
> |
final PaddedAtomicReference<Node<E>> head = this.head; |
214 |
> |
final PaddedAtomicReference<Node<E>> tail = this.tail; |
215 |
|
|
216 |
|
for (;;) { |
217 |
< |
QNode t = tail.get(); |
218 |
< |
QNode h = head.get(); |
217 |
> |
Node<E> t = tail.get(); |
218 |
> |
Node<E> h = head.get(); |
219 |
|
|
220 |
|
if (t != null && (t == h || t.isData == isData)) { |
221 |
|
if (s == null) |
222 |
< |
s = new QNode(e, isData); |
223 |
< |
QNode last = t.next; |
222 |
> |
s = new Node<E>(e, isData); |
223 |
> |
Node<E> last = t.next; |
224 |
|
if (last != null) { |
225 |
|
if (t == tail.get()) |
226 |
|
tail.compareAndSet(t, last); |
232 |
|
} |
233 |
|
|
234 |
|
else if (h != null) { |
235 |
< |
QNode first = h.next; |
235 |
> |
Node<E> first = h.next; |
236 |
|
if (t == tail.get() && first != null && |
237 |
|
advanceHead(h, first)) { |
238 |
|
Object x = first.get(); |
239 |
|
if (x != first && first.compareAndSet(x, e)) { |
240 |
|
LockSupport.unpark(first.waiter); |
241 |
< |
return isData? e : x; |
241 |
> |
return isData ? e : (E) x; |
242 |
|
} |
243 |
|
} |
244 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
|
* Version of xfer for poll() and tryTransfer, which |
251 |
< |
* simplifies control paths both here and in xfer |
251 |
> |
* simplifies control paths both here and in xfer. |
252 |
|
*/ |
253 |
< |
private Object fulfill(Object e) { |
253 |
> |
private E fulfill(E e) { |
254 |
|
boolean isData = (e != null); |
255 |
< |
final PaddedAtomicReference<QNode> head = this.head; |
256 |
< |
final PaddedAtomicReference<QNode> tail = this.tail; |
255 |
> |
final PaddedAtomicReference<Node<E>> head = this.head; |
256 |
> |
final PaddedAtomicReference<Node<E>> tail = this.tail; |
257 |
|
|
258 |
|
for (;;) { |
259 |
< |
QNode t = tail.get(); |
260 |
< |
QNode h = head.get(); |
259 |
> |
Node<E> t = tail.get(); |
260 |
> |
Node<E> h = head.get(); |
261 |
|
|
262 |
|
if (t != null && (t == h || t.isData == isData)) { |
263 |
< |
QNode last = t.next; |
263 |
> |
Node<E> last = t.next; |
264 |
|
if (t == tail.get()) { |
265 |
|
if (last != null) |
266 |
|
tail.compareAndSet(t, last); |
269 |
|
} |
270 |
|
} |
271 |
|
else if (h != null) { |
272 |
< |
QNode first = h.next; |
272 |
> |
Node<E> first = h.next; |
273 |
|
if (t == tail.get() && |
274 |
|
first != null && |
275 |
|
advanceHead(h, first)) { |
276 |
|
Object x = first.get(); |
277 |
|
if (x != first && first.compareAndSet(x, e)) { |
278 |
|
LockSupport.unpark(first.waiter); |
279 |
< |
return isData? e : x; |
279 |
> |
return isData ? e : (E) x; |
280 |
|
} |
281 |
|
} |
282 |
|
} |
294 |
|
* @param nanos timeout value |
295 |
|
* @return matched item, or s if cancelled |
296 |
|
*/ |
297 |
< |
private Object awaitFulfill(QNode pred, QNode s, Object e, |
298 |
< |
int mode, long nanos) { |
297 |
> |
private E awaitFulfill(Node<E> pred, Node<E> s, E e, |
298 |
> |
int mode, long nanos) { |
299 |
|
if (mode == NOWAIT) |
300 |
|
return null; |
301 |
|
|
302 |
< |
long lastTime = (mode == TIMEOUT)? System.nanoTime() : 0; |
302 |
> |
long lastTime = (mode == TIMEOUT) ? System.nanoTime() : 0; |
303 |
|
Thread w = Thread.currentThread(); |
304 |
|
int spins = -1; // set to desired spin count below |
305 |
|
for (;;) { |
308 |
|
Object x = s.get(); |
309 |
|
if (x != e) { // Node was matched or cancelled |
310 |
|
advanceHead(pred, s); // unlink if head |
311 |
< |
if (x == s) { // was cancelled |
311 |
> |
if (x == s) { // was cancelled |
312 |
|
clean(pred, s); |
313 |
|
return null; |
314 |
|
} |
315 |
|
else if (x != null) { |
316 |
|
s.set(s); // avoid garbage retention |
317 |
< |
return x; |
317 |
> |
return (E) x; |
318 |
|
} |
319 |
|
else |
320 |
|
return e; |
329 |
|
} |
330 |
|
} |
331 |
|
if (spins < 0) { |
332 |
< |
QNode h = head.get(); // only spin if at head |
332 |
> |
Node<E> h = head.get(); // only spin if at head |
333 |
|
spins = ((h != null && h.next == s) ? |
334 |
< |
(mode == TIMEOUT? |
334 |
> |
((mode == TIMEOUT) ? |
335 |
|
maxTimedSpins : maxUntimedSpins) : 0); |
336 |
|
} |
337 |
|
if (spins > 0) |
339 |
|
else if (s.waiter == null) |
340 |
|
s.waiter = w; |
341 |
|
else if (mode != TIMEOUT) { |
342 |
< |
// LockSupport.park(this); |
302 |
< |
LockSupport.park(); // allows run on java5 |
342 |
> |
LockSupport.park(this); |
343 |
|
s.waiter = null; |
344 |
|
spins = -1; |
345 |
|
} |
346 |
|
else if (nanos > spinForTimeoutThreshold) { |
347 |
< |
// LockSupport.parkNanos(this, nanos); |
308 |
< |
LockSupport.parkNanos(nanos); |
347 |
> |
LockSupport.parkNanos(this, nanos); |
348 |
|
s.waiter = null; |
349 |
|
spins = -1; |
350 |
|
} |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
< |
* Returns validated tail for use in cleaning methods |
355 |
> |
* Returns validated tail for use in cleaning methods. |
356 |
|
*/ |
357 |
< |
private QNode getValidatedTail() { |
357 |
> |
private Node<E> getValidatedTail() { |
358 |
|
for (;;) { |
359 |
< |
QNode h = head.get(); |
360 |
< |
QNode first = h.next; |
359 |
> |
Node<E> h = head.get(); |
360 |
> |
Node<E> first = h.next; |
361 |
|
if (first != null && first.next == first) { // help advance |
362 |
|
advanceHead(h, first); |
363 |
|
continue; |
364 |
|
} |
365 |
< |
QNode t = tail.get(); |
366 |
< |
QNode last = t.next; |
365 |
> |
Node<E> t = tail.get(); |
366 |
> |
Node<E> last = t.next; |
367 |
|
if (t == tail.get()) { |
368 |
|
if (last != null) |
369 |
|
tail.compareAndSet(t, last); // help advance |
375 |
|
|
376 |
|
/** |
377 |
|
* Gets rid of cancelled node s with original predecessor pred. |
378 |
+ |
* |
379 |
|
* @param pred predecessor of cancelled node |
380 |
|
* @param s the cancelled node |
381 |
|
*/ |
382 |
< |
private void clean(QNode pred, QNode s) { |
382 |
> |
private void clean(Node<E> pred, Node<E> s) { |
383 |
|
Thread w = s.waiter; |
384 |
|
if (w != null) { // Wake up thread |
385 |
|
s.waiter = null; |
386 |
|
if (w != Thread.currentThread()) |
387 |
|
LockSupport.unpark(w); |
388 |
|
} |
389 |
+ |
|
390 |
+ |
if (pred == null) |
391 |
+ |
return; |
392 |
+ |
|
393 |
|
/* |
394 |
|
* At any given time, exactly one node on list cannot be |
395 |
|
* deleted -- the last inserted node. To accommodate this, if |
399 |
|
* processed, so this always terminates. |
400 |
|
*/ |
401 |
|
while (pred.next == s) { |
402 |
< |
QNode oldpred = reclean(); // First, help get rid of cleanMe |
403 |
< |
QNode t = getValidatedTail(); |
402 |
> |
Node<E> oldpred = reclean(); // First, help get rid of cleanMe |
403 |
> |
Node<E> t = getValidatedTail(); |
404 |
|
if (s != t) { // If not tail, try to unsplice |
405 |
< |
QNode sn = s.next; // s.next == s means s already off list |
405 |
> |
Node<E> sn = s.next; // s.next == s means s already off list |
406 |
|
if (sn == s || pred.casNext(s, sn)) |
407 |
|
break; |
408 |
|
} |
415 |
|
/** |
416 |
|
* Tries to unsplice the cancelled node held in cleanMe that was |
417 |
|
* previously uncleanable because it was at tail. |
418 |
+ |
* |
419 |
|
* @return current cleanMe node (or null) |
420 |
|
*/ |
421 |
< |
private QNode reclean() { |
421 |
> |
private Node<E> reclean() { |
422 |
|
/* |
423 |
|
* cleanMe is, or at one time was, predecessor of cancelled |
424 |
|
* node s that was the tail so could not be unspliced. If s |
430 |
|
* This can loop only due to contention on casNext or |
431 |
|
* clearing cleanMe. |
432 |
|
*/ |
433 |
< |
QNode pred; |
433 |
> |
Node<E> pred; |
434 |
|
while ((pred = cleanMe.get()) != null) { |
435 |
< |
QNode t = getValidatedTail(); |
436 |
< |
QNode s = pred.next; |
435 |
> |
Node<E> t = getValidatedTail(); |
436 |
> |
Node<E> s = pred.next; |
437 |
|
if (s != t) { |
438 |
< |
QNode sn; |
438 |
> |
Node<E> sn; |
439 |
|
if (s == null || s == pred || s.get() != s || |
440 |
|
(sn = s.next) == s || pred.casNext(s, sn)) |
441 |
|
cleanMe.compareAndSet(pred, null); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
< |
* Creates an initially empty <tt>LinkedTransferQueue</tt>. |
450 |
> |
* Creates an initially empty {@code LinkedTransferQueue}. |
451 |
|
*/ |
452 |
|
public LinkedTransferQueue() { |
453 |
< |
QNode dummy = new QNode(null, false); |
454 |
< |
head = new PaddedAtomicReference<QNode>(dummy); |
455 |
< |
tail = new PaddedAtomicReference<QNode>(dummy); |
456 |
< |
cleanMe = new PaddedAtomicReference<QNode>(null); |
453 |
> |
Node<E> dummy = new Node<E>(null, false); |
454 |
> |
head = new PaddedAtomicReference<Node<E>>(dummy); |
455 |
> |
tail = new PaddedAtomicReference<Node<E>>(dummy); |
456 |
> |
cleanMe = new PaddedAtomicReference<Node<E>>(null); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
460 |
< |
* Creates a <tt>LinkedTransferQueue</tt> |
460 |
> |
* Creates a {@code LinkedTransferQueue} |
461 |
|
* initially containing the elements of the given collection, |
462 |
|
* added in traversal order of the collection's iterator. |
463 |
+ |
* |
464 |
|
* @param c the collection of elements to initially contain |
465 |
|
* @throws NullPointerException if the specified collection or any |
466 |
|
* of its elements are null |
470 |
|
addAll(c); |
471 |
|
} |
472 |
|
|
473 |
+ |
/** |
474 |
+ |
* @throws InterruptedException {@inheritDoc} |
475 |
+ |
* @throws NullPointerException {@inheritDoc} |
476 |
+ |
*/ |
477 |
|
public void put(E e) throws InterruptedException { |
478 |
|
if (e == null) throw new NullPointerException(); |
479 |
|
if (Thread.interrupted()) throw new InterruptedException(); |
480 |
|
xfer(e, NOWAIT, 0); |
481 |
|
} |
482 |
|
|
483 |
+ |
/** |
484 |
+ |
* @throws InterruptedException {@inheritDoc} |
485 |
+ |
* @throws NullPointerException {@inheritDoc} |
486 |
+ |
*/ |
487 |
|
public boolean offer(E e, long timeout, TimeUnit unit) |
488 |
|
throws InterruptedException { |
489 |
|
if (e == null) throw new NullPointerException(); |
492 |
|
return true; |
493 |
|
} |
494 |
|
|
495 |
+ |
/** |
496 |
+ |
* @throws NullPointerException {@inheritDoc} |
497 |
+ |
*/ |
498 |
|
public boolean offer(E e) { |
499 |
|
if (e == null) throw new NullPointerException(); |
500 |
|
xfer(e, NOWAIT, 0); |
501 |
|
return true; |
502 |
|
} |
503 |
|
|
504 |
+ |
/** |
505 |
+ |
* @throws NullPointerException {@inheritDoc} |
506 |
+ |
*/ |
507 |
+ |
public boolean add(E e) { |
508 |
+ |
if (e == null) throw new NullPointerException(); |
509 |
+ |
xfer(e, NOWAIT, 0); |
510 |
+ |
return true; |
511 |
+ |
} |
512 |
+ |
|
513 |
+ |
/** |
514 |
+ |
* @throws InterruptedException {@inheritDoc} |
515 |
+ |
* @throws NullPointerException {@inheritDoc} |
516 |
+ |
*/ |
517 |
|
public void transfer(E e) throws InterruptedException { |
518 |
|
if (e == null) throw new NullPointerException(); |
519 |
|
if (xfer(e, WAIT, 0) == null) { |
522 |
|
} |
523 |
|
} |
524 |
|
|
525 |
+ |
/** |
526 |
+ |
* @throws InterruptedException {@inheritDoc} |
527 |
+ |
* @throws NullPointerException {@inheritDoc} |
528 |
+ |
*/ |
529 |
|
public boolean tryTransfer(E e, long timeout, TimeUnit unit) |
530 |
|
throws InterruptedException { |
531 |
|
if (e == null) throw new NullPointerException(); |
536 |
|
throw new InterruptedException(); |
537 |
|
} |
538 |
|
|
539 |
+ |
/** |
540 |
+ |
* @throws NullPointerException {@inheritDoc} |
541 |
+ |
*/ |
542 |
|
public boolean tryTransfer(E e) { |
543 |
|
if (e == null) throw new NullPointerException(); |
544 |
|
return fulfill(e) != null; |
545 |
|
} |
546 |
|
|
547 |
+ |
/** |
548 |
+ |
* @throws InterruptedException {@inheritDoc} |
549 |
+ |
*/ |
550 |
|
public E take() throws InterruptedException { |
551 |
|
Object e = xfer(null, WAIT, 0); |
552 |
|
if (e != null) |
553 |
< |
return (E)e; |
553 |
> |
return (E) e; |
554 |
|
Thread.interrupted(); |
555 |
|
throw new InterruptedException(); |
556 |
|
} |
557 |
|
|
558 |
+ |
/** |
559 |
+ |
* @throws InterruptedException {@inheritDoc} |
560 |
+ |
*/ |
561 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
562 |
|
Object e = xfer(null, TIMEOUT, unit.toNanos(timeout)); |
563 |
|
if (e != null || !Thread.interrupted()) |
564 |
< |
return (E)e; |
564 |
> |
return (E) e; |
565 |
|
throw new InterruptedException(); |
566 |
|
} |
567 |
|
|
568 |
|
public E poll() { |
569 |
< |
return (E)fulfill(null); |
569 |
> |
return fulfill(null); |
570 |
|
} |
571 |
|
|
572 |
+ |
/** |
573 |
+ |
* @throws NullPointerException {@inheritDoc} |
574 |
+ |
* @throws IllegalArgumentException {@inheritDoc} |
575 |
+ |
*/ |
576 |
|
public int drainTo(Collection<? super E> c) { |
577 |
|
if (c == null) |
578 |
|
throw new NullPointerException(); |
587 |
|
return n; |
588 |
|
} |
589 |
|
|
590 |
+ |
/** |
591 |
+ |
* @throws NullPointerException {@inheritDoc} |
592 |
+ |
* @throws IllegalArgumentException {@inheritDoc} |
593 |
+ |
*/ |
594 |
|
public int drainTo(Collection<? super E> c, int maxElements) { |
595 |
|
if (c == null) |
596 |
|
throw new NullPointerException(); |
608 |
|
// Traversal-based methods |
609 |
|
|
610 |
|
/** |
611 |
< |
* Return head after performing any outstanding helping steps |
611 |
> |
* Returns head after performing any outstanding helping steps. |
612 |
|
*/ |
613 |
< |
private QNode traversalHead() { |
613 |
> |
private Node<E> traversalHead() { |
614 |
|
for (;;) { |
615 |
< |
QNode t = tail.get(); |
616 |
< |
QNode h = head.get(); |
615 |
> |
Node<E> t = tail.get(); |
616 |
> |
Node<E> h = head.get(); |
617 |
|
if (h != null && t != null) { |
618 |
< |
QNode last = t.next; |
619 |
< |
QNode first = h.next; |
618 |
> |
Node<E> last = t.next; |
619 |
> |
Node<E> first = h.next; |
620 |
|
if (t == tail.get()) { |
621 |
|
if (last != null) |
622 |
|
tail.compareAndSet(t, last); |
631 |
|
return h; |
632 |
|
} |
633 |
|
} |
634 |
+ |
reclean(); |
635 |
|
} |
636 |
|
} |
637 |
|
|
648 |
|
* if subsequently removed. |
649 |
|
*/ |
650 |
|
class Itr implements Iterator<E> { |
651 |
< |
QNode nextNode; // Next node to return next |
652 |
< |
QNode currentNode; // last returned node, for remove() |
653 |
< |
QNode prevNode; // predecessor of last returned node |
654 |
< |
E nextItem; // Cache of next item, once commited to in next |
651 |
> |
Node<E> next; // node to return next |
652 |
> |
Node<E> pnext; // predecessor of next |
653 |
> |
Node<E> snext; // successor of next |
654 |
> |
Node<E> curr; // last returned node, for remove() |
655 |
> |
Node<E> pcurr; // predecessor of curr, for remove() |
656 |
> |
E nextItem; // Cache of next item, once committed to in next |
657 |
|
|
658 |
|
Itr() { |
659 |
< |
nextNode = traversalHead(); |
566 |
< |
advance(); |
659 |
> |
findNext(); |
660 |
|
} |
661 |
|
|
662 |
< |
E advance() { |
663 |
< |
prevNode = currentNode; |
664 |
< |
currentNode = nextNode; |
665 |
< |
E x = nextItem; |
573 |
< |
|
574 |
< |
QNode p = nextNode.next; |
662 |
> |
/** |
663 |
> |
* Ensures next points to next valid node, or null if none. |
664 |
> |
*/ |
665 |
> |
void findNext() { |
666 |
|
for (;;) { |
667 |
< |
if (p == null || !p.isData) { |
668 |
< |
nextNode = null; |
669 |
< |
nextItem = null; |
670 |
< |
return x; |
671 |
< |
} |
672 |
< |
Object item = p.get(); |
673 |
< |
if (item != p && item != null) { |
674 |
< |
nextNode = p; |
675 |
< |
nextItem = (E)item; |
676 |
< |
return x; |
667 |
> |
Node<E> pred = pnext; |
668 |
> |
Node<E> q = next; |
669 |
> |
if (pred == null || pred == q) { |
670 |
> |
pred = traversalHead(); |
671 |
> |
q = pred.next; |
672 |
> |
} |
673 |
> |
if (q == null || !q.isData) { |
674 |
> |
next = null; |
675 |
> |
return; |
676 |
> |
} |
677 |
> |
Object x = q.get(); |
678 |
> |
Node<E> s = q.next; |
679 |
> |
if (x != null && q != x && q != s) { |
680 |
> |
nextItem = (E) x; |
681 |
> |
snext = s; |
682 |
> |
pnext = pred; |
683 |
> |
next = q; |
684 |
> |
return; |
685 |
|
} |
686 |
< |
prevNode = p; |
687 |
< |
p = p.next; |
686 |
> |
pnext = q; |
687 |
> |
next = s; |
688 |
|
} |
689 |
|
} |
690 |
|
|
691 |
|
public boolean hasNext() { |
692 |
< |
return nextNode != null; |
692 |
> |
return next != null; |
693 |
|
} |
694 |
|
|
695 |
|
public E next() { |
696 |
< |
if (nextNode == null) throw new NoSuchElementException(); |
697 |
< |
return advance(); |
696 |
> |
if (next == null) throw new NoSuchElementException(); |
697 |
> |
pcurr = pnext; |
698 |
> |
curr = next; |
699 |
> |
pnext = next; |
700 |
> |
next = snext; |
701 |
> |
E x = nextItem; |
702 |
> |
findNext(); |
703 |
> |
return x; |
704 |
|
} |
705 |
|
|
706 |
|
public void remove() { |
707 |
< |
QNode p = currentNode; |
708 |
< |
QNode prev = prevNode; |
604 |
< |
if (prev == null || p == null) |
707 |
> |
Node<E> p = curr; |
708 |
> |
if (p == null) |
709 |
|
throw new IllegalStateException(); |
710 |
|
Object x = p.get(); |
711 |
|
if (x != null && x != p && p.compareAndSet(x, p)) |
712 |
< |
clean(prev, p); |
712 |
> |
clean(pcurr, p); |
713 |
|
} |
714 |
|
} |
715 |
|
|
716 |
|
public E peek() { |
717 |
|
for (;;) { |
718 |
< |
QNode h = traversalHead(); |
719 |
< |
QNode p = h.next; |
718 |
> |
Node<E> h = traversalHead(); |
719 |
> |
Node<E> p = h.next; |
720 |
|
if (p == null) |
721 |
|
return null; |
722 |
|
Object x = p.get(); |
724 |
|
if (!p.isData) |
725 |
|
return null; |
726 |
|
if (x != null) |
727 |
< |
return (E)x; |
727 |
> |
return (E) x; |
728 |
|
} |
729 |
|
} |
730 |
|
} |
731 |
|
|
732 |
|
public boolean isEmpty() { |
733 |
|
for (;;) { |
734 |
< |
QNode h = traversalHead(); |
735 |
< |
QNode p = h.next; |
734 |
> |
Node<E> h = traversalHead(); |
735 |
> |
Node<E> p = h.next; |
736 |
|
if (p == null) |
737 |
|
return true; |
738 |
|
Object x = p.get(); |
747 |
|
|
748 |
|
public boolean hasWaitingConsumer() { |
749 |
|
for (;;) { |
750 |
< |
QNode h = traversalHead(); |
751 |
< |
QNode p = h.next; |
750 |
> |
Node<E> h = traversalHead(); |
751 |
> |
Node<E> p = h.next; |
752 |
|
if (p == null) |
753 |
|
return false; |
754 |
|
Object x = p.get(); |
759 |
|
|
760 |
|
/** |
761 |
|
* Returns the number of elements in this queue. If this queue |
762 |
< |
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
763 |
< |
* <tt>Integer.MAX_VALUE</tt>. |
762 |
> |
* contains more than {@code Integer.MAX_VALUE} elements, returns |
763 |
> |
* {@code Integer.MAX_VALUE}. |
764 |
|
* |
765 |
|
* <p>Beware that, unlike in most collections, this method is |
766 |
|
* <em>NOT</em> a constant-time operation. Because of the |
771 |
|
*/ |
772 |
|
public int size() { |
773 |
|
int count = 0; |
774 |
< |
QNode h = traversalHead(); |
775 |
< |
for (QNode p = h.next; p != null && p.isData; p = p.next) { |
774 |
> |
Node<E> h = traversalHead(); |
775 |
> |
for (Node<E> p = h.next; p != null && p.isData; p = p.next) { |
776 |
|
Object x = p.get(); |
777 |
|
if (x != null && x != p) { |
778 |
|
if (++count == Integer.MAX_VALUE) // saturated |
784 |
|
|
785 |
|
public int getWaitingConsumerCount() { |
786 |
|
int count = 0; |
787 |
< |
QNode h = traversalHead(); |
788 |
< |
for (QNode p = h.next; p != null && !p.isData; p = p.next) { |
787 |
> |
Node<E> h = traversalHead(); |
788 |
> |
for (Node<E> p = h.next; p != null && !p.isData; p = p.next) { |
789 |
|
if (p.get() == null) { |
790 |
|
if (++count == Integer.MAX_VALUE) |
791 |
|
break; |
798 |
|
return Integer.MAX_VALUE; |
799 |
|
} |
800 |
|
|
801 |
+ |
public boolean remove(Object o) { |
802 |
+ |
if (o == null) |
803 |
+ |
return false; |
804 |
+ |
for (;;) { |
805 |
+ |
Node<E> pred = traversalHead(); |
806 |
+ |
for (;;) { |
807 |
+ |
Node<E> q = pred.next; |
808 |
+ |
if (q == null || !q.isData) |
809 |
+ |
return false; |
810 |
+ |
if (q == pred) // restart |
811 |
+ |
break; |
812 |
+ |
Object x = q.get(); |
813 |
+ |
if (x != null && x != q && o.equals(x) && |
814 |
+ |
q.compareAndSet(x, q)) { |
815 |
+ |
clean(pred, q); |
816 |
+ |
return true; |
817 |
+ |
} |
818 |
+ |
pred = q; |
819 |
+ |
} |
820 |
+ |
} |
821 |
+ |
} |
822 |
+ |
|
823 |
|
/** |
824 |
|
* Save the state to a stream (that is, serialize it). |
825 |
|
* |
826 |
< |
* @serialData All of the elements (each an <tt>E</tt>) in |
826 |
> |
* @serialData All of the elements (each an {@code E}) in |
827 |
|
* the proper order, followed by a null |
828 |
|
* @param s the stream |
829 |
|
*/ |
830 |
|
private void writeObject(java.io.ObjectOutputStream s) |
831 |
|
throws java.io.IOException { |
832 |
|
s.defaultWriteObject(); |
833 |
< |
for (Iterator<E> it = iterator(); it.hasNext(); ) |
834 |
< |
s.writeObject(it.next()); |
833 |
> |
for (E e : this) |
834 |
> |
s.writeObject(e); |
835 |
|
// Use trailing null as sentinel |
836 |
|
s.writeObject(null); |
837 |
|
} |
839 |
|
/** |
840 |
|
* Reconstitute the Queue instance from a stream (that is, |
841 |
|
* deserialize it). |
842 |
+ |
* |
843 |
|
* @param s the stream |
844 |
|
*/ |
845 |
|
private void readObject(java.io.ObjectInputStream s) |
847 |
|
s.defaultReadObject(); |
848 |
|
resetHeadAndTail(); |
849 |
|
for (;;) { |
850 |
< |
E item = (E)s.readObject(); |
850 |
> |
@SuppressWarnings("unchecked") E item = (E) s.readObject(); |
851 |
|
if (item == null) |
852 |
|
break; |
853 |
|
else |
855 |
|
} |
856 |
|
} |
857 |
|
|
731 |
– |
|
858 |
|
// Support for resetting head/tail while deserializing |
859 |
+ |
private void resetHeadAndTail() { |
860 |
+ |
Node<E> dummy = new Node<E>(null, false); |
861 |
+ |
UNSAFE.putObjectVolatile(this, headOffset, |
862 |
+ |
new PaddedAtomicReference<Node<E>>(dummy)); |
863 |
+ |
UNSAFE.putObjectVolatile(this, tailOffset, |
864 |
+ |
new PaddedAtomicReference<Node<E>>(dummy)); |
865 |
+ |
UNSAFE.putObjectVolatile(this, cleanMeOffset, |
866 |
+ |
new PaddedAtomicReference<Node<E>>(null)); |
867 |
+ |
} |
868 |
+ |
|
869 |
+ |
// Unsafe mechanics |
870 |
+ |
|
871 |
+ |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
872 |
+ |
private static final long headOffset = |
873 |
+ |
objectFieldOffset(UNSAFE, "head", LinkedTransferQueue.class); |
874 |
+ |
private static final long tailOffset = |
875 |
+ |
objectFieldOffset(UNSAFE, "tail", LinkedTransferQueue.class); |
876 |
+ |
private static final long cleanMeOffset = |
877 |
+ |
objectFieldOffset(UNSAFE, "cleanMe", LinkedTransferQueue.class); |
878 |
+ |
|
879 |
|
|
880 |
< |
// Temporary Unsafe mechanics for preliminary release |
881 |
< |
private static final Unsafe _unsafe; |
736 |
< |
private static final long headOffset; |
737 |
< |
private static final long tailOffset; |
738 |
< |
private static final long cleanMeOffset; |
739 |
< |
static { |
880 |
> |
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, |
881 |
> |
String field, Class<?> klazz) { |
882 |
|
try { |
883 |
< |
if (LinkedTransferQueue.class.getClassLoader() != null) { |
884 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
885 |
< |
f.setAccessible(true); |
886 |
< |
_unsafe = (Unsafe)f.get(null); |
887 |
< |
} |
888 |
< |
else |
747 |
< |
_unsafe = Unsafe.getUnsafe(); |
748 |
< |
headOffset = _unsafe.objectFieldOffset |
749 |
< |
(LinkedTransferQueue.class.getDeclaredField("head")); |
750 |
< |
tailOffset = _unsafe.objectFieldOffset |
751 |
< |
(LinkedTransferQueue.class.getDeclaredField("tail")); |
752 |
< |
cleanMeOffset = _unsafe.objectFieldOffset |
753 |
< |
(LinkedTransferQueue.class.getDeclaredField("cleanMe")); |
754 |
< |
} catch (Exception e) { |
755 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
883 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
884 |
> |
} catch (NoSuchFieldException e) { |
885 |
> |
// Convert Exception to corresponding Error |
886 |
> |
NoSuchFieldError error = new NoSuchFieldError(field); |
887 |
> |
error.initCause(e); |
888 |
> |
throw error; |
889 |
|
} |
890 |
|
} |
891 |
|
|
892 |
< |
private void resetHeadAndTail() { |
893 |
< |
QNode dummy = new QNode(null, false); |
894 |
< |
_unsafe.putObjectVolatile(this, headOffset, |
895 |
< |
new PaddedAtomicReference<QNode>(dummy)); |
896 |
< |
_unsafe.putObjectVolatile(this, tailOffset, |
897 |
< |
new PaddedAtomicReference<QNode>(dummy)); |
898 |
< |
_unsafe.putObjectVolatile(this, cleanMeOffset, |
899 |
< |
new PaddedAtomicReference<QNode>(null)); |
900 |
< |
|
892 |
> |
/** |
893 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
894 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
895 |
> |
* into a jdk. |
896 |
> |
* |
897 |
> |
* @return a sun.misc.Unsafe |
898 |
> |
*/ |
899 |
> |
private static sun.misc.Unsafe getUnsafe() { |
900 |
> |
try { |
901 |
> |
return sun.misc.Unsafe.getUnsafe(); |
902 |
> |
} catch (SecurityException se) { |
903 |
> |
try { |
904 |
> |
return java.security.AccessController.doPrivileged |
905 |
> |
(new java.security |
906 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
907 |
> |
public sun.misc.Unsafe run() throws Exception { |
908 |
> |
java.lang.reflect.Field f = sun.misc |
909 |
> |
.Unsafe.class.getDeclaredField("theUnsafe"); |
910 |
> |
f.setAccessible(true); |
911 |
> |
return (sun.misc.Unsafe) f.get(null); |
912 |
> |
}}); |
913 |
> |
} catch (java.security.PrivilegedActionException e) { |
914 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
915 |
> |
e.getCause()); |
916 |
> |
} |
917 |
> |
} |
918 |
|
} |
769 |
– |
|
919 |
|
} |