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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines