ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/LinkedTransferQueue.java
Revision: 1.8
Committed: Tue Oct 27 23:21:32 2009 UTC (14 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +778 -483 lines
Log Message:
sync with jsr166y package

File Contents

# User Rev Content
1 jsr166 1.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 jsr166 1.5 import java.util.ConcurrentModificationException;
12 jsr166 1.1 import java.util.Iterator;
13     import java.util.NoSuchElementException;
14 jsr166 1.5 import java.util.Queue;
15 jsr166 1.1 import java.util.concurrent.locks.LockSupport;
16     /**
17 jsr166 1.6 * An unbounded {@link TransferQueue} based on linked nodes.
18 jsr166 1.1 * This queue orders elements FIFO (first-in-first-out) with respect
19     * to any given producer. The <em>head</em> of the queue is that
20     * element that has been on the queue the longest time for some
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 {@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.
28     *
29     * <p>This class and its iterator implement all of the
30     * <em>optional</em> methods of the {@link Collection} and {@link
31     * Iterator} interfaces.
32     *
33     * <p>Memory consistency effects: As with other concurrent
34     * collections, actions in a thread prior to placing an object into a
35     * {@code LinkedTransferQueue}
36     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
37     * actions subsequent to the access or removal of that element from
38     * the {@code LinkedTransferQueue} in another thread.
39     *
40     * <p>This class is a member of the
41     * <a href="{@docRoot}/../technotes/guides/collections/index.html">
42     * Java Collections Framework</a>.
43     *
44     * @since 1.7
45     * @author Doug Lea
46     * @param <E> the type of elements held in this collection
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     /*
53 jsr166 1.8 * *** Overview of Dual Queues with Slack ***
54 jsr166 1.1 *
55 jsr166 1.8 * Dual Queues, introduced by Scherer and Scott
56     * (http://www.cs.rice.edu/~wns1/papers/2004-DISC-DDS.pdf) are
57     * (linked) queues in which nodes may represent either data or
58     * requests. When a thread tries to enqueue a data node, but
59     * encounters a request node, it instead "matches" and removes it;
60     * and vice versa for enqueuing requests. Blocking Dual Queues
61     * arrange that threads enqueuing unmatched requests block until
62     * other threads provide the match. Dual Synchronous Queues (see
63     * Scherer, Lea, & Scott
64     * http://www.cs.rochester.edu/u/scott/papers/2009_Scherer_CACM_SSQ.pdf)
65     * additionally arrange that threads enqueuing unmatched data also
66     * block. Dual Transfer Queues support all of these modes, as
67     * dictated by callers.
68     *
69     * A FIFO dual queue may be implemented using a variation of the
70     * Michael & Scott (M&S) lock-free queue algorithm
71     * (http://www.cs.rochester.edu/u/scott/papers/1996_PODC_queues.pdf).
72     * It maintains two pointer fields, "head", pointing to a
73     * (matched) node that in turn points to the first actual
74     * (unmatched) queue node (or null if empty); and "tail" that
75     * points to the last node on the queue (or again null if
76     * empty). For example, here is a possible queue with four data
77     * elements:
78     *
79     * head tail
80     * | |
81     * v v
82     * M -> U -> U -> U -> U
83     *
84     * The M&S queue algorithm is known to be prone to scalability and
85     * overhead limitations when maintaining (via CAS) these head and
86     * tail pointers. This has led to the development of
87     * contention-reducing variants such as elimination arrays (see
88     * Moir et al http://portal.acm.org/citation.cfm?id=1074013) and
89     * optimistic back pointers (see Ladan-Mozes & Shavit
90     * http://people.csail.mit.edu/edya/publications/OptimisticFIFOQueue-journal.pdf).
91     * However, the nature of dual queues enables a simpler tactic for
92     * improving M&S-style implementations when dual-ness is needed.
93     *
94     * In a dual queue, each node must atomically maintain its match
95     * status. While there are other possible variants, we implement
96     * this here as: for a data-mode node, matching entails CASing an
97     * "item" field from a non-null data value to null upon match, and
98     * vice-versa for request nodes, CASing from null to a data
99     * value. (Note that the linearization properties of this style of
100     * queue are easy to verify -- elements are made available by
101     * linking, and unavailable by matching.) Compared to plain M&S
102     * queues, this property of dual queues requires one additional
103     * successful atomic operation per enq/deq pair. But it also
104     * enables lower cost variants of queue maintenance mechanics. (A
105     * variation of this idea applies even for non-dual queues that
106     * support deletion of interior elements, such as
107     * j.u.c.ConcurrentLinkedQueue.)
108     *
109     * Once a node is matched, its match status can never again
110     * change. We may thus arrange that the linked list of them
111     * contain a prefix of zero or more matched nodes, followed by a
112     * suffix of zero or more unmatched nodes. (Note that we allow
113     * both the prefix and suffix to be zero length, which in turn
114     * means that we do not use a dummy header.) If we were not
115     * concerned with either time or space efficiency, we could
116     * correctly perform enqueue and dequeue operations by traversing
117     * from a pointer to the initial node; CASing the item of the
118     * first unmatched node on match and CASing the next field of the
119     * trailing node on appends. (Plus some special-casing when
120     * initially empty). While this would be a terrible idea in
121     * itself, it does have the benefit of not requiring ANY atomic
122     * updates on head/tail fields.
123     *
124     * We introduce here an approach that lies between the extremes of
125     * never versus always updating queue (head and tail) pointers.
126     * This offers a tradeoff between sometimes requiring extra
127     * traversal steps to locate the first and/or last unmatched
128     * nodes, versus the reduced overhead and contention of fewer
129     * updates to queue pointers. For example, a possible snapshot of
130     * a queue is:
131     *
132     * head tail
133     * | |
134     * v v
135     * M -> M -> U -> U -> U -> U
136     *
137     * The best value for this "slack" (the targeted maximum distance
138     * between the value of "head" and the first unmatched node, and
139     * similarly for "tail") is an empirical matter. We have found
140     * that using very small constants in the range of 1-3 work best
141     * over a range of platforms. Larger values introduce increasing
142     * costs of cache misses and risks of long traversal chains, while
143     * smaller values increase CAS contention and overhead.
144     *
145     * Dual queues with slack differ from plain M&S dual queues by
146     * virtue of only sometimes updating head or tail pointers when
147     * matching, appending, or even traversing nodes; in order to
148     * maintain a targeted slack. The idea of "sometimes" may be
149     * operationalized in several ways. The simplest is to use a
150     * per-operation counter incremented on each traversal step, and
151     * to try (via CAS) to update the associated queue pointer
152     * whenever the count exceeds a threshold. Another, that requires
153     * more overhead, is to use random number generators to update
154     * with a given probability per traversal step.
155     *
156     * In any strategy along these lines, because CASes updating
157     * fields may fail, the actual slack may exceed targeted
158     * slack. However, they may be retried at any time to maintain
159     * targets. Even when using very small slack values, this
160     * approach works well for dual queues because it allows all
161     * operations up to the point of matching or appending an item
162     * (hence potentially allowing progress by another thread) to be
163     * read-only, thus not introducing any further contention. As
164     * described below, we implement this by performing slack
165     * maintenance retries only after these points.
166     *
167     * As an accompaniment to such techniques, traversal overhead can
168     * be further reduced without increasing contention of head
169     * pointer updates: Threads may sometimes shortcut the "next" link
170     * path from the current "head" node to be closer to the currently
171     * known first unmatched node, and similarly for tail. Again, this
172     * may be triggered with using thresholds or randomization.
173     *
174     * These ideas must be further extended to avoid unbounded amounts
175     * of costly-to-reclaim garbage caused by the sequential "next"
176     * links of nodes starting at old forgotten head nodes: As first
177     * described in detail by Boehm
178     * (http://portal.acm.org/citation.cfm?doid=503272.503282) if a GC
179     * delays noticing that any arbitrarily old node has become
180     * garbage, all newer dead nodes will also be unreclaimed.
181     * (Similar issues arise in non-GC environments.) To cope with
182     * this in our implementation, upon CASing to advance the head
183     * pointer, we set the "next" link of the previous head to point
184     * only to itself; thus limiting the length of connected dead lists.
185     * (We also take similar care to wipe out possibly garbage
186     * retaining values held in other Node fields.) However, doing so
187     * adds some further complexity to traversal: If any "next"
188     * pointer links to itself, it indicates that the current thread
189     * has lagged behind a head-update, and so the traversal must
190     * continue from the "head". Traversals trying to find the
191     * current tail starting from "tail" may also encounter
192     * self-links, in which case they also continue at "head".
193     *
194     * It is tempting in slack-based scheme to not even use CAS for
195     * updates (similarly to Ladan-Mozes & Shavit). However, this
196     * cannot be done for head updates under the above link-forgetting
197     * mechanics because an update may leave head at a detached node.
198     * And while direct writes are possible for tail updates, they
199     * increase the risk of long retraversals, and hence long garbage
200     * chains, which can be much more costly than is worthwhile
201     * considering that the cost difference of performing a CAS vs
202     * write is smaller when they are not triggered on each operation
203     * (especially considering that writes and CASes equally require
204     * additional GC bookkeeping ("write barriers") that are sometimes
205     * more costly than the writes themselves because of contention).
206     *
207     * Removal of interior nodes (due to timed out or interrupted
208     * waits, or calls to remove(x) or Iterator.remove) can use a
209     * scheme roughly similar to that described in Scherer, Lea, and
210     * Scott's SynchronousQueue. Given a predecessor, we can unsplice
211     * any node except the (actual) tail of the queue. To avoid
212     * build-up of cancelled trailing nodes, upon a request to remove
213     * a trailing node, it is placed in field "cleanMe" to be
214     * unspliced upon the next call to unsplice any other node.
215     * Situations needing such mechanics are not common but do occur
216     * in practice; for example when an unbounded series of short
217     * timed calls to poll repeatedly time out but never otherwise
218     * fall off the list because of an untimed call to take at the
219     * front of the queue. Note that maintaining field cleanMe does
220     * not otherwise much impact garbage retention even if never
221     * cleared by some other call because the held node will
222     * eventually either directly or indirectly lead to a self-link
223     * once off the list.
224     *
225     * *** Overview of implementation ***
226     *
227     * We use a threshold-based approach to updates, with a slack
228     * threshold of two -- that is, we update head/tail when the
229     * current pointer appears to be two or more steps away from the
230     * first/last node. The slack value is hard-wired: a path greater
231     * than one is naturally implemented by checking equality of
232     * traversal pointers except when the list has only one element,
233     * in which case we keep slack threshold at one. Avoiding tracking
234     * explicit counts across method calls slightly simplifies an
235     * already-messy implementation. Using randomization would
236     * probably work better if there were a low-quality dirt-cheap
237     * per-thread one available, but even ThreadLocalRandom is too
238     * heavy for these purposes.
239     *
240     * With such a small slack threshold value, it is rarely
241     * worthwhile to augment this with path short-circuiting; i.e.,
242     * unsplicing nodes between head and the first unmatched node, or
243     * similarly for tail, rather than advancing head or tail
244     * proper. However, it is used (in awaitMatch) immediately before
245     * a waiting thread starts to block, as a final bit of helping at
246     * a point when contention with others is extremely unlikely
247     * (since if other threads that could release it are operating,
248     * then the current thread wouldn't be blocking).
249     *
250     * We allow both the head and tail fields to be null before any
251     * nodes are enqueued; initializing upon first append. This
252     * simplifies some other logic, as well as providing more
253     * efficient explicit control paths instead of letting JVMs insert
254     * implicit NullPointerExceptions when they are null. While not
255     * currently fully implemented, we also leave open the possibility
256     * of re-nulling these fields when empty (which is complicated to
257     * arrange, for little benefit.)
258     *
259     * All enqueue/dequeue operations are handled by the single method
260     * "xfer" with parameters indicating whether to act as some form
261     * of offer, put, poll, take, or transfer (each possibly with
262     * timeout). The relative complexity of using one monolithic
263     * method outweighs the code bulk and maintenance problems of
264     * using separate methods for each case.
265     *
266     * Operation consists of up to three phases. The first is
267     * implemented within method xfer, the second in tryAppend, and
268     * the third in method awaitMatch.
269     *
270     * 1. Try to match an existing node
271     *
272     * Starting at head, skip already-matched nodes until finding
273     * an unmatched node of opposite mode, if one exists, in which
274     * case matching it and returning, also if necessary updating
275     * head to one past the matched node (or the node itself if the
276     * list has no other unmatched nodes). If the CAS misses, then
277     * a loop retries advancing head by two steps until either
278     * success or the slack is at most two. By requiring that each
279     * attempt advances head by two (if applicable), we ensure that
280     * the slack does not grow without bound. Traversals also check
281     * if the initial head is now off-list, in which case they
282     * start at the new head.
283     *
284     * If no candidates are found and the call was untimed
285     * poll/offer, (argument "how" is NOW) return.
286     *
287     * 2. Try to append a new node (method tryAppend)
288     *
289     * Starting at current tail pointer, find the actual last node
290     * and try to append a new node (or if head was null, establish
291     * the first node). Nodes can be appended only if their
292     * predecessors are either already matched or are of the same
293     * mode. If we detect otherwise, then a new node with opposite
294     * mode must have been appended during traversal, so we must
295     * restart at phase 1. The traversal and update steps are
296     * otherwise similar to phase 1: Retrying upon CAS misses and
297     * checking for staleness. In particular, if a self-link is
298     * encountered, then we can safely jump to a node on the list
299     * by continuing the traversal at current head.
300     *
301     * On successful append, if the call was ASYNC, return.
302     *
303     * 3. Await match or cancellation (method awaitMatch)
304     *
305     * Wait for another thread to match node; instead cancelling if
306     * the current thread was interrupted or the wait timed out. On
307     * multiprocessors, we use front-of-queue spinning: If a node
308     * appears to be the first unmatched node in the queue, it
309     * spins a bit before blocking. In either case, before blocking
310     * it tries to unsplice any nodes between the current "head"
311     * and the first unmatched node.
312     *
313     * Front-of-queue spinning vastly improves performance of
314     * heavily contended queues. And so long as it is relatively
315     * brief and "quiet", spinning does not much impact performance
316     * of less-contended queues. During spins threads check their
317     * interrupt status and generate a thread-local random number
318     * to decide to occasionally perform a Thread.yield. While
319     * yield has underdefined specs, we assume that might it help,
320     * and will not hurt in limiting impact of spinning on busy
321     * systems. We also use smaller (1/2) spins for nodes that are
322     * not known to be front but whose predecessors have not
323     * blocked -- these "chained" spins avoid artifacts of
324     * front-of-queue rules which otherwise lead to alternating
325     * nodes spinning vs blocking. Further, front threads that
326     * represent phase changes (from data to request node or vice
327     * versa) compared to their predecessors receive additional
328     * chained spins, reflecting longer paths typically required to
329     * unblock threads during phase changes.
330     */
331    
332     /** True if on multiprocessor */
333     private static final boolean MP =
334     Runtime.getRuntime().availableProcessors() > 1;
335    
336     /**
337     * The number of times to spin (with randomly interspersed calls
338     * to Thread.yield) on multiprocessor before blocking when a node
339     * is apparently the first waiter in the queue. See above for
340     * explanation. Must be a power of two. The value is empirically
341     * derived -- it works pretty well across a variety of processors,
342     * numbers of CPUs, and OSes.
343     */
344     private static final int FRONT_SPINS = 1 << 7;
345    
346     /**
347     * The number of times to spin before blocking when a node is
348     * preceded by another node that is apparently spinning. Also
349     * serves as an increment to FRONT_SPINS on phase changes, and as
350     * base average frequency for yielding during spins. Must be a
351     * power of two.
352     */
353     private static final int CHAINED_SPINS = FRONT_SPINS >>> 1;
354    
355     /**
356     * Queue nodes. Uses Object, not E, for items to allow forgetting
357     * them after use. Relies heavily on Unsafe mechanics to minimize
358     * unnecessary ordering constraints: Writes that intrinsically
359     * precede or follow CASes use simple relaxed forms. Other
360     * cleanups use releasing/lazy writes.
361     */
362     static final class Node<E> {
363     final boolean isData; // false if this is a request node
364     volatile Object item; // initially non-null if isData; CASed to match
365     volatile Node<E> next;
366     volatile Thread waiter; // null until waiting
367    
368     // CAS methods for fields
369     final boolean casNext(Node<E> cmp, Node<E> val) {
370     return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
371     }
372 jsr166 1.1
373 jsr166 1.8 final boolean casItem(Object cmp, Object val) {
374     assert cmp.getClass() != Node.class;
375     return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val);
376     }
377 jsr166 1.1
378 jsr166 1.8 /**
379     * Creates a new node. Uses relaxed write because item can only
380     * be seen if followed by CAS.
381     */
382     Node(E item, boolean isData) {
383     UNSAFE.putObject(this, itemOffset, item); // relaxed write
384     this.isData = isData;
385     }
386 jsr166 1.1
387 jsr166 1.8 /**
388     * Links node to itself to avoid garbage retention. Called
389     * only after CASing head field, so uses relaxed write.
390     */
391     final void forgetNext() {
392     UNSAFE.putObject(this, nextOffset, this);
393     }
394 jsr166 1.1
395 jsr166 1.8 /**
396     * Sets item to self (using a releasing/lazy write) and waiter
397     * to null, to avoid garbage retention after extracting or
398     * cancelling.
399     */
400     final void forgetContents() {
401     UNSAFE.putOrderedObject(this, itemOffset, this);
402     UNSAFE.putOrderedObject(this, waiterOffset, null);
403     }
404 jsr166 1.1
405 jsr166 1.8 /**
406     * Returns true if this node has been matched, including the
407     * case of artificial matches due to cancellation.
408     */
409     final boolean isMatched() {
410     Object x = item;
411     return x == this || (x != null) != isData;
412     }
413 jsr166 1.1
414 jsr166 1.8 /**
415     * Returns true if a node with the given mode cannot be
416     * appended to this node because this node is unmatched and
417     * has opposite data mode.
418     */
419     final boolean cannotPrecede(boolean haveData) {
420     boolean d = isData;
421     Object x;
422     return d != haveData && (x = item) != this && (x != null) == d;
423     }
424 jsr166 1.1
425 jsr166 1.8 /**
426     * Tries to artificially match a data node -- used by remove.
427     */
428     final boolean tryMatchData() {
429     Object x = item;
430     if (x != null && x != this && casItem(x, null)) {
431     LockSupport.unpark(waiter);
432     return true;
433     }
434     return false;
435 jsr166 1.1 }
436    
437 jsr166 1.4 // Unsafe mechanics
438     private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe();
439     private static final long nextOffset =
440     objectFieldOffset(UNSAFE, "next", Node.class);
441 jsr166 1.8 private static final long itemOffset =
442     objectFieldOffset(UNSAFE, "item", Node.class);
443     private static final long waiterOffset =
444     objectFieldOffset(UNSAFE, "waiter", Node.class);
445 jsr166 1.1
446     private static final long serialVersionUID = -3375979862319811754L;
447     }
448    
449 jsr166 1.8 /** head of the queue; null until first enqueue */
450     transient volatile Node<E> head;
451    
452     /** predecessor of dangling unspliceable node */
453     private transient volatile Node<E> cleanMe; // decl here reduces contention
454 jsr166 1.1
455 jsr166 1.8 /** tail of the queue; null until first append */
456     private transient volatile Node<E> tail;
457 jsr166 1.1
458 jsr166 1.8 // CAS methods for fields
459     private boolean casTail(Node<E> cmp, Node<E> val) {
460     return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val);
461     }
462 jsr166 1.1
463 jsr166 1.8 private boolean casHead(Node<E> cmp, Node<E> val) {
464     return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val);
465     }
466 jsr166 1.1
467 jsr166 1.8 private boolean casCleanMe(Node<E> cmp, Node<E> val) {
468     return UNSAFE.compareAndSwapObject(this, cleanMeOffset, cmp, val);
469     }
470 jsr166 1.1
471 jsr166 1.8 /*
472     * Possible values for "how" argument in xfer method. Beware that
473     * the order of assigned numerical values matters.
474 jsr166 1.1 */
475 jsr166 1.8 private static final int NOW = 0; // for untimed poll, tryTransfer
476     private static final int ASYNC = 1; // for offer, put, add
477     private static final int SYNC = 2; // for transfer, take
478     private static final int TIMEOUT = 3; // for timed poll, tryTransfer
479 jsr166 1.1
480     /**
481 jsr166 1.8 * Implements all queuing methods. See above for explanation.
482 jsr166 1.1 *
483 jsr166 1.8 * @param e the item or null for take
484     * @param haveData true if this is a put, else a take
485     * @param how NOW, ASYNC, SYNC, or TIMEOUT
486 jsr166 1.1 * @param nanos timeout in nanosecs, used only if mode is TIMEOUT
487 jsr166 1.8 * @return an item if matched, else e
488     * @throws NullPointerException if haveData mode but e is null
489 jsr166 1.1 */
490 jsr166 1.8 private E xfer(E e, boolean haveData, int how, long nanos) {
491     if (haveData && (e == null))
492     throw new NullPointerException();
493     Node<E> s = null; // the node to append, if needed
494 jsr166 1.1
495 jsr166 1.8 retry: for (;;) { // restart on append race
496 jsr166 1.1
497 jsr166 1.8 for (Node<E> h = head, p = h; p != null;) {
498     // find & match first node
499     boolean isData = p.isData;
500     Object item = p.item;
501     if (item != p && (item != null) == isData) { // unmatched
502     if (isData == haveData) // can't match
503     break;
504     if (p.casItem(item, e)) { // match
505     for (Node<E> q = p; q != h;) {
506     Node<E> n = q.next; // update head by 2
507     if (n != null) // unless singleton
508     q = n;
509     if (head == h && casHead(h, q)) {
510     h.forgetNext();
511     break;
512     } // advance and retry
513     if ((h = head) == null ||
514     (q = h.next) == null || !q.isMatched())
515     break; // unless slack < 2
516     }
517     LockSupport.unpark(p.waiter);
518     return this.<E>cast(item);
519 jsr166 1.1 }
520     }
521 jsr166 1.8 Node<E> n = p.next;
522     p = (p != n) ? n : (h = head); // Use head if p offlist
523     }
524    
525     if (how >= ASYNC) { // No matches available
526     if (s == null)
527     s = new Node<E>(e, haveData);
528     Node<E> pred = tryAppend(s, haveData);
529     if (pred == null)
530     continue retry; // lost race vs opposite mode
531     if (how >= SYNC)
532     return awaitMatch(s, pred, e, how, nanos);
533 jsr166 1.1 }
534 jsr166 1.8 return e; // not waiting
535 jsr166 1.1 }
536     }
537    
538     /**
539 jsr166 1.8 * Tries to append node s as tail.
540     *
541     * @param s the node to append
542     * @param haveData true if appending in data mode
543     * @return null on failure due to losing race with append in
544     * different mode, else s's predecessor, or s itself if no
545     * predecessor
546 jsr166 1.1 */
547 jsr166 1.8 private Node<E> tryAppend(Node<E> s, boolean haveData) {
548     for (Node<E> t = tail, p = t;;) { // move p to last node and append
549     Node<E> n, u; // temps for reads of next & tail
550     if (p == null && (p = head) == null) {
551     if (casHead(null, s))
552     return s; // initialize
553     }
554     else if (p.cannotPrecede(haveData))
555     return null; // lost race vs opposite mode
556     else if ((n = p.next) != null) // not last; keep traversing
557     p = p != t && t != (u = tail) ? (t = u) : // stale tail
558     (p != n) ? n : null; // restart if off list
559     else if (!p.casNext(null, s))
560     p = p.next; // re-read on CAS failure
561     else {
562     if (p != t) { // update if slack now >= 2
563     while ((tail != t || !casTail(t, s)) &&
564     (t = tail) != null &&
565     (s = t.next) != null && // advance and retry
566     (s = s.next) != null && s != t);
567 jsr166 1.1 }
568 jsr166 1.8 return p;
569 jsr166 1.1 }
570     }
571     }
572    
573     /**
574 jsr166 1.8 * Spins/yields/blocks until node s is matched or caller gives up.
575 jsr166 1.1 *
576     * @param s the waiting node
577 jsr166 1.8 * @param pred the predecessor of s, or s itself if it has no
578     * predecessor, or null if unknown (the null case does not occur
579     * in any current calls but may in possible future extensions)
580 jsr166 1.1 * @param e the comparison value for checking match
581 jsr166 1.8 * @param how either SYNC or TIMEOUT
582 jsr166 1.1 * @param nanos timeout value
583 jsr166 1.8 * @return matched item, or e if unmatched on interrupt or timeout
584 jsr166 1.1 */
585 jsr166 1.8 private E awaitMatch(Node<E> s, Node<E> pred, E e, int how, long nanos) {
586     long lastTime = (how == TIMEOUT) ? System.nanoTime() : 0L;
587     Thread w = Thread.currentThread();
588     int spins = -1; // initialized after first item and cancel checks
589     ThreadLocalRandom randomYields = null; // bound if needed
590 jsr166 1.1
591     for (;;) {
592 jsr166 1.8 Object item = s.item;
593     if (item != e) { // matched
594     assert item != s;
595     s.forgetContents(); // avoid garbage
596     return this.<E>cast(item);
597     }
598     if ((w.isInterrupted() || (how == TIMEOUT && nanos <= 0)) &&
599     s.casItem(e, s)) { // cancel
600     unsplice(pred, s);
601     return e;
602     }
603    
604     if (spins < 0) { // establish spins at/near front
605     if ((spins = spinsFor(pred, s.isData)) > 0)
606     randomYields = ThreadLocalRandom.current();
607     }
608     else if (spins > 0) { // spin
609     if (--spins == 0)
610     shortenHeadPath(); // reduce slack before blocking
611     else if (randomYields.nextInt(CHAINED_SPINS) == 0)
612     Thread.yield(); // occasionally yield
613     }
614     else if (s.waiter == null) {
615     s.waiter = w; // request unpark then recheck
616 jsr166 1.1 }
617 jsr166 1.8 else if (how == TIMEOUT) {
618 jsr166 1.1 long now = System.nanoTime();
619 jsr166 1.8 if ((nanos -= now - lastTime) > 0)
620     LockSupport.parkNanos(this, nanos);
621 jsr166 1.1 lastTime = now;
622     }
623 jsr166 1.8 else {
624 jsr166 1.1 LockSupport.park(this);
625     s.waiter = null;
626 jsr166 1.8 spins = -1; // spin if front upon wakeup
627 jsr166 1.1 }
628 jsr166 1.8 }
629     }
630    
631     /**
632     * Returns spin/yield value for a node with given predecessor and
633     * data mode. See above for explanation.
634     */
635     private static int spinsFor(Node<?> pred, boolean haveData) {
636     if (MP && pred != null) {
637     if (pred.isData != haveData) // phase change
638     return FRONT_SPINS + CHAINED_SPINS;
639     if (pred.isMatched()) // probably at front
640     return FRONT_SPINS;
641     if (pred.waiter == null) // pred apparently spinning
642     return CHAINED_SPINS;
643     }
644     return 0;
645     }
646    
647     /**
648     * Tries (once) to unsplice nodes between head and first unmatched
649     * or trailing node; failing on contention.
650     */
651     private void shortenHeadPath() {
652     Node<E> h, hn, p, q;
653     if ((p = h = head) != null && h.isMatched() &&
654     (q = hn = h.next) != null) {
655     Node<E> n;
656     while ((n = q.next) != q) {
657     if (n == null || !q.isMatched()) {
658     if (hn != q && h.next == hn)
659     h.casNext(hn, q);
660     break;
661     }
662     p = q;
663     q = n;
664 jsr166 1.1 }
665     }
666     }
667    
668 jsr166 1.8 /* -------------- Traversal methods -------------- */
669    
670     /**
671     * Returns the first unmatched node of the given mode, or null if
672     * none. Used by methods isEmpty, hasWaitingConsumer.
673     */
674     private Node<E> firstOfMode(boolean data) {
675     for (Node<E> p = head; p != null; ) {
676     if (!p.isMatched())
677     return (p.isData == data) ? p : null;
678     Node<E> n = p.next;
679     p = (n != p) ? n : head;
680     }
681     return null;
682     }
683    
684     @SuppressWarnings("unchecked")
685     static <E> E cast(Object item) {
686     assert item.getClass() != Node.class;
687     return (E) item;
688     }
689    
690     /**
691     * Returns the item in the first unmatched node with isData; or
692     * null if none. Used by peek.
693     */
694     private E firstDataItem() {
695     for (Node<E> p = head; p != null; ) {
696     boolean isData = p.isData;
697     Object item = p.item;
698     if (item != p && (item != null) == isData)
699     return isData ? this.<E>cast(item) : null;
700     Node<E> n = p.next;
701     p = (n != p) ? n : head;
702     }
703     return null;
704     }
705    
706 jsr166 1.1 /**
707 jsr166 1.8 * Traverses and counts unmatched nodes of the given mode.
708     * Used by methods size and getWaitingConsumerCount.
709 jsr166 1.1 */
710 jsr166 1.8 private int countOfMode(boolean data) {
711     int count = 0;
712     for (Node<E> p = head; p != null; ) {
713     if (!p.isMatched()) {
714     if (p.isData != data)
715     return 0;
716     if (++count == Integer.MAX_VALUE) // saturated
717     break;
718     }
719     Node<E> n = p.next;
720     if (n != p)
721     p = n;
722     else {
723     count = 0;
724     p = head;
725 jsr166 1.1 }
726 jsr166 1.8 }
727     return count;
728     }
729    
730     final class Itr implements Iterator<E> {
731     private Node<E> nextNode; // next node to return item for
732     private E nextItem; // the corresponding item
733     private Node<E> lastRet; // last returned node, to support remove
734    
735     /**
736     * Moves to next node after prev, or first node if prev null.
737     */
738     private void advance(Node<E> prev) {
739     lastRet = prev;
740     Node<E> p;
741     if (prev == null || (p = prev.next) == prev)
742     p = head;
743     while (p != null) {
744     Object item = p.item;
745     if (p.isData) {
746     if (item != null && item != p) {
747     nextItem = LinkedTransferQueue.this.<E>cast(item);
748     nextNode = p;
749     return;
750     }
751     }
752     else if (item == null)
753     break;
754     Node<E> n = p.next;
755     p = (n != p) ? n : head;
756 jsr166 1.1 }
757 jsr166 1.8 nextNode = null;
758     }
759    
760     Itr() {
761     advance(null);
762     }
763    
764     public final boolean hasNext() {
765     return nextNode != null;
766     }
767    
768     public final E next() {
769     Node<E> p = nextNode;
770     if (p == null) throw new NoSuchElementException();
771     E e = nextItem;
772     advance(p);
773     return e;
774     }
775    
776     public final void remove() {
777     Node<E> p = lastRet;
778     if (p == null) throw new IllegalStateException();
779     lastRet = null;
780     findAndRemoveNode(p);
781 jsr166 1.1 }
782     }
783    
784 jsr166 1.8 /* -------------- Removal methods -------------- */
785    
786 jsr166 1.1 /**
787 jsr166 1.8 * Unsplices (now or later) the given deleted/cancelled node with
788     * the given predecessor.
789 jsr166 1.1 *
790 jsr166 1.8 * @param pred predecessor of node to be unspliced
791     * @param s the node to be unspliced
792 jsr166 1.1 */
793 jsr166 1.8 private void unsplice(Node<E> pred, Node<E> s) {
794     s.forgetContents(); // clear unneeded fields
795 jsr166 1.1 /*
796     * At any given time, exactly one node on list cannot be
797 jsr166 1.8 * unlinked -- the last inserted node. To accommodate this, if
798     * we cannot unlink s, we save its predecessor as "cleanMe",
799     * processing the previously saved version first. Because only
800     * one node in the list can have a null next, at least one of
801     * node s or the node previously saved can always be
802 jsr166 1.1 * processed, so this always terminates.
803     */
804 jsr166 1.8 if (pred != null && pred != s) {
805     while (pred.next == s) {
806     Node<E> oldpred = (cleanMe == null) ? null : reclean();
807     Node<E> n = s.next;
808     if (n != null) {
809     if (n != s)
810     pred.casNext(s, n);
811 jsr166 1.1 break;
812 jsr166 1.8 }
813     if (oldpred == pred || // Already saved
814     (oldpred == null && casCleanMe(null, pred)))
815     break; // Postpone cleaning
816 jsr166 1.1 }
817     }
818     }
819    
820     /**
821 jsr166 1.8 * Tries to unsplice the deleted/cancelled node held in cleanMe
822     * that was previously uncleanable because it was at tail.
823 jsr166 1.1 *
824     * @return current cleanMe node (or null)
825     */
826     private Node<E> reclean() {
827     /*
828 jsr166 1.8 * cleanMe is, or at one time was, predecessor of a cancelled
829     * node s that was the tail so could not be unspliced. If it
830 jsr166 1.1 * is no longer the tail, try to unsplice if necessary and
831     * make cleanMe slot available. This differs from similar
832 jsr166 1.8 * code in unsplice() because we must check that pred still
833     * points to a matched node that can be unspliced -- if not,
834     * we can (must) clear cleanMe without unsplicing. This can
835     * loop only due to contention.
836 jsr166 1.1 */
837     Node<E> pred;
838 jsr166 1.8 while ((pred = cleanMe) != null) {
839 jsr166 1.1 Node<E> s = pred.next;
840 jsr166 1.8 Node<E> n;
841     if (s == null || s == pred || !s.isMatched())
842     casCleanMe(pred, null); // already gone
843     else if ((n = s.next) != null) {
844     if (n != s)
845     pred.casNext(s, n);
846     casCleanMe(pred, null);
847 jsr166 1.1 }
848 jsr166 1.8 else
849 jsr166 1.1 break;
850     }
851     return pred;
852     }
853    
854     /**
855 jsr166 1.8 * Main implementation of Iterator.remove(). Find
856     * and unsplice the given node.
857     */
858     final void findAndRemoveNode(Node<E> s) {
859     if (s.tryMatchData()) {
860     Node<E> pred = null;
861     Node<E> p = head;
862     while (p != null) {
863     if (p == s) {
864     unsplice(pred, p);
865     break;
866     }
867     if (!p.isData && !p.isMatched())
868     break;
869     pred = p;
870     if ((p = p.next) == pred) { // stale
871     pred = null;
872     p = head;
873     }
874     }
875     }
876     }
877    
878     /**
879     * Main implementation of remove(Object)
880     */
881     private boolean findAndRemove(Object e) {
882     if (e != null) {
883     Node<E> pred = null;
884     Node<E> p = head;
885     while (p != null) {
886     Object item = p.item;
887     if (p.isData) {
888     if (item != null && item != p && e.equals(item) &&
889     p.tryMatchData()) {
890     unsplice(pred, p);
891     return true;
892     }
893     }
894     else if (item == null)
895     break;
896     pred = p;
897     if ((p = p.next) == pred) {
898     pred = null;
899     p = head;
900     }
901     }
902     }
903     return false;
904     }
905    
906    
907     /**
908 jsr166 1.1 * Creates an initially empty {@code LinkedTransferQueue}.
909     */
910     public LinkedTransferQueue() {
911     }
912    
913     /**
914     * Creates a {@code LinkedTransferQueue}
915     * initially containing the elements of the given collection,
916     * added in traversal order of the collection's iterator.
917     *
918     * @param c the collection of elements to initially contain
919     * @throws NullPointerException if the specified collection or any
920     * of its elements are null
921     */
922     public LinkedTransferQueue(Collection<? extends E> c) {
923     this();
924     addAll(c);
925     }
926    
927 jsr166 1.4 /**
928 jsr166 1.5 * Inserts the specified element at the tail of this queue.
929     * As the queue is unbounded, this method will never block.
930     *
931     * @throws NullPointerException if the specified element is null
932 jsr166 1.4 */
933 jsr166 1.5 public void put(E e) {
934 jsr166 1.8 xfer(e, true, ASYNC, 0);
935 jsr166 1.1 }
936    
937 jsr166 1.4 /**
938 jsr166 1.5 * Inserts the specified element at the tail of this queue.
939     * As the queue is unbounded, this method will never block or
940     * return {@code false}.
941     *
942     * @return {@code true} (as specified by
943     * {@link BlockingQueue#offer(Object,long,TimeUnit) BlockingQueue.offer})
944     * @throws NullPointerException if the specified element is null
945 jsr166 1.4 */
946 jsr166 1.5 public boolean offer(E e, long timeout, TimeUnit unit) {
947 jsr166 1.8 xfer(e, true, ASYNC, 0);
948     return true;
949 jsr166 1.1 }
950    
951 jsr166 1.4 /**
952 jsr166 1.5 * Inserts the specified element at the tail of this queue.
953     * As the queue is unbounded, this method will never return {@code false}.
954     *
955     * @return {@code true} (as specified by
956     * {@link BlockingQueue#offer(Object) BlockingQueue.offer})
957     * @throws NullPointerException if the specified element is null
958 jsr166 1.4 */
959 jsr166 1.1 public boolean offer(E e) {
960 jsr166 1.8 xfer(e, true, ASYNC, 0);
961 jsr166 1.1 return true;
962     }
963    
964 jsr166 1.4 /**
965 jsr166 1.5 * Inserts the specified element at the tail of this queue.
966     * As the queue is unbounded, this method will never throw
967     * {@link IllegalStateException} or return {@code false}.
968     *
969     * @return {@code true} (as specified by {@link Collection#add})
970     * @throws NullPointerException if the specified element is null
971 jsr166 1.4 */
972 jsr166 1.1 public boolean add(E e) {
973 jsr166 1.8 xfer(e, true, ASYNC, 0);
974     return true;
975 jsr166 1.5 }
976    
977     /**
978 jsr166 1.6 * Transfers the element to a waiting consumer immediately, if possible.
979     *
980     * <p>More precisely, transfers the specified element immediately
981     * if there exists a consumer already waiting to receive it (in
982     * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
983     * otherwise returning {@code false} without enqueuing the element.
984 jsr166 1.5 *
985     * @throws NullPointerException if the specified element is null
986     */
987     public boolean tryTransfer(E e) {
988 jsr166 1.8 return xfer(e, true, NOW, 0) == null;
989 jsr166 1.1 }
990    
991 jsr166 1.4 /**
992 jsr166 1.6 * Transfers the element to a consumer, waiting if necessary to do so.
993     *
994     * <p>More precisely, transfers the specified element immediately
995     * if there exists a consumer already waiting to receive it (in
996     * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
997     * else inserts the specified element at the tail of this queue
998     * and waits until the element is received by a consumer.
999 jsr166 1.5 *
1000     * @throws NullPointerException if the specified element is null
1001 jsr166 1.4 */
1002 jsr166 1.1 public void transfer(E e) throws InterruptedException {
1003 jsr166 1.8 if (xfer(e, true, SYNC, 0) != null) {
1004     Thread.interrupted(); // failure possible only due to interrupt
1005 jsr166 1.1 throw new InterruptedException();
1006     }
1007     }
1008    
1009 jsr166 1.4 /**
1010 jsr166 1.6 * Transfers the element to a consumer if it is possible to do so
1011     * before the timeout elapses.
1012     *
1013     * <p>More precisely, transfers the specified element immediately
1014     * if there exists a consumer already waiting to receive it (in
1015     * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
1016     * else inserts the specified element at the tail of this queue
1017     * and waits until the element is received by a consumer,
1018     * returning {@code false} if the specified wait time elapses
1019     * before the element can be transferred.
1020 jsr166 1.5 *
1021     * @throws NullPointerException if the specified element is null
1022 jsr166 1.4 */
1023 jsr166 1.1 public boolean tryTransfer(E e, long timeout, TimeUnit unit)
1024     throws InterruptedException {
1025 jsr166 1.8 if (xfer(e, true, TIMEOUT, unit.toNanos(timeout)) == null)
1026 jsr166 1.1 return true;
1027     if (!Thread.interrupted())
1028     return false;
1029     throw new InterruptedException();
1030     }
1031    
1032     public E take() throws InterruptedException {
1033 jsr166 1.8 E e = xfer(null, false, SYNC, 0);
1034 jsr166 1.1 if (e != null)
1035 jsr166 1.5 return e;
1036 jsr166 1.1 Thread.interrupted();
1037     throw new InterruptedException();
1038     }
1039    
1040     public E poll(long timeout, TimeUnit unit) throws InterruptedException {
1041 jsr166 1.8 E e = xfer(null, false, TIMEOUT, unit.toNanos(timeout));
1042 jsr166 1.1 if (e != null || !Thread.interrupted())
1043 jsr166 1.5 return e;
1044 jsr166 1.1 throw new InterruptedException();
1045     }
1046    
1047     public E poll() {
1048 jsr166 1.8 return xfer(null, false, NOW, 0);
1049 jsr166 1.1 }
1050    
1051 jsr166 1.4 /**
1052     * @throws NullPointerException {@inheritDoc}
1053     * @throws IllegalArgumentException {@inheritDoc}
1054     */
1055 jsr166 1.1 public int drainTo(Collection<? super E> c) {
1056     if (c == null)
1057     throw new NullPointerException();
1058     if (c == this)
1059     throw new IllegalArgumentException();
1060     int n = 0;
1061     E e;
1062     while ( (e = poll()) != null) {
1063     c.add(e);
1064     ++n;
1065     }
1066     return n;
1067     }
1068    
1069 jsr166 1.4 /**
1070     * @throws NullPointerException {@inheritDoc}
1071     * @throws IllegalArgumentException {@inheritDoc}
1072     */
1073 jsr166 1.1 public int drainTo(Collection<? super E> c, int maxElements) {
1074     if (c == null)
1075     throw new NullPointerException();
1076     if (c == this)
1077     throw new IllegalArgumentException();
1078     int n = 0;
1079     E e;
1080     while (n < maxElements && (e = poll()) != null) {
1081     c.add(e);
1082     ++n;
1083     }
1084     return n;
1085     }
1086    
1087 jsr166 1.5 /**
1088     * Returns an iterator over the elements in this queue in proper
1089     * sequence, from head to tail.
1090     *
1091     * <p>The returned iterator is a "weakly consistent" iterator that
1092     * will never throw
1093     * {@link ConcurrentModificationException ConcurrentModificationException},
1094     * and guarantees to traverse elements as they existed upon
1095     * construction of the iterator, and may (but is not guaranteed
1096     * to) reflect any modifications subsequent to construction.
1097     *
1098     * @return an iterator over the elements in this queue in proper sequence
1099     */
1100 jsr166 1.1 public Iterator<E> iterator() {
1101     return new Itr();
1102     }
1103    
1104     public E peek() {
1105 jsr166 1.8 return firstDataItem();
1106 jsr166 1.1 }
1107    
1108 jsr166 1.6 /**
1109     * Returns {@code true} if this queue contains no elements.
1110     *
1111     * @return {@code true} if this queue contains no elements
1112     */
1113 jsr166 1.1 public boolean isEmpty() {
1114 jsr166 1.8 return firstOfMode(true) == null;
1115 jsr166 1.1 }
1116    
1117     public boolean hasWaitingConsumer() {
1118 jsr166 1.8 return firstOfMode(false) != null;
1119 jsr166 1.1 }
1120    
1121     /**
1122     * Returns the number of elements in this queue. If this queue
1123     * contains more than {@code Integer.MAX_VALUE} elements, returns
1124     * {@code Integer.MAX_VALUE}.
1125     *
1126     * <p>Beware that, unlike in most collections, this method is
1127     * <em>NOT</em> a constant-time operation. Because of the
1128     * asynchronous nature of these queues, determining the current
1129     * number of elements requires an O(n) traversal.
1130     *
1131     * @return the number of elements in this queue
1132     */
1133     public int size() {
1134 jsr166 1.8 return countOfMode(true);
1135 jsr166 1.1 }
1136    
1137     public int getWaitingConsumerCount() {
1138 jsr166 1.8 return countOfMode(false);
1139 jsr166 1.1 }
1140    
1141 jsr166 1.6 /**
1142     * Removes a single instance of the specified element from this queue,
1143     * if it is present. More formally, removes an element {@code e} such
1144     * that {@code o.equals(e)}, if this queue contains one or more such
1145     * elements.
1146     * Returns {@code true} if this queue contained the specified element
1147     * (or equivalently, if this queue changed as a result of the call).
1148     *
1149     * @param o element to be removed from this queue, if present
1150     * @return {@code true} if this queue changed as a result of the call
1151     */
1152 jsr166 1.1 public boolean remove(Object o) {
1153 jsr166 1.8 return findAndRemove(o);
1154 jsr166 1.1 }
1155    
1156     /**
1157 jsr166 1.5 * Always returns {@code Integer.MAX_VALUE} because a
1158     * {@code LinkedTransferQueue} is not capacity constrained.
1159     *
1160     * @return {@code Integer.MAX_VALUE} (as specified by
1161     * {@link BlockingQueue#remainingCapacity()})
1162     */
1163     public int remainingCapacity() {
1164     return Integer.MAX_VALUE;
1165     }
1166    
1167     /**
1168 jsr166 1.8 * Saves the state to a stream (that is, serializes it).
1169 jsr166 1.1 *
1170     * @serialData All of the elements (each an {@code E}) in
1171     * the proper order, followed by a null
1172     * @param s the stream
1173     */
1174     private void writeObject(java.io.ObjectOutputStream s)
1175     throws java.io.IOException {
1176     s.defaultWriteObject();
1177     for (E e : this)
1178     s.writeObject(e);
1179     // Use trailing null as sentinel
1180     s.writeObject(null);
1181     }
1182    
1183     /**
1184 jsr166 1.8 * Reconstitutes the Queue instance from a stream (that is,
1185     * deserializes it).
1186 jsr166 1.1 *
1187     * @param s the stream
1188     */
1189     private void readObject(java.io.ObjectInputStream s)
1190     throws java.io.IOException, ClassNotFoundException {
1191     s.defaultReadObject();
1192     for (;;) {
1193     @SuppressWarnings("unchecked") E item = (E) s.readObject();
1194     if (item == null)
1195     break;
1196     else
1197     offer(item);
1198     }
1199     }
1200    
1201 jsr166 1.3 // Unsafe mechanics
1202 jsr166 1.1
1203 jsr166 1.3 private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe();
1204     private static final long headOffset =
1205 jsr166 1.4 objectFieldOffset(UNSAFE, "head", LinkedTransferQueue.class);
1206 jsr166 1.3 private static final long tailOffset =
1207 jsr166 1.4 objectFieldOffset(UNSAFE, "tail", LinkedTransferQueue.class);
1208 jsr166 1.3 private static final long cleanMeOffset =
1209 jsr166 1.4 objectFieldOffset(UNSAFE, "cleanMe", LinkedTransferQueue.class);
1210    
1211     static long objectFieldOffset(sun.misc.Unsafe UNSAFE,
1212     String field, Class<?> klazz) {
1213 jsr166 1.1 try {
1214 jsr166 1.3 return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
1215 jsr166 1.1 } catch (NoSuchFieldException e) {
1216 jsr166 1.3 // Convert Exception to corresponding Error
1217     NoSuchFieldError error = new NoSuchFieldError(field);
1218 jsr166 1.1 error.initCause(e);
1219     throw error;
1220     }
1221     }
1222 jsr166 1.8
1223 jsr166 1.1 }