View Javadoc

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