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.47 by dl, Wed Jul 7 19:52:32 2010 UTC vs.
Revision 1.48 by dl, Sun Oct 24 21:45:39 2010 UTC

# Line 109 | Line 109 | import java.util.concurrent.locks.LockSu
109   * <p><b>Sample usages:</b>
110   *
111   * <p>A {@code Phaser} may be used instead of a {@code CountDownLatch}
112 < * to control a one-shot action serving a variable number of
113 < * parties. The typical idiom is for the method setting this up to
114 < * first register, then start the actions, then deregister, as in:
112 > * to control a one-shot action serving a variable number of parties.
113 > * The typical idiom is for the method setting this up to first
114 > * register, then start the actions, then deregister, as in:
115   *
116   *  <pre> {@code
117   * void runTasks(List<Runnable> tasks) {
# Line 184 | Line 184 | import java.util.concurrent.locks.LockSu
184   * <p>To create a set of tasks using a tree of phasers,
185   * you could use code of the following form, assuming a
186   * Task class with a constructor accepting a phaser that
187 < * it registers for upon construction:
187 > * it registers with upon construction:
188   *
189   *  <pre> {@code
190   * void build(Task[] actions, int lo, int hi, Phaser ph) {
# Line 207 | Line 207 | import java.util.concurrent.locks.LockSu
207   * be appropriate for extremely small per-barrier task bodies (thus
208   * high rates), or up to hundreds for extremely large ones.
209   *
210 * </pre>
211 *
210   * <p><b>Implementation notes</b>: This implementation restricts the
211   * maximum number of parties to 65535. Attempts to register additional
212   * parties result in {@code IllegalStateException}. However, you can and
# Line 348 | Line 346 | public class Phaser {
346      }
347  
348      /**
349 <     * Creates a new phaser with the given numbers of registered
349 >     * Creates a new phaser with the given number of registered
350       * unarrived parties, initial phase number 0, and no parent.
351       *
352       * @param parties the number of parties required to trip barrier
# Line 380 | Line 378 | public class Phaser {
378      }
379  
380      /**
381 <     * Creates a new phaser with the given parent and numbers of
381 >     * Creates a new phaser with the given parent and number of
382       * registered unarrived parties. If parent is non-null, this phaser
383       * is registered with the parent and its initial phase number is
384       * the same as that of parent phaser.
# Line 418 | Line 416 | public class Phaser {
416      /**
417       * Adds the given number of new unarrived parties to this phaser.
418       *
419 <     * @param parties the number of parties required to trip barrier
419 >     * @param parties the number of additional parties required to trip barrier
420       * @return the arrival phase number to which this registration applied
421       * @throws IllegalStateException if attempting to register more
422       * than the maximum supported number of parties
423 +     * @throws IllegalArgumentException if {@code parties < 0}
424       */
425      public int bulkRegister(int parties) {
426          if (parties < 0)
# Line 563 | Line 562 | public class Phaser {
562       * Arrives at the barrier and awaits others. Equivalent in effect
563       * to {@code awaitAdvance(arrive())}.  If you need to await with
564       * interruption or timeout, you can arrange this with an analogous
565 <     * construction using one of the other forms of the awaitAdvance
566 <     * method.  If instead you need to deregister upon arrival use
567 <     * {@code arriveAndDeregister}. It is an unenforced usage error
568 <     * for an unregistered party to invoke this method.
565 >     * construction using one of the other forms of the {@code
566 >     * awaitAdvance} method.  If instead you need to deregister upon
567 >     * arrival, use {@link #arriveAndDeregister}. It is an unenforced
568 >     * usage error for an unregistered party to invoke this method.
569       *
570       * @return the arrival phase number, or a negative number if terminated
571       * @throws IllegalStateException if not terminated and the number
# Line 829 | Line 828 | public class Phaser {
828          volatile boolean wasInterrupted = false;
829          volatile Thread thread; // nulled to cancel wait
830          QNode next;
831 +
832          QNode(Phaser phaser, int phase, boolean interruptible,
833                boolean timed, long startTime, long nanos) {
834              this.phaser = phaser;
# Line 839 | Line 839 | public class Phaser {
839              this.nanos = nanos;
840              thread = Thread.currentThread();
841          }
842 +
843          public boolean isReleasable() {
844              return (thread == null ||
845                      phaser.getPhase() != phase ||
846                      (interruptible && wasInterrupted) ||
847                      (timed && (nanos - (System.nanoTime() - startTime)) <= 0));
848          }
849 +
850          public boolean block() {
851              if (Thread.interrupted()) {
852                  wasInterrupted = true;
# Line 861 | Line 863 | public class Phaser {
863              }
864              return isReleasable();
865          }
866 +
867          void signal() {
868              Thread t = thread;
869              if (t != null) {
# Line 868 | Line 871 | public class Phaser {
871                  LockSupport.unpark(t);
872              }
873          }
874 +
875          boolean doWait() {
876              if (thread != null) {
877                  try {
878                      ForkJoinPool.managedBlock(this);
879                  } catch (InterruptedException ie) {
880 +                    wasInterrupted = true; // can't currently happen
881                  }
882              }
883              return wasInterrupted;
884          }
880
885      }
886  
887      /**
# Line 919 | Line 923 | public class Phaser {
923                  node = new QNode(this, phase, false, false, 0, 0);
924              else if (!queued)
925                  queued = tryEnqueue(node);
926 <            else
927 <                interrupted = node.doWait();
926 >            else if (node.doWait())
927 >                interrupted = true;
928          }
929          if (node != null)
930              node.thread = null;
# Line 946 | Line 950 | public class Phaser {
950                  node = new QNode(this, phase, true, false, 0, 0);
951              else if (!queued)
952                  queued = tryEnqueue(node);
953 <            else
954 <                interrupted = node.doWait();
953 >            else if (node.doWait())
954 >                interrupted = true;
955          }
956          if (node != null)
957              node.thread = null;
# Line 978 | Line 982 | public class Phaser {
982                  node = new QNode(this, phase, true, true, startTime, nanos);
983              else if (!queued)
984                  queued = tryEnqueue(node);
985 <            else
986 <                interrupted = node.doWait();
985 >            else if (node.doWait())
986 >                interrupted = true;
987          }
988          if (node != null)
989              node.thread = null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines