jaroslav@1890: /* jaroslav@1890: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1890: * jaroslav@1890: * This code is free software; you can redistribute it and/or modify it jaroslav@1890: * under the terms of the GNU General Public License version 2 only, as jaroslav@1890: * published by the Free Software Foundation. Oracle designates this jaroslav@1890: * particular file as subject to the "Classpath" exception as provided jaroslav@1890: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1890: * jaroslav@1890: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1890: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1890: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1890: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1890: * accompanied this code). jaroslav@1890: * jaroslav@1890: * You should have received a copy of the GNU General Public License version jaroslav@1890: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1890: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1890: * jaroslav@1890: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1890: * or visit www.oracle.com if you need additional information or have any jaroslav@1890: * questions. jaroslav@1890: */ jaroslav@1890: jaroslav@1890: /* jaroslav@1890: * This file is available under and governed by the GNU General Public jaroslav@1890: * License version 2 only, as published by the Free Software Foundation. jaroslav@1890: * However, the following notice accompanied the original version of this jaroslav@1890: * file: jaroslav@1890: * jaroslav@1890: * Written by Doug Lea with assistance from members of JCP JSR-166 jaroslav@1890: * Expert Group and released to the public domain, as explained at jaroslav@1890: * http://creativecommons.org/publicdomain/zero/1.0/ jaroslav@1890: */ jaroslav@1890: jaroslav@1890: package java.util.concurrent; jaroslav@1890: jaroslav@1890: import java.util.concurrent.TimeUnit; jaroslav@1890: import java.util.concurrent.TimeoutException; jaroslav@1890: import java.util.concurrent.atomic.AtomicReference; jaroslav@1890: import java.util.concurrent.locks.LockSupport; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * A reusable synchronization barrier, similar in functionality to jaroslav@1890: * {@link java.util.concurrent.CyclicBarrier CyclicBarrier} and jaroslav@1890: * {@link java.util.concurrent.CountDownLatch CountDownLatch} jaroslav@1890: * but supporting more flexible usage. jaroslav@1890: * jaroslav@1890: *
Registration. Unlike the case for other barriers, the jaroslav@1890: * number of parties registered to synchronize on a phaser jaroslav@1890: * may vary over time. Tasks may be registered at any time (using jaroslav@1890: * methods {@link #register}, {@link #bulkRegister}, or forms of jaroslav@1890: * constructors establishing initial numbers of parties), and jaroslav@1890: * optionally deregistered upon any arrival (using {@link jaroslav@1890: * #arriveAndDeregister}). As is the case with most basic jaroslav@1890: * synchronization constructs, registration and deregistration affect jaroslav@1890: * only internal counts; they do not establish any further internal jaroslav@1890: * bookkeeping, so tasks cannot query whether they are registered. jaroslav@1890: * (However, you can introduce such bookkeeping by subclassing this jaroslav@1890: * class.) jaroslav@1890: * jaroslav@1890: *
Synchronization. Like a {@code CyclicBarrier}, a {@code jaroslav@1890: * Phaser} may be repeatedly awaited. Method {@link jaroslav@1890: * #arriveAndAwaitAdvance} has effect analogous to {@link jaroslav@1890: * java.util.concurrent.CyclicBarrier#await CyclicBarrier.await}. Each jaroslav@1890: * generation of a phaser has an associated phase number. The phase jaroslav@1890: * number starts at zero, and advances when all parties arrive at the jaroslav@1890: * phaser, wrapping around to zero after reaching {@code jaroslav@1890: * Integer.MAX_VALUE}. The use of phase numbers enables independent jaroslav@1890: * control of actions upon arrival at a phaser and upon awaiting jaroslav@1890: * others, via two kinds of methods that may be invoked by any jaroslav@1890: * registered party: jaroslav@1890: * jaroslav@1890: *
Termination. A phaser may enter a termination jaroslav@1890: * state, that may be checked using method {@link #isTerminated}. Upon jaroslav@1890: * termination, all synchronization methods immediately return without jaroslav@1890: * waiting for advance, as indicated by a negative return value. jaroslav@1890: * Similarly, attempts to register upon termination have no effect. jaroslav@1890: * Termination is triggered when an invocation of {@code onAdvance} jaroslav@1890: * returns {@code true}. The default implementation returns {@code jaroslav@1890: * true} if a deregistration has caused the number of registered jaroslav@1890: * parties to become zero. As illustrated below, when phasers control jaroslav@1890: * actions with a fixed number of iterations, it is often convenient jaroslav@1890: * to override this method to cause termination when the current phase jaroslav@1890: * number reaches a threshold. Method {@link #forceTermination} is jaroslav@1890: * also available to abruptly release waiting threads and allow them jaroslav@1890: * to terminate. jaroslav@1890: * jaroslav@1890: *
Tiering. Phasers may be tiered (i.e., jaroslav@1890: * constructed in tree structures) to reduce contention. Phasers with jaroslav@1890: * large numbers of parties that would otherwise experience heavy jaroslav@1890: * synchronization contention costs may instead be set up so that jaroslav@1890: * groups of sub-phasers share a common parent. This may greatly jaroslav@1890: * increase throughput even though it incurs greater per-operation jaroslav@1890: * overhead. jaroslav@1890: * jaroslav@1890: *
In a tree of tiered phasers, registration and deregistration of jaroslav@1890: * child phasers with their parent are managed automatically. jaroslav@1890: * Whenever the number of registered parties of a child phaser becomes jaroslav@1890: * non-zero (as established in the {@link #Phaser(Phaser,int)} jaroslav@1890: * constructor, {@link #register}, or {@link #bulkRegister}), the jaroslav@1890: * child phaser is registered with its parent. Whenever the number of jaroslav@1890: * registered parties becomes zero as the result of an invocation of jaroslav@1890: * {@link #arriveAndDeregister}, the child phaser is deregistered jaroslav@1890: * from its parent. jaroslav@1890: * jaroslav@1890: *
Monitoring. While synchronization methods may be invoked jaroslav@1890: * only by registered parties, the current state of a phaser may be jaroslav@1890: * monitored by any caller. At any given moment there are {@link jaroslav@1890: * #getRegisteredParties} parties in total, of which {@link jaroslav@1890: * #getArrivedParties} have arrived at the current phase ({@link jaroslav@1890: * #getPhase}). When the remaining ({@link #getUnarrivedParties}) jaroslav@1890: * parties arrive, the phase advances. The values returned by these jaroslav@1890: * methods may reflect transient states and so are not in general jaroslav@1890: * useful for synchronization control. Method {@link #toString} jaroslav@1890: * returns snapshots of these state queries in a form convenient for jaroslav@1890: * informal monitoring. jaroslav@1890: * jaroslav@1890: *
Sample usages: jaroslav@1890: * jaroslav@1890: *
A {@code Phaser} may be used instead of a {@code CountDownLatch} jaroslav@1890: * to control a one-shot action serving a variable number of parties. jaroslav@1890: * The typical idiom is for the method setting this up to first jaroslav@1890: * register, then start the actions, then deregister, as in: jaroslav@1890: * jaroslav@1890: *
{@code jaroslav@1890: * void runTasks(Listjaroslav@1890: * jaroslav@1890: *tasks) { jaroslav@1890: * final Phaser phaser = new Phaser(1); // "1" to register self jaroslav@1890: * // create and start threads jaroslav@1890: * for (final Runnable task : tasks) { jaroslav@1890: * phaser.register(); jaroslav@1890: * new Thread() { jaroslav@1890: * public void run() { jaroslav@1890: * phaser.arriveAndAwaitAdvance(); // await all creation jaroslav@1890: * task.run(); jaroslav@1890: * } jaroslav@1890: * }.start(); jaroslav@1890: * } jaroslav@1890: * jaroslav@1890: * // allow threads to start and deregister self jaroslav@1890: * phaser.arriveAndDeregister(); jaroslav@1890: * }}
One way to cause a set of threads to repeatedly perform actions jaroslav@1890: * for a given number of iterations is to override {@code onAdvance}: jaroslav@1890: * jaroslav@1890: *
{@code jaroslav@1890: * void startTasks(Listjaroslav@1890: * jaroslav@1890: * If the main task must later await termination, it jaroslav@1890: * may re-register and then execute a similar loop: jaroslav@1890: *tasks, final int iterations) { jaroslav@1890: * final Phaser phaser = new Phaser() { jaroslav@1890: * protected boolean onAdvance(int phase, int registeredParties) { jaroslav@1890: * return phase >= iterations || registeredParties == 0; jaroslav@1890: * } jaroslav@1890: * }; jaroslav@1890: * phaser.register(); jaroslav@1890: * for (final Runnable task : tasks) { jaroslav@1890: * phaser.register(); jaroslav@1890: * new Thread() { jaroslav@1890: * public void run() { jaroslav@1890: * do { jaroslav@1890: * task.run(); jaroslav@1890: * phaser.arriveAndAwaitAdvance(); jaroslav@1890: * } while (!phaser.isTerminated()); jaroslav@1890: * } jaroslav@1890: * }.start(); jaroslav@1890: * } jaroslav@1890: * phaser.arriveAndDeregister(); // deregister self, don't wait jaroslav@1890: * }}
{@code jaroslav@1890: * // ... jaroslav@1890: * phaser.register(); jaroslav@1890: * while (!phaser.isTerminated()) jaroslav@1890: * phaser.arriveAndAwaitAdvance();}jaroslav@1890: * jaroslav@1890: *
Related constructions may be used to await particular phase numbers jaroslav@1890: * in contexts where you are sure that the phase will never wrap around jaroslav@1890: * {@code Integer.MAX_VALUE}. For example: jaroslav@1890: * jaroslav@1890: *
{@code jaroslav@1890: * void awaitPhase(Phaser phaser, int phase) { jaroslav@1890: * int p = phaser.register(); // assumes caller not already registered jaroslav@1890: * while (p < phase) { jaroslav@1890: * if (phaser.isTerminated()) jaroslav@1890: * // ... deal with unexpected termination jaroslav@1890: * else jaroslav@1890: * p = phaser.arriveAndAwaitAdvance(); jaroslav@1890: * } jaroslav@1890: * phaser.arriveAndDeregister(); jaroslav@1890: * }}jaroslav@1890: * jaroslav@1890: * jaroslav@1890: *
To create a set of {@code n} tasks using a tree of phasers, you jaroslav@1890: * could use code of the following form, assuming a Task class with a jaroslav@1890: * constructor accepting a {@code Phaser} that it registers with upon jaroslav@1890: * construction. After invocation of {@code build(new Task[n], 0, n, jaroslav@1890: * new Phaser())}, these tasks could then be started, for example by jaroslav@1890: * submitting to a pool: jaroslav@1890: * jaroslav@1890: *
{@code jaroslav@1890: * void build(Task[] tasks, int lo, int hi, Phaser ph) { jaroslav@1890: * if (hi - lo > TASKS_PER_PHASER) { jaroslav@1890: * for (int i = lo; i < hi; i += TASKS_PER_PHASER) { jaroslav@1890: * int j = Math.min(i + TASKS_PER_PHASER, hi); jaroslav@1890: * build(tasks, i, j, new Phaser(ph)); jaroslav@1890: * } jaroslav@1890: * } else { jaroslav@1890: * for (int i = lo; i < hi; ++i) jaroslav@1890: * tasks[i] = new Task(ph); jaroslav@1890: * // assumes new Task(ph) performs ph.register() jaroslav@1890: * } jaroslav@1890: * }}jaroslav@1890: * jaroslav@1890: * The best value of {@code TASKS_PER_PHASER} depends mainly on jaroslav@1890: * expected synchronization rates. A value as low as four may jaroslav@1890: * be appropriate for extremely small per-phase task bodies (thus jaroslav@1890: * high rates), or up to hundreds for extremely large ones. jaroslav@1890: * jaroslav@1890: *
Implementation notes: This implementation restricts the
jaroslav@1890: * maximum number of parties to 65535. Attempts to register additional
jaroslav@1890: * parties result in {@code IllegalStateException}. However, you can and
jaroslav@1890: * should create tiered phasers to accommodate arbitrarily large sets
jaroslav@1890: * of participants.
jaroslav@1890: *
jaroslav@1890: * @since 1.7
jaroslav@1890: * @author Doug Lea
jaroslav@1890: */
jaroslav@1890: public class Phaser {
jaroslav@1890: /*
jaroslav@1890: * This class implements an extension of X10 "clocks". Thanks to
jaroslav@1890: * Vijay Saraswat for the idea, and to Vivek Sarkar for
jaroslav@1890: * enhancements to extend functionality.
jaroslav@1890: */
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Primary state representation, holding four bit-fields:
jaroslav@1890: *
jaroslav@1890: * unarrived -- the number of parties yet to hit barrier (bits 0-15)
jaroslav@1890: * parties -- the number of parties to wait (bits 16-31)
jaroslav@1890: * phase -- the generation of the barrier (bits 32-62)
jaroslav@1890: * terminated -- set if barrier is terminated (bit 63 / sign)
jaroslav@1890: *
jaroslav@1890: * Except that a phaser with no registered parties is
jaroslav@1890: * distinguished by the otherwise illegal state of having zero
jaroslav@1890: * parties and one unarrived parties (encoded as EMPTY below).
jaroslav@1890: *
jaroslav@1890: * To efficiently maintain atomicity, these values are packed into
jaroslav@1890: * a single (atomic) long. Good performance relies on keeping
jaroslav@1890: * state decoding and encoding simple, and keeping race windows
jaroslav@1890: * short.
jaroslav@1890: *
jaroslav@1890: * All state updates are performed via CAS except initial
jaroslav@1890: * registration of a sub-phaser (i.e., one with a non-null
jaroslav@1890: * parent). In this (relatively rare) case, we use built-in
jaroslav@1890: * synchronization to lock while first registering with its
jaroslav@1890: * parent.
jaroslav@1890: *
jaroslav@1890: * The phase of a subphaser is allowed to lag that of its
jaroslav@1890: * ancestors until it is actually accessed -- see method
jaroslav@1890: * reconcileState.
jaroslav@1890: */
jaroslav@1890: private volatile long state;
jaroslav@1890:
jaroslav@1890: private static final int MAX_PARTIES = 0xffff;
jaroslav@1890: private static final int MAX_PHASE = Integer.MAX_VALUE;
jaroslav@1890: private static final int PARTIES_SHIFT = 16;
jaroslav@1890: private static final int PHASE_SHIFT = 32;
jaroslav@1890: private static final int UNARRIVED_MASK = 0xffff; // to mask ints
jaroslav@1890: private static final long PARTIES_MASK = 0xffff0000L; // to mask longs
jaroslav@1890: private static final long TERMINATION_BIT = 1L << 63;
jaroslav@1890:
jaroslav@1890: // some special values
jaroslav@1890: private static final int ONE_ARRIVAL = 1;
jaroslav@1890: private static final int ONE_PARTY = 1 << PARTIES_SHIFT;
jaroslav@1890: private static final int EMPTY = 1;
jaroslav@1890:
jaroslav@1890: // The following unpacking methods are usually manually inlined
jaroslav@1890:
jaroslav@1890: private static int unarrivedOf(long s) {
jaroslav@1890: int counts = (int)s;
jaroslav@1890: return (counts == EMPTY) ? 0 : counts & UNARRIVED_MASK;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: private static int partiesOf(long s) {
jaroslav@1890: return (int)s >>> PARTIES_SHIFT;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: private static int phaseOf(long s) {
jaroslav@1890: return (int)(s >>> PHASE_SHIFT);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: private static int arrivedOf(long s) {
jaroslav@1890: int counts = (int)s;
jaroslav@1890: return (counts == EMPTY) ? 0 :
jaroslav@1890: (counts >>> PARTIES_SHIFT) - (counts & UNARRIVED_MASK);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * The parent of this phaser, or null if none
jaroslav@1890: */
jaroslav@1890: private final Phaser parent;
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * The root of phaser tree. Equals this if not in a tree.
jaroslav@1890: */
jaroslav@1890: private final Phaser root;
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Heads of Treiber stacks for waiting threads. To eliminate
jaroslav@1890: * contention when releasing some threads while adding others, we
jaroslav@1890: * use two of them, alternating across even and odd phases.
jaroslav@1890: * Subphasers share queues with root to speed up releases.
jaroslav@1890: */
jaroslav@1890: private final AtomicReference It is a usage error for an unregistered party to invoke this
jaroslav@1890: * method. However, this error may result in an {@code
jaroslav@1890: * IllegalStateException} only upon some subsequent operation on
jaroslav@1890: * this phaser, if ever.
jaroslav@1890: *
jaroslav@1890: * @return the arrival phase number, or a negative value if terminated
jaroslav@1890: * @throws IllegalStateException if not terminated and the number
jaroslav@1890: * of unarrived parties would become negative
jaroslav@1890: */
jaroslav@1890: public int arrive() {
jaroslav@1890: return doArrive(false);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Arrives at this phaser and deregisters from it without waiting
jaroslav@1890: * for others to arrive. Deregistration reduces the number of
jaroslav@1890: * parties required to advance in future phases. If this phaser
jaroslav@1890: * has a parent, and deregistration causes this phaser to have
jaroslav@1890: * zero parties, this phaser is also deregistered from its parent.
jaroslav@1890: *
jaroslav@1890: * It is a usage error for an unregistered party to invoke this
jaroslav@1890: * method. However, this error may result in an {@code
jaroslav@1890: * IllegalStateException} only upon some subsequent operation on
jaroslav@1890: * this phaser, if ever.
jaroslav@1890: *
jaroslav@1890: * @return the arrival phase number, or a negative value if terminated
jaroslav@1890: * @throws IllegalStateException if not terminated and the number
jaroslav@1890: * of registered or unarrived parties would become negative
jaroslav@1890: */
jaroslav@1890: public int arriveAndDeregister() {
jaroslav@1890: return doArrive(true);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Arrives at this phaser and awaits others. Equivalent in effect
jaroslav@1890: * to {@code awaitAdvance(arrive())}. If you need to await with
jaroslav@1890: * interruption or timeout, you can arrange this with an analogous
jaroslav@1890: * construction using one of the other forms of the {@code
jaroslav@1890: * awaitAdvance} method. If instead you need to deregister upon
jaroslav@1890: * arrival, use {@code awaitAdvance(arriveAndDeregister())}.
jaroslav@1890: *
jaroslav@1890: * It is a usage error for an unregistered party to invoke this
jaroslav@1890: * method. However, this error may result in an {@code
jaroslav@1890: * IllegalStateException} only upon some subsequent operation on
jaroslav@1890: * this phaser, if ever.
jaroslav@1890: *
jaroslav@1890: * @return the arrival phase number, or the (negative)
jaroslav@1890: * {@linkplain #getPhase() current phase} if terminated
jaroslav@1890: * @throws IllegalStateException if not terminated and the number
jaroslav@1890: * of unarrived parties would become negative
jaroslav@1890: */
jaroslav@1890: public int arriveAndAwaitAdvance() {
jaroslav@1890: // Specialization of doArrive+awaitAdvance eliminating some reads/paths
jaroslav@1890: final Phaser root = this.root;
jaroslav@1890: for (;;) {
jaroslav@1890: long s = (root == this) ? state : reconcileState();
jaroslav@1890: int phase = (int)(s >>> PHASE_SHIFT);
jaroslav@1890: int counts = (int)s;
jaroslav@1890: int unarrived = (counts & UNARRIVED_MASK) - 1;
jaroslav@1890: if (phase < 0)
jaroslav@1890: return phase;
jaroslav@1890: else if (counts == EMPTY || unarrived < 0) {
jaroslav@1890: if (reconcileState() == s)
jaroslav@1890: throw new IllegalStateException(badArrive(s));
jaroslav@1890: }
jaroslav@1895: else if (compareAndSwapLong(s,
jaroslav@1890: s -= ONE_ARRIVAL)) {
jaroslav@1890: if (unarrived != 0)
jaroslav@1890: return root.internalAwaitAdvance(phase, null);
jaroslav@1890: if (root != this)
jaroslav@1890: return parent.arriveAndAwaitAdvance();
jaroslav@1890: long n = s & PARTIES_MASK; // base of next state
jaroslav@1890: int nextUnarrived = (int)n >>> PARTIES_SHIFT;
jaroslav@1890: if (onAdvance(phase, nextUnarrived))
jaroslav@1890: n |= TERMINATION_BIT;
jaroslav@1890: else if (nextUnarrived == 0)
jaroslav@1890: n |= EMPTY;
jaroslav@1890: else
jaroslav@1890: n |= nextUnarrived;
jaroslav@1890: int nextPhase = (phase + 1) & MAX_PHASE;
jaroslav@1890: n |= (long)nextPhase << PHASE_SHIFT;
jaroslav@1895: if (!compareAndSwapLong(s, n))
jaroslav@1890: return (int)(state >>> PHASE_SHIFT); // terminated
jaroslav@1890: releaseWaiters(phase);
jaroslav@1890: return nextPhase;
jaroslav@1890: }
jaroslav@1890: }
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Awaits the phase of this phaser to advance from the given phase
jaroslav@1890: * value, returning immediately if the current phase is not equal
jaroslav@1890: * to the given phase value or this phaser is terminated.
jaroslav@1890: *
jaroslav@1890: * @param phase an arrival phase number, or negative value if
jaroslav@1890: * terminated; this argument is normally the value returned by a
jaroslav@1890: * previous call to {@code arrive} or {@code arriveAndDeregister}.
jaroslav@1890: * @return the next arrival phase number, or the argument if it is
jaroslav@1890: * negative, or the (negative) {@linkplain #getPhase() current phase}
jaroslav@1890: * if terminated
jaroslav@1890: */
jaroslav@1890: public int awaitAdvance(int phase) {
jaroslav@1890: final Phaser root = this.root;
jaroslav@1890: long s = (root == this) ? state : reconcileState();
jaroslav@1890: int p = (int)(s >>> PHASE_SHIFT);
jaroslav@1890: if (phase < 0)
jaroslav@1890: return phase;
jaroslav@1890: if (p == phase)
jaroslav@1890: return root.internalAwaitAdvance(phase, null);
jaroslav@1890: return p;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Awaits the phase of this phaser to advance from the given phase
jaroslav@1890: * value, throwing {@code InterruptedException} if interrupted
jaroslav@1890: * while waiting, or returning immediately if the current phase is
jaroslav@1890: * not equal to the given phase value or this phaser is
jaroslav@1890: * terminated.
jaroslav@1890: *
jaroslav@1890: * @param phase an arrival phase number, or negative value if
jaroslav@1890: * terminated; this argument is normally the value returned by a
jaroslav@1890: * previous call to {@code arrive} or {@code arriveAndDeregister}.
jaroslav@1890: * @return the next arrival phase number, or the argument if it is
jaroslav@1890: * negative, or the (negative) {@linkplain #getPhase() current phase}
jaroslav@1890: * if terminated
jaroslav@1890: * @throws InterruptedException if thread interrupted while waiting
jaroslav@1890: */
jaroslav@1890: public int awaitAdvanceInterruptibly(int phase)
jaroslav@1890: throws InterruptedException {
jaroslav@1890: final Phaser root = this.root;
jaroslav@1890: long s = (root == this) ? state : reconcileState();
jaroslav@1890: int p = (int)(s >>> PHASE_SHIFT);
jaroslav@1890: if (phase < 0)
jaroslav@1890: return phase;
jaroslav@1890: if (p == phase) {
jaroslav@1890: QNode node = new QNode(this, phase, true, false, 0L);
jaroslav@1890: p = root.internalAwaitAdvance(phase, node);
jaroslav@1890: if (node.wasInterrupted)
jaroslav@1890: throw new InterruptedException();
jaroslav@1890: }
jaroslav@1890: return p;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Awaits the phase of this phaser to advance from the given phase
jaroslav@1890: * value or the given timeout to elapse, throwing {@code
jaroslav@1890: * InterruptedException} if interrupted while waiting, or
jaroslav@1890: * returning immediately if the current phase is not equal to the
jaroslav@1890: * given phase value or this phaser is terminated.
jaroslav@1890: *
jaroslav@1890: * @param phase an arrival phase number, or negative value if
jaroslav@1890: * terminated; this argument is normally the value returned by a
jaroslav@1890: * previous call to {@code arrive} or {@code arriveAndDeregister}.
jaroslav@1890: * @param timeout how long to wait before giving up, in units of
jaroslav@1890: * {@code unit}
jaroslav@1890: * @param unit a {@code TimeUnit} determining how to interpret the
jaroslav@1890: * {@code timeout} parameter
jaroslav@1890: * @return the next arrival phase number, or the argument if it is
jaroslav@1890: * negative, or the (negative) {@linkplain #getPhase() current phase}
jaroslav@1890: * if terminated
jaroslav@1890: * @throws InterruptedException if thread interrupted while waiting
jaroslav@1890: * @throws TimeoutException if timed out while waiting
jaroslav@1890: */
jaroslav@1890: public int awaitAdvanceInterruptibly(int phase,
jaroslav@1890: long timeout, TimeUnit unit)
jaroslav@1890: throws InterruptedException, TimeoutException {
jaroslav@1890: long nanos = unit.toNanos(timeout);
jaroslav@1890: final Phaser root = this.root;
jaroslav@1890: long s = (root == this) ? state : reconcileState();
jaroslav@1890: int p = (int)(s >>> PHASE_SHIFT);
jaroslav@1890: if (phase < 0)
jaroslav@1890: return phase;
jaroslav@1890: if (p == phase) {
jaroslav@1890: QNode node = new QNode(this, phase, true, true, nanos);
jaroslav@1890: p = root.internalAwaitAdvance(phase, node);
jaroslav@1890: if (node.wasInterrupted)
jaroslav@1890: throw new InterruptedException();
jaroslav@1890: else if (p == phase)
jaroslav@1890: throw new TimeoutException();
jaroslav@1890: }
jaroslav@1890: return p;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Forces this phaser to enter termination state. Counts of
jaroslav@1890: * registered parties are unaffected. If this phaser is a member
jaroslav@1890: * of a tiered set of phasers, then all of the phasers in the set
jaroslav@1890: * are terminated. If this phaser is already terminated, this
jaroslav@1890: * method has no effect. This method may be useful for
jaroslav@1890: * coordinating recovery after one or more tasks encounter
jaroslav@1890: * unexpected exceptions.
jaroslav@1890: */
jaroslav@1890: public void forceTermination() {
jaroslav@1890: // Only need to change root state
jaroslav@1890: final Phaser root = this.root;
jaroslav@1890: long s;
jaroslav@1890: while ((s = root.state) >= 0) {
jaroslav@1895: if (compareAndSwapLong( s, s | TERMINATION_BIT)) {
jaroslav@1890: // signal all threads
jaroslav@1890: releaseWaiters(0);
jaroslav@1890: releaseWaiters(1);
jaroslav@1890: return;
jaroslav@1890: }
jaroslav@1890: }
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns the current phase number. The maximum phase number is
jaroslav@1890: * {@code Integer.MAX_VALUE}, after which it restarts at
jaroslav@1890: * zero. Upon termination, the phase number is negative,
jaroslav@1890: * in which case the prevailing phase prior to termination
jaroslav@1890: * may be obtained via {@code getPhase() + Integer.MIN_VALUE}.
jaroslav@1890: *
jaroslav@1890: * @return the phase number, or a negative value if terminated
jaroslav@1890: */
jaroslav@1890: public final int getPhase() {
jaroslav@1890: return (int)(root.state >>> PHASE_SHIFT);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns the number of parties registered at this phaser.
jaroslav@1890: *
jaroslav@1890: * @return the number of parties
jaroslav@1890: */
jaroslav@1890: public int getRegisteredParties() {
jaroslav@1890: return partiesOf(state);
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns the number of registered parties that have arrived at
jaroslav@1890: * the current phase of this phaser. If this phaser has terminated,
jaroslav@1890: * the returned value is meaningless and arbitrary.
jaroslav@1890: *
jaroslav@1890: * @return the number of arrived parties
jaroslav@1890: */
jaroslav@1890: public int getArrivedParties() {
jaroslav@1890: return arrivedOf(reconcileState());
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns the number of registered parties that have not yet
jaroslav@1890: * arrived at the current phase of this phaser. If this phaser has
jaroslav@1890: * terminated, the returned value is meaningless and arbitrary.
jaroslav@1890: *
jaroslav@1890: * @return the number of unarrived parties
jaroslav@1890: */
jaroslav@1890: public int getUnarrivedParties() {
jaroslav@1890: return unarrivedOf(reconcileState());
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns the parent of this phaser, or {@code null} if none.
jaroslav@1890: *
jaroslav@1890: * @return the parent of this phaser, or {@code null} if none
jaroslav@1890: */
jaroslav@1890: public Phaser getParent() {
jaroslav@1890: return parent;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns the root ancestor of this phaser, which is the same as
jaroslav@1890: * this phaser if it has no parent.
jaroslav@1890: *
jaroslav@1890: * @return the root ancestor of this phaser
jaroslav@1890: */
jaroslav@1890: public Phaser getRoot() {
jaroslav@1890: return root;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns {@code true} if this phaser has been terminated.
jaroslav@1890: *
jaroslav@1890: * @return {@code true} if this phaser has been terminated
jaroslav@1890: */
jaroslav@1890: public boolean isTerminated() {
jaroslav@1890: return root.state < 0L;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Overridable method to perform an action upon impending phase
jaroslav@1890: * advance, and to control termination. This method is invoked
jaroslav@1890: * upon arrival of the party advancing this phaser (when all other
jaroslav@1890: * waiting parties are dormant). If this method returns {@code
jaroslav@1890: * true}, this phaser will be set to a final termination state
jaroslav@1890: * upon advance, and subsequent calls to {@link #isTerminated}
jaroslav@1890: * will return true. Any (unchecked) Exception or Error thrown by
jaroslav@1890: * an invocation of this method is propagated to the party
jaroslav@1890: * attempting to advance this phaser, in which case no advance
jaroslav@1890: * occurs.
jaroslav@1890: *
jaroslav@1890: * The arguments to this method provide the state of the phaser
jaroslav@1890: * prevailing for the current transition. The effects of invoking
jaroslav@1890: * arrival, registration, and waiting methods on this phaser from
jaroslav@1890: * within {@code onAdvance} are unspecified and should not be
jaroslav@1890: * relied on.
jaroslav@1890: *
jaroslav@1890: * If this phaser is a member of a tiered set of phasers, then
jaroslav@1890: * {@code onAdvance} is invoked only for its root phaser on each
jaroslav@1890: * advance.
jaroslav@1890: *
jaroslav@1890: * To support the most common use cases, the default
jaroslav@1890: * implementation of this method returns {@code true} when the
jaroslav@1890: * number of registered parties has become zero as the result of a
jaroslav@1890: * party invoking {@code arriveAndDeregister}. You can disable
jaroslav@1890: * this behavior, thus enabling continuation upon future
jaroslav@1890: * registrations, by overriding this method to always return
jaroslav@1890: * {@code false}:
jaroslav@1890: *
jaroslav@1890: * {@code
jaroslav@1890: * Phaser phaser = new Phaser() {
jaroslav@1890: * protected boolean onAdvance(int phase, int parties) { return false; }
jaroslav@1890: * }}
jaroslav@1890: *
jaroslav@1890: * @param phase the current phase number on entry to this method,
jaroslav@1890: * before this phaser is advanced
jaroslav@1890: * @param registeredParties the current number of registered parties
jaroslav@1890: * @return {@code true} if this phaser should terminate
jaroslav@1890: */
jaroslav@1890: protected boolean onAdvance(int phase, int registeredParties) {
jaroslav@1890: return registeredParties == 0;
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns a string identifying this phaser, as well as its
jaroslav@1890: * state. The state, in brackets, includes the String {@code
jaroslav@1890: * "phase = "} followed by the phase number, {@code "parties = "}
jaroslav@1890: * followed by the number of registered parties, and {@code
jaroslav@1890: * "arrived = "} followed by the number of arrived parties.
jaroslav@1890: *
jaroslav@1890: * @return a string identifying this phaser, as well as its state
jaroslav@1890: */
jaroslav@1890: public String toString() {
jaroslav@1890: return stateToString(reconcileState());
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Implementation of toString and string-based error messages
jaroslav@1890: */
jaroslav@1890: private String stateToString(long s) {
jaroslav@1890: return super.toString() +
jaroslav@1890: "[phase = " + phaseOf(s) +
jaroslav@1890: " parties = " + partiesOf(s) +
jaroslav@1890: " arrived = " + arrivedOf(s) + "]";
jaroslav@1890: }
jaroslav@1890:
jaroslav@1890: // Waiting mechanics
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Removes and signals threads from queue for phase.
jaroslav@1890: */
jaroslav@1890: private void releaseWaiters(int phase) {
jaroslav@1890: QNode q; // first element of queue
jaroslav@1890: Thread t; // its thread
jaroslav@1890: AtomicReference