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.26 by jsr166, Wed Aug 5 00:54:11 2009 UTC vs.
Revision 1.60 by dl, Sun Nov 28 15:49:49 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines