--- jsr166/src/jsr166y/Phaser.java 2009/01/06 14:30:31 1.10 +++ jsr166/src/jsr166y/Phaser.java 2009/07/20 22:40:09 1.13 @@ -93,18 +93,18 @@ import java.lang.reflect.*; * idiom is for the method setting this up to first register, then * start the actions, then deregister, as in: * - *
- *  void runTasks(List<Runnable> list) {
- *    final Phaser phaser = new Phaser(1); // "1" to register self
- *    for (Runnable r : list) {
- *      phaser.register();
- *      new Thread() {
- *        public void run() {
- *          phaser.arriveAndAwaitAdvance(); // await all creation
- *          r.run();
- *          phaser.arriveAndDeregister();   // signal completion
- *        }
- *      }.start();
+ *  
 {@code
+ * void runTasks(List list) {
+ *   final Phaser phaser = new Phaser(1); // "1" to register self
+ *   for (Runnable r : list) {
+ *     phaser.register();
+ *     new Thread() {
+ *       public void run() {
+ *         phaser.arriveAndAwaitAdvance(); // await all creation
+ *         r.run();
+ *         phaser.arriveAndDeregister();   // signal completion
+ *       }
+ *     }.start();
  *   }
  *
  *   doSomethingOnBehalfOfWorkers();
@@ -113,59 +113,55 @@ import java.lang.reflect.*;
  *   p = phaser.awaitAdvance(p); // ... and await arrival
  *   otherActions(); // do other things while tasks execute
  *   phaser.awaitAdvance(p); // await final completion
- * }
- * 
+ * }}
* *

One way to cause a set of threads to repeatedly perform actions * for a given number of iterations is to override {@code onAdvance}: * - *

- *  void startTasks(List<Runnable> list, final int iterations) {
- *    final Phaser phaser = new Phaser() {
- *       public boolean onAdvance(int phase, int registeredParties) {
- *         return phase >= iterations || registeredParties == 0;
+ *  
 {@code
+ * void startTasks(List list, final int iterations) {
+ *   final Phaser phaser = new Phaser() {
+ *     public boolean onAdvance(int phase, int registeredParties) {
+ *       return phase >= iterations || registeredParties == 0;
+ *     }
+ *   };
+ *   phaser.register();
+ *   for (Runnable r : list) {
+ *     phaser.register();
+ *     new Thread() {
+ *       public void run() {
+ *         do {
+ *           r.run();
+ *           phaser.arriveAndAwaitAdvance();
+ *         } while(!phaser.isTerminated();
  *       }
- *    };
- *    phaser.register();
- *    for (Runnable r : list) {
- *      phaser.register();
- *      new Thread() {
- *        public void run() {
- *           do {
- *             r.run();
- *             phaser.arriveAndAwaitAdvance();
- *           } while(!phaser.isTerminated();
- *        }
- *      }.start();
+ *     }.start();
  *   }
  *   phaser.arriveAndDeregister(); // deregister self, don't wait
- * }
- * 
+ * }}
* *

To create a set of tasks using a tree of Phasers, * you could use code of the following form, assuming a * Task class with a constructor accepting a Phaser that * it registers for upon construction: - *

- *  void build(Task[] actions, int lo, int hi, Phaser b) {
- *    int step = (hi - lo) / TASKS_PER_PHASER;
- *    if (step > 1) {
- *       int i = lo;
- *       while (i < hi) {
- *         int r = Math.min(i + step, hi);
- *         build(actions, i, r, new Phaser(b));
- *         i = r;
- *       }
- *    }
- *    else {
- *      for (int i = lo; i < hi; ++i)
- *        actions[i] = new Task(b);
- *        // assumes new Task(b) performs b.register()
- *    }
- *  }
- *  // .. initially called, for n tasks via
- *  build(new Task[n], 0, n, new Phaser());
- * 
+ *
 {@code
+ * void build(Task[] actions, int lo, int hi, Phaser b) {
+ *   int step = (hi - lo) / TASKS_PER_PHASER;
+ *   if (step > 1) {
+ *     int i = lo;
+ *     while (i < hi) {
+ *       int r = Math.min(i + step, hi);
+ *       build(actions, i, r, new Phaser(b));
+ *       i = r;
+ *     }
+ *   } else {
+ *     for (int i = lo; i < hi; ++i)
+ *       actions[i] = new Task(b);
+ *       // assumes new Task(b) performs b.register()
+ *   }
+ * }
+ * // .. initially called, for n tasks via
+ * build(new Task[n], 0, n, new Phaser());}
* * The best value of {@code TASKS_PER_PHASER} depends mainly on * expected barrier synchronization rates. A value as low as four may @@ -399,7 +395,7 @@ public class Phaser { phase = phaseOf(s); int unarrived = unarrivedOf(s) + registrations; int parties = partiesOf(s) + registrations; - if (phase < 0) + if (phase < 0) break; if (parties > ushortMask || unarrived > ushortMask) throw new IllegalStateException(badBounds(parties, unarrived)); @@ -557,7 +553,7 @@ public class Phaser { * @return the phase on exit from this method * @throws InterruptedException if thread interrupted while waiting */ - public int awaitAdvanceInterruptibly(int phase) + public int awaitAdvanceInterruptibly(int phase) throws InterruptedException { if (phase < 0) return phase; @@ -795,7 +791,7 @@ public class Phaser { try { ForkJoinPool.managedBlock(this, false); } catch (InterruptedException ie) { - } + } } return wasInterrupted; } @@ -913,22 +909,43 @@ public class Phaser { } // Temporary Unsafe mechanics for preliminary release + private static Unsafe getUnsafe() throws Throwable { + try { + return Unsafe.getUnsafe(); + } catch (SecurityException se) { + try { + return java.security.AccessController.doPrivileged + (new java.security.PrivilegedExceptionAction() { + public Unsafe run() throws Exception { + return getUnsafePrivileged(); + }}); + } catch (java.security.PrivilegedActionException e) { + throw e.getCause(); + } + } + } + + private static Unsafe getUnsafePrivileged() + throws NoSuchFieldException, IllegalAccessException { + Field f = Unsafe.class.getDeclaredField("theUnsafe"); + f.setAccessible(true); + return (Unsafe) f.get(null); + } + + private static long fieldOffset(String fieldName) + throws NoSuchFieldException { + return _unsafe.objectFieldOffset + (Phaser.class.getDeclaredField(fieldName)); + } static final Unsafe _unsafe; static final long stateOffset; static { try { - if (Phaser.class.getClassLoader() != null) { - Field f = Unsafe.class.getDeclaredField("theUnsafe"); - f.setAccessible(true); - _unsafe = (Unsafe)f.get(null); - } - else - _unsafe = Unsafe.getUnsafe(); - stateOffset = _unsafe.objectFieldOffset - (Phaser.class.getDeclaredField("state")); - } catch (Exception e) { + _unsafe = getUnsafe(); + stateOffset = fieldOffset("state"); + } catch (Throwable e) { throw new RuntimeException("Could not initialize intrinsics", e); } }