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