1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
130 |
|
* void runTasks(List<Runnable> tasks) { |
131 |
|
* final Phaser phaser = new Phaser(1); // "1" to register self |
132 |
|
* // create and start threads |
133 |
< |
* for (Runnable task : tasks) { |
133 |
> |
* for (final Runnable task : tasks) { |
134 |
|
* phaser.register(); |
135 |
|
* new Thread() { |
136 |
|
* public void run() { |
237 |
|
*/ |
238 |
|
|
239 |
|
/** |
240 |
< |
* Primary state representation, holding four fields: |
240 |
> |
* Primary state representation, holding four bit-fields: |
241 |
|
* |
242 |
< |
* * unarrived -- the number of parties yet to hit barrier (bits 0-15) |
243 |
< |
* * parties -- the number of parties to wait (bits 16-31) |
244 |
< |
* * phase -- the generation of the barrier (bits 32-62) |
245 |
< |
* * terminated -- set if barrier is terminated (bit 63 / sign) |
242 |
> |
* unarrived -- the number of parties yet to hit barrier (bits 0-15) |
243 |
> |
* parties -- the number of parties to wait (bits 16-31) |
244 |
> |
* phase -- the generation of the barrier (bits 32-62) |
245 |
> |
* terminated -- set if barrier is terminated (bit 63 / sign) |
246 |
|
* |
247 |
|
* Except that a phaser with no registered parties is |
248 |
< |
* distinguished with the otherwise illegal state of having zero |
248 |
> |
* distinguished by the otherwise illegal state of having zero |
249 |
|
* parties and one unarrived parties (encoded as EMPTY below). |
250 |
|
* |
251 |
|
* To efficiently maintain atomicity, these values are packed into |
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 |
|
|
268 |
|
private static final int MAX_PARTIES = 0xffff; |
269 |
< |
private static final int MAX_PHASE = 0x7fffffff; |
269 |
> |
private static final int MAX_PHASE = Integer.MAX_VALUE; |
270 |
|
private static final int PARTIES_SHIFT = 16; |
271 |
|
private static final int PHASE_SHIFT = 32; |
272 |
|
private static final int UNARRIVED_MASK = 0xffff; // to mask ints |
290 |
|
} |
291 |
|
|
292 |
|
private static int phaseOf(long s) { |
293 |
< |
return (int) (s >>> PHASE_SHIFT); |
293 |
> |
return (int)(s >>> PHASE_SHIFT); |
294 |
|
} |
295 |
|
|
296 |
|
private static int arrivedOf(long s) { |
362 |
|
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adj)) { |
363 |
|
if (unarrived == 0) { |
364 |
|
long n = s & PARTIES_MASK; // base of next state |
365 |
< |
int nextUnarrived = ((int)n) >>> PARTIES_SHIFT; |
365 |
> |
int nextUnarrived = (int)n >>> PARTIES_SHIFT; |
366 |
|
if (root != this) |
367 |
|
return parent.doArrive(nextUnarrived == 0); |
368 |
|
if (onAdvance(phase, nextUnarrived)) |
371 |
|
n |= EMPTY; |
372 |
|
else |
373 |
|
n |= nextUnarrived; |
374 |
< |
n |= ((long)((phase + 1) & MAX_PHASE)) << PHASE_SHIFT; |
374 |
> |
n |= (long)((phase + 1) & MAX_PHASE) << PHASE_SHIFT; |
375 |
|
UNSAFE.compareAndSwapLong(this, stateOffset, s, n); |
376 |
|
releaseWaiters(phase); |
377 |
|
} |
389 |
|
private int doRegister(int registrations) { |
390 |
|
// adjustment to state |
391 |
|
long adj = ((long)registrations << PARTIES_SHIFT) | registrations; |
392 |
< |
Phaser par = parent; |
392 |
> |
final Phaser parent = this.parent; |
393 |
|
int phase; |
394 |
|
for (;;) { |
395 |
|
long s = state; |
401 |
|
else if ((phase = (int)(s >>> PHASE_SHIFT)) < 0) |
402 |
|
break; |
403 |
|
else if (counts != EMPTY) { // not 1st registration |
404 |
< |
if (par == null || reconcileState() == s) { |
404 |
> |
if (parent == null || reconcileState() == s) { |
405 |
|
if (unarrived == 0) // wait out advance |
406 |
|
root.internalAwaitAdvance(phase, null); |
407 |
|
else if (UNSAFE.compareAndSwapLong(this, stateOffset, |
409 |
|
break; |
410 |
|
} |
411 |
|
} |
412 |
< |
else if (par == null) { // 1st root registration |
413 |
< |
long next = (((long) phase) << PHASE_SHIFT) | adj; |
412 |
> |
else if (parent == null) { // 1st root registration |
413 |
> |
long next = ((long)phase << PHASE_SHIFT) | adj; |
414 |
|
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) |
415 |
|
break; |
416 |
|
} |
417 |
|
else { |
418 |
|
synchronized (this) { // 1st sub registration |
419 |
|
if (state == s) { // recheck under lock |
420 |
< |
par.doRegister(1); |
420 |
> |
parent.doRegister(1); |
421 |
|
do { // force current phase |
422 |
|
phase = (int)(root.state >>> PHASE_SHIFT); |
423 |
|
// assert phase < 0 || (int)state == EMPTY; |
424 |
|
} while (!UNSAFE.compareAndSwapLong |
425 |
|
(this, stateOffset, state, |
426 |
< |
(((long) phase) << PHASE_SHIFT) | adj)); |
426 |
> |
((long)phase << PHASE_SHIFT) | adj)); |
427 |
|
break; |
428 |
|
} |
429 |
|
} |
434 |
|
|
435 |
|
/** |
436 |
|
* Resolves lagged phase propagation from root if necessary. |
437 |
+ |
* Reconciliation normally occurs when root has advanced but |
438 |
+ |
* subphasers have not yet done so, in which case they must finish |
439 |
+ |
* their own advance by setting unarrived to parties (or if |
440 |
+ |
* parties is zero, resetting to unregistered EMPTY state). |
441 |
+ |
* However, this method may also be called when "floating" |
442 |
+ |
* subphasers with possibly some unarrived parties are merely |
443 |
+ |
* catching up to current phase, in which case counts are |
444 |
+ |
* unaffected. |
445 |
+ |
* |
446 |
+ |
* @return reconciled state |
447 |
|
*/ |
448 |
|
private long reconcileState() { |
449 |
< |
Phaser rt = root; |
449 |
> |
final Phaser root = this.root; |
450 |
|
long s = state; |
451 |
< |
if (rt != this) { |
452 |
< |
int phase; |
453 |
< |
while ((phase = (int)(rt.state >>> PHASE_SHIFT)) != |
454 |
< |
(int)(s >>> PHASE_SHIFT)) { |
455 |
< |
// assert phase < 0 || unarrivedOf(s) == 0 |
456 |
< |
long t; // to reread s |
457 |
< |
long p = s & PARTIES_MASK; // unshifted parties field |
458 |
< |
long n = (((long) phase) << PHASE_SHIFT) | p; |
459 |
< |
if (phase >= 0) { |
460 |
< |
if (p == 0L) |
461 |
< |
n |= EMPTY; // reset to empty |
462 |
< |
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 |
< |
} |
451 |
> |
if (root != this) { |
452 |
> |
int phase, u, p; |
453 |
> |
// CAS root phase with current parties; possibly trip unarrived |
454 |
> |
while ((phase = (int)(root.state >>> PHASE_SHIFT)) != |
455 |
> |
(int)(s >>> PHASE_SHIFT) && |
456 |
> |
!UNSAFE.compareAndSwapLong |
457 |
> |
(this, stateOffset, s, |
458 |
> |
s = (((long)phase << PHASE_SHIFT) | |
459 |
> |
(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 |
|
} |
525 |
|
this.evenQ = new AtomicReference<QNode>(); |
526 |
|
this.oddQ = new AtomicReference<QNode>(); |
527 |
|
} |
528 |
< |
this.state = (parties == 0) ? (long) EMPTY : |
529 |
< |
((((long) phase) << PHASE_SHIFT) | |
530 |
< |
(((long) parties) << PARTIES_SHIFT) | |
531 |
< |
((long) parties)); |
528 |
> |
this.state = (parties == 0) ? (long)EMPTY : |
529 |
> |
((long)phase << PHASE_SHIFT) | |
530 |
> |
((long)parties << PARTIES_SHIFT) | |
531 |
> |
((long)parties); |
532 |
|
} |
533 |
|
|
534 |
|
/** |
652 |
|
if (root != this) |
653 |
|
return parent.arriveAndAwaitAdvance(); |
654 |
|
long n = s & PARTIES_MASK; // base of next state |
655 |
< |
int nextUnarrived = ((int)n) >>> PARTIES_SHIFT; |
655 |
> |
int nextUnarrived = (int)n >>> PARTIES_SHIFT; |
656 |
|
if (onAdvance(phase, nextUnarrived)) |
657 |
|
n |= TERMINATION_BIT; |
658 |
|
else if (nextUnarrived == 0) |
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 |
|
*/ |
1108 |
|
|
1109 |
|
// Unsafe mechanics |
1110 |
|
|
1111 |
< |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1112 |
< |
private static final long stateOffset = |
1113 |
< |
objectFieldOffset("state", Phaser.class); |
1111 |
< |
|
1112 |
< |
private static long objectFieldOffset(String field, Class<?> klazz) { |
1111 |
> |
private static final sun.misc.Unsafe UNSAFE; |
1112 |
> |
private static final long stateOffset; |
1113 |
> |
static { |
1114 |
|
try { |
1115 |
< |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
1116 |
< |
} catch (NoSuchFieldException e) { |
1117 |
< |
// Convert Exception to corresponding Error |
1118 |
< |
NoSuchFieldError error = new NoSuchFieldError(field); |
1119 |
< |
error.initCause(e); |
1120 |
< |
throw error; |
1115 |
> |
UNSAFE = getUnsafe(); |
1116 |
> |
Class<?> k = Phaser.class; |
1117 |
> |
stateOffset = UNSAFE.objectFieldOffset |
1118 |
> |
(k.getDeclaredField("state")); |
1119 |
> |
} catch (Exception e) { |
1120 |
> |
throw new Error(e); |
1121 |
|
} |
1122 |
|
} |
1123 |
|
|