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.49 by dl, Fri Nov 5 23:01:47 2010 UTC

# Line 7 | Line 7
7   package jsr166y;
8  
9   import java.util.concurrent.*;
10 < import java.util.concurrent.atomic.*;
10 > import java.util.concurrent.atomic.AtomicReference;
11   import java.util.concurrent.locks.LockSupport;
12 import sun.misc.Unsafe;
13 import java.lang.reflect.*;
12  
13   /**
14 < * A reusable synchronization barrier, similar in functionality to a
14 > * A reusable synchronization barrier, similar in functionality to
15   * {@link java.util.concurrent.CyclicBarrier CyclicBarrier} and
16   * {@link java.util.concurrent.CountDownLatch CountDownLatch}
17   * but supporting more flexible usage.
18   *
19 < * <ul>
20 < *
21 < * <li> The number of parties synchronizing on a phaser may vary over
22 < * time.  A task may register to be a party at any time, and may
23 < * deregister upon arriving at the barrier.  As is the case with most
24 < * basic synchronization constructs, registration and deregistration
25 < * affect only internal counts; they do not establish any further
26 < * internal bookkeeping, so tasks cannot query whether they are
27 < * registered. (However, you can introduce such bookkeeping by
28 < * subclassing this class.)
29 < *
30 < * <li> Each generation has an associated phase value, starting at
31 < * zero, and advancing when all parties reach the barrier (wrapping
32 < * around to zero after reaching {@code Integer.MAX_VALUE}).
33 < *
34 < * <li> Like a CyclicBarrier, a Phaser may be repeatedly awaited.
35 < * Method {@code arriveAndAwaitAdvance} has effect analogous to
36 < * {@code CyclicBarrier.await}.  However, Phasers separate two
37 < * aspects of coordination, that may also be invoked independently:
19 > * <p> <b>Registration.</b> Unlike the case for other barriers, the
20 > * number of parties <em>registered</em> to synchronize on a phaser
21 > * may vary over time.  Tasks may be registered at any time (using
22 > * methods {@link #register}, {@link #bulkRegister}, or forms of
23 > * constructors establishing initial numbers of parties), and
24 > * optionally deregistered upon any arrival (using {@link
25 > * #arriveAndDeregister}).  As is the case with most basic
26 > * synchronization constructs, registration and deregistration affect
27 > * only internal counts; they do not establish any further internal
28 > * bookkeeping, so tasks cannot query whether they are registered.
29 > * (However, you can introduce such bookkeeping by subclassing this
30 > * class.)
31 > *
32 > * <p> <b>Synchronization.</b> Like a {@code CyclicBarrier}, a {@code
33 > * Phaser} may be repeatedly awaited.  Method {@link
34 > * #arriveAndAwaitAdvance} has effect analogous to {@link
35 > * java.util.concurrent.CyclicBarrier#await CyclicBarrier.await}. Each
36 > * generation of a {@code Phaser} has an associated phase number. The
37 > * phase number starts at zero, and advances when all parties arrive
38 > * at the barrier, wrapping around to zero after reaching {@code
39 > * Integer.MAX_VALUE}. The use of phase numbers enables independent
40 > * control of actions upon arrival at a barrier and upon awaiting
41 > * others, via two kinds of methods that may be invoked by any
42 > * registered party:
43   *
44   * <ul>
45   *
46 < *   <li> Arriving at a barrier. Methods {@code arrive} and
47 < *       {@code arriveAndDeregister} do not block, but return
48 < *       the phase value current upon entry to the method.
49 < *
50 < *   <li> Awaiting others. Method {@code awaitAdvance} requires an
51 < *       argument indicating the entry phase, and returns when the
52 < *       barrier advances to a new phase.
53 < * </ul>
46 > *   <li> <b>Arrival.</b> Methods {@link #arrive} and
47 > *       {@link #arriveAndDeregister} record arrival at a
48 > *       barrier. These methods do not block, but return an associated
49 > *       <em>arrival phase number</em>; that is, the phase number of
50 > *       the barrier to which the arrival applied. When the final
51 > *       party for a given phase arrives, an optional barrier action
52 > *       is performed and the phase advances.  Barrier actions,
53 > *       performed by the party triggering a phase advance, are
54 > *       arranged by overriding method {@link #onAdvance(int, int)},
55 > *       which also controls termination. Overriding this method is
56 > *       similar to, but more flexible than, providing a barrier
57 > *       action to a {@code CyclicBarrier}.
58 > *
59 > *   <li> <b>Waiting.</b> Method {@link #awaitAdvance} requires an
60 > *       argument indicating an arrival phase number, and returns when
61 > *       the barrier advances to (or is already at) a different phase.
62 > *       Unlike similar constructions using {@code CyclicBarrier},
63 > *       method {@code awaitAdvance} continues to wait even if the
64 > *       waiting thread is interrupted. Interruptible and timeout
65 > *       versions are also available, but exceptions encountered while
66 > *       tasks wait interruptibly or with timeout do not change the
67 > *       state of the barrier. If necessary, you can perform any
68 > *       associated recovery within handlers of those exceptions,
69 > *       often after invoking {@code forceTermination}.  Phasers may
70 > *       also be used by tasks executing in a {@link ForkJoinPool},
71 > *       which will ensure sufficient parallelism to execute tasks
72 > *       when others are blocked waiting for a phase to advance.
73   *
74 + * </ul>
75   *
76 < * <li> Barrier actions, performed by the task triggering a phase
77 < * advance while others may be waiting, are arranged by overriding
78 < * method {@code onAdvance}, that also controls termination.
79 < * Overriding this method may be used to similar but more flexible
80 < * effect as providing a barrier action to a CyclicBarrier.
81 < *
82 < * <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
76 > * <p> <b>Termination.</b> A {@code Phaser} may enter a
77 > * <em>termination</em> state in which all synchronization methods
78 > * immediately return without updating phaser state or waiting for
79 > * advance, and indicating (via a negative phase value) that execution
80 > * is complete.  Termination is triggered when an invocation of {@code
81 > * onAdvance} returns {@code true}.  As illustrated below, when
82 > * phasers control actions with a fixed number of iterations, it is
83   * often convenient to override this method to cause termination when
84 < * the current phase number reaches a threshold. Method
85 < * {@code forceTermination} is also available to abruptly release
86 < * waiting threads and allow them to terminate.
84 > * the current phase number reaches a threshold. Method {@link
85 > * #forceTermination} is also available to abruptly release waiting
86 > * threads and allow them to terminate.
87   *
88 < * <li> Phasers may be tiered to reduce contention. Phasers with large
88 > * <p> <b>Tiering.</b> Phasers may be <em>tiered</em> (i.e., arranged
89 > * in tree structures) to reduce contention. Phasers with large
90   * numbers of parties that would otherwise experience heavy
91 < * synchronization contention costs may instead be arranged in trees.
92 < * This will typically greatly increase throughput even though it
93 < * incurs somewhat greater per-operation overhead.
94 < *
95 < * <li> By default, {@code awaitAdvance} continues to wait even if
96 < * the waiting thread is interrupted. And unlike the case in
97 < * CyclicBarriers, exceptions encountered while tasks wait
98 < * interruptibly or with timeout do not change the state of the
99 < * barrier. If necessary, you can perform any associated recovery
100 < * within handlers of those exceptions, often after invoking
101 < * {@code forceTermination}.
102 < *
103 < * </ul>
91 > * synchronization contention costs may instead be set up so that
92 > * groups of sub-phasers share a common parent.  This may greatly
93 > * increase throughput even though it incurs greater per-operation
94 > * overhead.
95 > *
96 > * <p><b>Monitoring.</b> While synchronization methods may be invoked
97 > * only by registered parties, the current state of a phaser may be
98 > * monitored by any caller.  At any given moment there are {@link
99 > * #getRegisteredParties} parties in total, of which {@link
100 > * #getArrivedParties} have arrived at the current phase ({@link
101 > * #getPhase}).  When the remaining ({@link #getUnarrivedParties})
102 > * parties arrive, the phase advances.  The values returned by these
103 > * methods may reflect transient states and so are not in general
104 > * useful for synchronization control.  Method {@link #toString}
105 > * returns snapshots of these state queries in a form convenient for
106 > * informal monitoring.
107   *
108   * <p><b>Sample usages:</b>
109   *
110 < * <p>A Phaser may be used instead of a {@code CountDownLatch} to control
111 < * a one-shot action serving a variable number of parties. The typical
112 < * idiom is for the method setting this up to first register, then
113 < * start the actions, then deregister, as in:
114 < *
115 < * <pre>
116 < *  void runTasks(List&lt;Runnable&gt; list) {
117 < *    final Phaser phaser = new Phaser(1); // "1" to register self
118 < *    for (Runnable r : list) {
119 < *      phaser.register();
120 < *      new Thread() {
121 < *        public void run() {
122 < *          phaser.arriveAndAwaitAdvance(); // await all creation
123 < *          r.run();
124 < *          phaser.arriveAndDeregister();   // signal completion
125 < *        }
126 < *      }.start();
110 > * <p>A {@code Phaser} may be used instead of a {@code CountDownLatch}
111 > * to control a one-shot action serving a variable number of parties.
112 > * The typical idiom is for the method setting this up to first
113 > * register, then start the actions, then deregister, as in:
114 > *
115 > *  <pre> {@code
116 > * void runTasks(List<Runnable> tasks) {
117 > *   final Phaser phaser = new Phaser(1); // "1" to register self
118 > *   // create and start threads
119 > *   for (Runnable task : tasks) {
120 > *     phaser.register();
121 > *     new Thread() {
122 > *       public void run() {
123 > *         phaser.arriveAndAwaitAdvance(); // await all creation
124 > *         task.run();
125 > *       }
126 > *     }.start();
127   *   }
128   *
129 < *   doSomethingOnBehalfOfWorkers();
130 < *   phaser.arrive(); // allow threads to start
131 < *   int p = phaser.arriveAndDeregister(); // deregister self  ...
110 < *   p = phaser.awaitAdvance(p); // ... and await arrival
111 < *   otherActions(); // do other things while tasks execute
112 < *   phaser.awaitAdvance(p); // await final completion
113 < * }
114 < * </pre>
129 > *   // allow threads to start and deregister self
130 > *   phaser.arriveAndDeregister();
131 > * }}</pre>
132   *
133   * <p>One way to cause a set of threads to repeatedly perform actions
134   * for a given number of iterations is to override {@code onAdvance}:
135   *
136 < * <pre>
137 < *  void startTasks(List&lt;Runnable&gt; list, final int iterations) {
138 < *    final Phaser phaser = new Phaser() {
139 < *       public boolean onAdvance(int phase, int registeredParties) {
140 < *         return phase &gt;= iterations || registeredParties == 0;
136 > *  <pre> {@code
137 > * void startTasks(List<Runnable> tasks, final int iterations) {
138 > *   final Phaser phaser = new Phaser() {
139 > *     protected boolean onAdvance(int phase, int registeredParties) {
140 > *       return phase >= iterations || registeredParties == 0;
141 > *     }
142 > *   };
143 > *   phaser.register();
144 > *   for (final Runnable task : tasks) {
145 > *     phaser.register();
146 > *     new Thread() {
147 > *       public void run() {
148 > *         do {
149 > *           task.run();
150 > *           phaser.arriveAndAwaitAdvance();
151 > *         } while (!phaser.isTerminated());
152   *       }
153 < *    };
126 < *    phaser.register();
127 < *    for (Runnable r : list) {
128 < *      phaser.register();
129 < *      new Thread() {
130 < *        public void run() {
131 < *           do {
132 < *             r.run();
133 < *             phaser.arriveAndAwaitAdvance();
134 < *           } while(!phaser.isTerminated();
135 < *        }
136 < *      }.start();
153 > *     }.start();
154   *   }
155   *   phaser.arriveAndDeregister(); // deregister self, don't wait
156 < * }
140 < * </pre>
156 > * }}</pre>
157   *
158 < * <p> To create a set of tasks using a tree of Phasers,
158 > * If the main task must later await termination, it
159 > * may re-register and then execute a similar loop:
160 > *  <pre> {@code
161 > *   // ...
162 > *   phaser.register();
163 > *   while (!phaser.isTerminated())
164 > *     phaser.arriveAndAwaitAdvance();}</pre>
165 > *
166 > * <p>Related constructions may be used to await particular phase numbers
167 > * in contexts where you are sure that the phase will never wrap around
168 > * {@code Integer.MAX_VALUE}. For example:
169 > *
170 > *  <pre> {@code
171 > * void awaitPhase(Phaser phaser, int phase) {
172 > *   int p = phaser.register(); // assumes caller not already registered
173 > *   while (p < phase) {
174 > *     if (phaser.isTerminated())
175 > *       // ... deal with unexpected termination
176 > *     else
177 > *       p = phaser.arriveAndAwaitAdvance();
178 > *   }
179 > *   phaser.arriveAndDeregister();
180 > * }}</pre>
181 > *
182 > *
183 > * <p>To create a set of tasks using a tree of phasers,
184   * you could use code of the following form, assuming a
185 < * Task class with a constructor accepting a Phaser that
186 < * it registers for upon construction:
187 < * <pre>
188 < *  void build(Task[] actions, int lo, int hi, Phaser b) {
189 < *    int step = (hi - lo) / TASKS_PER_PHASER;
190 < *    if (step &gt; 1) {
191 < *       int i = lo;
192 < *       while (i &lt; hi) {
193 < *         int r = Math.min(i + step, hi);
194 < *         build(actions, i, r, new Phaser(b));
195 < *         i = r;
196 < *       }
197 < *    }
198 < *    else {
199 < *      for (int i = lo; i &lt; hi; ++i)
200 < *        actions[i] = new Task(b);
201 < *        // assumes new Task(b) performs b.register()
202 < *    }
162 < *  }
163 < *  // .. initially called, for n tasks via
164 < *  build(new Task[n], 0, n, new Phaser());
165 < * </pre>
185 > * Task class with a constructor accepting a phaser that
186 > * it registers with upon construction:
187 > *
188 > *  <pre> {@code
189 > * void build(Task[] actions, int lo, int hi, Phaser ph) {
190 > *   if (hi - lo > TASKS_PER_PHASER) {
191 > *     for (int i = lo; i < hi; i += TASKS_PER_PHASER) {
192 > *       int j = Math.min(i + TASKS_PER_PHASER, hi);
193 > *       build(actions, i, j, new Phaser(ph));
194 > *     }
195 > *   } else {
196 > *     for (int i = lo; i < hi; ++i)
197 > *       actions[i] = new Task(ph);
198 > *       // assumes new Task(ph) performs ph.register()
199 > *   }
200 > * }
201 > * // .. initially called, for n tasks via
202 > * build(new Task[n], 0, n, new Phaser());}</pre>
203   *
204   * The best value of {@code TASKS_PER_PHASER} depends mainly on
205   * expected barrier synchronization rates. A value as low as four may
206   * be appropriate for extremely small per-barrier task bodies (thus
207   * high rates), or up to hundreds for extremely large ones.
208   *
172 * </pre>
173 *
209   * <p><b>Implementation notes</b>: This implementation restricts the
210   * maximum number of parties to 65535. Attempts to register additional
211 < * parties result in IllegalStateExceptions. However, you can and
211 > * parties result in {@code IllegalStateException}. However, you can and
212   * should create tiered phasers to accommodate arbitrarily large sets
213   * of participants.
214 + *
215 + * @since 1.7
216 + * @author Doug Lea
217   */
218   public class Phaser {
219      /*
# Line 200 | Line 238 | public class Phaser {
238       * and encoding simple, and keeping race windows short.
239       *
240       * Note: there are some cheats in arrive() that rely on unarrived
241 <     * being lowest 16 bits.
241 >     * count being lowest 16 bits.
242       */
243      private volatile long state;
244  
245 <    private static final int ushortBits = 16;
246 <    private static final int ushortMask =  (1 << ushortBits) - 1;
209 <    private static final int phaseMask = 0x7fffffff;
245 >    private static final int ushortMask = 0xffff;
246 >    private static final int phaseMask  = 0x7fffffff;
247  
248      private static int unarrivedOf(long s) {
249 <        return (int)(s & ushortMask);
249 >        return (int) (s & ushortMask);
250      }
251  
252      private static int partiesOf(long s) {
253 <        return (int)(s & (ushortMask << 16)) >>> 16;
253 >        return ((int) s) >>> 16;
254      }
255  
256      private static int phaseOf(long s) {
257 <        return (int)(s >>> 32);
257 >        return (int) (s >>> 32);
258      }
259  
260      private static int arrivedOf(long s) {
# Line 225 | Line 262 | public class Phaser {
262      }
263  
264      private static long stateFor(int phase, int parties, int unarrived) {
265 <        return (((long)phase) << 32) | ((parties << 16) | unarrived);
265 >        return ((((long) phase) << 32) | (((long) parties) << 16) |
266 >                (long) unarrived);
267      }
268  
269      private static long trippedStateFor(int phase, int parties) {
270 <        return (((long)phase) << 32) | ((parties << 16) | parties);
270 >        long lp = (long) parties;
271 >        return (((long) phase) << 32) | (lp << 16) | lp;
272      }
273  
274 <    private static IllegalStateException badBounds(int parties, int unarrived) {
275 <        return new IllegalStateException
276 <            ("Attempt to set " + unarrived +
277 <             " unarrived of " + parties + " parties");
274 >    /**
275 >     * Returns message string for bad bounds exceptions.
276 >     */
277 >    private static String badBounds(int parties, int unarrived) {
278 >        return ("Attempt to set " + unarrived +
279 >                " unarrived of " + parties + " parties");
280      }
281  
282      /**
# Line 244 | Line 285 | public class Phaser {
285      private final Phaser parent;
286  
287      /**
288 <     * The root of Phaser tree. Equals this if not in a tree.  Used to
288 >     * The root of phaser tree. Equals this if not in a tree.  Used to
289       * support faster state push-down.
290       */
291      private final Phaser root;
# Line 252 | Line 293 | public class Phaser {
293      // Wait queues
294  
295      /**
296 <     * Heads of Treiber stacks waiting for nonFJ threads. To eliminate
297 <     * contention while releasing some threads while adding others, we
296 >     * Heads of Treiber stacks for waiting threads. To eliminate
297 >     * contention when releasing some threads while adding others, we
298       * use two of them, alternating across even and odd phases.
299 +     * Subphasers share queues with root to speed up releases.
300       */
301      private final AtomicReference<QNode> evenQ = new AtomicReference<QNode>();
302      private final AtomicReference<QNode> oddQ  = new AtomicReference<QNode>();
303  
304      private AtomicReference<QNode> queueFor(int phase) {
305 <        return (phase & 1) == 0? evenQ : oddQ;
305 >        Phaser r = root;
306 >        return ((phase & 1) == 0) ? r.evenQ : r.oddQ;
307      }
308  
309      /**
# Line 268 | Line 311 | public class Phaser {
311       * root if necessary.
312       */
313      private long getReconciledState() {
314 <        return parent == null? state : reconcileState();
314 >        return (parent == null) ? state : reconcileState();
315      }
316  
317      /**
318       * Recursively resolves state.
319       */
320      private long reconcileState() {
321 <        Phaser p = parent;
321 >        Phaser par = parent;
322          long s = state;
323 <        if (p != null) {
324 <            while (unarrivedOf(s) == 0 && phaseOf(s) != phaseOf(root.state)) {
325 <                long parentState = p.getReconciledState();
323 >        if (par != null) {
324 >            int phase, rootPhase;
325 >            while ((phase = phaseOf(s)) >= 0 &&
326 >                   (rootPhase = phaseOf(root.state)) != phase &&
327 >                   (rootPhase < 0 || unarrivedOf(s) == 0)) {
328 >                long parentState = par.getReconciledState();
329                  int parentPhase = phaseOf(parentState);
330 <                int phase = phaseOf(s = state);
331 <                if (phase != parentPhase) {
332 <                    long next = trippedStateFor(parentPhase, partiesOf(s));
333 <                    if (casState(s, next)) {
334 <                        releaseWaiters(phase);
335 <                        s = next;
336 <                    }
330 >                int parties = partiesOf(s);
331 >                long next = trippedStateFor(parentPhase, parties);
332 >                if (phaseOf(root.state) == rootPhase &&
333 >                    parentPhase != phase &&
334 >                    state == s && casState(s, next)) {
335 >                    releaseWaiters(phase);
336 >                    if (parties == 0) // exit if the final deregistration
337 >                        break;
338                  }
339 +                s = state;
340              }
341          }
342          return s;
343      }
344  
345      /**
346 <     * Creates a new Phaser without any initially registered parties,
347 <     * initial phase number 0, and no parent.
346 >     * Creates a new phaser without any initially registered parties,
347 >     * initial phase number 0, and no parent. Any thread using this
348 >     * phaser will need to first register for it.
349       */
350      public Phaser() {
351          this(null);
352      }
353  
354      /**
355 <     * Creates a new Phaser with the given numbers of registered
355 >     * Creates a new phaser with the given number of registered
356       * unarrived parties, initial phase number 0, and no parent.
357 <     * @param parties the number of parties required to trip barrier.
357 >     *
358 >     * @param parties the number of parties required to trip barrier
359       * @throws IllegalArgumentException if parties less than zero
360 <     * or greater than the maximum number of parties supported.
360 >     * or greater than the maximum number of parties supported
361       */
362      public Phaser(int parties) {
363          this(null, parties);
364      }
365  
366      /**
367 <     * Creates a new Phaser with the given parent, without any
367 >     * Creates a new phaser with the given parent, without any
368       * initially registered parties. If parent is non-null this phaser
369       * is registered with the parent and its initial phase number is
370       * the same as that of parent phaser.
371 <     * @param parent the parent phaser.
371 >     *
372 >     * @param parent the parent phaser
373       */
374      public Phaser(Phaser parent) {
375          int phase = 0;
# Line 333 | Line 384 | public class Phaser {
384      }
385  
386      /**
387 <     * Creates a new Phaser with the given parent and numbers of
388 <     * registered unarrived parties. If parent is non-null this phaser
387 >     * Creates a new phaser with the given parent and number of
388 >     * registered unarrived parties. If parent is non-null, this phaser
389       * is registered with the parent and its initial phase number is
390       * the same as that of parent phaser.
391 <     * @param parent the parent phaser.
392 <     * @param parties the number of parties required to trip barrier.
391 >     *
392 >     * @param parent the parent phaser
393 >     * @param parties the number of parties required to trip barrier
394       * @throws IllegalArgumentException if parties less than zero
395 <     * or greater than the maximum number of parties supported.
395 >     * or greater than the maximum number of parties supported
396       */
397      public Phaser(Phaser parent, int parties) {
398          if (parties < 0 || parties > ushortMask)
# Line 358 | Line 410 | public class Phaser {
410  
411      /**
412       * Adds a new unarrived party to this phaser.
413 <     * @return the current barrier phase number upon registration
413 >     * If an ongoing invocation of {@link #onAdvance} is in progress,
414 >     * this method waits until its completion before registering.
415 >     *
416 >     * @return the arrival phase number to which this registration applied
417       * @throws IllegalStateException if attempting to register more
418 <     * than the maximum supported number of parties.
418 >     * than the maximum supported number of parties
419       */
420      public int register() {
421          return doRegister(1);
# Line 368 | Line 423 | public class Phaser {
423  
424      /**
425       * Adds the given number of new unarrived parties to this phaser.
426 <     * @param parties the number of parties required to trip barrier.
427 <     * @return the current barrier phase number upon registration
426 >     * If an ongoing invocation of {@link #onAdvance} is in progress,
427 >     * this method waits until its completion before registering.
428 >     *
429 >     * @param parties the number of additional parties required to trip barrier
430 >     * @return the arrival phase number to which this registration applied
431       * @throws IllegalStateException if attempting to register more
432 <     * than the maximum supported number of parties.
432 >     * than the maximum supported number of parties
433 >     * @throws IllegalArgumentException if {@code parties < 0}
434       */
435      public int bulkRegister(int parties) {
436          if (parties < 0)
# Line 385 | Line 444 | public class Phaser {
444       * Shared code for register, bulkRegister
445       */
446      private int doRegister(int registrations) {
447 +        Phaser par = parent;
448 +        long s;
449          int phase;
450 <        for (;;) {
451 <            long s = getReconciledState();
452 <            phase = phaseOf(s);
453 <            int unarrived = unarrivedOf(s) + registrations;
454 <            int parties = partiesOf(s) + registrations;
455 <            if (phase < 0)
456 <                break;
457 <            if (parties > ushortMask || unarrived > ushortMask)
458 <                throw badBounds(parties, unarrived);
459 <            if (phase == phaseOf(root.state) &&
460 <                casState(s, stateFor(phase, parties, unarrived)))
461 <                break;
450 >        while ((phase = phaseOf(s = par==null? state:reconcileState())) >= 0) {
451 >            int p = partiesOf(s);
452 >            int u = unarrivedOf(s);
453 >            int unarrived = u + registrations;
454 >            int parties = p + registrations;
455 >            if (par == null || phase == phaseOf(root.state)) {
456 >                if (parties > ushortMask || unarrived > ushortMask)
457 >                    throw new IllegalStateException(badBounds(parties,
458 >                                                              unarrived));
459 >                else if (p != 0 && u == 0)       // back off if advancing
460 >                    Thread.yield();              // not worth actually blocking
461 >                else if (casState(s, stateFor(phase, parties, unarrived)))
462 >                    break;
463 >            }
464          }
465          return phase;
466      }
467  
468      /**
469       * Arrives at the barrier, but does not wait for others.  (You can
470 <     * in turn wait for others via {@link #awaitAdvance}).
470 >     * in turn wait for others via {@link #awaitAdvance}).  It is an
471 >     * unenforced usage error for an unregistered party to invoke this
472 >     * method.
473       *
474 <     * @return the barrier phase number upon entry to this method, or a
410 <     * negative value if terminated;
474 >     * @return the arrival phase number, or a negative value if terminated
475       * @throws IllegalStateException if not terminated and the number
476 <     * of unarrived parties would become negative.
476 >     * of unarrived parties would become negative
477       */
478      public int arrive() {
479 +        Phaser par = parent;
480 +        long s;
481          int phase;
482 <        for (;;) {
417 <            long s = state;
418 <            phase = phaseOf(s);
482 >        while ((phase = phaseOf(s = par==null? state:reconcileState())) >= 0) {
483              int parties = partiesOf(s);
484              int unarrived = unarrivedOf(s) - 1;
485 <            if (unarrived > 0) {        // Not the last arrival
486 <                if (casState(s, s - 1)) // s-1 adds one arrival
485 >            if (parties == 0 || unarrived < 0)
486 >                throw new IllegalStateException(badBounds(parties,
487 >                                                          unarrived));
488 >            else if (unarrived > 0) {           // Not the last arrival
489 >                if (casState(s, s - 1))         // s-1 adds one arrival
490                      break;
491              }
492 <            else if (unarrived == 0) {  // the last arrival
493 <                Phaser par = parent;
494 <                if (par == null) {      // directly trip
495 <                    if (casState
496 <                        (s,
497 <                         trippedStateFor(onAdvance(phase, parties)? -1 :
431 <                                         ((phase + 1) & phaseMask), parties))) {
432 <                        releaseWaiters(phase);
433 <                        break;
434 <                    }
435 <                }
436 <                else {                  // cascade to parent
437 <                    if (casState(s, s - 1)) { // zeroes unarrived
438 <                        par.arrive();
439 <                        reconcileState();
440 <                        break;
441 <                    }
492 >            else if (par == null) {             // directly trip
493 >                if (casState(s, trippedStateFor(onAdvance(phase, parties) ? -1 :
494 >                                                ((phase + 1) & phaseMask),
495 >                                                parties))) {
496 >                    releaseWaiters(phase);
497 >                    break;
498                  }
499              }
500 <            else if (phase < 0) // Don't throw exception if terminated
501 <                break;
446 <            else if (phase != phaseOf(root.state)) // or if unreconciled
500 >            else if (phaseOf(root.state) == phase && casState(s, s - 1)) {
501 >                par.arrive();                   // cascade to parent
502                  reconcileState();
503 <            else
504 <                throw badBounds(parties, unarrived);
503 >                break;
504 >            }
505          }
506          return phase;
507      }
508  
509      /**
510 <     * Arrives at the barrier, and deregisters from it, without
511 <     * waiting for others. Deregistration reduces number of parties
510 >     * Arrives at the barrier and deregisters from it without waiting
511 >     * for others. Deregistration reduces the number of parties
512       * required to trip the barrier in future phases.  If this phaser
513       * has a parent, and deregistration causes this phaser to have
514 <     * zero parties, this phaser is also deregistered from its parent.
514 >     * zero parties, this phaser also arrives at and is deregistered
515 >     * from its parent.  It is an unenforced usage error for an
516 >     * unregistered party to invoke this method.
517       *
518 <     * @return the current barrier phase number upon entry to
462 <     * this method, or a negative value if terminated;
518 >     * @return the arrival phase number, or a negative value if terminated
519       * @throws IllegalStateException if not terminated and the number
520 <     * of registered or unarrived parties would become negative.
520 >     * of registered or unarrived parties would become negative
521       */
522      public int arriveAndDeregister() {
523 <        // similar code to arrive, but too different to merge
523 >        // similar to arrive, but too different to merge
524          Phaser par = parent;
525 +        long s;
526          int phase;
527 <        for (;;) {
471 <            long s = state;
472 <            phase = phaseOf(s);
527 >        while ((phase = phaseOf(s = par==null? state:reconcileState())) >= 0) {
528              int parties = partiesOf(s) - 1;
529              int unarrived = unarrivedOf(s) - 1;
530 <            if (parties >= 0) {
531 <                if (unarrived > 0 || (unarrived == 0 && par != null)) {
532 <                    if (casState
533 <                        (s,
534 <                         stateFor(phase, parties, unarrived))) {
535 <                        if (unarrived == 0) {
536 <                            par.arriveAndDeregister();
537 <                            reconcileState();
538 <                        }
539 <                        break;
540 <                    }
541 <                    continue;
487 <                }
488 <                if (unarrived == 0) {
489 <                    if (casState
490 <                        (s,
491 <                         trippedStateFor(onAdvance(phase, parties)? -1 :
492 <                                         ((phase + 1) & phaseMask), parties))) {
493 <                        releaseWaiters(phase);
494 <                        break;
495 <                    }
496 <                    continue;
497 <                }
498 <                if (phase < 0)
530 >            if (parties < 0 || unarrived < 0)
531 >                throw new IllegalStateException(badBounds(parties,
532 >                                                          unarrived));
533 >            else if (unarrived > 0) {
534 >                if (casState(s, stateFor(phase, parties, unarrived)))
535 >                    break;
536 >            }
537 >            else if (par == null) {
538 >                if (casState(s, trippedStateFor(onAdvance(phase, parties)? -1:
539 >                                                (phase + 1) & phaseMask,
540 >                                                parties))) {
541 >                    releaseWaiters(phase);
542                      break;
500                if (par != null && phase != phaseOf(root.state)) {
501                    reconcileState();
502                    continue;
543                  }
544              }
545 <            throw badBounds(parties, unarrived);
545 >            else if (phaseOf(root.state) == phase &&
546 >                     casState(s, stateFor(phase, parties, 0))) {
547 >                if (parties == 0)
548 >                    par.arriveAndDeregister();
549 >                else
550 >                    par.arrive();
551 >                reconcileState();
552 >                break;
553 >            }
554          }
555          return phase;
556      }
557  
558      /**
559       * Arrives at the barrier and awaits others. Equivalent in effect
560 <     * to {@code awaitAdvance(arrive())}.  If you instead need to
561 <     * await with interruption of timeout, and/or deregister upon
562 <     * arrival, you can arrange them using analogous constructions.
563 <     * @return the phase on entry to this method
560 >     * to {@code awaitAdvance(arrive())}.  If you need to await with
561 >     * interruption or timeout, you can arrange this with an analogous
562 >     * construction using one of the other forms of the {@code
563 >     * awaitAdvance} method.  If instead you need to deregister upon
564 >     * arrival, use {@link #arriveAndDeregister}. It is an unenforced
565 >     * usage error for an unregistered party to invoke this method.
566 >     *
567 >     * @return the arrival phase number, or a negative number if terminated
568       * @throws IllegalStateException if not terminated and the number
569 <     * of unarrived parties would become negative.
569 >     * of unarrived parties would become negative
570       */
571      public int arriveAndAwaitAdvance() {
572          return awaitAdvance(arrive());
573      }
574  
575      /**
576 <     * Awaits the phase of the barrier to advance from the given
577 <     * value, or returns immediately if argument is negative or this
578 <     * barrier is terminated.
579 <     * @param phase the phase on entry to this method
580 <     * @return the phase on exit from this method
576 >     * Awaits the phase of the barrier to advance from the given phase
577 >     * value, returning immediately if the current phase of the
578 >     * barrier is not equal to the given phase value or this barrier
579 >     * is terminated.  It is an unenforced usage error for an
580 >     * unregistered party to invoke this method.
581 >     *
582 >     * @param phase an arrival phase number, or negative value if
583 >     * terminated; this argument is normally the value returned by a
584 >     * previous call to {@code arrive} or its variants
585 >     * @return the next arrival phase number, or a negative value
586 >     * if terminated or argument is negative
587       */
588      public int awaitAdvance(int phase) {
589          if (phase < 0)
590              return phase;
591 <        long s = getReconciledState();
534 <        int p = phaseOf(s);
591 >        int p = getPhase();
592          if (p != phase)
593              return p;
537        if (unarrivedOf(s) == 0)
538            parent.awaitAdvance(phase);
539        // Fall here even if parent waited, to reconcile and help release
594          return untimedWait(phase);
595      }
596  
597      /**
598 <     * Awaits the phase of the barrier to advance from the given
599 <     * value, or returns immediately if argument is negative or this
600 <     * barrier is terminated, or throws InterruptedException if
601 <     * interrupted while waiting.
602 <     * @param phase the phase on entry to this method
603 <     * @return the phase on exit from this method
598 >     * Awaits the phase of the barrier to advance from the given phase
599 >     * value, throwing {@code InterruptedException} if interrupted
600 >     * while waiting, or returning immediately if the current phase of
601 >     * the barrier is not equal to the given phase value or this
602 >     * barrier is terminated. It is an unenforced usage error for an
603 >     * unregistered party to invoke this method.
604 >     *
605 >     * @param phase an arrival phase number, or negative value if
606 >     * terminated; this argument is normally the value returned by a
607 >     * previous call to {@code arrive} or its variants
608 >     * @return the next arrival phase number, or a negative value
609 >     * if terminated or argument is negative
610       * @throws InterruptedException if thread interrupted while waiting
611       */
612 <    public int awaitAdvanceInterruptibly(int phase) throws InterruptedException {
612 >    public int awaitAdvanceInterruptibly(int phase)
613 >        throws InterruptedException {
614          if (phase < 0)
615              return phase;
616 <        long s = getReconciledState();
556 <        int p = phaseOf(s);
616 >        int p = getPhase();
617          if (p != phase)
618              return p;
559        if (unarrivedOf(s) != 0)
560            parent.awaitAdvanceInterruptibly(phase);
619          return interruptibleWait(phase);
620      }
621  
622      /**
623 <     * Awaits the phase of the barrier to advance from the given value
624 <     * or the given timeout elapses, or returns immediately if
625 <     * argument is negative or this barrier is terminated.
626 <     * @param phase the phase on entry to this method
627 <     * @return the phase on exit from this method
623 >     * Awaits the phase of the barrier to advance from the given phase
624 >     * value or the given timeout to elapse, throwing {@code
625 >     * InterruptedException} if interrupted while waiting, or
626 >     * returning immediately if the current phase of the barrier is
627 >     * not equal to the given phase value or this barrier is
628 >     * terminated.  It is an unenforced usage error for an
629 >     * unregistered party to invoke this method.
630 >     *
631 >     * @param phase an arrival phase number, or negative value if
632 >     * terminated; this argument is normally the value returned by a
633 >     * previous call to {@code arrive} or its variants
634 >     * @param timeout how long to wait before giving up, in units of
635 >     *        {@code unit}
636 >     * @param unit a {@code TimeUnit} determining how to interpret the
637 >     *        {@code timeout} parameter
638 >     * @return the next arrival phase number, or a negative value
639 >     * if terminated or argument is negative
640       * @throws InterruptedException if thread interrupted while waiting
641       * @throws TimeoutException if timed out while waiting
642       */
643 <    public int awaitAdvanceInterruptibly(int phase, long timeout, TimeUnit unit)
643 >    public int awaitAdvanceInterruptibly(int phase,
644 >                                         long timeout, TimeUnit unit)
645          throws InterruptedException, TimeoutException {
646 +        long nanos = unit.toNanos(timeout);
647          if (phase < 0)
648              return phase;
649 <        long s = getReconciledState();
578 <        int p = phaseOf(s);
649 >        int p = getPhase();
650          if (p != phase)
651              return p;
652 <        if (unarrivedOf(s) == 0)
582 <            parent.awaitAdvanceInterruptibly(phase, timeout, unit);
583 <        return timedWait(phase, unit.toNanos(timeout));
652 >        return timedWait(phase, nanos);
653      }
654  
655      /**
# Line 591 | Line 660 | public class Phaser {
660       * unexpected exceptions.
661       */
662      public void forceTermination() {
663 <        for (;;) {
664 <            long s = getReconciledState();
665 <            int phase = phaseOf(s);
666 <            int parties = partiesOf(s);
667 <            int unarrived = unarrivedOf(s);
668 <            if (phase < 0 ||
669 <                casState(s, stateFor(-1, parties, unarrived))) {
601 <                releaseWaiters(0);
602 <                releaseWaiters(1);
603 <                if (parent != null)
604 <                    parent.forceTermination();
605 <                return;
606 <            }
607 <        }
663 >        Phaser r = root;    // force at root then reconcile
664 >        long s;
665 >        while (phaseOf(s = r.state) >= 0)
666 >            r.casState(s, stateFor(-1, partiesOf(s), unarrivedOf(s)));
667 >        reconcileState();
668 >        releaseWaiters(0);  // ensure wakeups on both queues
669 >        releaseWaiters(1);
670      }
671  
672      /**
673       * Returns the current phase number. The maximum phase number is
674       * {@code Integer.MAX_VALUE}, after which it restarts at
675       * zero. Upon termination, the phase number is negative.
676 +     *
677       * @return the phase number, or a negative value if terminated
678       */
679      public final int getPhase() {
# Line 618 | Line 681 | public class Phaser {
681      }
682  
683      /**
621     * Returns {@code true} if the current phase number equals the given phase.
622     * @param phase the phase
623     * @return {@code true} if the current phase number equals the given phase
624     */
625    public final boolean hasPhase(int phase) {
626        return phaseOf(getReconciledState()) == phase;
627    }
628
629    /**
684       * Returns the number of parties registered at this barrier.
685 +     *
686       * @return the number of parties
687       */
688      public int getRegisteredParties() {
689 <        return partiesOf(state);
689 >        return partiesOf(getReconciledState());
690      }
691  
692      /**
693 <     * Returns the number of parties that have arrived at the current
694 <     * phase of this barrier.
693 >     * Returns the number of registered parties that have arrived at
694 >     * the current phase of this barrier.
695 >     *
696       * @return the number of arrived parties
697       */
698      public int getArrivedParties() {
699 <        return arrivedOf(state);
699 >        return arrivedOf(getReconciledState());
700      }
701  
702      /**
703       * Returns the number of registered parties that have not yet
704       * arrived at the current phase of this barrier.
705 +     *
706       * @return the number of unarrived parties
707       */
708      public int getUnarrivedParties() {
709 <        return unarrivedOf(state);
709 >        return unarrivedOf(getReconciledState());
710      }
711  
712      /**
713 <     * Returns the parent of this phaser, or null if none.
714 <     * @return the parent of this phaser, or null if none
713 >     * Returns the parent of this phaser, or {@code null} if none.
714 >     *
715 >     * @return the parent of this phaser, or {@code null} if none
716       */
717      public Phaser getParent() {
718          return parent;
# Line 663 | Line 721 | public class Phaser {
721      /**
722       * Returns the root ancestor of this phaser, which is the same as
723       * this phaser if it has no parent.
724 +     *
725       * @return the root ancestor of this phaser
726       */
727      public Phaser getRoot() {
# Line 671 | Line 730 | public class Phaser {
730  
731      /**
732       * Returns {@code true} if this barrier has been terminated.
733 +     *
734       * @return {@code true} if this barrier has been terminated
735       */
736      public boolean isTerminated() {
# Line 678 | Line 738 | public class Phaser {
738      }
739  
740      /**
741 <     * Overridable method to perform an action upon phase advance, and
742 <     * to control termination. This method is invoked whenever the
743 <     * barrier is tripped (and thus all other waiting parties are
744 <     * dormant). If it returns true, then, rather than advance the
745 <     * phase number, this barrier will be set to a final termination
746 <     * state, and subsequent calls to {@code isTerminated} will
747 <     * return true.
741 >     * Overridable method to perform an action upon impending phase
742 >     * advance, and to control termination. This method is invoked
743 >     * upon arrival of the party tripping the barrier (when all other
744 >     * waiting parties are dormant).  If this method returns {@code
745 >     * true}, then, rather than advance the phase number, this barrier
746 >     * will be set to a final termination state, and subsequent calls
747 >     * to {@link #isTerminated} will return true. Any (unchecked)
748 >     * Exception or Error thrown by an invocation of this method is
749 >     * propagated to the party attempting to trip the barrier, in
750 >     * which case no advance occurs.
751 >     *
752 >     * <p>The arguments to this method provide the state of the phaser
753 >     * prevailing for the current transition. (When called from within
754 >     * an implementation of {@code onAdvance} the values returned by
755 >     * methods such as {@code getPhase} may or may not reliably
756 >     * indicate the state to which this transition applies.)
757       *
758 <     * <p> The default version returns true when the number of
758 >     * <p>The default version returns {@code true} when the number of
759       * registered parties is zero. Normally, overrides that arrange
760       * termination for other reasons should also preserve this
761       * property.
762       *
694     * <p> You may override this method to perform an action with side
695     * effects visible to participating tasks, but it is in general
696     * only sensible to do so in designs where all parties register
697     * before any arrive, and all {@code awaitAdvance} at each phase.
698     * Otherwise, you cannot ensure lack of interference. In
699     * particular, this method may be invoked more than once per
700     * transition if other parties successfully register while the
701     * invocation of this method is in progress, thus postponing the
702     * transition until those parties also arrive, re-triggering this
703     * method.
704     *
763       * @param phase the phase number on entering the barrier
764       * @param registeredParties the current number of registered parties
765       * @return {@code true} if this barrier should terminate
# Line 729 | Line 787 | public class Phaser {
787  
788      // methods for waiting
789  
732    /** The number of CPUs, for spin control */
733    static final int NCPUS = Runtime.getRuntime().availableProcessors();
734
790      /**
791 <     * The number of times to spin before blocking in timed waits.
737 <     * The value is empirically derived.
791 >     * Wait nodes for Treiber stack representing wait queue
792       */
793 <    static final int maxTimedSpins = (NCPUS < 2)? 0 : 32;
793 >    static final class QNode implements ForkJoinPool.ManagedBlocker {
794 >        final Phaser phaser;
795 >        final int phase;
796 >        final long startTime;
797 >        final long nanos;
798 >        final boolean timed;
799 >        final boolean interruptible;
800 >        volatile boolean wasInterrupted = false;
801 >        volatile Thread thread; // nulled to cancel wait
802 >        QNode next;
803  
804 <    /**
805 <     * The number of times to spin before blocking in untimed waits.
806 <     * This is greater than timed value because untimed waits spin
807 <     * faster since they don't need to check times on each spin.
808 <     */
809 <    static final int maxUntimedSpins = maxTimedSpins * 32;
804 >        QNode(Phaser phaser, int phase, boolean interruptible,
805 >              boolean timed, long startTime, long nanos) {
806 >            this.phaser = phaser;
807 >            this.phase = phase;
808 >            this.timed = timed;
809 >            this.interruptible = interruptible;
810 >            this.startTime = startTime;
811 >            this.nanos = nanos;
812 >            thread = Thread.currentThread();
813 >        }
814  
815 <    /**
816 <     * The number of nanoseconds for which it is faster to spin
817 <     * rather than to use timed park. A rough estimate suffices.
818 <     */
819 <    static final long spinForTimeoutThreshold = 1000L;
815 >        public boolean isReleasable() {
816 >            return (thread == null ||
817 >                    phaser.getPhase() != phase ||
818 >                    (interruptible && wasInterrupted) ||
819 >                    (timed && (nanos - (System.nanoTime() - startTime)) <= 0));
820 >        }
821  
822 <    /**
823 <     * Wait nodes for Treiber stack representing wait queue for non-FJ
824 <     * tasks.
825 <     */
826 <    static final class QNode {
827 <        QNode next;
828 <        volatile Thread thread; // nulled to cancel wait
829 <        QNode() {
830 <            thread = Thread.currentThread();
822 >        public boolean block() {
823 >            if (Thread.interrupted()) {
824 >                wasInterrupted = true;
825 >                if (interruptible)
826 >                    return true;
827 >            }
828 >            if (!timed)
829 >                LockSupport.park(this);
830 >            else {
831 >                long waitTime = nanos - (System.nanoTime() - startTime);
832 >                if (waitTime <= 0)
833 >                    return true;
834 >                LockSupport.parkNanos(this, waitTime);
835 >            }
836 >            return isReleasable();
837          }
838 +
839          void signal() {
840              Thread t = thread;
841              if (t != null) {
# Line 768 | Line 843 | public class Phaser {
843                  LockSupport.unpark(t);
844              }
845          }
846 +
847 +        boolean doWait() {
848 +            if (thread != null) {
849 +                try {
850 +                    ForkJoinPool.managedBlock(this);
851 +                } catch (InterruptedException ie) {
852 +                    wasInterrupted = true; // can't currently happen
853 +                }
854 +            }
855 +            return wasInterrupted;
856 +        }
857      }
858  
859      /**
860 <     * Removes and signals waiting threads from wait queue
860 >     * Removes and signals waiting threads from wait queue.
861       */
862      private void releaseWaiters(int phase) {
863          AtomicReference<QNode> head = queueFor(phase);
# Line 783 | Line 869 | public class Phaser {
869      }
870  
871      /**
872 +     * Tries to enqueue given node in the appropriate wait queue.
873 +     *
874 +     * @return true if successful
875 +     */
876 +    private boolean tryEnqueue(QNode node) {
877 +        AtomicReference<QNode> head = queueFor(node.phase);
878 +        return head.compareAndSet(node.next = head.get(), node);
879 +    }
880 +
881 +    /**
882       * Enqueues node and waits unless aborted or signalled.
883 +     *
884 +     * @return current phase
885       */
886      private int untimedWait(int phase) {
789        int spins = maxUntimedSpins;
887          QNode node = null;
791        boolean interrupted = false;
888          boolean queued = false;
889 +        boolean interrupted = false;
890          int p;
891          while ((p = getPhase()) == phase) {
892 <            interrupted = Thread.interrupted();
893 <            if (node != null) {
894 <                if (!queued) {
895 <                    AtomicReference<QNode> head = queueFor(phase);
896 <                    queued = head.compareAndSet(node.next = head.get(), node);
897 <                }
898 <                else if (node.thread != null)
899 <                    LockSupport.park(this);
803 <            }
804 <            else if (spins <= 0)
805 <                node = new QNode();
806 <            else
807 <                --spins;
892 >            if (Thread.interrupted())
893 >                interrupted = true;
894 >            else if (node == null)
895 >                node = new QNode(this, phase, false, false, 0, 0);
896 >            else if (!queued)
897 >                queued = tryEnqueue(node);
898 >            else if (node.doWait())
899 >                interrupted = true;
900          }
901          if (node != null)
902              node.thread = null;
903 +        releaseWaiters(phase);
904          if (interrupted)
905              Thread.currentThread().interrupt();
813        releaseWaiters(phase);
906          return p;
907      }
908  
909      /**
910 <     * Messier interruptible version
910 >     * Interruptible version
911 >     * @return current phase
912       */
913      private int interruptibleWait(int phase) throws InterruptedException {
821        int spins = maxUntimedSpins;
914          QNode node = null;
915          boolean queued = false;
916          boolean interrupted = false;
917          int p;
918 <        while ((p = getPhase()) == phase) {
919 <            if (interrupted = Thread.interrupted())
920 <                break;
921 <            if (node != null) {
922 <                if (!queued) {
923 <                    AtomicReference<QNode> head = queueFor(phase);
924 <                    queued = head.compareAndSet(node.next = head.get(), node);
925 <                }
926 <                else if (node.thread != null)
835 <                    LockSupport.park(this);
836 <            }
837 <            else if (spins <= 0)
838 <                node = new QNode();
839 <            else
840 <                --spins;
918 >        while ((p = getPhase()) == phase && !interrupted) {
919 >            if (Thread.interrupted())
920 >                interrupted = true;
921 >            else if (node == null)
922 >                node = new QNode(this, phase, true, false, 0, 0);
923 >            else if (!queued)
924 >                queued = tryEnqueue(node);
925 >            else if (node.doWait())
926 >                interrupted = true;
927          }
928          if (node != null)
929              node.thread = null;
930 +        if (p != phase || (p = getPhase()) != phase)
931 +            releaseWaiters(phase);
932          if (interrupted)
933              throw new InterruptedException();
846        releaseWaiters(phase);
934          return p;
935      }
936  
937      /**
938 <     * Even messier timeout version.
938 >     * Timeout version.
939 >     * @return current phase
940       */
941      private int timedWait(int phase, long nanos)
942          throws InterruptedException, TimeoutException {
943 +        long startTime = System.nanoTime();
944 +        QNode node = null;
945 +        boolean queued = false;
946 +        boolean interrupted = false;
947          int p;
948 <        if ((p = getPhase()) == phase) {
949 <            long lastTime = System.nanoTime();
950 <            int spins = maxTimedSpins;
951 <            QNode node = null;
952 <            boolean queued = false;
953 <            boolean interrupted = false;
954 <            while ((p = getPhase()) == phase) {
955 <                if (interrupted = Thread.interrupted())
956 <                    break;
957 <                long now = System.nanoTime();
958 <                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();
948 >        while ((p = getPhase()) == phase && !interrupted) {
949 >            if (Thread.interrupted())
950 >                interrupted = true;
951 >            else if (nanos - (System.nanoTime() - startTime) <= 0)
952 >                break;
953 >            else if (node == null)
954 >                node = new QNode(this, phase, true, true, startTime, nanos);
955 >            else if (!queued)
956 >                queued = tryEnqueue(node);
957 >            else if (node.doWait())
958 >                interrupted = true;
959          }
960 <        releaseWaiters(phase);
960 >        if (node != null)
961 >            node.thread = null;
962 >        if (p != phase || (p = getPhase()) != phase)
963 >            releaseWaiters(phase);
964 >        if (interrupted)
965 >            throw new InterruptedException();
966 >        if (p == phase)
967 >            throw new TimeoutException();
968          return p;
969      }
970  
971 <    // Temporary Unsafe mechanics for preliminary release
971 >    // Unsafe mechanics
972  
973 <    static final Unsafe _unsafe;
974 <    static final long stateOffset;
973 >    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
974 >    private static final long stateOffset =
975 >        objectFieldOffset("state", Phaser.class);
976  
977 <    static {
977 >    private final boolean casState(long cmp, long val) {
978 >        return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val);
979 >    }
980 >
981 >    private static long objectFieldOffset(String field, Class<?> klazz) {
982          try {
983 <            if (Phaser.class.getClassLoader() != null) {
984 <                Field f = Unsafe.class.getDeclaredField("theUnsafe");
985 <                f.setAccessible(true);
986 <                _unsafe = (Unsafe)f.get(null);
987 <            }
988 <            else
908 <                _unsafe = Unsafe.getUnsafe();
909 <            stateOffset = _unsafe.objectFieldOffset
910 <                (Phaser.class.getDeclaredField("state"));
911 <        } catch (Exception e) {
912 <            throw new RuntimeException("Could not initialize intrinsics", e);
983 >            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
984 >        } catch (NoSuchFieldException e) {
985 >            // Convert Exception to corresponding Error
986 >            NoSuchFieldError error = new NoSuchFieldError(field);
987 >            error.initCause(e);
988 >            throw error;
989          }
990      }
991  
992 <    final boolean casState(long cmp, long val) {
993 <        return _unsafe.compareAndSwapLong(this, stateOffset, cmp, val);
992 >    /**
993 >     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
994 >     * Replace with a simple call to Unsafe.getUnsafe when integrating
995 >     * into a jdk.
996 >     *
997 >     * @return a sun.misc.Unsafe
998 >     */
999 >    private static sun.misc.Unsafe getUnsafe() {
1000 >        try {
1001 >            return sun.misc.Unsafe.getUnsafe();
1002 >        } catch (SecurityException se) {
1003 >            try {
1004 >                return java.security.AccessController.doPrivileged
1005 >                    (new java.security
1006 >                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
1007 >                        public sun.misc.Unsafe run() throws Exception {
1008 >                            java.lang.reflect.Field f = sun.misc
1009 >                                .Unsafe.class.getDeclaredField("theUnsafe");
1010 >                            f.setAccessible(true);
1011 >                            return (sun.misc.Unsafe) f.get(null);
1012 >                        }});
1013 >            } catch (java.security.PrivilegedActionException e) {
1014 >                throw new RuntimeException("Could not initialize intrinsics",
1015 >                                           e.getCause());
1016 >            }
1017 >        }
1018      }
1019   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines