260 |
|
* parent. |
261 |
|
* |
262 |
|
* The phase of a subphaser is allowed to lag that of its |
263 |
< |
* ancestors until it is actually accessed. Method reconcileState |
264 |
< |
* is usually attempted only only when the number of unarrived |
265 |
< |
* parties appears to be zero, which indicates a potential lag in |
266 |
< |
* updating phase after the root advanced. |
263 |
> |
* ancestors until it is actually accessed -- see method |
264 |
> |
* reconcileState. |
265 |
|
*/ |
266 |
|
private volatile long state; |
267 |
|
|
269 |
|
private static final int MAX_PHASE = 0x7fffffff; |
270 |
|
private static final int PARTIES_SHIFT = 16; |
271 |
|
private static final int PHASE_SHIFT = 32; |
272 |
+ |
private static final long PHASE_MASK = -1L << PHASE_SHIFT; |
273 |
|
private static final int UNARRIVED_MASK = 0xffff; // to mask ints |
274 |
|
private static final long PARTIES_MASK = 0xffff0000L; // to mask longs |
275 |
|
private static final long TERMINATION_BIT = 1L << 63; |
435 |
|
|
436 |
|
/** |
437 |
|
* Resolves lagged phase propagation from root if necessary. |
438 |
+ |
* Reconciliation normally occurs when root has advanced but |
439 |
+ |
* subphasers have not yet done so, in which case they must finish |
440 |
+ |
* their own advance by setting unarrived to parties (or if |
441 |
+ |
* parties is zero, resetting to unregistered EMPTY state). |
442 |
+ |
* However, this method may also be called when "floating" |
443 |
+ |
* subphasers with possibly some unarrived parties are merely |
444 |
+ |
* catching up to current phase, in which case counts are |
445 |
+ |
* unaffected. |
446 |
+ |
* |
447 |
+ |
* @return reconciled state |
448 |
|
*/ |
449 |
|
private long reconcileState() { |
450 |
< |
Phaser rt = root; |
450 |
> |
final Phaser root = this.root; |
451 |
|
long s = state; |
452 |
< |
if (rt != this) { |
453 |
< |
int phase; |
454 |
< |
while ((phase = (int)(rt.state >>> PHASE_SHIFT)) != |
455 |
< |
(int)(s >>> PHASE_SHIFT)) { |
456 |
< |
// assert phase < 0 || unarrivedOf(s) == 0 |
457 |
< |
long t; // to reread s |
458 |
< |
long p = s & PARTIES_MASK; // unshifted parties field |
459 |
< |
long n = (((long) phase) << PHASE_SHIFT) | p; |
460 |
< |
if (phase >= 0) { |
461 |
< |
if (p == 0L) |
462 |
< |
n |= EMPTY; // reset to empty |
454 |
< |
else |
455 |
< |
n |= p >>> PARTIES_SHIFT; // set unarr to parties |
456 |
< |
} |
457 |
< |
if ((t = state) == s && |
458 |
< |
UNSAFE.compareAndSwapLong(this, stateOffset, s, s = n)) |
459 |
< |
break; |
460 |
< |
s = t; |
461 |
< |
} |
452 |
> |
if (root != this) { |
453 |
> |
int phase, u, p; |
454 |
> |
// CAS root phase with current parties; possibly trip unarrived |
455 |
> |
while ((phase = (int)(root.state >>> PHASE_SHIFT)) != |
456 |
> |
(int)(s >>> PHASE_SHIFT) && |
457 |
> |
!UNSAFE.compareAndSwapLong |
458 |
> |
(this, stateOffset, s, |
459 |
> |
s = ((((long) phase) << PHASE_SHIFT) | (s & PARTIES_MASK) | |
460 |
> |
((p = (int)s >>> PARTIES_SHIFT) == 0 ? EMPTY : |
461 |
> |
(u = (int)s & UNARRIVED_MASK) == 0 ? p : u)))) |
462 |
> |
s = state; |
463 |
|
} |
464 |
|
return s; |
465 |
|
} |
777 |
|
final Phaser root = this.root; |
778 |
|
long s; |
779 |
|
while ((s = root.state) >= 0) { |
780 |
< |
long next = (s & ~((long)UNARRIVED_MASK)) | TERMINATION_BIT; |
781 |
< |
if (UNSAFE.compareAndSwapLong(root, stateOffset, s, next)) { |
780 |
> |
if (UNSAFE.compareAndSwapLong(root, stateOffset, |
781 |
> |
s, s | TERMINATION_BIT)) { |
782 |
|
// signal all threads |
783 |
|
releaseWaiters(0); |
784 |
|
releaseWaiters(1); |
811 |
|
|
812 |
|
/** |
813 |
|
* Returns the number of registered parties that have arrived at |
814 |
< |
* the current phase of this phaser. |
814 |
> |
* the current phase of this phaser. If this phaser has terminated, |
815 |
> |
* the returned value is meaningless and arbitrary. |
816 |
|
* |
817 |
|
* @return the number of arrived parties |
818 |
|
*/ |
822 |
|
|
823 |
|
/** |
824 |
|
* Returns the number of registered parties that have not yet |
825 |
< |
* arrived at the current phase of this phaser. |
825 |
> |
* arrived at the current phase of this phaser. If this phaser has |
826 |
> |
* terminated, the returned value is meaningless and arbitrary. |
827 |
|
* |
828 |
|
* @return the number of unarrived parties |
829 |
|
*/ |