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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines