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.13 by jsr166, Mon Jul 20 22:40:09 2009 UTC vs.
Revision 1.17 by jsr166, Thu Jul 23 19:25:45 2009 UTC

# Line 175 | Line 175 | import java.lang.reflect.*;
175   * parties result in IllegalStateExceptions. However, you can and
176   * should create tiered phasers to accommodate arbitrarily large sets
177   * of participants.
178 + *
179 + * @since 1.7
180 + * @author Doug Lea
181   */
182   public class Phaser {
183      /*
# Line 212 | Line 215 | public class Phaser {
215      }
216  
217      private static int partiesOf(long s) {
218 <        return ((int)s) >>> 16;
218 >        return ((int) s) >>> 16;
219      }
220  
221      private static int phaseOf(long s) {
222 <        return (int)(s >>> 32);
222 >        return (int) (s >>> 32);
223      }
224  
225      private static int arrivedOf(long s) {
# Line 224 | Line 227 | public class Phaser {
227      }
228  
229      private static long stateFor(int phase, int parties, int unarrived) {
230 <        return ((((long)phase) << 32) | (((long)parties) << 16) |
231 <                (long)unarrived);
230 >        return ((((long) phase) << 32) | (((long) parties) << 16) |
231 >                (long) unarrived);
232      }
233  
234      private static long trippedStateFor(int phase, int parties) {
235 <        long lp = (long)parties;
236 <        return (((long)phase) << 32) | (lp << 16) | lp;
235 >        long lp = (long) parties;
236 >        return (((long) phase) << 32) | (lp << 16) | lp;
237      }
238  
239      /**
240 <     * Returns message string for bad bounds exceptions
240 >     * Returns message string for bad bounds exceptions.
241       */
242      private static String badBounds(int parties, int unarrived) {
243          return ("Attempt to set " + unarrived +
# Line 309 | Line 312 | public class Phaser {
312      /**
313       * Creates a new Phaser with the given numbers of registered
314       * unarrived parties, initial phase number 0, and no parent.
315 <     * @param parties the number of parties required to trip barrier.
315 >     *
316 >     * @param parties the number of parties required to trip barrier
317       * @throws IllegalArgumentException if parties less than zero
318 <     * or greater than the maximum number of parties supported.
318 >     * or greater than the maximum number of parties supported
319       */
320      public Phaser(int parties) {
321          this(null, parties);
# Line 322 | Line 326 | public class Phaser {
326       * initially registered parties. If parent is non-null this phaser
327       * is registered with the parent and its initial phase number is
328       * the same as that of parent phaser.
329 <     * @param parent the parent phaser.
329 >     *
330 >     * @param parent the parent phaser
331       */
332      public Phaser(Phaser parent) {
333          int phase = 0;
# Line 338 | Line 343 | public class Phaser {
343  
344      /**
345       * Creates a new Phaser with the given parent and numbers of
346 <     * registered unarrived parties. If parent is non-null this phaser
346 >     * registered unarrived parties. If parent is non-null, this phaser
347       * is registered with the parent and its initial phase number is
348       * the same as that of parent phaser.
349 <     * @param parent the parent phaser.
350 <     * @param parties the number of parties required to trip barrier.
349 >     *
350 >     * @param parent the parent phaser
351 >     * @param parties the number of parties required to trip barrier
352       * @throws IllegalArgumentException if parties less than zero
353 <     * or greater than the maximum number of parties supported.
353 >     * or greater than the maximum number of parties supported
354       */
355      public Phaser(Phaser parent, int parties) {
356          if (parties < 0 || parties > ushortMask)
# Line 362 | Line 368 | public class Phaser {
368  
369      /**
370       * Adds a new unarrived party to this phaser.
371 +     *
372       * @return the current barrier phase number upon registration
373       * @throws IllegalStateException if attempting to register more
374 <     * than the maximum supported number of parties.
374 >     * than the maximum supported number of parties
375       */
376      public int register() {
377          return doRegister(1);
# Line 372 | Line 379 | public class Phaser {
379  
380      /**
381       * Adds the given number of new unarrived parties to this phaser.
382 <     * @param parties the number of parties required to trip barrier.
382 >     *
383 >     * @param parties the number of parties required to trip barrier
384       * @return the current barrier phase number upon registration
385       * @throws IllegalStateException if attempting to register more
386 <     * than the maximum supported number of parties.
386 >     * than the maximum supported number of parties
387       */
388      public int bulkRegister(int parties) {
389          if (parties < 0)
# Line 411 | Line 419 | public class Phaser {
419       * in turn wait for others via {@link #awaitAdvance}).
420       *
421       * @return the barrier phase number upon entry to this method, or a
422 <     * negative value if terminated;
422 >     * negative value if terminated
423       * @throws IllegalStateException if not terminated and the number
424 <     * of unarrived parties would become negative.
424 >     * of unarrived parties would become negative
425       */
426      public int arrive() {
427          int phase;
# Line 463 | Line 471 | public class Phaser {
471       * zero parties, this phaser is also deregistered from its parent.
472       *
473       * @return the current barrier phase number upon entry to
474 <     * this method, or a negative value if terminated;
474 >     * this method, or a negative value if terminated
475       * @throws IllegalStateException if not terminated and the number
476 <     * of registered or unarrived parties would become negative.
476 >     * of registered or unarrived parties would become negative
477       */
478      public int arriveAndDeregister() {
479          // similar code to arrive, but too different to merge
# Line 516 | Line 524 | public class Phaser {
524       * to {@code awaitAdvance(arrive())}.  If you instead need to
525       * await with interruption of timeout, and/or deregister upon
526       * arrival, you can arrange them using analogous constructions.
527 +     *
528       * @return the phase on entry to this method
529       * @throws IllegalStateException if not terminated and the number
530 <     * of unarrived parties would become negative.
530 >     * of unarrived parties would become negative
531       */
532      public int arriveAndAwaitAdvance() {
533          return awaitAdvance(arrive());
# Line 528 | Line 537 | public class Phaser {
537       * Awaits the phase of the barrier to advance from the given
538       * value, or returns immediately if argument is negative or this
539       * barrier is terminated.
540 +     *
541       * @param phase the phase on entry to this method
542       * @return the phase on exit from this method
543       */
# Line 549 | Line 559 | public class Phaser {
559       * value, or returns immediately if argument is negative or this
560       * barrier is terminated, or throws InterruptedException if
561       * interrupted while waiting.
562 +     *
563       * @param phase the phase on entry to this method
564       * @return the phase on exit from this method
565       * @throws InterruptedException if thread interrupted while waiting
# Line 570 | Line 581 | public class Phaser {
581       * Awaits the phase of the barrier to advance from the given value
582       * or the given timeout elapses, or returns immediately if
583       * argument is negative or this barrier is terminated.
584 +     *
585       * @param phase the phase on entry to this method
586       * @return the phase on exit from this method
587       * @throws InterruptedException if thread interrupted while waiting
# Line 616 | Line 628 | public class Phaser {
628       * Returns the current phase number. The maximum phase number is
629       * {@code Integer.MAX_VALUE}, after which it restarts at
630       * zero. Upon termination, the phase number is negative.
631 +     *
632       * @return the phase number, or a negative value if terminated
633       */
634      public final int getPhase() {
# Line 624 | Line 637 | public class Phaser {
637  
638      /**
639       * Returns {@code true} if the current phase number equals the given phase.
640 +     *
641       * @param phase the phase
642       * @return {@code true} if the current phase number equals the given phase
643       */
# Line 633 | Line 647 | public class Phaser {
647  
648      /**
649       * Returns the number of parties registered at this barrier.
650 +     *
651       * @return the number of parties
652       */
653      public int getRegisteredParties() {
# Line 642 | Line 657 | public class Phaser {
657      /**
658       * Returns the number of parties that have arrived at the current
659       * phase of this barrier.
660 +     *
661       * @return the number of arrived parties
662       */
663      public int getArrivedParties() {
# Line 651 | Line 667 | public class Phaser {
667      /**
668       * Returns the number of registered parties that have not yet
669       * arrived at the current phase of this barrier.
670 +     *
671       * @return the number of unarrived parties
672       */
673      public int getUnarrivedParties() {
# Line 659 | Line 676 | public class Phaser {
676  
677      /**
678       * Returns the parent of this phaser, or null if none.
679 +     *
680       * @return the parent of this phaser, or null if none
681       */
682      public Phaser getParent() {
# Line 668 | Line 686 | public class Phaser {
686      /**
687       * Returns the root ancestor of this phaser, which is the same as
688       * this phaser if it has no parent.
689 +     *
690       * @return the root ancestor of this phaser
691       */
692      public Phaser getRoot() {
# Line 676 | Line 695 | public class Phaser {
695  
696      /**
697       * Returns {@code true} if this barrier has been terminated.
698 +     *
699       * @return {@code true} if this barrier has been terminated
700       */
701      public boolean isTerminated() {
# Line 799 | Line 819 | public class Phaser {
819      }
820  
821      /**
822 <     * Removes and signals waiting threads from wait queue
822 >     * Removes and signals waiting threads from wait queue.
823       */
824      private void releaseWaiters(int phase) {
825          AtomicReference<QNode> head = queueFor(phase);
# Line 811 | Line 831 | public class Phaser {
831      }
832  
833      /**
834 <     * Tries to enqueue given node in the appropriate wait queue
834 >     * Tries to enqueue given node in the appropriate wait queue.
835 >     *
836       * @return true if successful
837       */
838      private boolean tryEnqueue(QNode node) {
# Line 821 | Line 842 | public class Phaser {
842  
843      /**
844       * Enqueues node and waits unless aborted or signalled.
845 +     *
846       * @return current phase
847       */
848      private int untimedWait(int phase) {
# Line 934 | Line 956 | public class Phaser {
956  
957      private static long fieldOffset(String fieldName)
958              throws NoSuchFieldException {
959 <        return _unsafe.objectFieldOffset
959 >        return UNSAFE.objectFieldOffset
960              (Phaser.class.getDeclaredField(fieldName));
961      }
962  
963 <    static final Unsafe _unsafe;
963 >    static final Unsafe UNSAFE;
964      static final long stateOffset;
965  
966      static {
967          try {
968 <            _unsafe = getUnsafe();
968 >            UNSAFE = getUnsafe();
969              stateOffset = fieldOffset("state");
970          } catch (Throwable e) {
971              throw new RuntimeException("Could not initialize intrinsics", e);
# Line 951 | Line 973 | public class Phaser {
973      }
974  
975      final boolean casState(long cmp, long val) {
976 <        return _unsafe.compareAndSwapLong(this, stateOffset, cmp, val);
976 >        return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val);
977      }
978   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines