8 |
|
|
9 |
|
import java.util.AbstractQueue; |
10 |
|
import java.util.Collection; |
11 |
– |
import java.util.ConcurrentModificationException; |
11 |
|
import java.util.Iterator; |
12 |
|
import java.util.NoSuchElementException; |
13 |
|
import java.util.Queue; |
22 |
|
* producer. The <em>tail</em> of the queue is that element that has |
23 |
|
* been on the queue the shortest time for some producer. |
24 |
|
* |
25 |
< |
* <p>Beware that, unlike in most collections, the {@code size} |
26 |
< |
* method is <em>NOT</em> a constant-time operation. Because of the |
25 |
> |
* <p>Beware that, unlike in most collections, the {@code size} method |
26 |
> |
* is <em>NOT</em> a constant-time operation. Because of the |
27 |
|
* asynchronous nature of these queues, determining the current number |
28 |
< |
* of elements requires a traversal of the elements. |
28 |
> |
* of elements requires a traversal of the elements, and so may report |
29 |
> |
* inaccurate results if this collection is modified during traversal. |
30 |
> |
* Additionally, the bulk operations {@code addAll}, |
31 |
> |
* {@code removeAll}, {@code retainAll}, {@code containsAll}, |
32 |
> |
* {@code equals}, and {@code toArray} are <em>not</em> guaranteed |
33 |
> |
* to be performed atomically. For example, an iterator operating |
34 |
> |
* concurrently with an {@code addAll} operation might view only some |
35 |
> |
* of the added elements. |
36 |
|
* |
37 |
|
* <p>This class and its iterator implement all of the |
38 |
|
* <em>optional</em> methods of the {@link Collection} and {@link |
503 |
|
return false; |
504 |
|
} |
505 |
|
|
500 |
– |
// Unsafe mechanics |
501 |
– |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
502 |
– |
private static final long nextOffset = |
503 |
– |
objectFieldOffset(UNSAFE, "next", Node.class); |
504 |
– |
private static final long itemOffset = |
505 |
– |
objectFieldOffset(UNSAFE, "item", Node.class); |
506 |
– |
private static final long waiterOffset = |
507 |
– |
objectFieldOffset(UNSAFE, "waiter", Node.class); |
508 |
– |
|
506 |
|
private static final long serialVersionUID = -3375979862319811754L; |
507 |
+ |
|
508 |
+ |
// Unsafe mechanics |
509 |
+ |
private static final sun.misc.Unsafe UNSAFE; |
510 |
+ |
private static final long itemOffset; |
511 |
+ |
private static final long nextOffset; |
512 |
+ |
private static final long waiterOffset; |
513 |
+ |
static { |
514 |
+ |
try { |
515 |
+ |
UNSAFE = getUnsafe(); |
516 |
+ |
Class k = Node.class; |
517 |
+ |
itemOffset = UNSAFE.objectFieldOffset |
518 |
+ |
(k.getDeclaredField("item")); |
519 |
+ |
nextOffset = UNSAFE.objectFieldOffset |
520 |
+ |
(k.getDeclaredField("next")); |
521 |
+ |
waiterOffset = UNSAFE.objectFieldOffset |
522 |
+ |
(k.getDeclaredField("waiter")); |
523 |
+ |
} catch (Exception e) { |
524 |
+ |
throw new Error(e); |
525 |
+ |
} |
526 |
+ |
} |
527 |
|
} |
528 |
|
|
529 |
|
/** head of the queue; null until first enqueue */ |
823 |
|
} |
824 |
|
|
825 |
|
this.lastRet = prev; |
826 |
+ |
|
827 |
|
for (Node p = prev, s, n;;) { |
828 |
|
s = (p == null) ? head : p.next; |
829 |
|
if (s == null) |
1160 |
|
} |
1161 |
|
|
1162 |
|
/** |
1163 |
< |
* Returns an iterator over the elements in this queue in proper |
1164 |
< |
* sequence, from head to tail. |
1163 |
> |
* Returns an iterator over the elements in this queue in proper sequence. |
1164 |
> |
* The elements will be returned in order from first (head) to last (tail). |
1165 |
|
* |
1166 |
|
* <p>The returned iterator is a "weakly consistent" iterator that |
1167 |
< |
* will never throw |
1168 |
< |
* {@link ConcurrentModificationException ConcurrentModificationException}, |
1169 |
< |
* and guarantees to traverse elements as they existed upon |
1170 |
< |
* construction of the iterator, and may (but is not guaranteed |
1171 |
< |
* to) reflect any modifications subsequent to construction. |
1167 |
> |
* will never throw {@link java.util.ConcurrentModificationException |
1168 |
> |
* ConcurrentModificationException}, and guarantees to traverse |
1169 |
> |
* elements as they existed upon construction of the iterator, and |
1170 |
> |
* may (but is not guaranteed to) reflect any modifications |
1171 |
> |
* subsequent to construction. |
1172 |
|
* |
1173 |
|
* @return an iterator over the elements in this queue in proper sequence |
1174 |
|
*/ |
1301 |
|
|
1302 |
|
// Unsafe mechanics |
1303 |
|
|
1304 |
< |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1305 |
< |
private static final long headOffset = |
1306 |
< |
objectFieldOffset(UNSAFE, "head", LinkedTransferQueue.class); |
1307 |
< |
private static final long tailOffset = |
1308 |
< |
objectFieldOffset(UNSAFE, "tail", LinkedTransferQueue.class); |
1291 |
< |
private static final long sweepVotesOffset = |
1292 |
< |
objectFieldOffset(UNSAFE, "sweepVotes", LinkedTransferQueue.class); |
1293 |
< |
|
1294 |
< |
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, |
1295 |
< |
String field, Class<?> klazz) { |
1304 |
> |
private static final sun.misc.Unsafe UNSAFE; |
1305 |
> |
private static final long headOffset; |
1306 |
> |
private static final long tailOffset; |
1307 |
> |
private static final long sweepVotesOffset; |
1308 |
> |
static { |
1309 |
|
try { |
1310 |
< |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
1311 |
< |
} catch (NoSuchFieldException e) { |
1312 |
< |
// Convert Exception to corresponding Error |
1313 |
< |
NoSuchFieldError error = new NoSuchFieldError(field); |
1314 |
< |
error.initCause(e); |
1315 |
< |
throw error; |
1310 |
> |
UNSAFE = getUnsafe(); |
1311 |
> |
Class k = LinkedTransferQueue.class; |
1312 |
> |
headOffset = UNSAFE.objectFieldOffset |
1313 |
> |
(k.getDeclaredField("head")); |
1314 |
> |
tailOffset = UNSAFE.objectFieldOffset |
1315 |
> |
(k.getDeclaredField("tail")); |
1316 |
> |
sweepVotesOffset = UNSAFE.objectFieldOffset |
1317 |
> |
(k.getDeclaredField("sweepVotes")); |
1318 |
> |
} catch (Exception e) { |
1319 |
> |
throw new Error(e); |
1320 |
|
} |
1321 |
|
} |
1322 |
|
|