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.49 by jsr166, Thu Oct 22 15:58:44 2009 UTC vs.
Revision 1.71 by jsr166, Mon Nov 16 01:02:49 2009 UTC

# Line 161 | Line 161 | public class LinkedTransferQueue<E> exte
161       * targets.  Even when using very small slack values, this
162       * approach works well for dual queues because it allows all
163       * operations up to the point of matching or appending an item
164 <     * (hence potentially releasing another thread) to be read-only,
165 <     * thus not introducing any further contention. As described
166 <     * below, we implement this by performing slack maintenance
167 <     * retries only after these points.
164 >     * (hence potentially allowing progress by another thread) to be
165 >     * read-only, thus not introducing any further contention. As
166 >     * described below, we implement this by performing slack
167 >     * maintenance retries only after these points.
168       *
169       * As an accompaniment to such techniques, traversal overhead can
170       * be further reduced without increasing contention of head
171 <     * pointer updates.  During traversals, threads may sometimes
172 <     * shortcut the "next" link path from the current "head" node to
173 <     * be closer to the currently known first unmatched node. Again,
174 <     * this may be triggered with using thresholds or randomization.
171 >     * pointer updates: Threads may sometimes shortcut the "next" link
172 >     * path from the current "head" node to be closer to the currently
173 >     * known first unmatched node, and similarly for tail. Again, this
174 >     * may be triggered with using thresholds or randomization.
175       *
176       * These ideas must be further extended to avoid unbounded amounts
177       * of costly-to-reclaim garbage caused by the sequential "next"
# Line 199 | Line 199 | public class LinkedTransferQueue<E> exte
199       * mechanics because an update may leave head at a detached node.
200       * And while direct writes are possible for tail updates, they
201       * increase the risk of long retraversals, and hence long garbage
202 <     * chains which can be much more costly than is worthwhile
202 >     * chains, which can be much more costly than is worthwhile
203       * considering that the cost difference of performing a CAS vs
204       * write is smaller when they are not triggered on each operation
205       * (especially considering that writes and CASes equally require
206       * additional GC bookkeeping ("write barriers") that are sometimes
207       * more costly than the writes themselves because of contention).
208       *
209     * Removal of interior nodes (due to timed out or interrupted
210     * waits, or calls to remove or Iterator.remove) uses a scheme
211     * roughly similar to that in Scherer, Lea, and Scott's
212     * SynchronousQueue. Given a predecessor, we can unsplice any node
213     * except the (actual) tail of the queue. To avoid build-up of
214     * cancelled trailing nodes, upon a request to remove a trailing
215     * node, it is placed in field "cleanMe" to be unspliced upon the
216     * next call to unsplice any other node.  Situations needing such
217     * mechanics are not common but do occur in practice; for example
218     * when an unbounded series of short timed calls to poll
219     * repeatedly time out but never otherwise fall off the list
220     * because of an untimed call to take at the front of the
221     * queue. (Note that maintaining field cleanMe does not otherwise
222     * much impact garbage retention even if never cleared by some
223     * other call because the held node will eventually either
224     * directly or indirectly lead to a self-link once off the list.)
225     *
209       * *** Overview of implementation ***
210       *
211 <     * We use a threshold-based approach to updates, with a target
212 <     * slack of two.  The slack value is hard-wired: a path greater
211 >     * We use a threshold-based approach to updates, with a slack
212 >     * threshold of two -- that is, we update head/tail when the
213 >     * current pointer appears to be two or more steps away from the
214 >     * first/last node. The slack value is hard-wired: a path greater
215       * than one is naturally implemented by checking equality of
216       * traversal pointers except when the list has only one element,
217 <     * in which case we keep target slack at one. Avoiding tracking
217 >     * in which case we keep slack threshold at one. Avoiding tracking
218       * explicit counts across method calls slightly simplifies an
219       * already-messy implementation. Using randomization would
220       * probably work better if there were a low-quality dirt-cheap
221       * per-thread one available, but even ThreadLocalRandom is too
222       * heavy for these purposes.
223       *
224 <     * With such a small target slack value, it is rarely worthwhile
225 <     * to augment this with path short-circuiting; i.e., unsplicing
226 <     * nodes between head and the first unmatched node, or similarly
227 <     * for tail, rather than advancing head or tail proper. However,
243 <     * it is used (in awaitMatch) immediately before a waiting thread
244 <     * starts to block, as a final bit of helping at a point when
245 <     * contention with others is extremely unlikely (since if other
246 <     * threads that could release it are operating, then the current
247 <     * thread wouldn't be blocking).
224 >     * With such a small slack threshold value, it is not worthwhile
225 >     * to augment this with path short-circuiting (i.e., unsplicing
226 >     * interior nodes) except in the case of cancellation/removal (see
227 >     * below).
228       *
229       * We allow both the head and tail fields to be null before any
230       * nodes are enqueued; initializing upon first append.  This
# Line 260 | Line 240 | public class LinkedTransferQueue<E> exte
240       * of offer, put, poll, take, or transfer (each possibly with
241       * timeout). The relative complexity of using one monolithic
242       * method outweighs the code bulk and maintenance problems of
243 <     * using nine separate methods.
243 >     * using separate methods for each case.
244       *
245       * Operation consists of up to three phases. The first is
246       * implemented within method xfer, the second in tryAppend, and
# Line 285 | Line 265 | public class LinkedTransferQueue<E> exte
265       *
266       * 2. Try to append a new node (method tryAppend)
267       *
268 <     *    Starting at current tail pointer, try to append a new node
269 <     *    to the list (or if head was null, establish the first
270 <     *    node). Nodes can be appended only if their predecessors are
271 <     *    either already matched or are of the same mode. If we detect
272 <     *    otherwise, then a new node with opposite mode must have been
273 <     *    appended during traversal, so must restart at phase 1. The
274 <     *    traversal and update steps are otherwise similar to phase 1:
275 <     *    Retrying upon CAS misses and checking for staleness.  In
276 <     *    particular, if a self-link is encountered, then we can
277 <     *    safely jump to a node on the list by continuing the
278 <     *    traversal at current head.
268 >     *    Starting at current tail pointer, find the actual last node
269 >     *    and try to append a new node (or if head was null, establish
270 >     *    the first node). Nodes can be appended only if their
271 >     *    predecessors are either already matched or are of the same
272 >     *    mode. If we detect otherwise, then a new node with opposite
273 >     *    mode must have been appended during traversal, so we must
274 >     *    restart at phase 1. The traversal and update steps are
275 >     *    otherwise similar to phase 1: Retrying upon CAS misses and
276 >     *    checking for staleness.  In particular, if a self-link is
277 >     *    encountered, then we can safely jump to a node on the list
278 >     *    by continuing the traversal at current head.
279       *
280       *    On successful append, if the call was ASYNC, return.
281       *
282       * 3. Await match or cancellation (method awaitMatch)
283       *
284       *    Wait for another thread to match node; instead cancelling if
285 <     *    current thread was interrupted or the wait timed out. On
285 >     *    the current thread was interrupted or the wait timed out. On
286       *    multiprocessors, we use front-of-queue spinning: If a node
287       *    appears to be the first unmatched node in the queue, it
288       *    spins a bit before blocking. In either case, before blocking
# Line 317 | Line 297 | public class LinkedTransferQueue<E> exte
297       *    to decide to occasionally perform a Thread.yield. While
298       *    yield has underdefined specs, we assume that might it help,
299       *    and will not hurt in limiting impact of spinning on busy
300 <     *    systems.  We also use much smaller (1/4) spins for nodes
301 <     *    that are not known to be front but whose predecessors have
302 <     *    not blocked -- these "chained" spins avoid artifacts of
300 >     *    systems.  We also use smaller (1/2) spins for nodes that are
301 >     *    not known to be front but whose predecessors have not
302 >     *    blocked -- these "chained" spins avoid artifacts of
303       *    front-of-queue rules which otherwise lead to alternating
304       *    nodes spinning vs blocking. Further, front threads that
305       *    represent phase changes (from data to request node or vice
306       *    versa) compared to their predecessors receive additional
307 <     *    spins, reflecting the longer code path lengths necessary to
308 <     *    release them under contention.
307 >     *    chained spins, reflecting longer paths typically required to
308 >     *    unblock threads during phase changes.
309 >     *
310 >     *
311 >     * ** Unlinking removed interior nodes **
312 >     *
313 >     * In addition to minimizing garbage retention via self-linking
314 >     * described above, we also unlink removed interior nodes. These
315 >     * may arise due to timed out or interrupted waits, or calls to
316 >     * remove(x) or Iterator.remove.  Normally, given a node that was
317 >     * at one time known to be the predecessor of some node s that is
318 >     * to be removed, we can unsplice s by CASing the next field of
319 >     * its predecessor if it still points to s (otherwise s must
320 >     * already have been removed or is now offlist). But there are two
321 >     * situations in which we cannot guarantee to make node s
322 >     * unreachable in this way: (1) If s is the trailing node of list
323 >     * (i.e., with null next), then it is pinned as the target node
324 >     * for appends, so can only be removed later when other nodes are
325 >     * appended. (2) We cannot necessarily unlink s given a
326 >     * predecessor node that is matched (including the case of being
327 >     * cancelled): the predecessor may already be unspliced, in which
328 >     * case some previous reachable node may still point to s.
329 >     * (For further explanation see Herlihy & Shavit "The Art of
330 >     * Multiprocessor Programming" chapter 9).  Although, in both
331 >     * cases, we can rule out the need for further action if either s
332 >     * or its predecessor are (or can be made to be) at, or fall off
333 >     * from, the head of list.
334 >     *
335 >     * Without taking these into account, it would be possible for an
336 >     * unbounded number of supposedly removed nodes to remain
337 >     * reachable.  Situations leading to such buildup are uncommon but
338 >     * can occur in practice; for example when a series of short timed
339 >     * calls to poll repeatedly time out but never otherwise fall off
340 >     * the list because of an untimed call to take at the front of the
341 >     * queue.
342 >     *
343 >     * When these cases arise, rather than always retraversing the
344 >     * entire list to find an actual predecessor to unlink (which
345 >     * won't help for case (1) anyway), we record a conservative
346 >     * estimate of possible unsplice failures (in "sweepVotes").  We
347 >     * trigger a full sweep when the estimate exceeds a threshold
348 >     * indicating the maximum number of estimated removal failures to
349 >     * tolerate before sweeping through, unlinking cancelled nodes
350 >     * that were not unlinked upon initial removal. We perform sweeps
351 >     * by the thread hitting threshold (rather than background threads
352 >     * or by spreading work to other threads) because in the main
353 >     * contexts in which removal occurs, the caller is already
354 >     * timed-out, cancelled, or performing a potentially O(n)
355 >     * operation (i.e., remove(x)), none of which are time-critical
356 >     * enough to warrant the overhead that alternatives would impose
357 >     * on other threads.
358 >     *
359 >     * Because the sweepVotes estimate is conservative, and because
360 >     * nodes become unlinked "naturally" as they fall off the head of
361 >     * the queue, and because we allow votes to accumulate even while
362 >     * sweeps are in progress, there are typically significantly fewer
363 >     * such nodes than estimated.  Choice of a threshold value
364 >     * balances the likelihood of wasted effort and contention, versus
365 >     * providing a worst-case bound on retention of interior nodes in
366 >     * quiescent queues. The value defined below was chosen
367 >     * empirically to balance these under various timeout scenarios.
368 >     *
369 >     * Note that we cannot self-link unlinked interior nodes during
370 >     * sweeps. However, the associated garbage chains terminate when
371 >     * some successor ultimately falls off the head of the list and is
372 >     * self-linked.
373       */
374  
375      /** True if on multiprocessor */
# Line 333 | Line 377 | public class LinkedTransferQueue<E> exte
377          Runtime.getRuntime().availableProcessors() > 1;
378  
379      /**
380 <     * The number of times to spin (with on average one randomly
381 <     * interspersed call to Thread.yield) on multiprocessor before
382 <     * blocking when a node is apparently the first waiter in the
383 <     * queue.  See above for explanation. Must be a power of two. The
384 <     * value is empirically derived -- it works pretty well across a
385 <     * variety of processors, numbers of CPUs, and OSes.
380 >     * The number of times to spin (with randomly interspersed calls
381 >     * to Thread.yield) on multiprocessor before blocking when a node
382 >     * is apparently the first waiter in the queue.  See above for
383 >     * explanation. Must be a power of two. The value is empirically
384 >     * derived -- it works pretty well across a variety of processors,
385 >     * numbers of CPUs, and OSes.
386       */
387      private static final int FRONT_SPINS   = 1 << 7;
388  
389      /**
390       * The number of times to spin before blocking when a node is
391 <     * preceded by another node that is apparently spinning.
391 >     * preceded by another node that is apparently spinning.  Also
392 >     * serves as an increment to FRONT_SPINS on phase changes, and as
393 >     * base average frequency for yielding during spins. Must be a
394 >     * power of two.
395       */
396 <    private static final int CHAINED_SPINS = FRONT_SPINS >>> 2;
396 >    private static final int CHAINED_SPINS = FRONT_SPINS >>> 1;
397 >
398 >    /**
399 >     * The maximum number of estimated removal failures (sweepVotes)
400 >     * to tolerate before sweeping through the queue unlinking
401 >     * cancelled nodes that were not unlinked upon initial
402 >     * removal. See above for explanation. The value must be at least
403 >     * two to avoid useless sweeps when removing trailing nodes.
404 >     */
405 >    static final int SWEEP_THRESHOLD = 32;
406  
407      /**
408       * Queue nodes. Uses Object, not E, for items to allow forgetting
409       * them after use.  Relies heavily on Unsafe mechanics to minimize
410 <     * unnecessary ordering constraints: Writes that intrinsically
411 <     * precede or follow CASes use simple relaxed forms.  Other
356 <     * cleanups use releasing/lazy writes.
410 >     * unnecessary ordering constraints: Writes that are intrinsically
411 >     * ordered wrt other accesses or CASes use simple relaxed forms.
412       */
413      static final class Node {
414          final boolean isData;   // false if this is a request node
# Line 367 | Line 422 | public class LinkedTransferQueue<E> exte
422          }
423  
424          final boolean casItem(Object cmp, Object val) {
425 +            assert cmp == null || cmp.getClass() != Node.class;
426              return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val);
427          }
428  
# Line 388 | Line 444 | public class LinkedTransferQueue<E> exte
444          }
445  
446          /**
447 <         * Sets item to self (using a releasing/lazy write) and waiter
448 <         * to null, to avoid garbage retention after extracting or
449 <         * cancelling.
447 >         * Sets item to self and waiter to null, to avoid garbage
448 >         * retention after matching or cancelling. Uses relaxed writes
449 >         * bacause order is already constrained in the only calling
450 >         * contexts: item is forgotten only after volatile/atomic
451 >         * mechanics that extract items.  Similarly, clearing waiter
452 >         * follows either CAS or return from park (if ever parked;
453 >         * else we don't care).
454           */
455          final void forgetContents() {
456 <            UNSAFE.putOrderedObject(this, itemOffset, this);
457 <            UNSAFE.putOrderedObject(this, waiterOffset, null);
456 >            UNSAFE.putObject(this, itemOffset, this);
457 >            UNSAFE.putObject(this, waiterOffset, null);
458          }
459  
460          /**
# Line 403 | Line 463 | public class LinkedTransferQueue<E> exte
463           */
464          final boolean isMatched() {
465              Object x = item;
466 <            return x == this || (x != null) != isData;
466 >            return (x == this) || ((x == null) == isData);
467 >        }
468 >
469 >        /**
470 >         * Returns true if this is an unmatched request node.
471 >         */
472 >        final boolean isUnmatchedRequest() {
473 >            return !isData && item == null;
474          }
475  
476          /**
# Line 421 | Line 488 | public class LinkedTransferQueue<E> exte
488           * Tries to artificially match a data node -- used by remove.
489           */
490          final boolean tryMatchData() {
491 +            assert isData;
492              Object x = item;
493              if (x != null && x != this && casItem(x, null)) {
494                  LockSupport.unpark(waiter);
# Line 442 | Line 510 | public class LinkedTransferQueue<E> exte
510      }
511  
512      /** head of the queue; null until first enqueue */
513 <    private transient volatile Node head;
446 <
447 <    /** predecessor of dangling unspliceable node */
448 <    private transient volatile Node cleanMe; // decl here to reduce contention
513 >    transient volatile Node head;
514  
515      /** tail of the queue; null until first append */
516      private transient volatile Node tail;
517  
518 +    /** The number of apparent failures to unsplice removed nodes */
519 +    private transient volatile int sweepVotes;
520 +
521      // CAS methods for fields
522      private boolean casTail(Node cmp, Node val) {
523          return UNSAFE.compareAndSwapObject(this, tailOffset, cmp, val);
# Line 459 | Line 527 | public class LinkedTransferQueue<E> exte
527          return UNSAFE.compareAndSwapObject(this, headOffset, cmp, val);
528      }
529  
530 <    private boolean casCleanMe(Node cmp, Node val) {
531 <        return UNSAFE.compareAndSwapObject(this, cleanMeOffset, cmp, val);
530 >    private boolean casSweepVotes(int cmp, int val) {
531 >        return UNSAFE.compareAndSwapInt(this, sweepVotesOffset, cmp, val);
532      }
533  
534      /*
535 <     * Possible values for "how" argument in xfer method. Beware that
468 <     * the order of assigned numerical values matters.
535 >     * Possible values for "how" argument in xfer method.
536       */
537 <    private static final int NOW     = 0; // for untimed poll, tryTransfer
538 <    private static final int ASYNC   = 1; // for offer, put, add
539 <    private static final int SYNC    = 2; // for transfer, take
540 <    private static final int TIMEOUT = 3; // for timed poll, tryTransfer
537 >    private static final int NOW   = 0; // for untimed poll, tryTransfer
538 >    private static final int ASYNC = 1; // for offer, put, add
539 >    private static final int SYNC  = 2; // for transfer, take
540 >    private static final int TIMED = 3; // for timed poll, tryTransfer
541 >
542 >    @SuppressWarnings("unchecked")
543 >    static <E> E cast(Object item) {
544 >        assert item == null || item.getClass() != Node.class;
545 >        return (E) item;
546 >    }
547  
548      /**
549       * Implements all queuing methods. See above for explanation.
550       *
551       * @param e the item or null for take
552       * @param haveData true if this is a put, else a take
553 <     * @param how NOW, ASYNC, SYNC, or TIMEOUT
554 <     * @param nanos timeout in nanosecs, used only if mode is TIMEOUT
553 >     * @param how NOW, ASYNC, SYNC, or TIMED
554 >     * @param nanos timeout in nanosecs, used only if mode is TIMED
555       * @return an item if matched, else e
556       * @throws NullPointerException if haveData mode but e is null
557       */
558 <    private Object xfer(Object e, boolean haveData, int how, long nanos) {
558 >    private E xfer(E e, boolean haveData, int how, long nanos) {
559          if (haveData && (e == null))
560              throw new NullPointerException();
561          Node s = null;                        // the node to append, if needed
# Line 496 | Line 569 | public class LinkedTransferQueue<E> exte
569                      if (isData == haveData)   // can't match
570                          break;
571                      if (p.casItem(item, e)) { // match
572 <                        Thread w = p.waiter;
573 <                        while (p != h) {      // update head
574 <                            Node n = p.next;  // by 2 unless singleton
502 <                            if (n != null)
503 <                                p = n;
504 <                            if (head == h && casHead(h, p)) {
572 >                        for (Node q = p; q != h;) {
573 >                            Node n = q.next;  // update by 2 unless singleton
574 >                            if (head == h && casHead(h, n == null? q : n)) {
575                                  h.forgetNext();
576                                  break;
577                              }                 // advance and retry
578                              if ((h = head)   == null ||
579 <                                (p = h.next) == null || !p.isMatched())
579 >                                (q = h.next) == null || !q.isMatched())
580                                  break;        // unless slack < 2
581                          }
582 <                        LockSupport.unpark(w);
583 <                        return item;
582 >                        LockSupport.unpark(p.waiter);
583 >                        return this.<E>cast(item);
584                      }
585                  }
586                  Node n = p.next;
587                  p = (p != n) ? n : (h = head); // Use head if p offlist
588              }
589  
590 <            if (how >= ASYNC) {               // No matches available
590 >            if (how != NOW) {                 // No matches available
591                  if (s == null)
592                      s = new Node(e, haveData);
593                  Node pred = tryAppend(s, haveData);
594                  if (pred == null)
595                      continue retry;           // lost race vs opposite mode
596 <                if (how >= SYNC)
597 <                    return awaitMatch(pred, s, e, how, nanos);
596 >                if (how != ASYNC)
597 >                    return awaitMatch(s, pred, e, (how == TIMED), nanos);
598              }
599              return e; // not waiting
600          }
# Line 540 | Line 610 | public class LinkedTransferQueue<E> exte
610       * predecessor
611       */
612      private Node tryAppend(Node s, boolean haveData) {
613 <        for (Node t = tail, p = t;;) { // move p to last node and append
613 >        for (Node t = tail, p = t;;) {        // move p to last node and append
614              Node n, u;                        // temps for reads of next & tail
615              if (p == null && (p = head) == null) {
616                  if (casHead(null, s))
# Line 568 | Line 638 | public class LinkedTransferQueue<E> exte
638      /**
639       * Spins/yields/blocks until node s is matched or caller gives up.
640       *
571     * @param pred the predecessor of s, or s or null if none
641       * @param s the waiting node
642 +     * @param pred the predecessor of s, or s itself if it has no
643 +     * predecessor, or null if unknown (the null case does not occur
644 +     * in any current calls but may in possible future extensions)
645       * @param e the comparison value for checking match
646 <     * @param how either SYNC or TIMEOUT
647 <     * @param nanos timeout value
646 >     * @param timed if true, wait only until timeout elapses
647 >     * @param nanos timeout in nanosecs, used only if timed is true
648       * @return matched item, or e if unmatched on interrupt or timeout
649       */
650 <    private Object awaitMatch(Node pred, Node s, Object e,
651 <                              int how, long nanos) {
580 <        long lastTime = (how == TIMEOUT) ? System.nanoTime() : 0L;
650 >    private E awaitMatch(Node s, Node pred, E e, boolean timed, long nanos) {
651 >        long lastTime = timed ? System.nanoTime() : 0L;
652          Thread w = Thread.currentThread();
653          int spins = -1; // initialized after first item and cancel checks
654          ThreadLocalRandom randomYields = null; // bound if needed
# Line 585 | Line 656 | public class LinkedTransferQueue<E> exte
656          for (;;) {
657              Object item = s.item;
658              if (item != e) {                  // matched
659 +                assert item != s;
660                  s.forgetContents();           // avoid garbage
661 <                return item;
661 >                return this.<E>cast(item);
662              }
663 <            if ((w.isInterrupted() || (how == TIMEOUT && nanos <= 0)) &&
664 <                     s.casItem(e, s)) {       // cancel
663 >            if ((w.isInterrupted() || (timed && nanos <= 0)) &&
664 >                    s.casItem(e, s)) {        // cancel
665                  unsplice(pred, s);
666                  return e;
667              }
# Line 598 | Line 670 | public class LinkedTransferQueue<E> exte
670                  if ((spins = spinsFor(pred, s.isData)) > 0)
671                      randomYields = ThreadLocalRandom.current();
672              }
673 <            else if (spins > 0) {             // spin, occasionally yield
602 <                if (randomYields.nextInt(FRONT_SPINS) == 0)
603 <                    Thread.yield();
673 >            else if (spins > 0) {             // spin
674                  --spins;
675 +                if (randomYields.nextInt(CHAINED_SPINS) == 0)
676 +                    Thread.yield();           // occasionally yield
677              }
678              else if (s.waiter == null) {
679 <                shortenHeadPath();            // reduce slack before blocking
608 <                s.waiter = w;                 // request unpark
679 >                s.waiter = w;                 // request unpark then recheck
680              }
681 <            else if (how == TIMEOUT) {
681 >            else if (timed) {
682                  long now = System.nanoTime();
683                  if ((nanos -= now - lastTime) > 0)
684                      LockSupport.parkNanos(this, nanos);
# Line 615 | Line 686 | public class LinkedTransferQueue<E> exte
686              }
687              else {
688                  LockSupport.park(this);
618                spins = -1;                   // spin if front upon wakeup
689              }
690          }
691      }
# Line 626 | Line 696 | public class LinkedTransferQueue<E> exte
696       */
697      private static int spinsFor(Node pred, boolean haveData) {
698          if (MP && pred != null) {
699 <            boolean predData = pred.isData;
700 <            if (predData != haveData)         // front and phase change
701 <                return FRONT_SPINS + (FRONT_SPINS >>> 1);
632 <            if (predData != (pred.item != null)) // probably at front
699 >            if (pred.isData != haveData)      // phase change
700 >                return FRONT_SPINS + CHAINED_SPINS;
701 >            if (pred.isMatched())             // probably at front
702                  return FRONT_SPINS;
703              if (pred.waiter == null)          // pred apparently spinning
704                  return CHAINED_SPINS;
# Line 637 | Line 706 | public class LinkedTransferQueue<E> exte
706          return 0;
707      }
708  
709 +    /* -------------- Traversal methods -------------- */
710 +
711      /**
712 <     * Tries (once) to unsplice nodes between head and first unmatched
713 <     * or trailing node; failing on contention.
714 <     */
715 <    private void shortenHeadPath() {
716 <        Node h, hn, p, q;
717 <        if ((p = h = head) != null && h.isMatched() &&
718 <            (q = hn = h.next) != null) {
648 <            Node n;
649 <            while ((n = q.next) != q) {
650 <                if (n == null || !q.isMatched()) {
651 <                    if (hn != q && h.next == hn)
652 <                        h.casNext(hn, q);
653 <                    break;
654 <                }
655 <                p = q;
656 <                q = n;
657 <            }
658 <        }
712 >     * Returns the successor of p, or the head node if p.next has been
713 >     * linked to self, which will only be true if traversing with a
714 >     * stale pointer that is now off the list.
715 >     */
716 >    final Node succ(Node p) {
717 >        Node next = p.next;
718 >        return (p == next) ? head : next;
719      }
720  
661    /* -------------- Traversal methods -------------- */
662
721      /**
722       * Returns the first unmatched node of the given mode, or null if
723       * none.  Used by methods isEmpty, hasWaitingConsumer.
724       */
725 <    private Node firstOfMode(boolean data) {
726 <        for (Node p = head; p != null; ) {
725 >    private Node firstOfMode(boolean isData) {
726 >        for (Node p = head; p != null; p = succ(p)) {
727              if (!p.isMatched())
728 <                return (p.isData == data) ? p : null;
671 <            Node n = p.next;
672 <            p = (n != p) ? n : head;
728 >                return (p.isData == isData) ? p : null;
729          }
730          return null;
731      }
732  
733      /**
734       * Returns the item in the first unmatched node with isData; or
735 <     * null if none. Used by peek.
735 >     * null if none.  Used by peek.
736       */
737 <    private Object firstDataItem() {
738 <        for (Node p = head; p != null; ) {
683 <            boolean isData = p.isData;
737 >    private E firstDataItem() {
738 >        for (Node p = head; p != null; p = succ(p)) {
739              Object item = p.item;
740 <            if (item != p && (item != null) == isData)
741 <                return isData ? item : null;
742 <            Node n = p.next;
743 <            p = (n != p) ? n : head;
740 >            if (p.isData) {
741 >                if (item != null && item != p)
742 >                    return this.<E>cast(item);
743 >            }
744 >            else if (item == null)
745 >                return null;
746          }
747          return null;
748      }
# Line 716 | Line 773 | public class LinkedTransferQueue<E> exte
773  
774      final class Itr implements Iterator<E> {
775          private Node nextNode;   // next node to return item for
776 <        private Object nextItem; // the corresponding item
776 >        private E nextItem;      // the corresponding item
777          private Node lastRet;    // last returned node, to support remove
778 +        private Node lastPred;   // predecessor to unlink lastRet
779  
780          /**
781           * Moves to next node after prev, or first node if prev null.
782           */
783          private void advance(Node prev) {
784 +            lastPred = lastRet;
785              lastRet = prev;
786 <            Node p;
787 <            if (prev == null || (p = prev.next) == prev)
729 <                p = head;
730 <            while (p != null) {
786 >            for (Node p = (prev == null) ? head : succ(prev);
787 >                 p != null; p = succ(p)) {
788                  Object item = p.item;
789                  if (p.isData) {
790                      if (item != null && item != p) {
791 <                        nextItem = item;
791 >                        nextItem = LinkedTransferQueue.this.<E>cast(item);
792                          nextNode = p;
793                          return;
794                      }
795                  }
796                  else if (item == null)
797                      break;
741                Node n = p.next;
742                p = (n != p) ? n : head;
798              }
799              nextNode = null;
800          }
# Line 755 | Line 810 | public class LinkedTransferQueue<E> exte
810          public final E next() {
811              Node p = nextNode;
812              if (p == null) throw new NoSuchElementException();
813 <            Object e = nextItem;
813 >            E e = nextItem;
814              advance(p);
815 <            return (E) e;
815 >            return e;
816          }
817  
818          public final void remove() {
819              Node p = lastRet;
820              if (p == null) throw new IllegalStateException();
821 <            lastRet = null;
822 <            findAndRemoveNode(p);
821 >            if (p.tryMatchData())
822 >                unsplice(lastPred, p);
823          }
824      }
825  
# Line 774 | Line 829 | public class LinkedTransferQueue<E> exte
829       * Unsplices (now or later) the given deleted/cancelled node with
830       * the given predecessor.
831       *
832 <     * @param pred predecessor of node to be unspliced
832 >     * @param pred a node that was at one time known to be the
833 >     * predecessor of s, or null or s itself if s is/was at head
834       * @param s the node to be unspliced
835       */
836 <    private void unsplice(Node pred, Node s) {
837 <        s.forgetContents(); // clear unneeded fields
836 >    final void unsplice(Node pred, Node s) {
837 >        s.forgetContents(); // forget unneeded fields
838          /*
839 <         * At any given time, exactly one node on list cannot be
840 <         * unlinked -- the last inserted node. To accommodate this, if
841 <         * we cannot unlink s, we save its predecessor as "cleanMe",
842 <         * processing the previously saved version first. Because only
843 <         * one node in the list can have a null next, at least one of
788 <         * node s or the node previously saved can always be
789 <         * processed, so this always terminates.
839 >         * See above for rationale. Briefly: if pred still points to
840 >         * s, try to unlink s.  If s cannot be unlinked, because it is
841 >         * trailing node or pred might be unlinked, and neither pred
842 >         * nor s are head or offlist, add to sweepVotes, and if enough
843 >         * votes have accumulated, sweep.
844           */
845 <        if (pred != null && pred != s) {
846 <            while (pred.next == s) {
847 <                Node oldpred = (cleanMe == null) ? null : reclean();
848 <                Node n = s.next;
849 <                if (n != null) {
850 <                    if (n != s)
851 <                        pred.casNext(s, n);
852 <                    break;
845 >        if (pred != null && pred != s && pred.next == s) {
846 >            Node n = s.next;
847 >            if (n == null ||
848 >                (n != s && pred.casNext(s, n) && pred.isMatched())) {
849 >                for (;;) {               // check if at, or could be, head
850 >                    Node h = head;
851 >                    if (h == pred || h == s || h == null)
852 >                        return;          // at head or list empty
853 >                    if (!h.isMatched())
854 >                        break;
855 >                    Node hn = h.next;
856 >                    if (hn == null)
857 >                        return;          // now empty
858 >                    if (hn != h && casHead(h, hn))
859 >                        h.forgetNext();  // advance head
860 >                }
861 >                if (pred.next != pred && s.next != s) { // recheck if offlist
862 >                    for (;;) {           // sweep now if enough votes
863 >                        int v = sweepVotes;
864 >                        if (v < SWEEP_THRESHOLD) {
865 >                            if (casSweepVotes(v, v + 1))
866 >                                break;
867 >                        }
868 >                        else if (casSweepVotes(v, 0)) {
869 >                            sweep();
870 >                            break;
871 >                        }
872 >                    }
873                  }
800                if (oldpred == pred ||      // Already saved
801                    (oldpred == null && casCleanMe(null, pred)))
802                    break;                  // Postpone cleaning
874              }
875          }
876      }
877  
878      /**
879 <     * Tries to unsplice the deleted/cancelled node held in cleanMe
809 <     * that was previously uncleanable because it was at tail.
810 <     *
811 <     * @return current cleanMe node (or null)
879 >     * Unlinks matched nodes encountered in a traversal from head.
880       */
881 <    private Node reclean() {
882 <        /*
883 <         * cleanMe is, or at one time was, predecessor of a cancelled
884 <         * node s that was the tail so could not be unspliced.  If it
885 <         * is no longer the tail, try to unsplice if necessary and
886 <         * make cleanMe slot available.  This differs from similar
887 <         * code in unsplice() because we must check that pred still
820 <         * points to a matched node that can be unspliced -- if not,
821 <         * we can (must) clear cleanMe without unsplicing.  This can
822 <         * loop only due to contention.
823 <         */
824 <        Node pred;
825 <        while ((pred = cleanMe) != null) {
826 <            Node s = pred.next;
827 <            Node n;
828 <            if (s == null || s == pred || !s.isMatched())
829 <                casCleanMe(pred, null); // already gone
830 <            else if ((n = s.next) != null) {
831 <                if (n != s)
832 <                    pred.casNext(s, n);
833 <                casCleanMe(pred, null);
834 <            }
835 <            else
881 >    private void sweep() {
882 >        for (Node p = head, s, n; p != null && (s = p.next) != null; ) {
883 >            if (p == s)                    // stale
884 >                p = head;
885 >            else if (!s.isMatched())
886 >                p = s;
887 >            else if ((n = s.next) == null) // trailing node is pinned
888                  break;
889 <        }
890 <        return pred;
839 <    }
840 <
841 <    /**
842 <     * Main implementation of Iterator.remove(). Find
843 <     * and unsplice the given node.
844 <     */
845 <    final void findAndRemoveNode(Node s) {
846 <        if (s.tryMatchData()) {
847 <            Node pred = null;
848 <            Node p = head;
849 <            while (p != null) {
850 <                if (p == s) {
851 <                    unsplice(pred, p);
852 <                    break;
853 <                }
854 <                if (!p.isData && !p.isMatched())
855 <                    break;
856 <                pred = p;
857 <                if ((p = p.next) == pred) { // stale
858 <                    pred = null;
859 <                    p = head;
860 <                }
861 <            }
889 >            else
890 >                p.casNext(s, n);
891          }
892      }
893  
# Line 867 | Line 896 | public class LinkedTransferQueue<E> exte
896       */
897      private boolean findAndRemove(Object e) {
898          if (e != null) {
899 <            Node pred = null;
871 <            Node p = head;
872 <            while (p != null) {
899 >            for (Node pred = null, p = head; p != null; ) {
900                  Object item = p.item;
901                  if (p.isData) {
902                      if (item != null && item != p && e.equals(item) &&
# Line 881 | Line 908 | public class LinkedTransferQueue<E> exte
908                  else if (item == null)
909                      break;
910                  pred = p;
911 <                if ((p = p.next) == pred) {
911 >                if ((p = p.next) == pred) { // stale
912                      pred = null;
913                      p = head;
914                  }
# Line 1009 | Line 1036 | public class LinkedTransferQueue<E> exte
1036       */
1037      public boolean tryTransfer(E e, long timeout, TimeUnit unit)
1038          throws InterruptedException {
1039 <        if (xfer(e, true, TIMEOUT, unit.toNanos(timeout)) == null)
1039 >        if (xfer(e, true, TIMED, unit.toNanos(timeout)) == null)
1040              return true;
1041          if (!Thread.interrupted())
1042              return false;
# Line 1017 | Line 1044 | public class LinkedTransferQueue<E> exte
1044      }
1045  
1046      public E take() throws InterruptedException {
1047 <        Object e = xfer(null, false, SYNC, 0);
1047 >        E e = xfer(null, false, SYNC, 0);
1048          if (e != null)
1049 <            return (E)e;
1049 >            return e;
1050          Thread.interrupted();
1051          throw new InterruptedException();
1052      }
1053  
1054      public E poll(long timeout, TimeUnit unit) throws InterruptedException {
1055 <        Object e = xfer(null, false, TIMEOUT, unit.toNanos(timeout));
1055 >        E e = xfer(null, false, TIMED, unit.toNanos(timeout));
1056          if (e != null || !Thread.interrupted())
1057 <            return (E)e;
1057 >            return e;
1058          throw new InterruptedException();
1059      }
1060  
1061      public E poll() {
1062 <        return (E)xfer(null, false, NOW, 0);
1062 >        return xfer(null, false, NOW, 0);
1063      }
1064  
1065      /**
# Line 1089 | Line 1116 | public class LinkedTransferQueue<E> exte
1116      }
1117  
1118      public E peek() {
1119 <        return (E) firstDataItem();
1119 >        return firstDataItem();
1120      }
1121  
1122      /**
# Line 1185 | Line 1212 | public class LinkedTransferQueue<E> exte
1212          }
1213      }
1214  
1188
1215      // Unsafe mechanics
1216  
1217      private static final sun.misc.Unsafe UNSAFE = getUnsafe();
# Line 1193 | Line 1219 | public class LinkedTransferQueue<E> exte
1219          objectFieldOffset(UNSAFE, "head", LinkedTransferQueue.class);
1220      private static final long tailOffset =
1221          objectFieldOffset(UNSAFE, "tail", LinkedTransferQueue.class);
1222 <    private static final long cleanMeOffset =
1223 <        objectFieldOffset(UNSAFE, "cleanMe", LinkedTransferQueue.class);
1222 >    private static final long sweepVotesOffset =
1223 >        objectFieldOffset(UNSAFE, "sweepVotes", LinkedTransferQueue.class);
1224  
1225      static long objectFieldOffset(sun.misc.Unsafe UNSAFE,
1226                                    String field, Class<?> klazz) {
# Line 1208 | Line 1234 | public class LinkedTransferQueue<E> exte
1234          }
1235      }
1236  
1237 <    private static sun.misc.Unsafe getUnsafe() {
1237 >    /**
1238 >     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
1239 >     * Replace with a simple call to Unsafe.getUnsafe when integrating
1240 >     * into a jdk.
1241 >     *
1242 >     * @return a sun.misc.Unsafe
1243 >     */
1244 >    static sun.misc.Unsafe getUnsafe() {
1245          try {
1246              return sun.misc.Unsafe.getUnsafe();
1247          } catch (SecurityException se) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines