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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines