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.3 by jsr166, Fri Jul 25 18:11:53 2008 UTC vs.
Revision 1.6 by dl, Tue Oct 28 23:03:24 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines