--- jsr166/src/jsr166y/Phaser.java 2010/11/17 10:48:59 1.56 +++ 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
@@ -245,8 +245,7 @@ public class Phaser {
private static final int PARTIES_SHIFT = 16;
private static final int PHASE_SHIFT = 32;
private static final int UNARRIVED_MASK = 0xffff;
- private static final int PARTIES_MASK = 0xffff0000;
- private static final long LPARTIES_MASK = 0xffff0000L; // long version
+ 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;
@@ -304,30 +303,31 @@ public class Phaser {
*/
private int doArrive(long adj) {
for (;;) {
- long s;
- int phase, unarrived;
- if ((phase = (int)((s = state) >>> PHASE_SHIFT)) < 0)
+ long s = state;
+ int phase = (int)(s >>> PHASE_SHIFT);
+ if (phase < 0)
return phase;
- else if ((unarrived = (int)s & UNARRIVED_MASK) == 0)
+ int unarrived = (int)s & UNARRIVED_MASK;
+ if (unarrived == 0)
checkBadArrive(s);
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adj)) {
if (unarrived == 1) {
- Phaser par;
- long p = s & LPARTIES_MASK; // unshifted parties field
+ 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) {
+ 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 {
- par.doArrive(u == 0?
- ONE_ARRIVAL|ONE_PARTY : ONE_ARRIVAL);
- if ((int)(par.state >>> PHASE_SHIFT) != nextPhase ||
+ 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)))
@@ -353,21 +353,22 @@ public class Phaser {
/**
* 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;
+ // adjustment to state
+ long adj = ((long)registrations << PARTIES_SHIFT) | registrations;
+ final Phaser parent = this.parent;
for (;;) {
- int phase, parties;
- long s = par == null? state : reconcileState();
- if ((phase = (int)(s >>> PHASE_SHIFT)) < 0)
+ long s = (parent == null) ? state : reconcileState();
+ int parties = (int)s >>> PARTIES_SHIFT;
+ int phase = (int)(s >>> PHASE_SHIFT);
+ if (phase < 0)
return phase;
- if ((parties = (int)s >>> PARTIES_SHIFT) != 0 &&
- ((int)s & UNARRIVED_MASK) == 0)
+ else if (parties != 0 && ((int)s & UNARRIVED_MASK) == 0)
internalAwaitAdvance(phase, null); // wait for onAdvance
- else if (parties + registrations > MAX_PARTIES)
+ else if (registrations > MAX_PARTIES - parties)
throw new IllegalStateException(badRegister(s));
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s + adj))
return phase;
@@ -387,29 +388,27 @@ public class Phaser {
*/
private long reconcileState() {
Phaser par = parent;
- if (par == null)
- return state;
- Phaser rt = root;
- for (;;) {
- long s, u;
- int phase, rPhase, pPhase;
- if ((phase = (int)((s = state)>>> PHASE_SHIFT)) < 0 ||
- (rPhase = (int)(rt.state >>> PHASE_SHIFT)) == phase)
- return s;
- long pState = par.parent == null? par.state : par.reconcileState();
- if (state == s) {
- if ((rPhase < 0 || ((int)s & UNARRIVED_MASK) == 0) &&
- ((pPhase = (int)(pState >>> PHASE_SHIFT)) < 0 ||
- pPhase == ((phase + 1) & MAX_PHASE)))
- UNSAFE.compareAndSwapLong
- (this, stateOffset, s,
- (((long) pPhase) << PHASE_SHIFT) |
- (u = s & LPARTIES_MASK) |
- (u >>> PARTIES_SHIFT)); // reset unarrived to parties
- else
- releaseWaiters(phase); // help release others
+ 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;
}
}
+ 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
@@ -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;
@@ -505,8 +512,6 @@ public class Phaser {
public int bulkRegister(int parties) {
if (parties < 0)
throw new IllegalArgumentException();
- if (parties > MAX_PARTIES)
- throw new IllegalStateException(badRegister(state));
if (parties == 0)
return getPhase();
return doRegister(parties);
@@ -573,14 +578,11 @@ public class Phaser {
* if terminated or argument is negative
*/
public int awaitAdvance(int phase) {
- int p;
if (phase < 0)
return phase;
- else if ((p = (int)((parent == null? state : reconcileState())
- >>> PHASE_SHIFT)) == phase)
- return internalAwaitAdvance(phase, null);
- else
- return p;
+ long s = (parent == null) ? state : reconcileState();
+ int p = (int)(s >>> PHASE_SHIFT);
+ return (p != phase) ? p : internalAwaitAdvance(phase, null);
}
/**
@@ -599,11 +601,11 @@ public class Phaser {
*/
public int awaitAdvanceInterruptibly(int phase)
throws InterruptedException {
- int p;
if (phase < 0)
return phase;
- if ((p = (int)((parent == null? state : reconcileState())
- >>> PHASE_SHIFT)) == phase) {
+ 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)
@@ -635,12 +637,12 @@ public class Phaser {
public int awaitAdvanceInterruptibly(int phase,
long timeout, TimeUnit unit)
throws InterruptedException, TimeoutException {
- long nanos = unit.toNanos(timeout);
- int p;
if (phase < 0)
return phase;
- if ((p = (int)((parent == null? state : reconcileState())
- >>> PHASE_SHIFT)) == phase) {
+ 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)
@@ -682,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);
}
/**
@@ -691,7 +693,7 @@ public class Phaser {
* @return the number of parties
*/
public int getRegisteredParties() {
- return partiesOf(parent==null? state : reconcileState());
+ return partiesOf(state);
}
/**
@@ -739,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;
}
/**
@@ -803,7 +805,7 @@ public class Phaser {
// Waiting mechanics
/**
- * Removes and signals threads from queue for phase
+ * Removes and signals threads from queue for phase.
*/
private void releaseWaiters(int phase) {
AtomicReference