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.23 by jsr166, Mon Jul 27 20:57:44 2009 UTC vs.
Revision 1.50 by dl, Sat Nov 6 16:12:10 2010 UTC

# Line 7 | Line 7
7   package jsr166y;
8  
9   import java.util.concurrent.*;
10
10   import java.util.concurrent.atomic.AtomicReference;
11   import java.util.concurrent.locks.LockSupport;
12  
13   /**
14 < * A reusable synchronization barrier, similar in functionality to a
14 > * A reusable synchronization barrier, similar in functionality to
15   * {@link java.util.concurrent.CyclicBarrier CyclicBarrier} and
16   * {@link java.util.concurrent.CountDownLatch CountDownLatch}
17   * but supporting more flexible usage.
18   *
19 < * <ul>
20 < *
21 < * <li> The number of parties synchronizing on a phaser may vary over
22 < * time.  A task may register to be a party at any time, and may
23 < * deregister upon arriving at the barrier.  As is the case with most
24 < * basic synchronization constructs, registration and deregistration
25 < * affect only internal counts; they do not establish any further
26 < * internal bookkeeping, so tasks cannot query whether they are
27 < * registered. (However, you can introduce such bookkeeping by
28 < * subclassing this class.)
29 < *
30 < * <li> Each generation has an associated phase value, starting at
31 < * zero, and advancing when all parties reach the barrier (wrapping
32 < * around to zero after reaching {@code Integer.MAX_VALUE}).
33 < *
34 < * <li> Like a CyclicBarrier, a Phaser may be repeatedly awaited.
35 < * Method {@code arriveAndAwaitAdvance} has effect analogous to
36 < * {@code CyclicBarrier.await}.  However, Phasers separate two
37 < * aspects of coordination, that may also be invoked independently:
19 > * <p> <b>Registration.</b> Unlike the case for other barriers, the
20 > * number of parties <em>registered</em> to synchronize on a phaser
21 > * may vary over time.  Tasks may be registered at any time (using
22 > * methods {@link #register}, {@link #bulkRegister}, or forms of
23 > * constructors establishing initial numbers of parties), and
24 > * optionally deregistered upon any arrival (using {@link
25 > * #arriveAndDeregister}).  As is the case with most basic
26 > * synchronization constructs, registration and deregistration affect
27 > * only internal counts; they do not establish any further internal
28 > * bookkeeping, so tasks cannot query whether they are registered.
29 > * (However, you can introduce such bookkeeping by subclassing this
30 > * class.)
31 > *
32 > * <p> <b>Synchronization.</b> Like a {@code CyclicBarrier}, a {@code
33 > * Phaser} may be repeatedly awaited.  Method {@link
34 > * #arriveAndAwaitAdvance} has effect analogous to {@link
35 > * java.util.concurrent.CyclicBarrier#await CyclicBarrier.await}. Each
36 > * generation of a {@code Phaser} has an associated phase number. The
37 > * phase number starts at zero, and advances when all parties arrive
38 > * at the barrier, wrapping around to zero after reaching {@code
39 > * Integer.MAX_VALUE}. The use of phase numbers enables independent
40 > * control of actions upon arrival at a barrier and upon awaiting
41 > * others, via two kinds of methods that may be invoked by any
42 > * registered party:
43   *
44   * <ul>
45   *
46 < *   <li> Arriving at a barrier. Methods {@code arrive} and
47 < *       {@code arriveAndDeregister} do not block, but return
48 < *       the phase value current upon entry to the method.
49 < *
50 < *   <li> Awaiting others. Method {@code awaitAdvance} requires an
51 < *       argument indicating the entry phase, and returns when the
52 < *       barrier advances to a new phase.
53 < * </ul>
46 > *   <li> <b>Arrival.</b> Methods {@link #arrive} and
47 > *       {@link #arriveAndDeregister} record arrival at a
48 > *       barrier. These methods do not block, but return an associated
49 > *       <em>arrival phase number</em>; that is, the phase number of
50 > *       the barrier to which the arrival applied. When the final
51 > *       party for a given phase arrives, an optional barrier action
52 > *       is performed and the phase advances.  Barrier actions,
53 > *       performed by the party triggering a phase advance, are
54 > *       arranged by overriding method {@link #onAdvance(int, int)},
55 > *       which also controls termination. Overriding this method is
56 > *       similar to, but more flexible than, providing a barrier
57 > *       action to a {@code CyclicBarrier}.
58 > *
59 > *   <li> <b>Waiting.</b> Method {@link #awaitAdvance} requires an
60 > *       argument indicating an arrival phase number, and returns when
61 > *       the barrier advances to (or is already at) a different phase.
62 > *       Unlike similar constructions using {@code CyclicBarrier},
63 > *       method {@code awaitAdvance} continues to wait even if the
64 > *       waiting thread is interrupted. Interruptible and timeout
65 > *       versions are also available, but exceptions encountered while
66 > *       tasks wait interruptibly or with timeout do not change the
67 > *       state of the barrier. If necessary, you can perform any
68 > *       associated recovery within handlers of those exceptions,
69 > *       often after invoking {@code forceTermination}.  Phasers may
70 > *       also be used by tasks executing in a {@link ForkJoinPool},
71 > *       which will ensure sufficient parallelism to execute tasks
72 > *       when others are blocked waiting for a phase to advance.
73   *
74 + * </ul>
75   *
76 < * <li> Barrier actions, performed by the task triggering a phase
77 < * advance while others may be waiting, are arranged by overriding
78 < * method {@code onAdvance}, that also controls termination.
79 < * Overriding this method may be used to similar but more flexible
80 < * effect as providing a barrier action to a CyclicBarrier.
81 < *
82 < * <li> Phasers may enter a <em>termination</em> state in which all
83 < * actions immediately return without updating phaser state or waiting
84 < * for advance, and indicating (via a negative phase value) that
85 < * execution is complete.  Termination is triggered by executing the
86 < * overridable {@code onAdvance} method that is invoked each time the
63 < * barrier is about to be tripped. When a Phaser is controlling an
64 < * action with a fixed number of iterations, it is often convenient to
65 < * override this method to cause termination when the current phase
66 < * number reaches a threshold. Method {@code forceTermination} is also
67 < * available to abruptly release waiting threads and allow them to
68 < * terminate.
76 > * <p> <b>Termination.</b> A {@code Phaser} may enter a
77 > * <em>termination</em> state in which all synchronization methods
78 > * immediately return without updating phaser state or waiting for
79 > * advance, and indicating (via a negative phase value) that execution
80 > * is complete.  Termination is triggered when an invocation of {@code
81 > * onAdvance} returns {@code true}.  As illustrated below, when
82 > * phasers control actions with a fixed number of iterations, it is
83 > * often convenient to override this method to cause termination when
84 > * the current phase number reaches a threshold. Method {@link
85 > * #forceTermination} is also available to abruptly release waiting
86 > * threads and allow them to terminate.
87   *
88 < * <li> Phasers may be tiered to reduce contention. Phasers with large
88 > * <p> <b>Tiering.</b> Phasers may be <em>tiered</em> (i.e., arranged
89 > * in tree structures) to reduce contention. Phasers with large
90   * numbers of parties that would otherwise experience heavy
91 < * synchronization contention costs may instead be arranged in trees.
92 < * This will typically greatly increase throughput even though it
93 < * incurs somewhat greater per-operation overhead.
94 < *
95 < * <li> By default, {@code awaitAdvance} continues to wait even if
96 < * the waiting thread is interrupted. And unlike the case in
97 < * CyclicBarriers, exceptions encountered while tasks wait
98 < * interruptibly or with timeout do not change the state of the
99 < * barrier. If necessary, you can perform any associated recovery
100 < * within handlers of those exceptions, often after invoking
101 < * {@code forceTermination}.
102 < *
103 < * <li>Phasers ensure lack of starvation when used by ForkJoinTasks.
104 < *
105 < * </ul>
91 > * synchronization contention costs may instead be set up so that
92 > * groups of sub-phasers share a common parent.  This may greatly
93 > * increase throughput even though it incurs greater per-operation
94 > * overhead.
95 > *
96 > * <p><b>Monitoring.</b> While synchronization methods may be invoked
97 > * only by registered parties, the current state of a phaser may be
98 > * monitored by any caller.  At any given moment there are {@link
99 > * #getRegisteredParties} parties in total, of which {@link
100 > * #getArrivedParties} have arrived at the current phase ({@link
101 > * #getPhase}).  When the remaining ({@link #getUnarrivedParties})
102 > * parties arrive, the phase advances.  The values returned by these
103 > * methods may reflect transient states and so are not in general
104 > * useful for synchronization control.  Method {@link #toString}
105 > * returns snapshots of these state queries in a form convenient for
106 > * informal monitoring.
107   *
108   * <p><b>Sample usages:</b>
109   *
110 < * <p>A Phaser may be used instead of a {@code CountDownLatch} to control
111 < * a one-shot action serving a variable number of parties. The typical
112 < * idiom is for the method setting this up to first register, then
113 < * start the actions, then deregister, as in:
110 > * <p>A {@code Phaser} may be used instead of a {@code CountDownLatch}
111 > * to control a one-shot action serving a variable number of parties.
112 > * The typical idiom is for the method setting this up to first
113 > * register, then start the actions, then deregister, as in:
114   *
115   *  <pre> {@code
116 < * void runTasks(List<Runnable> list) {
116 > * void runTasks(List<Runnable> tasks) {
117   *   final Phaser phaser = new Phaser(1); // "1" to register self
118 < *   for (Runnable r : list) {
118 > *   // create and start threads
119 > *   for (Runnable task : tasks) {
120   *     phaser.register();
121   *     new Thread() {
122   *       public void run() {
123   *         phaser.arriveAndAwaitAdvance(); // await all creation
124 < *         r.run();
104 < *         phaser.arriveAndDeregister();   // signal completion
124 > *         task.run();
125   *       }
126   *     }.start();
127   *   }
128   *
129 < *   doSomethingOnBehalfOfWorkers();
130 < *   phaser.arrive(); // allow threads to start
111 < *   int p = phaser.arriveAndDeregister(); // deregister self  ...
112 < *   p = phaser.awaitAdvance(p); // ... and await arrival
113 < *   otherActions(); // do other things while tasks execute
114 < *   phaser.awaitAdvance(p); // await final completion
129 > *   // allow threads to start and deregister self
130 > *   phaser.arriveAndDeregister();
131   * }}</pre>
132   *
133   * <p>One way to cause a set of threads to repeatedly perform actions
134   * for a given number of iterations is to override {@code onAdvance}:
135   *
136   *  <pre> {@code
137 < * void startTasks(List<Runnable> list, final int iterations) {
137 > * void startTasks(List<Runnable> tasks, final int iterations) {
138   *   final Phaser phaser = new Phaser() {
139 < *     public boolean onAdvance(int phase, int registeredParties) {
139 > *     protected boolean onAdvance(int phase, int registeredParties) {
140   *       return phase >= iterations || registeredParties == 0;
141   *     }
142   *   };
143   *   phaser.register();
144 < *   for (Runnable r : list) {
144 > *   for (final Runnable task : tasks) {
145   *     phaser.register();
146   *     new Thread() {
147   *       public void run() {
148   *         do {
149 < *           r.run();
149 > *           task.run();
150   *           phaser.arriveAndAwaitAdvance();
151 < *         } while(!phaser.isTerminated();
151 > *         } while (!phaser.isTerminated());
152   *       }
153   *     }.start();
154   *   }
155   *   phaser.arriveAndDeregister(); // deregister self, don't wait
156   * }}</pre>
157   *
158 < * <p> To create a set of tasks using a tree of Phasers,
158 > * If the main task must later await termination, it
159 > * may re-register and then execute a similar loop:
160 > *  <pre> {@code
161 > *   // ...
162 > *   phaser.register();
163 > *   while (!phaser.isTerminated())
164 > *     phaser.arriveAndAwaitAdvance();}</pre>
165 > *
166 > * <p>Related constructions may be used to await particular phase numbers
167 > * in contexts where you are sure that the phase will never wrap around
168 > * {@code Integer.MAX_VALUE}. For example:
169 > *
170 > *  <pre> {@code
171 > * void awaitPhase(Phaser phaser, int phase) {
172 > *   int p = phaser.register(); // assumes caller not already registered
173 > *   while (p < phase) {
174 > *     if (phaser.isTerminated())
175 > *       // ... deal with unexpected termination
176 > *     else
177 > *       p = phaser.arriveAndAwaitAdvance();
178 > *   }
179 > *   phaser.arriveAndDeregister();
180 > * }}</pre>
181 > *
182 > *
183 > * <p>To create a set of tasks using a tree of phasers,
184   * you could use code of the following form, assuming a
185 < * Task class with a constructor accepting a Phaser that
186 < * it registers for upon construction:
185 > * Task class with a constructor accepting a phaser that
186 > * it registers with upon construction:
187 > *
188   *  <pre> {@code
189 < * void build(Task[] actions, int lo, int hi, Phaser b) {
190 < *   int step = (hi - lo) / TASKS_PER_PHASER;
191 < *   if (step > 1) {
192 < *     int i = lo;
193 < *     while (i < hi) {
152 < *       int r = Math.min(i + step, hi);
153 < *       build(actions, i, r, new Phaser(b));
154 < *       i = r;
189 > * void build(Task[] actions, int lo, int hi, Phaser ph) {
190 > *   if (hi - lo > TASKS_PER_PHASER) {
191 > *     for (int i = lo; i < hi; i += TASKS_PER_PHASER) {
192 > *       int j = Math.min(i + TASKS_PER_PHASER, hi);
193 > *       build(actions, i, j, new Phaser(ph));
194   *     }
195   *   } else {
196   *     for (int i = lo; i < hi; ++i)
197 < *       actions[i] = new Task(b);
198 < *       // assumes new Task(b) performs b.register()
197 > *       actions[i] = new Task(ph);
198 > *       // assumes new Task(ph) performs ph.register()
199   *   }
200   * }
201   * // .. initially called, for n tasks via
# Line 167 | Line 206 | import java.util.concurrent.locks.LockSu
206   * be appropriate for extremely small per-barrier task bodies (thus
207   * high rates), or up to hundreds for extremely large ones.
208   *
170 * </pre>
171 *
209   * <p><b>Implementation notes</b>: This implementation restricts the
210   * maximum number of parties to 65535. Attempts to register additional
211 < * parties result in IllegalStateExceptions. However, you can and
211 > * parties result in {@code IllegalStateException}. However, you can and
212   * should create tiered phasers to accommodate arbitrarily large sets
213   * of participants.
214   *
# Line 205 | Line 242 | public class Phaser {
242       */
243      private volatile long state;
244  
208    private static final int ushortBits = 16;
245      private static final int ushortMask = 0xffff;
246      private static final int phaseMask  = 0x7fffffff;
247  
# Line 249 | Line 285 | public class Phaser {
285      private final Phaser parent;
286  
287      /**
288 <     * The root of Phaser tree. Equals this if not in a tree.  Used to
288 >     * The root of phaser tree. Equals this if not in a tree.  Used to
289       * support faster state push-down.
290       */
291      private final Phaser root;
# Line 258 | Line 294 | public class Phaser {
294  
295      /**
296       * Heads of Treiber stacks for waiting threads. To eliminate
297 <     * contention while releasing some threads while adding others, we
297 >     * contention when releasing some threads while adding others, we
298       * use two of them, alternating across even and odd phases.
299 +     * Subphasers share queues with root to speed up releases.
300       */
301 <    private final AtomicReference<QNode> evenQ = new AtomicReference<QNode>();
302 <    private final AtomicReference<QNode> oddQ  = new AtomicReference<QNode>();
301 >    private final AtomicReference<QNode> evenQ;
302 >    private final AtomicReference<QNode> oddQ;
303  
304      private AtomicReference<QNode> queueFor(int phase) {
305          return ((phase & 1) == 0) ? evenQ : oddQ;
# Line 280 | Line 317 | public class Phaser {
317       * Recursively resolves state.
318       */
319      private long reconcileState() {
320 <        Phaser p = parent;
320 >        Phaser par = parent;
321          long s = state;
322 <        if (p != null) {
323 <            while (unarrivedOf(s) == 0 && phaseOf(s) != phaseOf(root.state)) {
324 <                long parentState = p.getReconciledState();
325 <                int parentPhase = phaseOf(parentState);
326 <                int phase = phaseOf(s = state);
327 <                if (phase != parentPhase) {
322 >        if (par != null) {
323 >            int phase, rootPhase;
324 >            while ((phase = phaseOf(s)) >= 0 &&
325 >                   (rootPhase = phaseOf(root.state)) != phase &&
326 >                   (rootPhase < 0 || unarrivedOf(s) == 0)) {
327 >                int parentPhase = phaseOf(par.getReconciledState());
328 >                if (parentPhase != phase) {
329                      long next = trippedStateFor(parentPhase, partiesOf(s));
330 <                    if (casState(s, next)) {
331 <                        releaseWaiters(phase);
294 <                        s = next;
295 <                    }
330 >                    if (state == s)
331 >                        UNSAFE.compareAndSwapLong(this, stateOffset, s, next);
332                  }
333 +                s = state;
334              }
335          }
336          return s;
337      }
338  
339      /**
340 <     * Creates a new Phaser without any initially registered parties,
340 >     * Creates a new phaser without any initially registered parties,
341       * initial phase number 0, and no parent. Any thread using this
342 <     * Phaser will need to first register for it.
342 >     * phaser will need to first register for it.
343       */
344      public Phaser() {
345 <        this(null);
345 >        this(null, 0);
346      }
347  
348      /**
349 <     * Creates a new Phaser with the given numbers of registered
349 >     * Creates a new phaser with the given number of registered
350       * unarrived parties, initial phase number 0, and no parent.
351       *
352       * @param parties the number of parties required to trip barrier
# Line 321 | Line 358 | public class Phaser {
358      }
359  
360      /**
361 <     * Creates a new Phaser with the given parent, without any
361 >     * Creates a new phaser with the given parent, without any
362       * initially registered parties. If parent is non-null this phaser
363       * is registered with the parent and its initial phase number is
364       * the same as that of parent phaser.
# Line 329 | Line 366 | public class Phaser {
366       * @param parent the parent phaser
367       */
368      public Phaser(Phaser parent) {
369 <        int phase = 0;
333 <        this.parent = parent;
334 <        if (parent != null) {
335 <            this.root = parent.root;
336 <            phase = parent.register();
337 <        }
338 <        else
339 <            this.root = this;
340 <        this.state = trippedStateFor(phase, 0);
369 >        this(parent, 0);
370      }
371  
372      /**
373 <     * Creates a new Phaser with the given parent and numbers of
373 >     * Creates a new phaser with the given parent and number of
374       * registered unarrived parties. If parent is non-null, this phaser
375       * is registered with the parent and its initial phase number is
376       * the same as that of parent phaser.
# Line 354 | Line 383 | public class Phaser {
383      public Phaser(Phaser parent, int parties) {
384          if (parties < 0 || parties > ushortMask)
385              throw new IllegalArgumentException("Illegal number of parties");
386 <        int phase = 0;
386 >        int phase;
387          this.parent = parent;
388          if (parent != null) {
389 <            this.root = parent.root;
389 >            Phaser r = parent.root;
390 >            this.root = r;
391 >            this.evenQ = r.evenQ;
392 >            this.oddQ = r.oddQ;
393              phase = parent.register();
394          }
395 <        else
395 >        else {
396              this.root = this;
397 +            this.evenQ = new AtomicReference<QNode>();
398 +            this.oddQ = new AtomicReference<QNode>();
399 +            phase = 0;
400 +        }
401          this.state = trippedStateFor(phase, parties);
402      }
403  
404      /**
405       * Adds a new unarrived party to this phaser.
406 +     * If an ongoing invocation of {@link #onAdvance} is in progress,
407 +     * this method may wait until its completion before registering.
408       *
409 <     * @return the current barrier phase number upon registration
409 >     * @return the arrival phase number to which this registration applied
410       * @throws IllegalStateException if attempting to register more
411       * than the maximum supported number of parties
412       */
# Line 378 | Line 416 | public class Phaser {
416  
417      /**
418       * Adds the given number of new unarrived parties to this phaser.
419 +     * If an ongoing invocation of {@link #onAdvance} is in progress,
420 +     * this method may wait until its completion before registering.
421       *
422 <     * @param parties the number of parties required to trip barrier
423 <     * @return the current barrier phase number upon registration
422 >     * @param parties the number of additional parties required to trip barrier
423 >     * @return the arrival phase number to which this registration applied
424       * @throws IllegalStateException if attempting to register more
425       * than the maximum supported number of parties
426 +     * @throws IllegalArgumentException if {@code parties < 0}
427       */
428      public int bulkRegister(int parties) {
429          if (parties < 0)
# Line 396 | Line 437 | public class Phaser {
437       * Shared code for register, bulkRegister
438       */
439      private int doRegister(int registrations) {
440 +        Phaser par = parent;
441 +        long s;
442          int phase;
443 <        for (;;) {
444 <            long s = getReconciledState();
445 <            phase = phaseOf(s);
446 <            int unarrived = unarrivedOf(s) + registrations;
447 <            int parties = partiesOf(s) + registrations;
448 <            if (phase < 0)
449 <                break;
450 <            if (parties > ushortMask || unarrived > ushortMask)
443 >        while ((phase = phaseOf(s = par==null? state:reconcileState())) >= 0) {
444 >            int p = partiesOf(s);
445 >            int u = unarrivedOf(s);
446 >            int unarrived = u + registrations;
447 >            int parties = p + registrations;
448 >            if (u == 0 && p != 0)  // if tripped, wait for advance
449 >                untimedWait(phase);
450 >            else if (parties > ushortMask)
451                  throw new IllegalStateException(badBounds(parties, unarrived));
452 <            if (phase == phaseOf(root.state) &&
453 <                casState(s, stateFor(phase, parties, unarrived)))
454 <                break;
452 >            else if (par == null || phaseOf(root.state) == phase) {
453 >                long next = stateFor(phase, parties, unarrived);
454 >                if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next))
455 >                    break;
456 >            }
457          }
458          return phase;
459      }
460  
461      /**
462       * Arrives at the barrier, but does not wait for others.  (You can
463 <     * in turn wait for others via {@link #awaitAdvance}).
463 >     * in turn wait for others via {@link #awaitAdvance}).  It is an
464 >     * unenforced usage error for an unregistered party to invoke this
465 >     * method.
466       *
467 <     * @return the barrier phase number upon entry to this method, or a
421 <     * negative value if terminated
467 >     * @return the arrival phase number, or a negative value if terminated
468       * @throws IllegalStateException if not terminated and the number
469       * of unarrived parties would become negative
470       */
471      public int arrive() {
472 +        Phaser par = parent;
473 +        long s;
474          int phase;
475 <        for (;;) {
428 <            long s = state;
429 <            phase = phaseOf(s);
430 <            if (phase < 0)
431 <                break;
475 >        while ((phase = phaseOf(s = par==null? state:reconcileState())) >= 0) {
476              int parties = partiesOf(s);
477              int unarrived = unarrivedOf(s) - 1;
478 <            if (unarrived > 0) {        // Not the last arrival
479 <                if (casState(s, s - 1)) // s-1 adds one arrival
480 <                    break;
478 >            if (unarrived > 0) {                // Not the last arrival
479 >                if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s - 1))
480 >                    break;                      // s-1 adds one arrival
481              }
482 <            else if (unarrived == 0) {  // the last arrival
483 <                Phaser par = parent;
484 <                if (par == null) {      // directly trip
485 <                    if (casState
486 <                        (s,
487 <                         trippedStateFor(onAdvance(phase, parties) ? -1 :
488 <                                         ((phase + 1) & phaseMask), parties))) {
489 <                        releaseWaiters(phase);
490 <                        break;
447 <                    }
448 <                }
449 <                else {                  // cascade to parent
450 <                    if (casState(s, s - 1)) { // zeroes unarrived
451 <                        par.arrive();
452 <                        reconcileState();
453 <                        break;
454 <                    }
482 >            else if (unarrived < 0)
483 >                throw new IllegalStateException(badBounds(parties, unarrived));
484 >            else if (par == null) {             // directly trip
485 >                long next = trippedStateFor(onAdvance(phase, parties) ? -1 :
486 >                                            ((phase + 1) & phaseMask),
487 >                                            parties);
488 >                if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) {
489 >                    releaseWaiters(phase);
490 >                    break;
491                  }
492              }
493 <            else if (phase != phaseOf(root.state)) // or if unreconciled
493 >            else if (phaseOf(root.state) == phase &&
494 >                     UNSAFE.compareAndSwapLong(this, stateOffset, s, s - 1)) {
495 >                par.arrive();                   // cascade to parent
496                  reconcileState();
497 <            else
498 <                throw new IllegalStateException(badBounds(parties, unarrived));
497 >                break;
498 >            }
499          }
500          return phase;
501      }
502  
503      /**
504 <     * Arrives at the barrier, and deregisters from it, without
505 <     * waiting for others. Deregistration reduces number of parties
504 >     * Arrives at the barrier and deregisters from it without waiting
505 >     * for others. Deregistration reduces the number of parties
506       * required to trip the barrier in future phases.  If this phaser
507       * has a parent, and deregistration causes this phaser to have
508 <     * zero parties, this phaser is also deregistered from its parent.
508 >     * zero parties, this phaser also arrives at and is deregistered
509 >     * from its parent.  It is an unenforced usage error for an
510 >     * unregistered party to invoke this method.
511       *
512 <     * @return the current barrier phase number upon entry to
473 <     * this method, or a negative value if terminated
512 >     * @return the arrival phase number, or a negative value if terminated
513       * @throws IllegalStateException if not terminated and the number
514       * of registered or unarrived parties would become negative
515       */
516      public int arriveAndDeregister() {
517 <        // similar code to arrive, but too different to merge
517 >        // similar to arrive, but too different to merge
518          Phaser par = parent;
519 +        long s;
520          int phase;
521 <        for (;;) {
482 <            long s = state;
483 <            phase = phaseOf(s);
484 <            if (phase < 0)
485 <                break;
521 >        while ((phase = phaseOf(s = par==null? state:reconcileState())) >= 0) {
522              int parties = partiesOf(s) - 1;
523              int unarrived = unarrivedOf(s) - 1;
524 <            if (parties >= 0) {
525 <                if (unarrived > 0 || (unarrived == 0 && par != null)) {
526 <                    if (casState
527 <                        (s,
528 <                         stateFor(phase, parties, unarrived))) {
529 <                        if (unarrived == 0) {
530 <                            par.arriveAndDeregister();
531 <                            reconcileState();
532 <                        }
533 <                        break;
534 <                    }
535 <                    continue;
536 <                }
537 <                if (unarrived == 0) {
502 <                    if (casState
503 <                        (s,
504 <                         trippedStateFor(onAdvance(phase, parties) ? -1 :
505 <                                         ((phase + 1) & phaseMask), parties))) {
506 <                        releaseWaiters(phase);
507 <                        break;
508 <                    }
509 <                    continue;
524 >            if (unarrived > 0) {
525 >                long next = stateFor(phase, parties, unarrived);
526 >                if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next))
527 >                    break;
528 >            }
529 >            else if (unarrived < 0)
530 >                throw new IllegalStateException(badBounds(parties, unarrived));
531 >            else if (par == null) {
532 >                long next = trippedStateFor(onAdvance(phase, parties)? -1:
533 >                                            (phase + 1) & phaseMask,
534 >                                            parties);
535 >                if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) {
536 >                    releaseWaiters(phase);
537 >                    break;
538                  }
539 <                if (par != null && phase != phaseOf(root.state)) {
539 >            }
540 >            else if (phaseOf(root.state) == phase) {
541 >                long next = stateFor(phase, parties, 0);
542 >                if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) {
543 >                    if (parties == 0)
544 >                        par.arriveAndDeregister();
545 >                    else
546 >                        par.arrive();
547                      reconcileState();
548 <                    continue;
548 >                    break;
549                  }
550              }
516            throw new IllegalStateException(badBounds(parties, unarrived));
551          }
552          return phase;
553      }
554  
555      /**
556       * Arrives at the barrier and awaits others. Equivalent in effect
557 <     * to {@code awaitAdvance(arrive())}.  If you instead need to
558 <     * await with interruption of timeout, and/or deregister upon
559 <     * arrival, you can arrange them using analogous constructions.
557 >     * to {@code awaitAdvance(arrive())}.  If you need to await with
558 >     * interruption or timeout, you can arrange this with an analogous
559 >     * construction using one of the other forms of the {@code
560 >     * awaitAdvance} method.  If instead you need to deregister upon
561 >     * arrival, use {@link #arriveAndDeregister}. It is an unenforced
562 >     * usage error for an unregistered party to invoke this method.
563       *
564 <     * @return the phase on entry to this method
564 >     * @return the arrival phase number, or a negative number if terminated
565       * @throws IllegalStateException if not terminated and the number
566       * of unarrived parties would become negative
567       */
# Line 533 | Line 570 | public class Phaser {
570      }
571  
572      /**
573 <     * Awaits the phase of the barrier to advance from the given
574 <     * value, or returns immediately if argument is negative or this
575 <     * barrier is terminated.
576 <     *
577 <     * @param phase the phase on entry to this method
578 <     * @return the phase on exit from this method
573 >     * Awaits the phase of the barrier to advance from the given phase
574 >     * value, returning immediately if the current phase of the
575 >     * barrier is not equal to the given phase value or this barrier
576 >     * is terminated.
577 >     *
578 >     * @param phase an arrival phase number, or negative value if
579 >     * terminated; this argument is normally the value returned by a
580 >     * previous call to {@code arrive} or its variants
581 >     * @return the next arrival phase number, or a negative value
582 >     * if terminated or argument is negative
583       */
584      public int awaitAdvance(int phase) {
585          if (phase < 0)
586              return phase;
587 <        long s = getReconciledState();
547 <        int p = phaseOf(s);
587 >        int p = getPhase();
588          if (p != phase)
589              return p;
550        if (unarrivedOf(s) == 0 && parent != null)
551            parent.awaitAdvance(phase);
552        // Fall here even if parent waited, to reconcile and help release
590          return untimedWait(phase);
591      }
592  
593      /**
594 <     * Awaits the phase of the barrier to advance from the given
595 <     * value, or returns immediately if argument is negative or this
596 <     * barrier is terminated, or throws InterruptedException if
597 <     * interrupted while waiting.
594 >     * Awaits the phase of the barrier to advance from the given phase
595 >     * value, throwing {@code InterruptedException} if interrupted
596 >     * while waiting, or returning immediately if the current phase of
597 >     * the barrier is not equal to the given phase value or this
598 >     * barrier is terminated.
599       *
600 <     * @param phase the phase on entry to this method
601 <     * @return the phase on exit from this method
600 >     * @param phase an arrival phase number, or negative value if
601 >     * terminated; this argument is normally the value returned by a
602 >     * previous call to {@code arrive} or its variants
603 >     * @return the next arrival phase number, or a negative value
604 >     * if terminated or argument is negative
605       * @throws InterruptedException if thread interrupted while waiting
606       */
607      public int awaitAdvanceInterruptibly(int phase)
608          throws InterruptedException {
609          if (phase < 0)
610              return phase;
611 <        long s = getReconciledState();
571 <        int p = phaseOf(s);
611 >        int p = getPhase();
612          if (p != phase)
613              return p;
574        if (unarrivedOf(s) == 0 && parent != null)
575            parent.awaitAdvanceInterruptibly(phase);
614          return interruptibleWait(phase);
615      }
616  
617      /**
618 <     * Awaits the phase of the barrier to advance from the given value
619 <     * or the given timeout elapses, or returns immediately if
620 <     * argument is negative or this barrier is terminated.
621 <     *
622 <     * @param phase the phase on entry to this method
623 <     * @return the phase on exit from this method
618 >     * Awaits the phase of the barrier to advance from the given phase
619 >     * value or the given timeout to elapse, throwing {@code
620 >     * InterruptedException} if interrupted while waiting, or
621 >     * returning immediately if the current phase of the barrier is
622 >     * not equal to the given phase value or this barrier is
623 >     * terminated.
624 >     *
625 >     * @param phase an arrival phase number, or negative value if
626 >     * terminated; this argument is normally the value returned by a
627 >     * previous call to {@code arrive} or its variants
628 >     * @param timeout how long to wait before giving up, in units of
629 >     *        {@code unit}
630 >     * @param unit a {@code TimeUnit} determining how to interpret the
631 >     *        {@code timeout} parameter
632 >     * @return the next arrival phase number, or a negative value
633 >     * if terminated or argument is negative
634       * @throws InterruptedException if thread interrupted while waiting
635       * @throws TimeoutException if timed out while waiting
636       */
637      public int awaitAdvanceInterruptibly(int phase,
638                                           long timeout, TimeUnit unit)
639          throws InterruptedException, TimeoutException {
640 +        long nanos = unit.toNanos(timeout);
641          if (phase < 0)
642              return phase;
643 <        long s = getReconciledState();
595 <        int p = phaseOf(s);
643 >        int p = getPhase();
644          if (p != phase)
645              return p;
646 <        if (unarrivedOf(s) == 0 && parent != null)
599 <            parent.awaitAdvanceInterruptibly(phase, timeout, unit);
600 <        return timedWait(phase, unit.toNanos(timeout));
646 >        return timedWait(phase, nanos);
647      }
648  
649      /**
# Line 608 | Line 654 | public class Phaser {
654       * unexpected exceptions.
655       */
656      public void forceTermination() {
657 <        for (;;) {
658 <            long s = getReconciledState();
659 <            int phase = phaseOf(s);
660 <            int parties = partiesOf(s);
661 <            int unarrived = unarrivedOf(s);
662 <            if (phase < 0 ||
663 <                casState(s, stateFor(-1, parties, unarrived))) {
664 <                releaseWaiters(0);
665 <                releaseWaiters(1);
620 <                if (parent != null)
621 <                    parent.forceTermination();
622 <                return;
623 <            }
624 <        }
657 >        Phaser r = root;    // force at root then reconcile
658 >        long s;
659 >        while (phaseOf(s = r.state) >= 0)
660 >            UNSAFE.compareAndSwapLong(r, stateOffset, s,
661 >                                      stateFor(-1, partiesOf(s),
662 >                                               unarrivedOf(s)));
663 >        reconcileState();
664 >        releaseWaiters(0);  // ensure wakeups on both queues
665 >        releaseWaiters(1);
666      }
667  
668      /**
# Line 636 | Line 677 | public class Phaser {
677      }
678  
679      /**
639     * Returns {@code true} if the current phase number equals the given phase.
640     *
641     * @param phase the phase
642     * @return {@code true} if the current phase number equals the given phase
643     */
644    public final boolean hasPhase(int phase) {
645        return phaseOf(getReconciledState()) == phase;
646    }
647
648    /**
680       * Returns the number of parties registered at this barrier.
681       *
682       * @return the number of parties
683       */
684      public int getRegisteredParties() {
685 <        return partiesOf(state);
685 >        return partiesOf(getReconciledState());
686      }
687  
688      /**
689 <     * Returns the number of parties that have arrived at the current
690 <     * phase of this barrier.
689 >     * Returns the number of registered parties that have arrived at
690 >     * the current phase of this barrier.
691       *
692       * @return the number of arrived parties
693       */
694      public int getArrivedParties() {
695 <        return arrivedOf(state);
695 >        return arrivedOf(getReconciledState());
696      }
697  
698      /**
# Line 671 | Line 702 | public class Phaser {
702       * @return the number of unarrived parties
703       */
704      public int getUnarrivedParties() {
705 <        return unarrivedOf(state);
705 >        return unarrivedOf(getReconciledState());
706      }
707  
708      /**
# Line 703 | Line 734 | public class Phaser {
734      }
735  
736      /**
737 <     * Overridable method to perform an action upon phase advance, and
738 <     * to control termination. This method is invoked whenever the
739 <     * barrier is tripped (and thus all other waiting parties are
740 <     * dormant). If it returns {@code true}, then, rather than advance
741 <     * the phase number, this barrier will be set to a final
742 <     * termination state, and subsequent calls to {@link #isTerminated}
743 <     * will return true.
737 >     * Overridable method to perform an action upon impending phase
738 >     * advance, and to control termination. This method is invoked
739 >     * upon arrival of the party tripping the barrier (when all other
740 >     * waiting parties are dormant).  If this method returns {@code
741 >     * true}, then, rather than advance the phase number, this barrier
742 >     * will be set to a final termination state, and subsequent calls
743 >     * to {@link #isTerminated} will return true. Any (unchecked)
744 >     * Exception or Error thrown by an invocation of this method is
745 >     * propagated to the party attempting to trip the barrier, in
746 >     * which case no advance occurs.
747 >     *
748 >     * <p>The arguments to this method provide the state of the phaser
749 >     * prevailing for the current transition.  The results and effects
750 >     * of invoking phase-related methods (including {@code getPhase}
751 >     * as well as arrival, registration, and waiting methods) from
752 >     * within {@code onAdvance} are unspecified and should not be
753 >     * relied on. Similarly, while it is possible to override this
754 >     * method to produce side-effects visible to participating tasks,
755 >     * it is in general safe to do so only in designs in which all
756 >     * parties register before any arrive, and all {@link
757 >     * #awaitAdvance} at each phase.
758       *
759 <     * <p> The default version returns {@code true} when the number of
759 >     * <p>The default version returns {@code true} when the number of
760       * registered parties is zero. Normally, overrides that arrange
761       * termination for other reasons should also preserve this
762       * property.
763       *
719     * <p> You may override this method to perform an action with side
720     * effects visible to participating tasks, but it is in general
721     * only sensible to do so in designs where all parties register
722     * before any arrive, and all {@code awaitAdvance} at each phase.
723     * Otherwise, you cannot ensure lack of interference. In
724     * particular, this method may be invoked more than once per
725     * transition if other parties successfully register while the
726     * invocation of this method is in progress, thus postponing the
727     * transition until those parties also arrive, re-triggering this
728     * method.
729     *
764       * @param phase the phase number on entering the barrier
765       * @param registeredParties the current number of registered parties
766       * @return {@code true} if this barrier should terminate
# Line 767 | Line 801 | public class Phaser {
801          volatile boolean wasInterrupted = false;
802          volatile Thread thread; // nulled to cancel wait
803          QNode next;
804 +
805          QNode(Phaser phaser, int phase, boolean interruptible,
806                boolean timed, long startTime, long nanos) {
807              this.phaser = phaser;
# Line 777 | Line 812 | public class Phaser {
812              this.nanos = nanos;
813              thread = Thread.currentThread();
814          }
815 +
816          public boolean isReleasable() {
817              return (thread == null ||
818                      phaser.getPhase() != phase ||
819                      (interruptible && wasInterrupted) ||
820                      (timed && (nanos - (System.nanoTime() - startTime)) <= 0));
821          }
822 +
823          public boolean block() {
824              if (Thread.interrupted()) {
825                  wasInterrupted = true;
# Line 799 | Line 836 | public class Phaser {
836              }
837              return isReleasable();
838          }
839 +
840          void signal() {
841              Thread t = thread;
842              if (t != null) {
# Line 806 | Line 844 | public class Phaser {
844                  LockSupport.unpark(t);
845              }
846          }
847 +
848          boolean doWait() {
849              if (thread != null) {
850                  try {
851 <                    ForkJoinPool.managedBlock(this, false);
851 >                    ForkJoinPool.managedBlock(this);
852                  } catch (InterruptedException ie) {
853 +                    wasInterrupted = true; // can't currently happen
854                  }
855              }
856              return wasInterrupted;
857          }
818
858      }
859  
860      /**
# Line 841 | Line 880 | public class Phaser {
880      }
881  
882      /**
883 +     * The number of times to spin before blocking waiting for advance.
884 +     */
885 +    static final int MAX_SPINS =
886 +        Runtime.getRuntime().availableProcessors() == 1 ? 0 : 1 << 8;
887 +
888 +    /**
889       * Enqueues node and waits unless aborted or signalled.
890       *
891       * @return current phase
# Line 849 | Line 894 | public class Phaser {
894          QNode node = null;
895          boolean queued = false;
896          boolean interrupted = false;
897 +        int spins = MAX_SPINS;
898          int p;
899          while ((p = getPhase()) == phase) {
900              if (Thread.interrupted())
901                  interrupted = true;
902 +            else if (spins > 0) {
903 +                if (--spins == 0)
904 +                    Thread.yield();
905 +            }
906              else if (node == null)
907                  node = new QNode(this, phase, false, false, 0, 0);
908              else if (!queued)
909                  queued = tryEnqueue(node);
910 <            else
911 <                interrupted = node.doWait();
910 >            else if (node.doWait())
911 >                interrupted = true;
912          }
913          if (node != null)
914              node.thread = null;
# Line 876 | Line 926 | public class Phaser {
926          QNode node = null;
927          boolean queued = false;
928          boolean interrupted = false;
929 +        int spins = MAX_SPINS;
930          int p;
931          while ((p = getPhase()) == phase && !interrupted) {
932              if (Thread.interrupted())
933                  interrupted = true;
934 +            else if (spins > 0) {
935 +                if (--spins == 0)
936 +                    Thread.yield();
937 +            }
938              else if (node == null)
939                  node = new QNode(this, phase, true, false, 0, 0);
940              else if (!queued)
941                  queued = tryEnqueue(node);
942 <            else
943 <                interrupted = node.doWait();
942 >            else if (node.doWait())
943 >                interrupted = true;
944          }
945          if (node != null)
946              node.thread = null;
# Line 906 | Line 961 | public class Phaser {
961          QNode node = null;
962          boolean queued = false;
963          boolean interrupted = false;
964 +        int spins = MAX_SPINS;
965          int p;
966          while ((p = getPhase()) == phase && !interrupted) {
967              if (Thread.interrupted())
968                  interrupted = true;
969              else if (nanos - (System.nanoTime() - startTime) <= 0)
970                  break;
971 +            else if (spins > 0) {
972 +                if (--spins == 0)
973 +                    Thread.yield();
974 +            }
975              else if (node == null)
976                  node = new QNode(this, phase, true, true, startTime, nanos);
977              else if (!queued)
978                  queued = tryEnqueue(node);
979 <            else
980 <                interrupted = node.doWait();
979 >            else if (node.doWait())
980 >                interrupted = true;
981          }
982          if (node != null)
983              node.thread = null;
# Line 936 | Line 996 | public class Phaser {
996      private static final long stateOffset =
997          objectFieldOffset("state", Phaser.class);
998  
939    private final boolean casState(long cmp, long val) {
940        return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val);
941    }
942
999      private static long objectFieldOffset(String field, Class<?> klazz) {
1000          try {
1001              return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines