18 |
|
* but supporting more flexible usage. |
19 |
|
* |
20 |
|
* <p> <b>Registration.</b> Unlike the case for other barriers, the |
21 |
< |
* number of parties <em>registered</em> to synchronize on a phaser |
21 |
> |
* number of parties <em>registered</em> to synchronize on a Phaser |
22 |
|
* may vary over time. Tasks may be registered at any time (using |
23 |
|
* methods {@link #register}, {@link #bulkRegister}, or forms of |
24 |
|
* constructors establishing initial numbers of parties), and |
76 |
|
* |
77 |
|
* <p> <b>Termination.</b> A {@code Phaser} may enter a |
78 |
|
* <em>termination</em> state in which all synchronization methods |
79 |
< |
* immediately return without updating phaser state or waiting for |
79 |
> |
* immediately return without updating Phaser state or waiting for |
80 |
|
* advance, and indicating (via a negative phase value) that execution |
81 |
|
* is complete. Termination is triggered when an invocation of {@code |
82 |
< |
* onAdvance} returns {@code true}. As illustrated below, when |
83 |
< |
* phasers control actions with a fixed number of iterations, it is |
82 |
> |
* onAdvance} returns {@code true}. The default implementation returns |
83 |
> |
* {@code true} if a deregistration has caused the number of |
84 |
> |
* registered parties to become zero. As illustrated below, when |
85 |
> |
* Phasers control actions with a fixed number of iterations, it is |
86 |
|
* often convenient to override this method to cause termination when |
87 |
|
* the current phase number reaches a threshold. Method {@link |
88 |
|
* #forceTermination} is also available to abruptly release waiting |
89 |
|
* threads and allow them to terminate. |
90 |
|
* |
91 |
< |
* <p> <b>Tiering.</b> Phasers may be <em>tiered</em> (i.e., arranged |
92 |
< |
* in tree structures) to reduce contention. Phasers with large |
93 |
< |
* numbers of parties that would otherwise experience heavy |
91 |
> |
* <p> <b>Tiering.</b> Phasers may be <em>tiered</em> (i.e., |
92 |
> |
* constructed in tree structures) to reduce contention. Phasers with |
93 |
> |
* large numbers of parties that would otherwise experience heavy |
94 |
|
* synchronization contention costs may instead be set up so that |
95 |
|
* groups of sub-phasers share a common parent. This may greatly |
96 |
|
* increase throughput even though it incurs greater per-operation |
97 |
|
* overhead. |
98 |
|
* |
99 |
|
* <p><b>Monitoring.</b> While synchronization methods may be invoked |
100 |
< |
* only by registered parties, the current state of a phaser may be |
100 |
> |
* only by registered parties, the current state of a Phaser may be |
101 |
|
* monitored by any caller. At any given moment there are {@link |
102 |
|
* #getRegisteredParties} parties in total, of which {@link |
103 |
|
* #getArrivedParties} have arrived at the current phase ({@link |
183 |
|
* }}</pre> |
184 |
|
* |
185 |
|
* |
186 |
< |
* <p>To create a set of tasks using a tree of phasers, |
186 |
> |
* <p>To create a set of tasks using a tree of Phasers, |
187 |
|
* you could use code of the following form, assuming a |
188 |
< |
* Task class with a constructor accepting a phaser that |
188 |
> |
* Task class with a constructor accepting a Phaser that |
189 |
|
* it registers with upon construction: |
190 |
|
* |
191 |
|
* <pre> {@code |
212 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
213 |
|
* maximum number of parties to 65535. Attempts to register additional |
214 |
|
* parties result in {@code IllegalStateException}. However, you can and |
215 |
< |
* should create tiered phasers to accommodate arbitrarily large sets |
215 |
> |
* should create tiered Phasers to accommodate arbitrarily large sets |
216 |
|
* of participants. |
217 |
|
* |
218 |
|
* @since 1.7 |
242 |
|
*/ |
243 |
|
private volatile long state; |
244 |
|
|
245 |
< |
private static final int MAX_COUNT = 0xffff; |
246 |
< |
private static final int MAX_PHASE = 0x7fffffff; |
247 |
< |
private static final int PARTIES_SHIFT = 16; |
248 |
< |
private static final int PHASE_SHIFT = 32; |
249 |
< |
private static final long UNARRIVED_MASK = 0xffffL; |
250 |
< |
private static final long PARTIES_MASK = 0xffff0000L; |
251 |
< |
private static final long ONE_ARRIVAL = 1L; |
252 |
< |
private static final long ONE_PARTY = 1L << PARTIES_SHIFT; |
253 |
< |
private static final long TERMINATION_PHASE = -1L << PHASE_SHIFT; |
245 |
> |
private static final int MAX_PARTIES = 0xffff; |
246 |
> |
private static final int MAX_PHASE = 0x7fffffff; |
247 |
> |
private static final int PARTIES_SHIFT = 16; |
248 |
> |
private static final int PHASE_SHIFT = 32; |
249 |
> |
private static final int UNARRIVED_MASK = 0xffff; // to mask ints |
250 |
> |
private static final long PARTIES_MASK = 0xffff0000L; // to mask longs |
251 |
> |
private static final long ONE_ARRIVAL = 1L; |
252 |
> |
private static final long ONE_PARTY = 1L << PARTIES_SHIFT; |
253 |
> |
private static final long TERMINATION_BIT = 1L << 63; |
254 |
|
|
255 |
|
// The following unpacking methods are usually manually inlined |
256 |
|
|
257 |
|
private static int unarrivedOf(long s) { |
258 |
< |
return (int) (s & UNARRIVED_MASK); |
258 |
> |
return (int)s & UNARRIVED_MASK; |
259 |
|
} |
260 |
|
|
261 |
|
private static int partiesOf(long s) { |
262 |
< |
return ((int) (s & PARTIES_MASK)) >>> PARTIES_SHIFT; |
262 |
> |
return (int)s >>> PARTIES_SHIFT; |
263 |
|
} |
264 |
|
|
265 |
|
private static int phaseOf(long s) { |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
+ |
* Returns message string for bounds exceptions on arrival. |
299 |
+ |
*/ |
300 |
+ |
private String badArrive(long s) { |
301 |
+ |
return "Attempted arrival of unregistered party for " + |
302 |
+ |
stateToString(s); |
303 |
+ |
} |
304 |
+ |
|
305 |
+ |
/** |
306 |
+ |
* Returns message string for bounds exceptions on registration. |
307 |
+ |
*/ |
308 |
+ |
private String badRegister(long s) { |
309 |
+ |
return "Attempt to register more than " + |
310 |
+ |
MAX_PARTIES + " parties for " + stateToString(s); |
311 |
+ |
} |
312 |
+ |
|
313 |
+ |
/** |
314 |
|
* Main implementation for methods arrive and arriveAndDeregister. |
315 |
|
* Manually tuned to speed up and minimize race windows for the |
316 |
|
* common case of just decrementing unarrived field. |
321 |
|
*/ |
322 |
|
private int doArrive(long adj) { |
323 |
|
for (;;) { |
324 |
< |
long s; |
325 |
< |
int phase, unarrived; |
326 |
< |
if ((phase = (int)((s = state) >>> PHASE_SHIFT)) < 0) |
324 |
> |
long s = state; |
325 |
> |
int unarrived = (int)s & UNARRIVED_MASK; |
326 |
> |
int phase = (int)(s >>> PHASE_SHIFT); |
327 |
> |
if (phase < 0) |
328 |
|
return phase; |
329 |
< |
else if ((unarrived = (int)(s & UNARRIVED_MASK)) == 0) |
330 |
< |
checkBadArrive(s); |
331 |
< |
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s -= adj)){ |
329 |
> |
else if (unarrived == 0) { |
330 |
> |
if (reconcileState() == s) // recheck |
331 |
> |
throw new IllegalStateException(badArrive(s)); |
332 |
> |
} |
333 |
> |
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adj)) { |
334 |
|
if (unarrived == 1) { |
314 |
– |
Phaser par; |
335 |
|
long p = s & PARTIES_MASK; // unshifted parties field |
336 |
|
long lu = p >>> PARTIES_SHIFT; |
337 |
|
int u = (int)lu; |
338 |
|
int nextPhase = (phase + 1) & MAX_PHASE; |
339 |
|
long next = ((long)nextPhase << PHASE_SHIFT) | p | lu; |
340 |
< |
if ((par = parent) == null) { |
341 |
< |
UNSAFE.compareAndSwapLong |
342 |
< |
(this, stateOffset, s, onAdvance(phase, u)? |
343 |
< |
next | TERMINATION_PHASE : next); |
340 |
> |
final Phaser parent = this.parent; |
341 |
> |
if (parent == null) { |
342 |
> |
if (onAdvance(phase, u)) |
343 |
> |
next |= TERMINATION_BIT; |
344 |
> |
UNSAFE.compareAndSwapLong(this, stateOffset, s, next); |
345 |
|
releaseWaiters(phase); |
346 |
|
} |
347 |
|
else { |
348 |
< |
par.doArrive(u == 0? |
349 |
< |
ONE_ARRIVAL|ONE_PARTY : ONE_ARRIVAL); |
350 |
< |
if ((int)(par.state >>> PHASE_SHIFT) != nextPhase || |
330 |
< |
((int)(state >>> PHASE_SHIFT) != nextPhase && |
331 |
< |
!UNSAFE.compareAndSwapLong(this, stateOffset, |
332 |
< |
s, next))) |
348 |
> |
parent.doArrive((u == 0) ? |
349 |
> |
ONE_ARRIVAL|ONE_PARTY : ONE_ARRIVAL); |
350 |
> |
if ((int)(parent.state >>> PHASE_SHIFT) != nextPhase) |
351 |
|
reconcileState(); |
352 |
+ |
else if (state == s) |
353 |
+ |
UNSAFE.compareAndSwapLong(this, stateOffset, s, |
354 |
+ |
next); |
355 |
|
} |
356 |
|
} |
357 |
|
return phase; |
360 |
|
} |
361 |
|
|
362 |
|
/** |
342 |
– |
* Rechecks state and throws bounds exceptions on arrival -- called |
343 |
– |
* only if unarrived is apparently zero. |
344 |
– |
*/ |
345 |
– |
private void checkBadArrive(long s) { |
346 |
– |
if (reconcileState() == s) |
347 |
– |
throw new IllegalStateException |
348 |
– |
("Attempted arrival of unregistered party for " + |
349 |
– |
stateToString(s)); |
350 |
– |
} |
351 |
– |
|
352 |
– |
/** |
363 |
|
* Implementation of register, bulkRegister |
364 |
|
* |
365 |
< |
* @param registrations number to add to both parties and unarrived fields |
365 |
> |
* @param registrations number to add to both parties and |
366 |
> |
* unarrived fields. Must be greater than zero. |
367 |
|
*/ |
368 |
|
private int doRegister(int registrations) { |
369 |
< |
long adj = (long)registrations; // adjustment to state |
370 |
< |
adj |= adj << PARTIES_SHIFT; |
371 |
< |
Phaser par = parent; |
369 |
> |
// adjustment to state |
370 |
> |
long adj = ((long)registrations << PARTIES_SHIFT) | registrations; |
371 |
> |
final Phaser parent = this.parent; |
372 |
|
for (;;) { |
373 |
< |
int phase, parties; |
374 |
< |
long s = par == null? state : reconcileState(); |
375 |
< |
if ((phase = (int)(s >>> PHASE_SHIFT)) < 0) |
373 |
> |
long s = (parent == null) ? state : reconcileState(); |
374 |
> |
int parties = (int)s >>> PARTIES_SHIFT; |
375 |
> |
int phase = (int)(s >>> PHASE_SHIFT); |
376 |
> |
if (phase < 0) |
377 |
|
return phase; |
378 |
< |
if ((parties = ((int)(s & PARTIES_MASK)) >>> PARTIES_SHIFT) != 0 && |
367 |
< |
(s & UNARRIVED_MASK) == 0) |
368 |
< |
internalAwaitAdvance(phase, null); // wait for onAdvance |
369 |
< |
else if (parties + registrations > MAX_COUNT) |
378 |
> |
else if (registrations > MAX_PARTIES - parties) |
379 |
|
throw new IllegalStateException(badRegister(s)); |
380 |
< |
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s + adj)) |
381 |
< |
return phase; |
380 |
> |
else if ((parties == 0 && parent == null) || // first reg of root |
381 |
> |
((int)s & UNARRIVED_MASK) != 0) { // not advancing |
382 |
> |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s + adj)) |
383 |
> |
return phase; |
384 |
> |
} |
385 |
> |
else if (parties != 0) // wait for onAdvance |
386 |
> |
root.internalAwaitAdvance(phase, null); |
387 |
> |
else { // 1st registration of child |
388 |
> |
synchronized (this) { // register parent first |
389 |
> |
if (reconcileState() == s) { // recheck under lock |
390 |
> |
parent.doRegister(1); // OK if throws IllegalState |
391 |
> |
for (;;) { // simpler form of outer loop |
392 |
> |
s = reconcileState(); |
393 |
> |
phase = (int)(s >>> PHASE_SHIFT); |
394 |
> |
if (phase < 0 || |
395 |
> |
UNSAFE.compareAndSwapLong(this, stateOffset, |
396 |
> |
s, s + adj)) |
397 |
> |
return phase; |
398 |
> |
} |
399 |
> |
} |
400 |
> |
} |
401 |
> |
} |
402 |
|
} |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
< |
* Returns message string for bounds exceptions on registration |
378 |
< |
*/ |
379 |
< |
private String badRegister(long s) { |
380 |
< |
return "Attempt to register more than " + |
381 |
< |
MAX_COUNT + " parties for " + stateToString(s); |
382 |
< |
} |
383 |
< |
|
384 |
< |
/** |
385 |
< |
* Recursively resolves lagged phase propagation from root if |
386 |
< |
* necessary. |
406 |
> |
* Recursively resolves lagged phase propagation from root if necessary. |
407 |
|
*/ |
408 |
|
private long reconcileState() { |
409 |
|
Phaser par = parent; |
410 |
< |
if (par == null) |
411 |
< |
return state; |
412 |
< |
Phaser rt = root; |
413 |
< |
for (;;) { |
414 |
< |
long s, u; |
415 |
< |
int phase, rPhase, pPhase; |
416 |
< |
if ((phase = (int)((s = state)>>> PHASE_SHIFT)) < 0 || |
417 |
< |
(rPhase = (int)(rt.state >>> PHASE_SHIFT)) == phase) |
418 |
< |
return s; |
419 |
< |
long pState = par.parent == null? par.state : par.reconcileState(); |
420 |
< |
if (state == s) { |
421 |
< |
if ((rPhase < 0 || (s & UNARRIVED_MASK) == 0) && |
422 |
< |
((pPhase = (int)(pState >>> PHASE_SHIFT)) < 0 || |
423 |
< |
pPhase == ((phase + 1) & MAX_PHASE))) |
424 |
< |
UNSAFE.compareAndSwapLong |
405 |
< |
(this, stateOffset, s, |
406 |
< |
(((long) pPhase) << PHASE_SHIFT) | |
407 |
< |
(u = s & PARTIES_MASK) | |
408 |
< |
(u >>> PARTIES_SHIFT)); // reset unarrived to parties |
409 |
< |
else |
410 |
< |
releaseWaiters(phase); // help release others |
410 |
> |
long s = state; |
411 |
> |
if (par != null) { |
412 |
> |
Phaser rt = root; |
413 |
> |
int phase, rPhase; |
414 |
> |
while ((phase = (int)(s >>> PHASE_SHIFT)) >= 0 && |
415 |
> |
(rPhase = (int)(rt.state >>> PHASE_SHIFT)) != phase) { |
416 |
> |
if (par != rt && (int)(par.state >>> PHASE_SHIFT) != rPhase) |
417 |
> |
par.reconcileState(); |
418 |
> |
else if (rPhase < 0 || ((int)s & UNARRIVED_MASK) == 0) { |
419 |
> |
long u = s & PARTIES_MASK; // reset unarrived to parties |
420 |
> |
long next = ((((long) rPhase) << PHASE_SHIFT) | u | |
421 |
> |
(u >>> PARTIES_SHIFT)); |
422 |
> |
UNSAFE.compareAndSwapLong(this, stateOffset, s, next); |
423 |
> |
} |
424 |
> |
s = state; |
425 |
|
} |
426 |
|
} |
427 |
+ |
return s; |
428 |
|
} |
429 |
|
|
430 |
|
/** |
431 |
< |
* Creates a new phaser without any initially registered parties, |
431 |
> |
* Creates a new Phaser without any initially registered parties, |
432 |
|
* initial phase number 0, and no parent. Any thread using this |
433 |
< |
* phaser will need to first register for it. |
433 |
> |
* Phaser will need to first register for it. |
434 |
|
*/ |
435 |
|
public Phaser() { |
436 |
|
this(null, 0); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
< |
* Creates a new phaser with the given number of registered |
440 |
> |
* Creates a new Phaser with the given number of registered |
441 |
|
* unarrived parties, initial phase number 0, and no parent. |
442 |
|
* |
443 |
|
* @param parties the number of parties required to trip barrier |
449 |
|
} |
450 |
|
|
451 |
|
/** |
452 |
< |
* Creates a new phaser with the given parent, without any |
438 |
< |
* initially registered parties. If parent is non-null this phaser |
439 |
< |
* is registered with the parent and its initial phase number is |
440 |
< |
* the same as that of parent phaser. |
452 |
> |
* Equivalent to {@link #Phaser(Phaser, int) Phaser(parent, 0)}. |
453 |
|
* |
454 |
< |
* @param parent the parent phaser |
454 |
> |
* @param parent the parent Phaser |
455 |
|
*/ |
456 |
|
public Phaser(Phaser parent) { |
457 |
|
this(parent, 0); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* Creates a new phaser with the given parent and number of |
462 |
< |
* registered unarrived parties. If parent is non-null, this phaser |
463 |
< |
* is registered with the parent and its initial phase number is |
464 |
< |
* the same as that of parent phaser. |
461 |
> |
* Creates a new Phaser with the given parent and number of |
462 |
> |
* registered unarrived parties. Registration and deregistration |
463 |
> |
* of this child Phaser with its parent are managed automatically. |
464 |
> |
* If the given parent is non-null, whenever this child Phaser has |
465 |
> |
* any registered parties (as established in this constructor, |
466 |
> |
* {@link #register}, or {@link #bulkRegister}), this child Phaser |
467 |
> |
* is registered with its parent. Whenever the number of |
468 |
> |
* registered parties becomes zero as the result of an invocation |
469 |
> |
* of {@link #arriveAndDeregister}, this child Phaser is |
470 |
> |
* deregistered from its parent. |
471 |
|
* |
472 |
< |
* @param parent the parent phaser |
472 |
> |
* @param parent the parent Phaser |
473 |
|
* @param parties the number of parties required to trip barrier |
474 |
|
* @throws IllegalArgumentException if parties less than zero |
475 |
|
* or greater than the maximum number of parties supported |
476 |
|
*/ |
477 |
|
public Phaser(Phaser parent, int parties) { |
478 |
< |
if (parties < 0 || parties > MAX_COUNT) |
478 |
> |
if (parties >>> PARTIES_SHIFT != 0) |
479 |
|
throw new IllegalArgumentException("Illegal number of parties"); |
480 |
< |
int phase; |
480 |
> |
long s = ((long) parties) | (((long) parties) << PARTIES_SHIFT); |
481 |
|
this.parent = parent; |
482 |
|
if (parent != null) { |
483 |
|
Phaser r = parent.root; |
484 |
|
this.root = r; |
485 |
|
this.evenQ = r.evenQ; |
486 |
|
this.oddQ = r.oddQ; |
487 |
< |
phase = parent.register(); |
487 |
> |
if (parties != 0) |
488 |
> |
s |= ((long)(parent.doRegister(1))) << PHASE_SHIFT; |
489 |
|
} |
490 |
|
else { |
491 |
|
this.root = this; |
492 |
|
this.evenQ = new AtomicReference<QNode>(); |
493 |
|
this.oddQ = new AtomicReference<QNode>(); |
475 |
– |
phase = 0; |
494 |
|
} |
495 |
< |
long p = (long)parties; |
478 |
< |
this.state = (((long) phase) << PHASE_SHIFT) | p | (p << PARTIES_SHIFT); |
495 |
> |
this.state = s; |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
< |
* Adds a new unarrived party to this phaser. |
500 |
< |
* If an ongoing invocation of {@link #onAdvance} is in progress, |
501 |
< |
* this method may wait until its completion before registering. |
499 |
> |
* Adds a new unarrived party to this Phaser. If an ongoing |
500 |
> |
* invocation of {@link #onAdvance} is in progress, this method |
501 |
> |
* may await its completion before returning. If this Phaser has |
502 |
> |
* a parent, and this Phaser previously had no registered parties, |
503 |
> |
* this Phaser is also registered with its parent. |
504 |
|
* |
505 |
|
* @return the arrival phase number to which this registration applied |
506 |
|
* @throws IllegalStateException if attempting to register more |
511 |
|
} |
512 |
|
|
513 |
|
/** |
514 |
< |
* Adds the given number of new unarrived parties to this phaser. |
514 |
> |
* Adds the given number of new unarrived parties to this Phaser. |
515 |
|
* If an ongoing invocation of {@link #onAdvance} is in progress, |
516 |
< |
* this method may wait until its completion before registering. |
516 |
> |
* this method may await its completion before returning. If this |
517 |
> |
* Phaser has a parent, and the given number of parities is |
518 |
> |
* greater than zero, and this Phaser previously had no registered |
519 |
> |
* parties, this Phaser is also registered with its parent. |
520 |
|
* |
521 |
|
* @param parties the number of additional parties required to trip barrier |
522 |
|
* @return the arrival phase number to which this registration applied |
527 |
|
public int bulkRegister(int parties) { |
528 |
|
if (parties < 0) |
529 |
|
throw new IllegalArgumentException(); |
508 |
– |
if (parties > MAX_COUNT) |
509 |
– |
throw new IllegalStateException(badRegister(state)); |
530 |
|
if (parties == 0) |
531 |
|
return getPhase(); |
532 |
|
return doRegister(parties); |
533 |
|
} |
534 |
|
|
535 |
|
/** |
536 |
< |
* Arrives at the barrier, but does not wait for others. (You can |
537 |
< |
* in turn wait for others via {@link #awaitAdvance}). It is an |
538 |
< |
* unenforced usage error for an unregistered party to invoke this |
539 |
< |
* method. |
536 |
> |
* Arrives at the barrier, without waiting for others to arrive. |
537 |
> |
* |
538 |
> |
* <p>It is a usage error for an unregistered party to invoke this |
539 |
> |
* method. However, this error may result in an {@code |
540 |
> |
* IllegalStateException} only upon some subsequent operation on |
541 |
> |
* this Phaser, if ever. |
542 |
|
* |
543 |
|
* @return the arrival phase number, or a negative value if terminated |
544 |
|
* @throws IllegalStateException if not terminated and the number |
550 |
|
|
551 |
|
/** |
552 |
|
* Arrives at the barrier and deregisters from it without waiting |
553 |
< |
* for others. Deregistration reduces the number of parties |
554 |
< |
* required to trip the barrier in future phases. If this phaser |
555 |
< |
* has a parent, and deregistration causes this phaser to have |
556 |
< |
* zero parties, this phaser also arrives at and is deregistered |
557 |
< |
* from its parent. It is an unenforced usage error for an |
558 |
< |
* unregistered party to invoke this method. |
553 |
> |
* for others to arrive. Deregistration reduces the number of |
554 |
> |
* parties required to trip the barrier in future phases. If this |
555 |
> |
* Phaser has a parent, and deregistration causes this Phaser to |
556 |
> |
* have zero parties, this Phaser is also deregistered from its |
557 |
> |
* parent. |
558 |
> |
* |
559 |
> |
* <p>It is a usage error for an unregistered party to invoke this |
560 |
> |
* method. However, this error may result in an {@code |
561 |
> |
* IllegalStateException} only upon some subsequent operation on |
562 |
> |
* this Phaser, if ever. |
563 |
|
* |
564 |
|
* @return the arrival phase number, or a negative value if terminated |
565 |
|
* @throws IllegalStateException if not terminated and the number |
575 |
|
* interruption or timeout, you can arrange this with an analogous |
576 |
|
* construction using one of the other forms of the {@code |
577 |
|
* awaitAdvance} method. If instead you need to deregister upon |
578 |
< |
* arrival, use {@link #arriveAndDeregister}. It is an unenforced |
579 |
< |
* usage error for an unregistered party to invoke this method. |
578 |
> |
* arrival, use {@code awaitAdvance(arriveAndDeregister())}. |
579 |
> |
* |
580 |
> |
* <p>It is a usage error for an unregistered party to invoke this |
581 |
> |
* method. However, this error may result in an {@code |
582 |
> |
* IllegalStateException} only upon some subsequent operation on |
583 |
> |
* this Phaser, if ever. |
584 |
|
* |
585 |
|
* @return the arrival phase number, or a negative number if terminated |
586 |
|
* @throws IllegalStateException if not terminated and the number |
587 |
|
* of unarrived parties would become negative |
588 |
|
*/ |
589 |
|
public int arriveAndAwaitAdvance() { |
590 |
< |
return awaitAdvance(arrive()); |
590 |
> |
return awaitAdvance(doArrive(ONE_ARRIVAL)); |
591 |
|
} |
592 |
|
|
593 |
|
/** |
603 |
|
* if terminated or argument is negative |
604 |
|
*/ |
605 |
|
public int awaitAdvance(int phase) { |
606 |
+ |
Phaser rt; |
607 |
+ |
int p = (int)(state >>> PHASE_SHIFT); |
608 |
|
if (phase < 0) |
609 |
|
return phase; |
610 |
< |
int p = (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT); |
611 |
< |
if (p != phase) |
612 |
< |
return p; |
613 |
< |
return internalAwaitAdvance(phase, null); |
610 |
> |
if (p == phase && |
611 |
> |
(p = (int)((rt = root).state >>> PHASE_SHIFT)) == phase) |
612 |
> |
return rt.internalAwaitAdvance(phase, null); |
613 |
> |
return p; |
614 |
|
} |
615 |
|
|
616 |
|
/** |
629 |
|
*/ |
630 |
|
public int awaitAdvanceInterruptibly(int phase) |
631 |
|
throws InterruptedException { |
632 |
+ |
Phaser rt; |
633 |
+ |
int p = (int)(state >>> PHASE_SHIFT); |
634 |
|
if (phase < 0) |
635 |
|
return phase; |
636 |
< |
int p = (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT); |
637 |
< |
if (p != phase) |
638 |
< |
return p; |
639 |
< |
QNode node = new QNode(this, phase, true, false, 0L); |
640 |
< |
p = internalAwaitAdvance(phase, node); |
641 |
< |
if (node.wasInterrupted) |
642 |
< |
throw new InterruptedException(); |
643 |
< |
else |
610 |
< |
return p; |
636 |
> |
if (p == phase && |
637 |
> |
(p = (int)((rt = root).state >>> PHASE_SHIFT)) == phase) { |
638 |
> |
QNode node = new QNode(this, phase, true, false, 0L); |
639 |
> |
p = rt.internalAwaitAdvance(phase, node); |
640 |
> |
if (node.wasInterrupted) |
641 |
> |
throw new InterruptedException(); |
642 |
> |
} |
643 |
> |
return p; |
644 |
|
} |
645 |
|
|
646 |
|
/** |
667 |
|
long timeout, TimeUnit unit) |
668 |
|
throws InterruptedException, TimeoutException { |
669 |
|
long nanos = unit.toNanos(timeout); |
670 |
+ |
Phaser rt; |
671 |
+ |
int p = (int)(state >>> PHASE_SHIFT); |
672 |
|
if (phase < 0) |
673 |
|
return phase; |
674 |
< |
int p = (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT); |
675 |
< |
if (p != phase) |
676 |
< |
return p; |
677 |
< |
QNode node = new QNode(this, phase, true, true, nanos); |
678 |
< |
p = internalAwaitAdvance(phase, node); |
679 |
< |
if (node.wasInterrupted) |
680 |
< |
throw new InterruptedException(); |
681 |
< |
else if (p == phase) |
682 |
< |
throw new TimeoutException(); |
683 |
< |
else |
649 |
< |
return p; |
674 |
> |
if (p == phase && |
675 |
> |
(p = (int)((rt = root).state >>> PHASE_SHIFT)) == phase) { |
676 |
> |
QNode node = new QNode(this, phase, true, true, nanos); |
677 |
> |
p = rt.internalAwaitAdvance(phase, node); |
678 |
> |
if (node.wasInterrupted) |
679 |
> |
throw new InterruptedException(); |
680 |
> |
else if (p == phase) |
681 |
> |
throw new TimeoutException(); |
682 |
> |
} |
683 |
> |
return p; |
684 |
|
} |
685 |
|
|
686 |
|
/** |
687 |
< |
* Forces this barrier to enter termination state. Counts of |
688 |
< |
* arrived and registered parties are unaffected. If this phaser |
689 |
< |
* has a parent, it too is terminated. This method may be useful |
690 |
< |
* for coordinating recovery after one or more tasks encounter |
691 |
< |
* unexpected exceptions. |
687 |
> |
* Forces this barrier to enter termination state. Counts of |
688 |
> |
* arrived and registered parties are unaffected. If this Phaser |
689 |
> |
* is a member of a tiered set of Phasers, then all of the Phasers |
690 |
> |
* in the set are terminated. If this Phaser is already |
691 |
> |
* terminated, this method has no effect. This method may be |
692 |
> |
* useful for coordinating recovery after one or more tasks |
693 |
> |
* encounter unexpected exceptions. |
694 |
|
*/ |
695 |
|
public void forceTermination() { |
696 |
< |
Phaser r = root; // force at root then reconcile |
696 |
> |
// Only need to change root state |
697 |
> |
final Phaser root = this.root; |
698 |
|
long s; |
699 |
< |
while ((s = r.state) >= 0) |
700 |
< |
UNSAFE.compareAndSwapLong(r, stateOffset, s, s | TERMINATION_PHASE); |
701 |
< |
reconcileState(); |
702 |
< |
releaseWaiters(0); // signal all threads |
703 |
< |
releaseWaiters(1); |
699 |
> |
while ((s = root.state) >= 0) { |
700 |
> |
if (UNSAFE.compareAndSwapLong(root, stateOffset, |
701 |
> |
s, s | TERMINATION_BIT)) { |
702 |
> |
releaseWaiters(0); // signal all threads |
703 |
> |
releaseWaiters(1); |
704 |
> |
return; |
705 |
> |
} |
706 |
> |
} |
707 |
|
} |
708 |
|
|
709 |
|
/** |
714 |
|
* @return the phase number, or a negative value if terminated |
715 |
|
*/ |
716 |
|
public final int getPhase() { |
717 |
< |
return (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT); |
717 |
> |
return (int)(root.state >>> PHASE_SHIFT); |
718 |
|
} |
719 |
|
|
720 |
|
/** |
723 |
|
* @return the number of parties |
724 |
|
*/ |
725 |
|
public int getRegisteredParties() { |
726 |
< |
return partiesOf(parent==null? state : reconcileState()); |
726 |
> |
return partiesOf(state); |
727 |
|
} |
728 |
|
|
729 |
|
/** |
733 |
|
* @return the number of arrived parties |
734 |
|
*/ |
735 |
|
public int getArrivedParties() { |
736 |
< |
return arrivedOf(parent==null? state : reconcileState()); |
736 |
> |
long s = state; |
737 |
> |
int u = unarrivedOf(s); // only reconcile if possibly needed |
738 |
> |
return (u != 0 || parent == null) ? |
739 |
> |
partiesOf(s) - u : |
740 |
> |
arrivedOf(reconcileState()); |
741 |
|
} |
742 |
|
|
743 |
|
/** |
747 |
|
* @return the number of unarrived parties |
748 |
|
*/ |
749 |
|
public int getUnarrivedParties() { |
750 |
< |
return unarrivedOf(parent==null? state : reconcileState()); |
750 |
> |
int u = unarrivedOf(state); |
751 |
> |
return (u != 0 || parent == null) ? u : unarrivedOf(reconcileState()); |
752 |
|
} |
753 |
|
|
754 |
|
/** |
755 |
< |
* Returns the parent of this phaser, or {@code null} if none. |
755 |
> |
* Returns the parent of this Phaser, or {@code null} if none. |
756 |
|
* |
757 |
< |
* @return the parent of this phaser, or {@code null} if none |
757 |
> |
* @return the parent of this Phaser, or {@code null} if none |
758 |
|
*/ |
759 |
|
public Phaser getParent() { |
760 |
|
return parent; |
761 |
|
} |
762 |
|
|
763 |
|
/** |
764 |
< |
* Returns the root ancestor of this phaser, which is the same as |
765 |
< |
* this phaser if it has no parent. |
764 |
> |
* Returns the root ancestor of this Phaser, which is the same as |
765 |
> |
* this Phaser if it has no parent. |
766 |
|
* |
767 |
< |
* @return the root ancestor of this phaser |
767 |
> |
* @return the root ancestor of this Phaser |
768 |
|
*/ |
769 |
|
public Phaser getRoot() { |
770 |
|
return root; |
776 |
|
* @return {@code true} if this barrier has been terminated |
777 |
|
*/ |
778 |
|
public boolean isTerminated() { |
779 |
< |
return (parent == null? state : reconcileState()) < 0; |
779 |
> |
return root.state < 0L; |
780 |
|
} |
781 |
|
|
782 |
|
/** |
791 |
|
* propagated to the party attempting to trip the barrier, in |
792 |
|
* which case no advance occurs. |
793 |
|
* |
794 |
< |
* <p>The arguments to this method provide the state of the phaser |
794 |
> |
* <p>The arguments to this method provide the state of the Phaser |
795 |
|
* prevailing for the current transition. The effects of invoking |
796 |
|
* arrival, registration, and waiting methods on this Phaser from |
797 |
|
* within {@code onAdvance} are unspecified and should not be |
801 |
|
* {@code onAdvance} is invoked only for its root Phaser on each |
802 |
|
* advance. |
803 |
|
* |
804 |
< |
* <p>The default version returns {@code true} when the number of |
805 |
< |
* registered parties is zero. Normally, overrides that arrange |
806 |
< |
* termination for other reasons should also preserve this |
807 |
< |
* property. |
804 |
> |
* <p>To support the most common use cases, the default |
805 |
> |
* implementation of this method returns {@code true} when the |
806 |
> |
* number of registered parties has become zero as the result of a |
807 |
> |
* party invoking {@code arriveAndDeregister}. You can disable |
808 |
> |
* this behavior, thus enabling continuation upon future |
809 |
> |
* registrations, by overriding this method to always return |
810 |
> |
* {@code false}: |
811 |
> |
* |
812 |
> |
* <pre> {@code |
813 |
> |
* Phaser phaser = new Phaser() { |
814 |
> |
* protected boolean onAdvance(int phase, int parties) { return false; } |
815 |
> |
* }}</pre> |
816 |
|
* |
817 |
|
* @param phase the phase number on entering the barrier |
818 |
|
* @param registeredParties the current number of registered parties |
819 |
|
* @return {@code true} if this barrier should terminate |
820 |
|
*/ |
821 |
|
protected boolean onAdvance(int phase, int registeredParties) { |
822 |
< |
return registeredParties <= 0; |
822 |
> |
return registeredParties == 0; |
823 |
|
} |
824 |
|
|
825 |
|
/** |
826 |
< |
* Returns a string identifying this phaser, as well as its |
826 |
> |
* Returns a string identifying this Phaser, as well as its |
827 |
|
* state. The state, in brackets, includes the String {@code |
828 |
|
* "phase = "} followed by the phase number, {@code "parties = "} |
829 |
|
* followed by the number of registered parties, and {@code |
848 |
|
// Waiting mechanics |
849 |
|
|
850 |
|
/** |
851 |
< |
* Removes and signals threads from queue for phase |
851 |
> |
* Removes and signals threads from queue for phase. |
852 |
|
*/ |
853 |
|
private void releaseWaiters(int phase) { |
854 |
< |
AtomicReference<QNode> head = queueFor(phase); |
854 |
> |
AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ; |
855 |
|
QNode q; |
856 |
|
int p; |
857 |
|
while ((q = head.get()) != null && |
862 |
|
} |
863 |
|
} |
864 |
|
|
812 |
– |
/** |
813 |
– |
* Tries to enqueue given node in the appropriate wait queue. |
814 |
– |
* |
815 |
– |
* @return true if successful |
816 |
– |
*/ |
817 |
– |
private boolean tryEnqueue(int phase, QNode node) { |
818 |
– |
releaseWaiters(phase-1); // ensure old queue clean |
819 |
– |
AtomicReference<QNode> head = queueFor(phase); |
820 |
– |
QNode q = head.get(); |
821 |
– |
return ((q == null || q.phase == phase) && |
822 |
– |
(int)(root.state >>> PHASE_SHIFT) == phase && |
823 |
– |
head.compareAndSet(node.next = q, node)); |
824 |
– |
} |
825 |
– |
|
865 |
|
/** The number of CPUs, for spin control */ |
866 |
|
private static final int NCPU = Runtime.getRuntime().availableProcessors(); |
867 |
|
|
873 |
|
* avoid it when threads regularly arrive: When a thread in |
874 |
|
* internalAwaitAdvance notices another arrival before blocking, |
875 |
|
* and there appear to be enough CPUs available, it spins |
876 |
< |
* SPINS_PER_ARRIVAL more times before continuing to try to |
877 |
< |
* block. The value trades off good-citizenship vs big unnecessary |
839 |
< |
* slowdowns. |
876 |
> |
* SPINS_PER_ARRIVAL more times before blocking. The value trades |
877 |
> |
* off good-citizenship vs big unnecessary slowdowns. |
878 |
|
*/ |
879 |
< |
static final int SPINS_PER_ARRIVAL = NCPU < 2? 1 : 1 << 8; |
879 |
> |
static final int SPINS_PER_ARRIVAL = (NCPU < 2) ? 1 : 1 << 8; |
880 |
|
|
881 |
|
/** |
882 |
|
* Possibly blocks and waits for phase to advance unless aborted. |
883 |
+ |
* Call only from root node. |
884 |
|
* |
885 |
|
* @param phase current phase |
886 |
|
* @param node if non-null, the wait node to track interrupt and timeout; |
888 |
|
* @return current phase |
889 |
|
*/ |
890 |
|
private int internalAwaitAdvance(int phase, QNode node) { |
852 |
– |
Phaser current = this; // to eventually wait at root if tiered |
891 |
|
boolean queued = false; // true when node is enqueued |
892 |
|
int lastUnarrived = -1; // to increase spins upon change |
893 |
|
int spins = SPINS_PER_ARRIVAL; |
894 |
< |
for (;;) { |
895 |
< |
int p, unarrived; |
896 |
< |
Phaser par; |
897 |
< |
long s = current.state; |
898 |
< |
if ((p = (int)(s >>> PHASE_SHIFT)) != phase) { |
899 |
< |
if (node != null) |
900 |
< |
node.onRelease(); |
901 |
< |
releaseWaiters(phase); |
902 |
< |
return p; |
894 |
> |
long s; |
895 |
> |
int p; |
896 |
> |
while ((p = (int)((s = state) >>> PHASE_SHIFT)) == phase) { |
897 |
> |
int unarrived = (int)s & UNARRIVED_MASK; |
898 |
> |
if (node != null && node.isReleasable()) { |
899 |
> |
p = (int)(state >>> PHASE_SHIFT); |
900 |
> |
break; // done or aborted |
901 |
> |
} |
902 |
> |
else if (node == null && Thread.interrupted()) { |
903 |
> |
node = new QNode(this, phase, false, false, 0L); |
904 |
> |
node.wasInterrupted = true; |
905 |
|
} |
906 |
< |
else if ((unarrived = (int)(s & UNARRIVED_MASK)) != lastUnarrived) { |
906 |
> |
else if (unarrived != lastUnarrived) { |
907 |
> |
if (lastUnarrived == -1) // ensure old queue clean |
908 |
> |
releaseWaiters(phase-1); |
909 |
|
if ((lastUnarrived = unarrived) < NCPU) |
910 |
|
spins += SPINS_PER_ARRIVAL; |
911 |
|
} |
870 |
– |
else if (unarrived == 0 && (par = current.parent) != null) { |
871 |
– |
current = par; // if all arrived, use parent |
872 |
– |
par = par.parent; |
873 |
– |
lastUnarrived = -1; |
874 |
– |
} |
912 |
|
else if (spins > 0) |
913 |
|
--spins; |
914 |
< |
else if (node == null) // must be noninterruptible |
914 |
> |
else if (node == null) // null if noninterruptible mode |
915 |
|
node = new QNode(this, phase, false, false, 0L); |
916 |
< |
else if (node.isReleasable()) { |
917 |
< |
if ((int)(reconcileState() >>> PHASE_SHIFT) == phase) |
918 |
< |
return phase; // aborted |
916 |
> |
else if (!queued) { // push onto queue |
917 |
> |
AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ; |
918 |
> |
QNode q = head.get(); |
919 |
> |
if (q == null || q.phase == phase) { |
920 |
> |
node.next = q; |
921 |
> |
if ((p = (int)(state >>> PHASE_SHIFT)) != phase) |
922 |
> |
break; // recheck to avoid stale enqueue |
923 |
> |
queued = head.compareAndSet(q, node); |
924 |
> |
} |
925 |
|
} |
883 |
– |
else if (!queued) |
884 |
– |
queued = tryEnqueue(phase, node); |
926 |
|
else { |
927 |
|
try { |
928 |
|
ForkJoinPool.managedBlock(node); |
931 |
|
} |
932 |
|
} |
933 |
|
} |
934 |
+ |
|
935 |
+ |
if (node != null) { |
936 |
+ |
if (node.thread != null) |
937 |
+ |
node.thread = null; // disable unpark() in node.signal |
938 |
+ |
if (node.wasInterrupted && !node.interruptible) |
939 |
+ |
Thread.currentThread().interrupt(); |
940 |
+ |
} |
941 |
+ |
if (p != phase) |
942 |
+ |
releaseWaiters(phase); |
943 |
+ |
return p; |
944 |
|
} |
945 |
|
|
946 |
|
/** |
976 |
|
else { |
977 |
|
if (Thread.interrupted()) |
978 |
|
wasInterrupted = true; |
979 |
< |
if (interruptible && wasInterrupted) |
979 |
> |
if (wasInterrupted && interruptible) |
980 |
|
t = null; |
981 |
|
else if (timed) { |
982 |
|
if (nanos > 0) { |
1012 |
|
LockSupport.unpark(t); |
1013 |
|
} |
1014 |
|
} |
964 |
– |
|
965 |
– |
void onRelease() { // actions upon return from internalAwaitAdvance |
966 |
– |
if (!interruptible && wasInterrupted) |
967 |
– |
Thread.currentThread().interrupt(); |
968 |
– |
if (thread != null) |
969 |
– |
thread = null; |
970 |
– |
} |
971 |
– |
|
1015 |
|
} |
1016 |
|
|
1017 |
|
// Unsafe mechanics |