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.62 by jsr166, Mon Nov 2 03:01:10 2009 UTC vs.
Revision 1.68 by jsr166, Sun Nov 15 01:53:11 2009 UTC

# Line 206 | Line 206 | public class LinkedTransferQueue<E> exte
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(x) or Iterator.remove) can use a
211     * scheme roughly similar to that described in Scherer, Lea, and
212     * Scott's SynchronousQueue. Given a predecessor, we can unsplice
213     * any node except the (actual) tail of the queue. To avoid
214     * build-up of cancelled trailing nodes, upon a request to remove
215     * a trailing node, it is placed in field "cleanMe" to be
216     * unspliced upon the next call to unsplice any other node.
217     * Situations needing such mechanics are not common but do occur
218     * in practice; for example when an unbounded series of short
219     * timed calls to poll repeatedly time out but never otherwise
220     * fall off the list because of an untimed call to take at the
221     * front of the queue. Note that maintaining field cleanMe does
222     * not otherwise much impact garbage retention even if never
223     * cleared by some other call because the held node will
224     * eventually either directly or indirectly lead to a self-link
225     * once off the list.
226     *
209       * *** Overview of implementation ***
210       *
211       * We use a threshold-based approach to updates, with a slack
# Line 239 | Line 221 | public class LinkedTransferQueue<E> exte
221       * per-thread one available, but even ThreadLocalRandom is too
222       * heavy for these purposes.
223       *
224 <     * With such a small slack threshold value, it is rarely
225 <     * worthwhile to augment this with path short-circuiting; i.e.,
226 <     * unsplicing nodes between head and the first unmatched node, or
227 <     * similarly for tail, rather than advancing head or tail
246 <     * proper. However, it is used (in awaitMatch) immediately before
247 <     * a waiting thread starts to block, as a final bit of helping at
248 <     * a point when contention with others is extremely unlikely
249 <     * (since if other threads that could release it are operating,
250 <     * then the current 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 329 | Line 306 | public class LinkedTransferQueue<E> exte
306       *    versa) compared to their predecessors receive additional
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 355 | Line 396 | public class LinkedTransferQueue<E> exte
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
362 <     * 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 395 | 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 459 | Line 512 | public class LinkedTransferQueue<E> exte
512      /** head of the queue; null until first enqueue */
513      transient volatile Node head;
514  
462    /** predecessor of dangling unspliceable node */
463    private transient volatile Node cleanMe; // decl here reduces contention
464
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 474 | 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
483 <     * 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) {
# Line 498 | Line 550 | public class LinkedTransferQueue<E> exte
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       */
# Line 518 | Line 570 | public class LinkedTransferQueue<E> exte
570                          break;
571                      if (p.casItem(item, e)) { // match
572                          for (Node q = p; q != h;) {
573 <                            Node n = q.next;  // update head by 2
574 <                            if (n != null)    // unless singleton
523 <                                q = n;
524 <                            if (head == h && casHead(h, q)) {
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
# Line 537 | Line 587 | public class LinkedTransferQueue<E> exte
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(s, pred, e, how, nanos);
596 >                if (how != ASYNC)
597 >                    return awaitMatch(s, pred, e, (how == TIMED), nanos);
598              }
599              return e; // not waiting
600          }
# Line 593 | Line 643 | public class LinkedTransferQueue<E> exte
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 E awaitMatch(Node s, Node pred, E e, int how, long nanos) {
651 <        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 610 | Line 660 | public class LinkedTransferQueue<E> exte
660                  s.forgetContents();           // avoid garbage
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 621 | Line 671 | public class LinkedTransferQueue<E> exte
671                      randomYields = ThreadLocalRandom.current();
672              }
673              else if (spins > 0) {             // spin
674 <                if (--spins == 0)
675 <                    shortenHeadPath();        // reduce slack before blocking
626 <                else if (randomYields.nextInt(CHAINED_SPINS) == 0)
674 >                --spins;
675 >                if (randomYields.nextInt(CHAINED_SPINS) == 0)
676                      Thread.yield();           // occasionally yield
677              }
678              else if (s.waiter == null) {
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 637 | Line 686 | public class LinkedTransferQueue<E> exte
686              }
687              else {
688                  LockSupport.park(this);
640                s.waiter = null;
641                spins = -1;                   // spin if front upon wakeup
689              }
690          }
691      }
# Line 659 | Line 706 | public class LinkedTransferQueue<E> exte
706          return 0;
707      }
708  
662    /**
663     * Tries (once) to unsplice nodes between head and first unmatched
664     * or trailing node; failing on contention.
665     */
666    private void shortenHeadPath() {
667        Node h, hn, p, q;
668        if ((p = h = head) != null && h.isMatched() &&
669            (q = hn = h.next) != null) {
670            Node n;
671            while ((n = q.next) != q) {
672                if (n == null || !q.isMatched()) {
673                    if (hn != q && h.next == hn)
674                        h.casNext(hn, q);
675                    break;
676                }
677                p = q;
678                q = n;
679            }
680        }
681    }
682
709      /* -------------- Traversal methods -------------- */
710  
711      /**
# Line 792 | Line 818 | public class LinkedTransferQueue<E> exte
818          public final void remove() {
819              Node p = lastRet;
820              if (p == null) throw new IllegalStateException();
821 <            findAndRemoveDataNode(lastPred, p);
821 >            if (p.tryMatchData())
822 >                unsplice(lastPred, p);
823          }
824      }
825  
# Line 802 | 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
816 <         * node s or the node previously saved can always be
817 <         * 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 (oldpred == pred ||      // Already saved
862 <                    ((oldpred == null || oldpred.next == s) &&
863 <                     casCleanMe(oldpred, pred))) {
864 <                    break;
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                  }
874              }
875          }
876      }
877  
878      /**
879 <     * Tries to unsplice the deleted/cancelled node held in cleanMe
839 <     * that was previously uncleanable because it was at tail.
840 <     *
841 <     * @return current cleanMe node (or null)
879 >     * Unlink 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
850 <         * points to a matched node that can be unspliced -- if not,
851 <         * we can (must) clear cleanMe without unsplicing.  This can
852 <         * loop only due to contention.
853 <         */
854 <        Node pred;
855 <        while ((pred = cleanMe) != null) {
856 <            Node s = pred.next;
857 <            Node n;
858 <            if (s == null || s == pred || !s.isMatched())
859 <                casCleanMe(pred, null); // already gone
860 <            else if ((n = s.next) != null) {
861 <                if (n != s)
862 <                    pred.casNext(s, n);
863 <                casCleanMe(pred, null);
864 <            }
881 >    private void sweep() {
882 >        Node p = head, s, n;
883 >        while (p != null && (s = p.next) != null && (n = s.next) != null) {
884 >            if (p == s || s == n)
885 >                p = head; // stale
886 >            else if (s.isMatched())
887 >                p.casNext(s, n);
888              else
889 <                break;
867 <        }
868 <        return pred;
869 <    }
870 <
871 <    /**
872 <     * Main implementation of Iterator.remove(). Find
873 <     * and unsplice the given data node.
874 <     * @param possiblePred possible predecessor of s
875 <     * @param s the node to remove
876 <     */
877 <    final void findAndRemoveDataNode(Node possiblePred, Node s) {
878 <        assert s.isData;
879 <        if (s.tryMatchData()) {
880 <            if (possiblePred != null && possiblePred.next == s)
881 <                unsplice(possiblePred, s); // was actual predecessor
882 <            else {
883 <                for (Node pred = null, p = head; p != null; ) {
884 <                    if (p == s) {
885 <                        unsplice(pred, p);
886 <                        break;
887 <                    }
888 <                    if (p.isUnmatchedRequest())
889 <                        break;
890 <                    pred = p;
891 <                    if ((p = p.next) == pred) { // stale
892 <                        pred = null;
893 <                        p = head;
894 <                    }
895 <                }
896 <            }
889 >                p = s;
890          }
891      }
892  
# Line 1042 | Line 1035 | public class LinkedTransferQueue<E> exte
1035       */
1036      public boolean tryTransfer(E e, long timeout, TimeUnit unit)
1037          throws InterruptedException {
1038 <        if (xfer(e, true, TIMEOUT, unit.toNanos(timeout)) == null)
1038 >        if (xfer(e, true, TIMED, unit.toNanos(timeout)) == null)
1039              return true;
1040          if (!Thread.interrupted())
1041              return false;
# Line 1058 | Line 1051 | public class LinkedTransferQueue<E> exte
1051      }
1052  
1053      public E poll(long timeout, TimeUnit unit) throws InterruptedException {
1054 <        E e = xfer(null, false, TIMEOUT, unit.toNanos(timeout));
1054 >        E e = xfer(null, false, TIMED, unit.toNanos(timeout));
1055          if (e != null || !Thread.interrupted())
1056              return e;
1057          throw new InterruptedException();
# Line 1225 | Line 1218 | public class LinkedTransferQueue<E> exte
1218          objectFieldOffset(UNSAFE, "head", LinkedTransferQueue.class);
1219      private static final long tailOffset =
1220          objectFieldOffset(UNSAFE, "tail", LinkedTransferQueue.class);
1221 <    private static final long cleanMeOffset =
1222 <        objectFieldOffset(UNSAFE, "cleanMe", LinkedTransferQueue.class);
1221 >    private static final long sweepVotesOffset =
1222 >        objectFieldOffset(UNSAFE, "sweepVotes", LinkedTransferQueue.class);
1223  
1224      static long objectFieldOffset(sun.misc.Unsafe UNSAFE,
1225                                    String field, Class<?> klazz) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines