ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/LinkedTransferQueue.java
Revision: 1.3
Committed: Sun Jul 26 17:48:58 2009 UTC (14 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +13 -14 lines
Log Message:
sync with jsr166y package

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 */
6
7 package java.util.concurrent;
8
9 import java.util.AbstractQueue;
10 import java.util.Collection;
11 import java.util.Iterator;
12 import java.util.NoSuchElementException;
13 import java.util.concurrent.locks.LockSupport;
14 import java.util.concurrent.atomic.AtomicReference;
15 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
16
17 /**
18 * An unbounded {@linkplain TransferQueue} based on linked nodes.
19 * This queue orders elements FIFO (first-in-first-out) with respect
20 * to any given producer. The <em>head</em> of the queue is that
21 * element that has been on the queue the longest time for some
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
27 * asynchronous nature of these queues, determining the current number
28 * of elements requires a traversal of the elements.
29 *
30 * <p>This class and its iterator implement all of the
31 * <em>optional</em> methods of the {@link Collection} and {@link
32 * Iterator} interfaces.
33 *
34 * <p>Memory consistency effects: As with other concurrent
35 * collections, actions in a thread prior to placing an object into a
36 * {@code LinkedTransferQueue}
37 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
38 * actions subsequent to the access or removal of that element from
39 * the {@code LinkedTransferQueue} in another thread.
40 *
41 * <p>This class is a member of the
42 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
43 * Java Collections Framework</a>.
44 *
45 * @since 1.7
46 * @author Doug Lea
47 * @param <E> the type of elements held in this collection
48 */
49 public class LinkedTransferQueue<E> extends AbstractQueue<E>
50 implements TransferQueue<E>, java.io.Serializable {
51 private static final long serialVersionUID = -3223113410248163686L;
52
53 /*
54 * This class extends the approach used in FIFO-mode
55 * SynchronousQueues. See the internal documentation, as well as
56 * the PPoPP 2006 paper "Scalable Synchronous Queues" by Scherer,
57 * Lea & Scott
58 * (http://www.cs.rice.edu/~wns1/papers/2006-PPoPP-SQ.pdf)
59 *
60 * The main extension is to provide different Wait modes for the
61 * main "xfer" method that puts or takes items. These don't
62 * impact the basic dual-queue logic, but instead control whether
63 * or how threads block upon insertion of request or data nodes
64 * into the dual queue. It also uses slightly different
65 * conventions for tracking whether nodes are off-list or
66 * cancelled.
67 */
68
69 // Wait modes for xfer method
70 static final int NOWAIT = 0;
71 static final int TIMEOUT = 1;
72 static final int WAIT = 2;
73
74 /** The number of CPUs, for spin control */
75 static final int NCPUS = Runtime.getRuntime().availableProcessors();
76
77 /**
78 * The number of times to spin before blocking in timed waits.
79 * The value is empirically derived -- it works well across a
80 * variety of processors and OSes. Empirically, the best value
81 * seems not to vary with number of CPUs (beyond 2) so is just
82 * a constant.
83 */
84 static final int maxTimedSpins = (NCPUS < 2) ? 0 : 32;
85
86 /**
87 * The number of times to spin before blocking in untimed waits.
88 * This is greater than timed value because untimed waits spin
89 * faster since they don't need to check times on each spin.
90 */
91 static final int maxUntimedSpins = maxTimedSpins * 16;
92
93 /**
94 * The number of nanoseconds for which it is faster to spin
95 * rather than to use timed park. A rough estimate suffices.
96 */
97 static final long spinForTimeoutThreshold = 1000L;
98
99 /**
100 * Node class for LinkedTransferQueue. Opportunistically
101 * subclasses from AtomicReference to represent item. Uses Object,
102 * not E, to allow setting item to "this" after use, to avoid
103 * garbage retention. Similarly, setting the next field to this is
104 * used as sentinel that node is off list.
105 */
106 static final class Node<E> extends AtomicReference<Object> {
107 volatile Node<E> next;
108 volatile Thread waiter; // to control park/unpark
109 final boolean isData;
110
111 Node(E item, boolean isData) {
112 super(item);
113 this.isData = isData;
114 }
115
116 @SuppressWarnings("rawtypes")
117 static final AtomicReferenceFieldUpdater<Node, Node>
118 nextUpdater = AtomicReferenceFieldUpdater.newUpdater
119 (Node.class, Node.class, "next");
120
121 final boolean casNext(Node<E> cmp, Node<E> val) {
122 return nextUpdater.compareAndSet(this, cmp, val);
123 }
124
125 final void clearNext() {
126 nextUpdater.lazySet(this, this);
127 }
128
129 private static final long serialVersionUID = -3375979862319811754L;
130 }
131
132 /**
133 * Padded version of AtomicReference used for head, tail and
134 * cleanMe, to alleviate contention across threads CASing one vs
135 * the other.
136 */
137 static final class PaddedAtomicReference<T> extends AtomicReference<T> {
138 // enough padding for 64bytes with 4byte refs
139 Object p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pa, pb, pc, pd, pe;
140 PaddedAtomicReference(T r) { super(r); }
141 private static final long serialVersionUID = 8170090609809740854L;
142 }
143
144
145 /** head of the queue */
146 private transient final PaddedAtomicReference<Node<E>> head;
147
148 /** tail of the queue */
149 private transient final PaddedAtomicReference<Node<E>> tail;
150
151 /**
152 * Reference to a cancelled node that might not yet have been
153 * unlinked from queue because it was the last inserted node
154 * when it cancelled.
155 */
156 private transient final PaddedAtomicReference<Node<E>> cleanMe;
157
158 /**
159 * Tries to cas nh as new head; if successful, unlink
160 * old head's next node to avoid garbage retention.
161 */
162 private boolean advanceHead(Node<E> h, Node<E> nh) {
163 if (h == head.get() && head.compareAndSet(h, nh)) {
164 h.clearNext(); // forget old next
165 return true;
166 }
167 return false;
168 }
169
170 /**
171 * Puts or takes an item. Used for most queue operations (except
172 * poll() and tryTransfer()). See the similar code in
173 * SynchronousQueue for detailed explanation.
174 *
175 * @param e the item or if null, signifies that this is a take
176 * @param mode the wait mode: NOWAIT, TIMEOUT, WAIT
177 * @param nanos timeout in nanosecs, used only if mode is TIMEOUT
178 * @return an item, or null on failure
179 */
180 private E xfer(E e, int mode, long nanos) {
181 boolean isData = (e != null);
182 Node<E> s = null;
183 final PaddedAtomicReference<Node<E>> head = this.head;
184 final PaddedAtomicReference<Node<E>> tail = this.tail;
185
186 for (;;) {
187 Node<E> t = tail.get();
188 Node<E> h = head.get();
189
190 if (t != null && (t == h || t.isData == isData)) {
191 if (s == null)
192 s = new Node<E>(e, isData);
193 Node<E> last = t.next;
194 if (last != null) {
195 if (t == tail.get())
196 tail.compareAndSet(t, last);
197 }
198 else if (t.casNext(null, s)) {
199 tail.compareAndSet(t, s);
200 return awaitFulfill(t, s, e, mode, nanos);
201 }
202 }
203
204 else if (h != null) {
205 Node<E> first = h.next;
206 if (t == tail.get() && first != null &&
207 advanceHead(h, first)) {
208 Object x = first.get();
209 if (x != first && first.compareAndSet(x, e)) {
210 LockSupport.unpark(first.waiter);
211 return isData ? e : (E) x;
212 }
213 }
214 }
215 }
216 }
217
218
219 /**
220 * Version of xfer for poll() and tryTransfer, which
221 * simplifies control paths both here and in xfer.
222 */
223 private E fulfill(E e) {
224 boolean isData = (e != null);
225 final PaddedAtomicReference<Node<E>> head = this.head;
226 final PaddedAtomicReference<Node<E>> tail = this.tail;
227
228 for (;;) {
229 Node<E> t = tail.get();
230 Node<E> h = head.get();
231
232 if (t != null && (t == h || t.isData == isData)) {
233 Node<E> last = t.next;
234 if (t == tail.get()) {
235 if (last != null)
236 tail.compareAndSet(t, last);
237 else
238 return null;
239 }
240 }
241 else if (h != null) {
242 Node<E> first = h.next;
243 if (t == tail.get() &&
244 first != null &&
245 advanceHead(h, first)) {
246 Object x = first.get();
247 if (x != first && first.compareAndSet(x, e)) {
248 LockSupport.unpark(first.waiter);
249 return isData ? e : (E) x;
250 }
251 }
252 }
253 }
254 }
255
256 /**
257 * Spins/blocks until node s is fulfilled or caller gives up,
258 * depending on wait mode.
259 *
260 * @param pred the predecessor of waiting node
261 * @param s the waiting node
262 * @param e the comparison value for checking match
263 * @param mode mode
264 * @param nanos timeout value
265 * @return matched item, or s if cancelled
266 */
267 private E awaitFulfill(Node<E> pred, Node<E> s, E e,
268 int mode, long nanos) {
269 if (mode == NOWAIT)
270 return null;
271
272 long lastTime = (mode == TIMEOUT) ? System.nanoTime() : 0;
273 Thread w = Thread.currentThread();
274 int spins = -1; // set to desired spin count below
275 for (;;) {
276 if (w.isInterrupted())
277 s.compareAndSet(e, s);
278 Object x = s.get();
279 if (x != e) { // Node was matched or cancelled
280 advanceHead(pred, s); // unlink if head
281 if (x == s) { // was cancelled
282 clean(pred, s);
283 return null;
284 }
285 else if (x != null) {
286 s.set(s); // avoid garbage retention
287 return (E) x;
288 }
289 else
290 return e;
291 }
292 if (mode == TIMEOUT) {
293 long now = System.nanoTime();
294 nanos -= now - lastTime;
295 lastTime = now;
296 if (nanos <= 0) {
297 s.compareAndSet(e, s); // try to cancel
298 continue;
299 }
300 }
301 if (spins < 0) {
302 Node<E> h = head.get(); // only spin if at head
303 spins = ((h != null && h.next == s) ?
304 ((mode == TIMEOUT) ?
305 maxTimedSpins : maxUntimedSpins) : 0);
306 }
307 if (spins > 0)
308 --spins;
309 else if (s.waiter == null)
310 s.waiter = w;
311 else if (mode != TIMEOUT) {
312 LockSupport.park(this);
313 s.waiter = null;
314 spins = -1;
315 }
316 else if (nanos > spinForTimeoutThreshold) {
317 LockSupport.parkNanos(this, nanos);
318 s.waiter = null;
319 spins = -1;
320 }
321 }
322 }
323
324 /**
325 * Returns validated tail for use in cleaning methods.
326 */
327 private Node<E> getValidatedTail() {
328 for (;;) {
329 Node<E> h = head.get();
330 Node<E> first = h.next;
331 if (first != null && first.next == first) { // help advance
332 advanceHead(h, first);
333 continue;
334 }
335 Node<E> t = tail.get();
336 Node<E> last = t.next;
337 if (t == tail.get()) {
338 if (last != null)
339 tail.compareAndSet(t, last); // help advance
340 else
341 return t;
342 }
343 }
344 }
345
346 /**
347 * Gets rid of cancelled node s with original predecessor pred.
348 *
349 * @param pred predecessor of cancelled node
350 * @param s the cancelled node
351 */
352 private void clean(Node<E> pred, Node<E> s) {
353 Thread w = s.waiter;
354 if (w != null) { // Wake up thread
355 s.waiter = null;
356 if (w != Thread.currentThread())
357 LockSupport.unpark(w);
358 }
359
360 if (pred == null)
361 return;
362
363 /*
364 * At any given time, exactly one node on list cannot be
365 * deleted -- the last inserted node. To accommodate this, if
366 * we cannot delete s, we save its predecessor as "cleanMe",
367 * processing the previously saved version first. At least one
368 * of node s or the node previously saved can always be
369 * processed, so this always terminates.
370 */
371 while (pred.next == s) {
372 Node<E> oldpred = reclean(); // First, help get rid of cleanMe
373 Node<E> t = getValidatedTail();
374 if (s != t) { // If not tail, try to unsplice
375 Node<E> sn = s.next; // s.next == s means s already off list
376 if (sn == s || pred.casNext(s, sn))
377 break;
378 }
379 else if (oldpred == pred || // Already saved
380 (oldpred == null && cleanMe.compareAndSet(null, pred)))
381 break; // Postpone cleaning
382 }
383 }
384
385 /**
386 * Tries to unsplice the cancelled node held in cleanMe that was
387 * previously uncleanable because it was at tail.
388 *
389 * @return current cleanMe node (or null)
390 */
391 private Node<E> reclean() {
392 /*
393 * cleanMe is, or at one time was, predecessor of cancelled
394 * node s that was the tail so could not be unspliced. If s
395 * is no longer the tail, try to unsplice if necessary and
396 * make cleanMe slot available. This differs from similar
397 * code in clean() because we must check that pred still
398 * points to a cancelled node that must be unspliced -- if
399 * not, we can (must) clear cleanMe without unsplicing.
400 * This can loop only due to contention on casNext or
401 * clearing cleanMe.
402 */
403 Node<E> pred;
404 while ((pred = cleanMe.get()) != null) {
405 Node<E> t = getValidatedTail();
406 Node<E> s = pred.next;
407 if (s != t) {
408 Node<E> sn;
409 if (s == null || s == pred || s.get() != s ||
410 (sn = s.next) == s || pred.casNext(s, sn))
411 cleanMe.compareAndSet(pred, null);
412 }
413 else // s is still tail; cannot clean
414 break;
415 }
416 return pred;
417 }
418
419 /**
420 * Creates an initially empty {@code LinkedTransferQueue}.
421 */
422 public LinkedTransferQueue() {
423 Node<E> dummy = new Node<E>(null, false);
424 head = new PaddedAtomicReference<Node<E>>(dummy);
425 tail = new PaddedAtomicReference<Node<E>>(dummy);
426 cleanMe = new PaddedAtomicReference<Node<E>>(null);
427 }
428
429 /**
430 * Creates a {@code LinkedTransferQueue}
431 * initially containing the elements of the given collection,
432 * added in traversal order of the collection's iterator.
433 *
434 * @param c the collection of elements to initially contain
435 * @throws NullPointerException if the specified collection or any
436 * of its elements are null
437 */
438 public LinkedTransferQueue(Collection<? extends E> c) {
439 this();
440 addAll(c);
441 }
442
443 public void put(E e) throws InterruptedException {
444 if (e == null) throw new NullPointerException();
445 if (Thread.interrupted()) throw new InterruptedException();
446 xfer(e, NOWAIT, 0);
447 }
448
449 public boolean offer(E e, long timeout, TimeUnit unit)
450 throws InterruptedException {
451 if (e == null) throw new NullPointerException();
452 if (Thread.interrupted()) throw new InterruptedException();
453 xfer(e, NOWAIT, 0);
454 return true;
455 }
456
457 public boolean offer(E e) {
458 if (e == null) throw new NullPointerException();
459 xfer(e, NOWAIT, 0);
460 return true;
461 }
462
463 public boolean add(E e) {
464 if (e == null) throw new NullPointerException();
465 xfer(e, NOWAIT, 0);
466 return true;
467 }
468
469 public void transfer(E e) throws InterruptedException {
470 if (e == null) throw new NullPointerException();
471 if (xfer(e, WAIT, 0) == null) {
472 Thread.interrupted();
473 throw new InterruptedException();
474 }
475 }
476
477 public boolean tryTransfer(E e, long timeout, TimeUnit unit)
478 throws InterruptedException {
479 if (e == null) throw new NullPointerException();
480 if (xfer(e, TIMEOUT, unit.toNanos(timeout)) != null)
481 return true;
482 if (!Thread.interrupted())
483 return false;
484 throw new InterruptedException();
485 }
486
487 public boolean tryTransfer(E e) {
488 if (e == null) throw new NullPointerException();
489 return fulfill(e) != null;
490 }
491
492 public E take() throws InterruptedException {
493 Object e = xfer(null, WAIT, 0);
494 if (e != null)
495 return (E) e;
496 Thread.interrupted();
497 throw new InterruptedException();
498 }
499
500 public E poll(long timeout, TimeUnit unit) throws InterruptedException {
501 Object e = xfer(null, TIMEOUT, unit.toNanos(timeout));
502 if (e != null || !Thread.interrupted())
503 return (E) e;
504 throw new InterruptedException();
505 }
506
507 public E poll() {
508 return fulfill(null);
509 }
510
511 public int drainTo(Collection<? super E> c) {
512 if (c == null)
513 throw new NullPointerException();
514 if (c == this)
515 throw new IllegalArgumentException();
516 int n = 0;
517 E e;
518 while ( (e = poll()) != null) {
519 c.add(e);
520 ++n;
521 }
522 return n;
523 }
524
525 public int drainTo(Collection<? super E> c, int maxElements) {
526 if (c == null)
527 throw new NullPointerException();
528 if (c == this)
529 throw new IllegalArgumentException();
530 int n = 0;
531 E e;
532 while (n < maxElements && (e = poll()) != null) {
533 c.add(e);
534 ++n;
535 }
536 return n;
537 }
538
539 // Traversal-based methods
540
541 /**
542 * Returns head after performing any outstanding helping steps.
543 */
544 private Node<E> traversalHead() {
545 for (;;) {
546 Node<E> t = tail.get();
547 Node<E> h = head.get();
548 if (h != null && t != null) {
549 Node<E> last = t.next;
550 Node<E> first = h.next;
551 if (t == tail.get()) {
552 if (last != null)
553 tail.compareAndSet(t, last);
554 else if (first != null) {
555 Object x = first.get();
556 if (x == first)
557 advanceHead(h, first);
558 else
559 return h;
560 }
561 else
562 return h;
563 }
564 }
565 reclean();
566 }
567 }
568
569
570 public Iterator<E> iterator() {
571 return new Itr();
572 }
573
574 /**
575 * Iterators. Basic strategy is to traverse list, treating
576 * non-data (i.e., request) nodes as terminating list.
577 * Once a valid data node is found, the item is cached
578 * so that the next call to next() will return it even
579 * if subsequently removed.
580 */
581 class Itr implements Iterator<E> {
582 Node<E> next; // node to return next
583 Node<E> pnext; // predecessor of next
584 Node<E> snext; // successor of next
585 Node<E> curr; // last returned node, for remove()
586 Node<E> pcurr; // predecessor of curr, for remove()
587 E nextItem; // Cache of next item, once committed to in next
588
589 Itr() {
590 findNext();
591 }
592
593 /**
594 * Ensures next points to next valid node, or null if none.
595 */
596 void findNext() {
597 for (;;) {
598 Node<E> pred = pnext;
599 Node<E> q = next;
600 if (pred == null || pred == q) {
601 pred = traversalHead();
602 q = pred.next;
603 }
604 if (q == null || !q.isData) {
605 next = null;
606 return;
607 }
608 Object x = q.get();
609 Node<E> s = q.next;
610 if (x != null && q != x && q != s) {
611 nextItem = (E) x;
612 snext = s;
613 pnext = pred;
614 next = q;
615 return;
616 }
617 pnext = q;
618 next = s;
619 }
620 }
621
622 public boolean hasNext() {
623 return next != null;
624 }
625
626 public E next() {
627 if (next == null) throw new NoSuchElementException();
628 pcurr = pnext;
629 curr = next;
630 pnext = next;
631 next = snext;
632 E x = nextItem;
633 findNext();
634 return x;
635 }
636
637 public void remove() {
638 Node<E> p = curr;
639 if (p == null)
640 throw new IllegalStateException();
641 Object x = p.get();
642 if (x != null && x != p && p.compareAndSet(x, p))
643 clean(pcurr, p);
644 }
645 }
646
647 public E peek() {
648 for (;;) {
649 Node<E> h = traversalHead();
650 Node<E> p = h.next;
651 if (p == null)
652 return null;
653 Object x = p.get();
654 if (p != x) {
655 if (!p.isData)
656 return null;
657 if (x != null)
658 return (E) x;
659 }
660 }
661 }
662
663 public boolean isEmpty() {
664 for (;;) {
665 Node<E> h = traversalHead();
666 Node<E> p = h.next;
667 if (p == null)
668 return true;
669 Object x = p.get();
670 if (p != x) {
671 if (!p.isData)
672 return true;
673 if (x != null)
674 return false;
675 }
676 }
677 }
678
679 public boolean hasWaitingConsumer() {
680 for (;;) {
681 Node<E> h = traversalHead();
682 Node<E> p = h.next;
683 if (p == null)
684 return false;
685 Object x = p.get();
686 if (p != x)
687 return !p.isData;
688 }
689 }
690
691 /**
692 * Returns the number of elements in this queue. If this queue
693 * contains more than {@code Integer.MAX_VALUE} elements, returns
694 * {@code Integer.MAX_VALUE}.
695 *
696 * <p>Beware that, unlike in most collections, this method is
697 * <em>NOT</em> a constant-time operation. Because of the
698 * asynchronous nature of these queues, determining the current
699 * number of elements requires an O(n) traversal.
700 *
701 * @return the number of elements in this queue
702 */
703 public int size() {
704 int count = 0;
705 Node<E> h = traversalHead();
706 for (Node<E> p = h.next; p != null && p.isData; p = p.next) {
707 Object x = p.get();
708 if (x != null && x != p) {
709 if (++count == Integer.MAX_VALUE) // saturated
710 break;
711 }
712 }
713 return count;
714 }
715
716 public int getWaitingConsumerCount() {
717 int count = 0;
718 Node<E> h = traversalHead();
719 for (Node<E> p = h.next; p != null && !p.isData; p = p.next) {
720 if (p.get() == null) {
721 if (++count == Integer.MAX_VALUE)
722 break;
723 }
724 }
725 return count;
726 }
727
728 public int remainingCapacity() {
729 return Integer.MAX_VALUE;
730 }
731
732 public boolean remove(Object o) {
733 if (o == null)
734 return false;
735 for (;;) {
736 Node<E> pred = traversalHead();
737 for (;;) {
738 Node<E> q = pred.next;
739 if (q == null || !q.isData)
740 return false;
741 if (q == pred) // restart
742 break;
743 Object x = q.get();
744 if (x != null && x != q && o.equals(x) &&
745 q.compareAndSet(x, q)) {
746 clean(pred, q);
747 return true;
748 }
749 pred = q;
750 }
751 }
752 }
753
754 /**
755 * Save the state to a stream (that is, serialize it).
756 *
757 * @serialData All of the elements (each an {@code E}) in
758 * the proper order, followed by a null
759 * @param s the stream
760 */
761 private void writeObject(java.io.ObjectOutputStream s)
762 throws java.io.IOException {
763 s.defaultWriteObject();
764 for (E e : this)
765 s.writeObject(e);
766 // Use trailing null as sentinel
767 s.writeObject(null);
768 }
769
770 /**
771 * Reconstitute the Queue instance from a stream (that is,
772 * deserialize it).
773 *
774 * @param s the stream
775 */
776 private void readObject(java.io.ObjectInputStream s)
777 throws java.io.IOException, ClassNotFoundException {
778 s.defaultReadObject();
779 resetHeadAndTail();
780 for (;;) {
781 @SuppressWarnings("unchecked") E item = (E) s.readObject();
782 if (item == null)
783 break;
784 else
785 offer(item);
786 }
787 }
788
789 // Support for resetting head/tail while deserializing
790 private void resetHeadAndTail() {
791 Node<E> dummy = new Node<E>(null, false);
792 UNSAFE.putObjectVolatile(this, headOffset,
793 new PaddedAtomicReference<Node<E>>(dummy));
794 UNSAFE.putObjectVolatile(this, tailOffset,
795 new PaddedAtomicReference<Node<E>>(dummy));
796 UNSAFE.putObjectVolatile(this, cleanMeOffset,
797 new PaddedAtomicReference<Node<E>>(null));
798 }
799
800 // Unsafe mechanics
801
802 private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe();
803 private static final long headOffset =
804 objectFieldOffset("head", LinkedTransferQueue.class);
805 private static final long tailOffset =
806 objectFieldOffset("tail", LinkedTransferQueue.class);
807 private static final long cleanMeOffset =
808 objectFieldOffset("cleanMe", LinkedTransferQueue.class);
809
810 private static long objectFieldOffset(String field, Class<?> klazz) {
811 try {
812 return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
813 } catch (NoSuchFieldException e) {
814 // Convert Exception to corresponding Error
815 NoSuchFieldError error = new NoSuchFieldError(field);
816 error.initCause(e);
817 throw error;
818 }
819 }
820 }