ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/LinkedTransferQueue.java
(Generate patch)

Comparing jsr166/src/jsr166y/LinkedTransferQueue.java (file contents):
Revision 1.7 by dl, Wed Sep 24 10:48:43 2008 UTC vs.
Revision 1.47 by jsr166, Thu Oct 22 09:06:38 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines