--- jsr166/src/jsr166y/Phaser.java 2010/11/13 00:55:51 1.51 +++ jsr166/src/jsr166y/Phaser.java 2010/11/24 15:48:01 1.58 @@ -86,9 +86,9 @@ import java.util.concurrent.locks.LockSu * #forceTermination} is also available to abruptly release waiting * threads and allow them to terminate. * - *
Tiering. Phasers may be tiered (i.e., arranged - * in tree structures) to reduce contention. Phasers with large - * numbers of parties that would otherwise experience heavy + *
Tiering. Phasers may be tiered (i.e.,
+ * constructed in tree structures) to reduce contention. Phasers with
+ * large numbers of parties that would otherwise experience heavy
* synchronization contention costs may instead be set up so that
* groups of sub-phasers share a common parent. This may greatly
* increase throughput even though it incurs greater per-operation
@@ -240,12 +240,12 @@ public class Phaser {
*/
private volatile long state;
- private static final int MAX_COUNT = 0xffff;
+ private static final int MAX_PARTIES = 0xffff;
private static final int MAX_PHASE = 0x7fffffff;
private static final int PARTIES_SHIFT = 16;
private static final int PHASE_SHIFT = 32;
- private static final long UNARRIVED_MASK = 0xffffL;
- private static final long PARTIES_MASK = 0xffff0000L;
+ private static final int UNARRIVED_MASK = 0xffff;
+ private static final long PARTIES_MASK = 0xffff0000L; // for masking long
private static final long ONE_ARRIVAL = 1L;
private static final long ONE_PARTY = 1L << PARTIES_SHIFT;
private static final long TERMINATION_PHASE = -1L << PHASE_SHIFT;
@@ -253,11 +253,11 @@ public class Phaser {
// The following unpacking methods are usually manually inlined
private static int unarrivedOf(long s) {
- return (int) (s & UNARRIVED_MASK);
+ return (int)s & UNARRIVED_MASK;
}
private static int partiesOf(long s) {
- return ((int) (s & PARTIES_MASK)) >>> PARTIES_SHIFT;
+ return (int)s >>> PARTIES_SHIFT;
}
private static int phaseOf(long s) {
@@ -302,112 +302,111 @@ public class Phaser {
* ONE_ARRIVAL|ONE_PARTY (for arriveAndDeregister)
*/
private int doArrive(long adj) {
- long s;
- int phase, unarrived;
- while ((phase = (int)((s = state) >>> PHASE_SHIFT)) >= 0) {
- if ((unarrived = (int)(s & UNARRIVED_MASK)) != 0) {
- if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s -= adj)) {
- if (unarrived == 1) {
- Phaser par;
- long p = s & PARTIES_MASK; // unshifted parties field
- long lu = p >>> PARTIES_SHIFT;
- int u = (int)lu;
- int nextPhase = (phase + 1) & MAX_PHASE;
- long next = ((long)nextPhase << PHASE_SHIFT) | p | lu;
- if ((par = parent) == null) {
- UNSAFE.compareAndSwapLong
- (this, stateOffset, s, onAdvance(phase, u)?
- next | TERMINATION_PHASE : next);
- releaseWaiters(phase);
- }
- else {
- par.doArrive(u == 0?
- ONE_ARRIVAL|ONE_PARTY : ONE_ARRIVAL);
- if ((int)(par.state >>> PHASE_SHIFT) != nextPhase ||
- ((int)(state >>> PHASE_SHIFT) != nextPhase &&
- !UNSAFE.compareAndSwapLong(this, stateOffset,
- s, next)))
- reconcileState();
- }
+ for (;;) {
+ long s = state;
+ int phase = (int)(s >>> PHASE_SHIFT);
+ if (phase < 0)
+ return phase;
+ int unarrived = (int)s & UNARRIVED_MASK;
+ if (unarrived == 0)
+ checkBadArrive(s);
+ else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adj)) {
+ if (unarrived == 1) {
+ long p = s & PARTIES_MASK; // unshifted parties field
+ long lu = p >>> PARTIES_SHIFT;
+ int u = (int)lu;
+ int nextPhase = (phase + 1) & MAX_PHASE;
+ long next = ((long)nextPhase << PHASE_SHIFT) | p | lu;
+ final Phaser parent = this.parent;
+ if (parent == null) {
+ if (onAdvance(phase, u))
+ next |= TERMINATION_PHASE; // obliterate phase
+ UNSAFE.compareAndSwapLong(this, stateOffset, s, next);
+ releaseWaiters(phase);
+ }
+ else {
+ parent.doArrive((u == 0) ?
+ ONE_ARRIVAL|ONE_PARTY : ONE_ARRIVAL);
+ if ((int)(parent.state >>> PHASE_SHIFT) != nextPhase ||
+ ((int)(state >>> PHASE_SHIFT) != nextPhase &&
+ !UNSAFE.compareAndSwapLong(this, stateOffset,
+ s, next)))
+ reconcileState();
}
- break;
}
+ return phase;
}
- else if (state == s && reconcileState() == s) // recheck
- throw new IllegalStateException(badArrive());
}
- return phase;
}
/**
- * Returns message string for bounds exceptions on arrival.
- * Declared out of-line from doArrive to reduce string op bulk.
+ * Rechecks state and throws bounds exceptions on arrival -- called
+ * only if unarrived is apparently zero.
*/
- private String badArrive() {
- return ("Attempted arrival of unregistered party for " +
- this.toString());
+ private void checkBadArrive(long s) {
+ if (reconcileState() == s)
+ throw new IllegalStateException
+ ("Attempted arrival of unregistered party for " +
+ stateToString(s));
}
/**
* Implementation of register, bulkRegister
*
- * @param registrations number to add to both parties and unarrived fields
+ * @param registrations number to add to both parties and
+ * unarrived fields. Must be greater than zero.
*/
private int doRegister(int registrations) {
- long adj = (long)registrations; // adjustment to state
- adj |= adj << PARTIES_SHIFT;
- Phaser par = parent;
- long s;
- int phase;
- while ((phase = (int)((s = (par == null? state : reconcileState()))
- >>> PHASE_SHIFT)) >= 0) {
- int parties = ((int)(s & PARTIES_MASK)) >>> PARTIES_SHIFT;
- if (parties != 0 && (s & UNARRIVED_MASK) == 0)
+ // adjustment to state
+ long adj = ((long)registrations << PARTIES_SHIFT) | registrations;
+ final Phaser parent = this.parent;
+ for (;;) {
+ long s = (parent == null) ? state : reconcileState();
+ int parties = (int)s >>> PARTIES_SHIFT;
+ int phase = (int)(s >>> PHASE_SHIFT);
+ if (phase < 0)
+ return phase;
+ else if (parties != 0 && ((int)s & UNARRIVED_MASK) == 0)
internalAwaitAdvance(phase, null); // wait for onAdvance
- else if (parties + registrations > MAX_COUNT)
- throw new IllegalStateException(badRegister());
+ else if (registrations > MAX_PARTIES - parties)
+ throw new IllegalStateException(badRegister(s));
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s + adj))
- break;
+ return phase;
}
- return phase;
}
/**
- * Returns message string for bounds exceptions on registration
+ * Returns message string for out of bounds exceptions on registration.
*/
- private String badRegister() {
- return ("Attempt to register more than " + MAX_COUNT + " parties for "+
- this.toString());
+ private String badRegister(long s) {
+ return "Attempt to register more than " +
+ MAX_PARTIES + " parties for " + stateToString(s);
}
/**
- * Recursively resolves lagged phase propagation from root if
- * necessary.
+ * Recursively resolves lagged phase propagation from root if necessary.
*/
private long reconcileState() {
Phaser par = parent;
- if (par == null)
- return state;
- Phaser rt = root;
- long s;
- int phase, rPhase;
- while ((phase = (int)((s = state) >>> PHASE_SHIFT)) >= 0 &&
- (rPhase = (int)(rt.state >>> PHASE_SHIFT)) != phase) {
- if (rPhase < 0 || (s & UNARRIVED_MASK) == 0) {
- long ps = par.parent == null? par.state : par.reconcileState();
- int pPhase = (int)(ps >>> PHASE_SHIFT);
- if (pPhase < 0 || pPhase == ((phase + 1) & MAX_PHASE)) {
- if (state != s)
- continue;
- long p = s & PARTIES_MASK;
- long next = ((((long) pPhase) << PHASE_SHIFT) |
- (p >>> PARTIES_SHIFT) | p);
- if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next))
- return next;
+ long s = state;
+ if (par != null) {
+ Phaser rt = root;
+ int phase, rPhase;
+ while ((phase = (int)(s >>> PHASE_SHIFT)) >= 0 &&
+ (rPhase = (int)(rt.state >>> PHASE_SHIFT)) != phase) {
+ if ((int)(par.state >>> PHASE_SHIFT) != rPhase)
+ par.reconcileState();
+ else if (rPhase < 0 || ((int)s & UNARRIVED_MASK) == 0) {
+ long u = s & PARTIES_MASK; // reset unarrived to parties
+ long next = ((((long) rPhase) << PHASE_SHIFT) | u |
+ (u >>> PARTIES_SHIFT));
+ if (state == s &&
+ UNSAFE.compareAndSwapLong(this, stateOffset,
+ s, s = next))
+ break;
}
+ s = state;
}
- if (state == s)
- releaseWaiters(phase); // help release others
}
return s;
}
@@ -434,10 +433,7 @@ public class Phaser {
}
/**
- * Creates a new phaser with the given parent, without any
- * initially registered parties. If parent is non-null this phaser
- * is registered with the parent and its initial phase number is
- * the same as that of parent phaser.
+ * Equivalent to {@link #Phaser(Phaser, int) Phaser(parent, 0)}.
*
* @param parent the parent phaser
*/
@@ -447,9 +443,20 @@ public class Phaser {
/**
* Creates a new phaser with the given parent and number of
- * registered unarrived parties. If parent is non-null, this phaser
- * is registered with the parent and its initial phase number is
- * the same as that of parent phaser.
+ * registered unarrived parties. If parent is non-null, this
+ * phaser is registered with the parent and its initial phase
+ * number is the same as that of parent phaser. If the number of
+ * parties is zero, the parent phaser will not proceed until this
+ * child phaser registers parties and advances, or this child
+ * phaser deregisters with its parent, or the parent is otherwise
+ * terminated. This child Phaser will be deregistered from its
+ * parent automatically upon any invocation of the child's {@link
+ * #arriveAndDeregister} method that results in the child's number
+ * of registered parties becoming zero. (Although rarely
+ * appropriate, this child may also explicity deregister from its
+ * parent using {@code getParent().arriveAndDeregister()}.) After
+ * deregistration, the child cannot re-register. (Instead, you can
+ * create a new child Phaser.)
*
* @param parent the parent phaser
* @param parties the number of parties required to trip barrier
@@ -457,7 +464,7 @@ public class Phaser {
* or greater than the maximum number of parties supported
*/
public Phaser(Phaser parent, int parties) {
- if (parties < 0 || parties > MAX_COUNT)
+ if (parties >>> PARTIES_SHIFT != 0)
throw new IllegalArgumentException("Illegal number of parties");
int phase;
this.parent = parent;
@@ -466,7 +473,7 @@ public class Phaser {
this.root = r;
this.evenQ = r.evenQ;
this.oddQ = r.oddQ;
- phase = parent.register();
+ phase = parent.doRegister(1);
}
else {
this.root = this;
@@ -475,7 +482,7 @@ public class Phaser {
phase = 0;
}
long p = (long)parties;
- this.state = (((long) phase) << PHASE_SHIFT) | p | (p << PARTIES_SHIFT);
+ this.state = (((long)phase) << PHASE_SHIFT) | p | (p << PARTIES_SHIFT);
}
/**
@@ -505,8 +512,6 @@ public class Phaser {
public int bulkRegister(int parties) {
if (parties < 0)
throw new IllegalArgumentException();
- if (parties > MAX_COUNT)
- throw new IllegalStateException(badRegister());
if (parties == 0)
return getPhase();
return doRegister(parties);
@@ -575,10 +580,9 @@ public class Phaser {
public int awaitAdvance(int phase) {
if (phase < 0)
return phase;
- int p = (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT);
- if (p != phase)
- return p;
- return internalAwaitAdvance(phase, null);
+ long s = (parent == null) ? state : reconcileState();
+ int p = (int)(s >>> PHASE_SHIFT);
+ return (p != phase) ? p : internalAwaitAdvance(phase, null);
}
/**
@@ -599,15 +603,15 @@ public class Phaser {
throws InterruptedException {
if (phase < 0)
return phase;
- int p = (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT);
- if (p != phase)
- return p;
- QNode node = new QNode(this, phase, true, false, 0L);
- p = internalAwaitAdvance(phase, node);
- if (node.wasInterrupted)
- throw new InterruptedException();
- else
- return p;
+ long s = (parent == null) ? state : reconcileState();
+ int p = (int)(s >>> PHASE_SHIFT);
+ if (p == phase) {
+ QNode node = new QNode(this, phase, true, false, 0L);
+ p = internalAwaitAdvance(phase, node);
+ if (node.wasInterrupted)
+ throw new InterruptedException();
+ }
+ return p;
}
/**
@@ -633,37 +637,43 @@ public class Phaser {
public int awaitAdvanceInterruptibly(int phase,
long timeout, TimeUnit unit)
throws InterruptedException, TimeoutException {
- long nanos = unit.toNanos(timeout);
if (phase < 0)
return phase;
- int p = (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT);
- if (p != phase)
- return p;
- QNode node = new QNode(this, phase, true, true, nanos);
- p = internalAwaitAdvance(phase, node);
- if (node.wasInterrupted)
- throw new InterruptedException();
- else if (p == phase)
- throw new TimeoutException();
- else
- return p;
+ long s = (parent == null) ? state : reconcileState();
+ int p = (int)(s >>> PHASE_SHIFT);
+ if (p == phase) {
+ long nanos = unit.toNanos(timeout);
+ QNode node = new QNode(this, phase, true, true, nanos);
+ p = internalAwaitAdvance(phase, node);
+ if (node.wasInterrupted)
+ throw new InterruptedException();
+ else if (p == phase)
+ throw new TimeoutException();
+ }
+ return p;
}
/**
- * Forces this barrier to enter termination state. Counts of
- * arrived and registered parties are unaffected. If this phaser
- * has a parent, it too is terminated. This method may be useful
- * for coordinating recovery after one or more tasks encounter
- * unexpected exceptions.
+ * Forces this barrier to enter termination state. Counts of
+ * arrived and registered parties are unaffected. If this phaser
+ * is a member of a tiered set of phasers, then all of the phasers
+ * in the set are terminated. If this phaser is already
+ * terminated, this method has no effect. This method may be
+ * useful for coordinating recovery after one or more tasks
+ * encounter unexpected exceptions.
*/
public void forceTermination() {
- Phaser r = root; // force at root then reconcile
+ // Only need to change root state
+ final Phaser root = this.root;
long s;
- while ((s = r.state) >= 0)
- UNSAFE.compareAndSwapLong(r, stateOffset, s, s | TERMINATION_PHASE);
- reconcileState();
- releaseWaiters(0); // signal all threads
- releaseWaiters(1);
+ while ((s = root.state) >= 0) {
+ if (UNSAFE.compareAndSwapLong(root, stateOffset,
+ s, s | TERMINATION_PHASE)) {
+ releaseWaiters(0); // signal all threads
+ releaseWaiters(1);
+ return;
+ }
+ }
}
/**
@@ -674,7 +684,7 @@ public class Phaser {
* @return the phase number, or a negative value if terminated
*/
public final int getPhase() {
- return (int)((parent == null? state : reconcileState()) >>> PHASE_SHIFT);
+ return (int)(root.state >>> PHASE_SHIFT);
}
/**
@@ -683,7 +693,7 @@ public class Phaser {
* @return the number of parties
*/
public int getRegisteredParties() {
- return partiesOf(parent == null? state : reconcileState());
+ return partiesOf(state);
}
/**
@@ -693,7 +703,7 @@ public class Phaser {
* @return the number of arrived parties
*/
public int getArrivedParties() {
- return arrivedOf(parent == null? state : reconcileState());
+ return arrivedOf(parent==null? state : reconcileState());
}
/**
@@ -703,7 +713,7 @@ public class Phaser {
* @return the number of unarrived parties
*/
public int getUnarrivedParties() {
- return unarrivedOf(parent == null? state : reconcileState());
+ return unarrivedOf(parent==null? state : reconcileState());
}
/**
@@ -731,7 +741,7 @@ public class Phaser {
* @return {@code true} if this barrier has been terminated
*/
public boolean isTerminated() {
- return (parent == null? state : reconcileState()) < 0;
+ return root.state < 0L;
}
/**
@@ -779,15 +789,23 @@ public class Phaser {
* @return a string identifying this barrier, as well as its state
*/
public String toString() {
- long s = reconcileState();
+ return stateToString(reconcileState());
+ }
+
+ /**
+ * Implementation of toString and string-based error messages
+ */
+ private String stateToString(long s) {
return super.toString() +
"[phase = " + phaseOf(s) +
" parties = " + partiesOf(s) +
" arrived = " + arrivedOf(s) + "]";
}
+ // Waiting mechanics
+
/**
- * Removes and signals threads from queue for phase
+ * Removes and signals threads from queue for phase.
*/
private void releaseWaiters(int phase) {
AtomicReference