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

Comparing jsr166/src/jsr166y/Phaser.java (file contents):
Revision 1.9 by jsr166, Mon Jan 5 09:11:26 2009 UTC vs.
Revision 1.10 by dl, Tue Jan 6 14:30:31 2009 UTC

# Line 57 | Line 57 | import java.lang.reflect.*;
57   * effect as providing a barrier action to a CyclicBarrier.
58   *
59   * <li> Phasers may enter a <em>termination</em> state in which all
60 < * await actions immediately return, indicating (via a negative phase
61 < * value) that execution is complete.  Termination is triggered by
62 < * executing the overridable {@code onAdvance} method that is invoked
63 < * each time the barrier is about to be tripped. When a Phaser is
64 < * controlling an action with a fixed number of iterations, it is
65 < * often convenient to override this method to cause termination when
66 < * the current phase number reaches a threshold. Method
67 < * {@code forceTermination} is also available to abruptly release
68 < * waiting threads and allow them to terminate.
60 > * actions immediately return without updating phaser state or waiting
61 > * for advance, and indicating (via a negative phase value) that
62 > * execution is complete.  Termination is triggered by executing the
63 > * overridable {@code onAdvance} method that is invoked each time the
64 > * barrier is about to be tripped. When a Phaser is controlling an
65 > * action with a fixed number of iterations, it is often convenient to
66 > * override this method to cause termination when the current phase
67 > * number reaches a threshold. Method {@code forceTermination} is also
68 > * available to abruptly release waiting threads and allow them to
69 > * terminate.
70   *
71   * <li> Phasers may be tiered to reduce contention. Phasers with large
72   * numbers of parties that would otherwise experience heavy
# Line 81 | Line 82 | import java.lang.reflect.*;
82   * within handlers of those exceptions, often after invoking
83   * {@code forceTermination}.
84   *
85 + * <li>Phasers ensure lack of starvation when used by ForkJoinTasks.
86 + *
87   * </ul>
88   *
89   * <p><b>Sample usages:</b>
# Line 200 | Line 203 | public class Phaser {
203       * and encoding simple, and keeping race windows short.
204       *
205       * Note: there are some cheats in arrive() that rely on unarrived
206 <     * being lowest 16 bits.
206 >     * count being lowest 16 bits.
207       */
208      private volatile long state;
209  
210      private static final int ushortBits = 16;
211 <    private static final int ushortMask =  (1 << ushortBits) - 1;
212 <    private static final int phaseMask = 0x7fffffff;
211 >    private static final int ushortMask = 0xffff;
212 >    private static final int phaseMask  = 0x7fffffff;
213  
214      private static int unarrivedOf(long s) {
215          return (int)(s & ushortMask);
216      }
217  
218      private static int partiesOf(long s) {
219 <        return (int)(s & (ushortMask << 16)) >>> 16;
219 >        return ((int)s) >>> 16;
220      }
221  
222      private static int phaseOf(long s) {
# Line 225 | Line 228 | public class Phaser {
228      }
229  
230      private static long stateFor(int phase, int parties, int unarrived) {
231 <        return (((long)phase) << 32) | ((parties << 16) | unarrived);
231 >        return ((((long)phase) << 32) | (((long)parties) << 16) |
232 >                (long)unarrived);
233      }
234  
235      private static long trippedStateFor(int phase, int parties) {
236 <        return (((long)phase) << 32) | ((parties << 16) | parties);
236 >        long lp = (long)parties;
237 >        return (((long)phase) << 32) | (lp << 16) | lp;
238      }
239  
240 <    private static IllegalStateException badBounds(int parties, int unarrived) {
241 <        return new IllegalStateException
242 <            ("Attempt to set " + unarrived +
243 <             " unarrived of " + parties + " parties");
240 >    /**
241 >     * Returns message string for bad bounds exceptions
242 >     */
243 >    private static String badBounds(int parties, int unarrived) {
244 >        return ("Attempt to set " + unarrived +
245 >                " unarrived of " + parties + " parties");
246      }
247  
248      /**
# Line 252 | Line 259 | public class Phaser {
259      // Wait queues
260  
261      /**
262 <     * Heads of Treiber stacks waiting for nonFJ threads. To eliminate
262 >     * Heads of Treiber stacks for waiting threads. To eliminate
263       * contention while releasing some threads while adding others, we
264       * use two of them, alternating across even and odd phases.
265       */
# Line 296 | Line 303 | public class Phaser {
303  
304      /**
305       * Creates a new Phaser without any initially registered parties,
306 <     * initial phase number 0, and no parent.
306 >     * initial phase number 0, and no parent. Any thread using this
307 >     * Phaser will need to first register for it.
308       */
309      public Phaser() {
310          this(null);
# Line 391 | Line 399 | public class Phaser {
399              phase = phaseOf(s);
400              int unarrived = unarrivedOf(s) + registrations;
401              int parties = partiesOf(s) + registrations;
402 <            if (phase < 0)
402 >            if (phase < 0)
403                  break;
404              if (parties > ushortMask || unarrived > ushortMask)
405 <                throw badBounds(parties, unarrived);
405 >                throw new IllegalStateException(badBounds(parties, unarrived));
406              if (phase == phaseOf(root.state) &&
407                  casState(s, stateFor(phase, parties, unarrived)))
408                  break;
# Line 416 | Line 424 | public class Phaser {
424          for (;;) {
425              long s = state;
426              phase = phaseOf(s);
427 +            if (phase < 0)
428 +                break;
429              int parties = partiesOf(s);
430              int unarrived = unarrivedOf(s) - 1;
431              if (unarrived > 0) {        // Not the last arrival
# Line 441 | Line 451 | public class Phaser {
451                      }
452                  }
453              }
444            else if (phase < 0) // Don't throw exception if terminated
445                break;
454              else if (phase != phaseOf(root.state)) // or if unreconciled
455                  reconcileState();
456              else
457 <                throw badBounds(parties, unarrived);
457 >                throw new IllegalStateException(badBounds(parties, unarrived));
458          }
459          return phase;
460      }
# Line 470 | Line 478 | public class Phaser {
478          for (;;) {
479              long s = state;
480              phase = phaseOf(s);
481 +            if (phase < 0)
482 +                break;
483              int parties = partiesOf(s) - 1;
484              int unarrived = unarrivedOf(s) - 1;
485              if (parties >= 0) {
# Line 495 | Line 505 | public class Phaser {
505                      }
506                      continue;
507                  }
498                if (phase < 0)
499                    break;
508                  if (par != null && phase != phaseOf(root.state)) {
509                      reconcileState();
510                      continue;
511                  }
512              }
513 <            throw badBounds(parties, unarrived);
513 >            throw new IllegalStateException(badBounds(parties, unarrived));
514          }
515          return phase;
516      }
# Line 534 | Line 542 | public class Phaser {
542          int p = phaseOf(s);
543          if (p != phase)
544              return p;
545 <        if (unarrivedOf(s) == 0)
545 >        if (unarrivedOf(s) == 0 && parent != null)
546              parent.awaitAdvance(phase);
547          // Fall here even if parent waited, to reconcile and help release
548          return untimedWait(phase);
# Line 549 | Line 557 | public class Phaser {
557       * @return the phase on exit from this method
558       * @throws InterruptedException if thread interrupted while waiting
559       */
560 <    public int awaitAdvanceInterruptibly(int phase) throws InterruptedException {
560 >    public int awaitAdvanceInterruptibly(int phase)
561 >        throws InterruptedException {
562          if (phase < 0)
563              return phase;
564          long s = getReconciledState();
565          int p = phaseOf(s);
566          if (p != phase)
567              return p;
568 <        if (unarrivedOf(s) != 0)
568 >        if (unarrivedOf(s) == 0 && parent != null)
569              parent.awaitAdvanceInterruptibly(phase);
570          return interruptibleWait(phase);
571      }
# Line 578 | Line 587 | public class Phaser {
587          int p = phaseOf(s);
588          if (p != phase)
589              return p;
590 <        if (unarrivedOf(s) == 0)
590 >        if (unarrivedOf(s) == 0 && parent != null)
591              parent.awaitAdvanceInterruptibly(phase, timeout, unit);
592          return timedWait(phase, unit.toNanos(timeout));
593      }
# Line 729 | Line 738 | public class Phaser {
738  
739      // methods for waiting
740  
732    /** The number of CPUs, for spin control */
733    static final int NCPUS = Runtime.getRuntime().availableProcessors();
734
735    /**
736     * The number of times to spin before blocking in timed waits.
737     * The value is empirically derived.
738     */
739    static final int maxTimedSpins = (NCPUS < 2)? 0 : 32;
740
741    /**
742     * The number of times to spin before blocking in untimed waits.
743     * This is greater than timed value because untimed waits spin
744     * faster since they don't need to check times on each spin.
745     */
746    static final int maxUntimedSpins = maxTimedSpins * 32;
747
748    /**
749     * The number of nanoseconds for which it is faster to spin
750     * rather than to use timed park. A rough estimate suffices.
751     */
752    static final long spinForTimeoutThreshold = 1000L;
753
741      /**
742 <     * Wait nodes for Treiber stack representing wait queue for non-FJ
756 <     * tasks.
742 >     * Wait nodes for Treiber stack representing wait queue
743       */
744 <    static final class QNode {
745 <        QNode next;
744 >    static final class QNode implements ForkJoinPool.ManagedBlocker {
745 >        final Phaser phaser;
746 >        final int phase;
747 >        final long startTime;
748 >        final long nanos;
749 >        final boolean timed;
750 >        final boolean interruptible;
751 >        volatile boolean wasInterrupted = false;
752          volatile Thread thread; // nulled to cancel wait
753 <        QNode() {
753 >        QNode next;
754 >        QNode(Phaser phaser, int phase, boolean interruptible,
755 >              boolean timed, long startTime, long nanos) {
756 >            this.phaser = phaser;
757 >            this.phase = phase;
758 >            this.timed = timed;
759 >            this.interruptible = interruptible;
760 >            this.startTime = startTime;
761 >            this.nanos = nanos;
762              thread = Thread.currentThread();
763          }
764 +        public boolean isReleasable() {
765 +            return (thread == null ||
766 +                    phaser.getPhase() != phase ||
767 +                    (interruptible && wasInterrupted) ||
768 +                    (timed && (nanos - (System.nanoTime() - startTime)) <= 0));
769 +        }
770 +        public boolean block() {
771 +            if (Thread.interrupted()) {
772 +                wasInterrupted = true;
773 +                if (interruptible)
774 +                    return true;
775 +            }
776 +            if (!timed)
777 +                LockSupport.park(this);
778 +            else {
779 +                long waitTime = nanos - (System.nanoTime() - startTime);
780 +                if (waitTime <= 0)
781 +                    return true;
782 +                LockSupport.parkNanos(this, waitTime);
783 +            }
784 +            return isReleasable();
785 +        }
786          void signal() {
787              Thread t = thread;
788              if (t != null) {
# Line 768 | Line 790 | public class Phaser {
790                  LockSupport.unpark(t);
791              }
792          }
793 +        boolean doWait() {
794 +            if (thread != null) {
795 +                try {
796 +                    ForkJoinPool.managedBlock(this, false);
797 +                } catch (InterruptedException ie) {
798 +                }
799 +            }
800 +            return wasInterrupted;
801 +        }
802 +
803      }
804  
805      /**
# Line 783 | Line 815 | public class Phaser {
815      }
816  
817      /**
818 +     * Tries to enqueue given node in the appropriate wait queue
819 +     * @return true if successful
820 +     */
821 +    private boolean tryEnqueue(QNode node) {
822 +        AtomicReference<QNode> head = queueFor(node.phase);
823 +        return head.compareAndSet(node.next = head.get(), node);
824 +    }
825 +
826 +    /**
827       * Enqueues node and waits unless aborted or signalled.
828 +     * @return current phase
829       */
830      private int untimedWait(int phase) {
789        int spins = maxUntimedSpins;
831          QNode node = null;
791        boolean interrupted = false;
832          boolean queued = false;
833 +        boolean interrupted = false;
834          int p;
835          while ((p = getPhase()) == phase) {
836 <            interrupted = Thread.interrupted();
837 <            if (node != null) {
838 <                if (!queued) {
839 <                    AtomicReference<QNode> head = queueFor(phase);
840 <                    queued = head.compareAndSet(node.next = head.get(), node);
841 <                }
801 <                else if (node.thread != null)
802 <                    LockSupport.park(this);
803 <            }
804 <            else if (spins <= 0)
805 <                node = new QNode();
836 >            if (Thread.interrupted())
837 >                interrupted = true;
838 >            else if (node == null)
839 >                node = new QNode(this, phase, false, false, 0, 0);
840 >            else if (!queued)
841 >                queued = tryEnqueue(node);
842              else
843 <                --spins;
843 >                interrupted = node.doWait();
844          }
845          if (node != null)
846              node.thread = null;
847 +        releaseWaiters(phase);
848          if (interrupted)
849              Thread.currentThread().interrupt();
813        releaseWaiters(phase);
850          return p;
851      }
852  
853      /**
854 <     * Messier interruptible version
854 >     * Interruptible version
855 >     * @return current phase
856       */
857      private int interruptibleWait(int phase) throws InterruptedException {
821        int spins = maxUntimedSpins;
858          QNode node = null;
859          boolean queued = false;
860          boolean interrupted = false;
861          int p;
862 <        while ((p = getPhase()) == phase) {
863 <            if (interrupted = Thread.interrupted())
864 <                break;
865 <            if (node != null) {
866 <                if (!queued) {
867 <                    AtomicReference<QNode> head = queueFor(phase);
868 <                    queued = head.compareAndSet(node.next = head.get(), node);
833 <                }
834 <                else if (node.thread != null)
835 <                    LockSupport.park(this);
836 <            }
837 <            else if (spins <= 0)
838 <                node = new QNode();
862 >        while ((p = getPhase()) == phase && !interrupted) {
863 >            if (Thread.interrupted())
864 >                interrupted = true;
865 >            else if (node == null)
866 >                node = new QNode(this, phase, true, false, 0, 0);
867 >            else if (!queued)
868 >                queued = tryEnqueue(node);
869              else
870 <                --spins;
870 >                interrupted = node.doWait();
871          }
872          if (node != null)
873              node.thread = null;
874 +        if (p != phase || (p = getPhase()) != phase)
875 +            releaseWaiters(phase);
876          if (interrupted)
877              throw new InterruptedException();
846        releaseWaiters(phase);
878          return p;
879      }
880  
881      /**
882 <     * Even messier timeout version.
882 >     * Timeout version.
883 >     * @return current phase
884       */
885      private int timedWait(int phase, long nanos)
886          throws InterruptedException, TimeoutException {
887 +        long startTime = System.nanoTime();
888 +        QNode node = null;
889 +        boolean queued = false;
890 +        boolean interrupted = false;
891          int p;
892 <        if ((p = getPhase()) == phase) {
893 <            long lastTime = System.nanoTime();
894 <            int spins = maxTimedSpins;
895 <            QNode node = null;
896 <            boolean queued = false;
897 <            boolean interrupted = false;
898 <            while ((p = getPhase()) == phase) {
899 <                if (interrupted = Thread.interrupted())
900 <                    break;
901 <                long now = System.nanoTime();
902 <                if ((nanos -= now - lastTime) <= 0)
867 <                    break;
868 <                lastTime = now;
869 <                if (node != null) {
870 <                    if (!queued) {
871 <                        AtomicReference<QNode> head = queueFor(phase);
872 <                        queued = head.compareAndSet(node.next = head.get(), node);
873 <                    }
874 <                    else if (node.thread != null &&
875 <                             nanos > spinForTimeoutThreshold) {
876 <                        LockSupport.parkNanos(this, nanos);
877 <                    }
878 <                }
879 <                else if (spins <= 0)
880 <                    node = new QNode();
881 <                else
882 <                    --spins;
883 <            }
884 <            if (node != null)
885 <                node.thread = null;
886 <            if (interrupted)
887 <                throw new InterruptedException();
888 <            if (p == phase && (p = getPhase()) == phase)
889 <                throw new TimeoutException();
892 >        while ((p = getPhase()) == phase && !interrupted) {
893 >            if (Thread.interrupted())
894 >                interrupted = true;
895 >            else if (nanos - (System.nanoTime() - startTime) <= 0)
896 >                break;
897 >            else if (node == null)
898 >                node = new QNode(this, phase, true, true, startTime, nanos);
899 >            else if (!queued)
900 >                queued = tryEnqueue(node);
901 >            else
902 >                interrupted = node.doWait();
903          }
904 <        releaseWaiters(phase);
904 >        if (node != null)
905 >            node.thread = null;
906 >        if (p != phase || (p = getPhase()) != phase)
907 >            releaseWaiters(phase);
908 >        if (interrupted)
909 >            throw new InterruptedException();
910 >        if (p == phase)
911 >            throw new TimeoutException();
912          return p;
913      }
914  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines