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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines