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.2 by jsr166, Fri Jul 25 18:10:41 2008 UTC vs.
Revision 1.23 by jsr166, Mon Jul 27 20:57:44 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines