7 |
|
package jsr166y; |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
– |
|
10 |
|
import java.util.concurrent.atomic.AtomicReference; |
11 |
|
import java.util.concurrent.locks.LockSupport; |
12 |
|
|
108 |
|
* <p><b>Sample usages:</b> |
109 |
|
* |
110 |
|
* <p>A {@code Phaser} may be used instead of a {@code CountDownLatch} |
111 |
< |
* to control a one-shot action serving a variable number of |
112 |
< |
* parties. The typical idiom is for the method setting this up to |
113 |
< |
* first register, then start the actions, then deregister, as in: |
111 |
> |
* to control a one-shot action serving a variable number of parties. |
112 |
> |
* The typical idiom is for the method setting this up to first |
113 |
> |
* register, then start the actions, then deregister, as in: |
114 |
|
* |
115 |
|
* <pre> {@code |
116 |
|
* void runTasks(List<Runnable> tasks) { |
183 |
|
* <p>To create a set of tasks using a tree of phasers, |
184 |
|
* you could use code of the following form, assuming a |
185 |
|
* Task class with a constructor accepting a phaser that |
186 |
< |
* it registers for upon construction: |
186 |
> |
* it registers with upon construction: |
187 |
|
* |
188 |
|
* <pre> {@code |
189 |
|
* void build(Task[] actions, int lo, int hi, Phaser ph) { |
206 |
|
* be appropriate for extremely small per-barrier task bodies (thus |
207 |
|
* high rates), or up to hundreds for extremely large ones. |
208 |
|
* |
210 |
– |
* </pre> |
211 |
– |
* |
209 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
210 |
|
* maximum number of parties to 65535. Attempts to register additional |
211 |
|
* parties result in {@code IllegalStateException}. However, you can and |
294 |
|
|
295 |
|
/** |
296 |
|
* Heads of Treiber stacks for waiting threads. To eliminate |
297 |
< |
* contention while releasing some threads while adding others, we |
297 |
> |
* contention when releasing some threads while adding others, we |
298 |
|
* use two of them, alternating across even and odd phases. |
299 |
+ |
* Subphasers share queues with root to speed up releases. |
300 |
|
*/ |
301 |
< |
private final AtomicReference<QNode> evenQ = new AtomicReference<QNode>(); |
302 |
< |
private final AtomicReference<QNode> oddQ = new AtomicReference<QNode>(); |
301 |
> |
private final AtomicReference<QNode> evenQ; |
302 |
> |
private final AtomicReference<QNode> oddQ; |
303 |
|
|
304 |
|
private AtomicReference<QNode> queueFor(int phase) { |
305 |
|
return ((phase & 1) == 0) ? evenQ : oddQ; |
317 |
|
* Recursively resolves state. |
318 |
|
*/ |
319 |
|
private long reconcileState() { |
320 |
< |
Phaser p = parent; |
320 |
> |
Phaser par = parent; |
321 |
|
long s = state; |
322 |
< |
if (p != null) { |
323 |
< |
while (unarrivedOf(s) == 0 && phaseOf(s) != phaseOf(root.state)) { |
324 |
< |
long parentState = p.getReconciledState(); |
325 |
< |
int parentPhase = phaseOf(parentState); |
326 |
< |
int phase = phaseOf(s = state); |
327 |
< |
if (phase != parentPhase) { |
322 |
> |
if (par != null) { |
323 |
> |
int phase, rootPhase; |
324 |
> |
while ((phase = phaseOf(s)) >= 0 && |
325 |
> |
(rootPhase = phaseOf(root.state)) != phase && |
326 |
> |
(rootPhase < 0 || unarrivedOf(s) == 0)) { |
327 |
> |
int parentPhase = phaseOf(par.getReconciledState()); |
328 |
> |
if (parentPhase != phase) { |
329 |
|
long next = trippedStateFor(parentPhase, partiesOf(s)); |
330 |
< |
if (casState(s, next)) { |
331 |
< |
releaseWaiters(phase); |
333 |
< |
s = next; |
334 |
< |
} |
330 |
> |
if (state == s) |
331 |
> |
UNSAFE.compareAndSwapLong(this, stateOffset, s, next); |
332 |
|
} |
333 |
+ |
s = state; |
334 |
|
} |
335 |
|
} |
336 |
|
return s; |
342 |
|
* phaser will need to first register for it. |
343 |
|
*/ |
344 |
|
public Phaser() { |
345 |
< |
this(null); |
345 |
> |
this(null, 0); |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
< |
* Creates a new phaser with the given numbers of registered |
349 |
> |
* Creates a new phaser with the given number of registered |
350 |
|
* unarrived parties, initial phase number 0, and no parent. |
351 |
|
* |
352 |
|
* @param parties the number of parties required to trip barrier |
366 |
|
* @param parent the parent phaser |
367 |
|
*/ |
368 |
|
public Phaser(Phaser parent) { |
369 |
< |
int phase = 0; |
372 |
< |
this.parent = parent; |
373 |
< |
if (parent != null) { |
374 |
< |
this.root = parent.root; |
375 |
< |
phase = parent.register(); |
376 |
< |
} |
377 |
< |
else |
378 |
< |
this.root = this; |
379 |
< |
this.state = trippedStateFor(phase, 0); |
369 |
> |
this(parent, 0); |
370 |
|
} |
371 |
|
|
372 |
|
/** |
373 |
< |
* Creates a new phaser with the given parent and numbers of |
373 |
> |
* Creates a new phaser with the given parent and number of |
374 |
|
* registered unarrived parties. If parent is non-null, this phaser |
375 |
|
* is registered with the parent and its initial phase number is |
376 |
|
* the same as that of parent phaser. |
383 |
|
public Phaser(Phaser parent, int parties) { |
384 |
|
if (parties < 0 || parties > ushortMask) |
385 |
|
throw new IllegalArgumentException("Illegal number of parties"); |
386 |
< |
int phase = 0; |
386 |
> |
int phase; |
387 |
|
this.parent = parent; |
388 |
|
if (parent != null) { |
389 |
< |
this.root = parent.root; |
389 |
> |
Phaser r = parent.root; |
390 |
> |
this.root = r; |
391 |
> |
this.evenQ = r.evenQ; |
392 |
> |
this.oddQ = r.oddQ; |
393 |
|
phase = parent.register(); |
394 |
|
} |
395 |
< |
else |
395 |
> |
else { |
396 |
|
this.root = this; |
397 |
+ |
this.evenQ = new AtomicReference<QNode>(); |
398 |
+ |
this.oddQ = new AtomicReference<QNode>(); |
399 |
+ |
phase = 0; |
400 |
+ |
} |
401 |
|
this.state = trippedStateFor(phase, parties); |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
|
* Adds a new unarrived party to this phaser. |
406 |
+ |
* If an ongoing invocation of {@link #onAdvance} is in progress, |
407 |
+ |
* this method may wait until its completion before registering. |
408 |
|
* |
409 |
|
* @return the arrival phase number to which this registration applied |
410 |
|
* @throws IllegalStateException if attempting to register more |
416 |
|
|
417 |
|
/** |
418 |
|
* Adds the given number of new unarrived parties to this phaser. |
419 |
+ |
* If an ongoing invocation of {@link #onAdvance} is in progress, |
420 |
+ |
* this method may wait until its completion before registering. |
421 |
|
* |
422 |
< |
* @param parties the number of parties required to trip barrier |
422 |
> |
* @param parties the number of additional parties required to trip barrier |
423 |
|
* @return the arrival phase number to which this registration applied |
424 |
|
* @throws IllegalStateException if attempting to register more |
425 |
|
* than the maximum supported number of parties |
426 |
+ |
* @throws IllegalArgumentException if {@code parties < 0} |
427 |
|
*/ |
428 |
|
public int bulkRegister(int parties) { |
429 |
|
if (parties < 0) |
437 |
|
* Shared code for register, bulkRegister |
438 |
|
*/ |
439 |
|
private int doRegister(int registrations) { |
440 |
+ |
Phaser par = parent; |
441 |
+ |
long s; |
442 |
|
int phase; |
443 |
< |
for (;;) { |
444 |
< |
long s = getReconciledState(); |
445 |
< |
phase = phaseOf(s); |
446 |
< |
int unarrived = unarrivedOf(s) + registrations; |
447 |
< |
int parties = partiesOf(s) + registrations; |
448 |
< |
if (phase < 0) |
449 |
< |
break; |
450 |
< |
if (parties > ushortMask || unarrived > ushortMask) |
443 |
> |
while ((phase = phaseOf(s = par==null? state:reconcileState())) >= 0) { |
444 |
> |
int p = partiesOf(s); |
445 |
> |
int u = unarrivedOf(s); |
446 |
> |
int unarrived = u + registrations; |
447 |
> |
int parties = p + registrations; |
448 |
> |
if (u == 0 && p != 0) // if tripped, wait for advance |
449 |
> |
untimedWait(phase); |
450 |
> |
else if (parties > ushortMask) |
451 |
|
throw new IllegalStateException(badBounds(parties, unarrived)); |
452 |
< |
if (phase == phaseOf(root.state) && |
453 |
< |
casState(s, stateFor(phase, parties, unarrived))) |
454 |
< |
break; |
452 |
> |
else if (par == null || phaseOf(root.state) == phase) { |
453 |
> |
long next = stateFor(phase, parties, unarrived); |
454 |
> |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) |
455 |
> |
break; |
456 |
> |
} |
457 |
|
} |
458 |
|
return phase; |
459 |
|
} |
469 |
|
* of unarrived parties would become negative |
470 |
|
*/ |
471 |
|
public int arrive() { |
472 |
+ |
Phaser par = parent; |
473 |
+ |
long s; |
474 |
|
int phase; |
475 |
< |
for (;;) { |
468 |
< |
long s = state; |
469 |
< |
phase = phaseOf(s); |
470 |
< |
if (phase < 0) |
471 |
< |
break; |
475 |
> |
while ((phase = phaseOf(s = par==null? state:reconcileState())) >= 0) { |
476 |
|
int parties = partiesOf(s); |
477 |
|
int unarrived = unarrivedOf(s) - 1; |
478 |
< |
if (unarrived > 0) { // Not the last arrival |
479 |
< |
if (casState(s, s - 1)) // s-1 adds one arrival |
480 |
< |
break; |
478 |
> |
if (unarrived > 0) { // Not the last arrival |
479 |
> |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s - 1)) |
480 |
> |
break; // s-1 adds one arrival |
481 |
|
} |
482 |
< |
else if (unarrived == 0) { // the last arrival |
483 |
< |
Phaser par = parent; |
484 |
< |
if (par == null) { // directly trip |
485 |
< |
if (casState |
486 |
< |
(s, |
487 |
< |
trippedStateFor(onAdvance(phase, parties) ? -1 : |
488 |
< |
((phase + 1) & phaseMask), parties))) { |
489 |
< |
releaseWaiters(phase); |
490 |
< |
break; |
487 |
< |
} |
488 |
< |
} |
489 |
< |
else { // cascade to parent |
490 |
< |
if (casState(s, s - 1)) { // zeroes unarrived |
491 |
< |
par.arrive(); |
492 |
< |
reconcileState(); |
493 |
< |
break; |
494 |
< |
} |
482 |
> |
else if (unarrived < 0) |
483 |
> |
throw new IllegalStateException(badBounds(parties, unarrived)); |
484 |
> |
else if (par == null) { // directly trip |
485 |
> |
long next = trippedStateFor(onAdvance(phase, parties) ? -1 : |
486 |
> |
((phase + 1) & phaseMask), |
487 |
> |
parties); |
488 |
> |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) { |
489 |
> |
releaseWaiters(phase); |
490 |
> |
break; |
491 |
|
} |
492 |
|
} |
493 |
< |
else if (phase != phaseOf(root.state)) // or if unreconciled |
493 |
> |
else if (phaseOf(root.state) == phase && |
494 |
> |
UNSAFE.compareAndSwapLong(this, stateOffset, s, s - 1)) { |
495 |
> |
par.arrive(); // cascade to parent |
496 |
|
reconcileState(); |
497 |
< |
else |
498 |
< |
throw new IllegalStateException(badBounds(parties, unarrived)); |
497 |
> |
break; |
498 |
> |
} |
499 |
|
} |
500 |
|
return phase; |
501 |
|
} |
514 |
|
* of registered or unarrived parties would become negative |
515 |
|
*/ |
516 |
|
public int arriveAndDeregister() { |
517 |
< |
// similar code to arrive, but too different to merge |
517 |
> |
// similar to arrive, but too different to merge |
518 |
|
Phaser par = parent; |
519 |
+ |
long s; |
520 |
|
int phase; |
521 |
< |
for (;;) { |
523 |
< |
long s = state; |
524 |
< |
phase = phaseOf(s); |
525 |
< |
if (phase < 0) |
526 |
< |
break; |
521 |
> |
while ((phase = phaseOf(s = par==null? state:reconcileState())) >= 0) { |
522 |
|
int parties = partiesOf(s) - 1; |
523 |
|
int unarrived = unarrivedOf(s) - 1; |
524 |
< |
if (parties >= 0) { |
525 |
< |
if (unarrived > 0 || (unarrived == 0 && par != null)) { |
526 |
< |
if (casState |
527 |
< |
(s, |
528 |
< |
stateFor(phase, parties, unarrived))) { |
529 |
< |
if (unarrived == 0) { |
530 |
< |
par.arriveAndDeregister(); |
531 |
< |
reconcileState(); |
532 |
< |
} |
533 |
< |
break; |
534 |
< |
} |
535 |
< |
continue; |
536 |
< |
} |
537 |
< |
if (unarrived == 0) { |
543 |
< |
if (casState |
544 |
< |
(s, |
545 |
< |
trippedStateFor(onAdvance(phase, parties) ? -1 : |
546 |
< |
((phase + 1) & phaseMask), parties))) { |
547 |
< |
releaseWaiters(phase); |
548 |
< |
break; |
549 |
< |
} |
550 |
< |
continue; |
524 |
> |
if (unarrived > 0) { |
525 |
> |
long next = stateFor(phase, parties, unarrived); |
526 |
> |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) |
527 |
> |
break; |
528 |
> |
} |
529 |
> |
else if (unarrived < 0) |
530 |
> |
throw new IllegalStateException(badBounds(parties, unarrived)); |
531 |
> |
else if (par == null) { |
532 |
> |
long next = trippedStateFor(onAdvance(phase, parties)? -1: |
533 |
> |
(phase + 1) & phaseMask, |
534 |
> |
parties); |
535 |
> |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) { |
536 |
> |
releaseWaiters(phase); |
537 |
> |
break; |
538 |
|
} |
539 |
< |
if (par != null && phase != phaseOf(root.state)) { |
539 |
> |
} |
540 |
> |
else if (phaseOf(root.state) == phase) { |
541 |
> |
long next = stateFor(phase, parties, 0); |
542 |
> |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) { |
543 |
> |
if (parties == 0) |
544 |
> |
par.arriveAndDeregister(); |
545 |
> |
else |
546 |
> |
par.arrive(); |
547 |
|
reconcileState(); |
548 |
< |
continue; |
548 |
> |
break; |
549 |
|
} |
550 |
|
} |
557 |
– |
throw new IllegalStateException(badBounds(parties, unarrived)); |
551 |
|
} |
552 |
|
return phase; |
553 |
|
} |
556 |
|
* Arrives at the barrier and awaits others. Equivalent in effect |
557 |
|
* to {@code awaitAdvance(arrive())}. If you need to await with |
558 |
|
* interruption or timeout, you can arrange this with an analogous |
559 |
< |
* construction using one of the other forms of the awaitAdvance |
560 |
< |
* method. If instead you need to deregister upon arrival use |
561 |
< |
* {@code arriveAndDeregister}. It is an unenforced usage error |
562 |
< |
* for an unregistered party to invoke this method. |
559 |
> |
* construction using one of the other forms of the {@code |
560 |
> |
* awaitAdvance} method. If instead you need to deregister upon |
561 |
> |
* arrival, use {@link #arriveAndDeregister}. It is an unenforced |
562 |
> |
* usage error for an unregistered party to invoke this method. |
563 |
|
* |
564 |
|
* @return the arrival phase number, or a negative number if terminated |
565 |
|
* @throws IllegalStateException if not terminated and the number |
573 |
|
* Awaits the phase of the barrier to advance from the given phase |
574 |
|
* value, returning immediately if the current phase of the |
575 |
|
* barrier is not equal to the given phase value or this barrier |
576 |
< |
* is terminated. It is an unenforced usage error for an |
584 |
< |
* unregistered party to invoke this method. |
576 |
> |
* is terminated. |
577 |
|
* |
578 |
|
* @param phase an arrival phase number, or negative value if |
579 |
|
* terminated; this argument is normally the value returned by a |
584 |
|
public int awaitAdvance(int phase) { |
585 |
|
if (phase < 0) |
586 |
|
return phase; |
587 |
< |
long s = getReconciledState(); |
596 |
< |
int p = phaseOf(s); |
587 |
> |
int p = getPhase(); |
588 |
|
if (p != phase) |
589 |
|
return p; |
599 |
– |
if (unarrivedOf(s) == 0 && parent != null) |
600 |
– |
parent.awaitAdvance(phase); |
601 |
– |
// Fall here even if parent waited, to reconcile and help release |
590 |
|
return untimedWait(phase); |
591 |
|
} |
592 |
|
|
595 |
|
* value, throwing {@code InterruptedException} if interrupted |
596 |
|
* while waiting, or returning immediately if the current phase of |
597 |
|
* the barrier is not equal to the given phase value or this |
598 |
< |
* barrier is terminated. It is an unenforced usage error for an |
611 |
< |
* unregistered party to invoke this method. |
598 |
> |
* barrier is terminated. |
599 |
|
* |
600 |
|
* @param phase an arrival phase number, or negative value if |
601 |
|
* terminated; this argument is normally the value returned by a |
608 |
|
throws InterruptedException { |
609 |
|
if (phase < 0) |
610 |
|
return phase; |
611 |
< |
long s = getReconciledState(); |
625 |
< |
int p = phaseOf(s); |
611 |
> |
int p = getPhase(); |
612 |
|
if (p != phase) |
613 |
|
return p; |
628 |
– |
if (unarrivedOf(s) == 0 && parent != null) |
629 |
– |
parent.awaitAdvanceInterruptibly(phase); |
614 |
|
return interruptibleWait(phase); |
615 |
|
} |
616 |
|
|
620 |
|
* InterruptedException} if interrupted while waiting, or |
621 |
|
* returning immediately if the current phase of the barrier is |
622 |
|
* not equal to the given phase value or this barrier is |
623 |
< |
* terminated. It is an unenforced usage error for an |
640 |
< |
* unregistered party to invoke this method. |
623 |
> |
* terminated. |
624 |
|
* |
625 |
|
* @param phase an arrival phase number, or negative value if |
626 |
|
* terminated; this argument is normally the value returned by a |
637 |
|
public int awaitAdvanceInterruptibly(int phase, |
638 |
|
long timeout, TimeUnit unit) |
639 |
|
throws InterruptedException, TimeoutException { |
640 |
+ |
long nanos = unit.toNanos(timeout); |
641 |
|
if (phase < 0) |
642 |
|
return phase; |
643 |
< |
long s = getReconciledState(); |
660 |
< |
int p = phaseOf(s); |
643 |
> |
int p = getPhase(); |
644 |
|
if (p != phase) |
645 |
|
return p; |
646 |
< |
if (unarrivedOf(s) == 0 && parent != null) |
664 |
< |
parent.awaitAdvanceInterruptibly(phase, timeout, unit); |
665 |
< |
return timedWait(phase, unit.toNanos(timeout)); |
646 |
> |
return timedWait(phase, nanos); |
647 |
|
} |
648 |
|
|
649 |
|
/** |
654 |
|
* unexpected exceptions. |
655 |
|
*/ |
656 |
|
public void forceTermination() { |
657 |
< |
for (;;) { |
658 |
< |
long s = getReconciledState(); |
659 |
< |
int phase = phaseOf(s); |
660 |
< |
int parties = partiesOf(s); |
661 |
< |
int unarrived = unarrivedOf(s); |
662 |
< |
if (phase < 0 || |
663 |
< |
casState(s, stateFor(-1, parties, unarrived))) { |
664 |
< |
releaseWaiters(0); |
665 |
< |
releaseWaiters(1); |
685 |
< |
if (parent != null) |
686 |
< |
parent.forceTermination(); |
687 |
< |
return; |
688 |
< |
} |
689 |
< |
} |
657 |
> |
Phaser r = root; // force at root then reconcile |
658 |
> |
long s; |
659 |
> |
while (phaseOf(s = r.state) >= 0) |
660 |
> |
UNSAFE.compareAndSwapLong(r, stateOffset, s, |
661 |
> |
stateFor(-1, partiesOf(s), |
662 |
> |
unarrivedOf(s))); |
663 |
> |
reconcileState(); |
664 |
> |
releaseWaiters(0); // ensure wakeups on both queues |
665 |
> |
releaseWaiters(1); |
666 |
|
} |
667 |
|
|
668 |
|
/** |
682 |
|
* @return the number of parties |
683 |
|
*/ |
684 |
|
public int getRegisteredParties() { |
685 |
< |
return partiesOf(state); |
685 |
> |
return partiesOf(getReconciledState()); |
686 |
|
} |
687 |
|
|
688 |
|
/** |
692 |
|
* @return the number of arrived parties |
693 |
|
*/ |
694 |
|
public int getArrivedParties() { |
695 |
< |
return arrivedOf(state); |
695 |
> |
return arrivedOf(getReconciledState()); |
696 |
|
} |
697 |
|
|
698 |
|
/** |
702 |
|
* @return the number of unarrived parties |
703 |
|
*/ |
704 |
|
public int getUnarrivedParties() { |
705 |
< |
return unarrivedOf(state); |
705 |
> |
return unarrivedOf(getReconciledState()); |
706 |
|
} |
707 |
|
|
708 |
|
/** |
746 |
|
* which case no advance occurs. |
747 |
|
* |
748 |
|
* <p>The arguments to this method provide the state of the phaser |
749 |
< |
* prevailing for the current transition. (When called from within |
750 |
< |
* an implementation of {@code onAdvance} the values returned by |
751 |
< |
* methods such as {@code getPhase} may or may not reliably |
752 |
< |
* indicate the state to which this transition applies.) |
749 |
> |
* prevailing for the current transition. The results and effects |
750 |
> |
* of invoking phase-related methods (including {@code getPhase} |
751 |
> |
* as well as arrival, registration, and waiting methods) from |
752 |
> |
* within {@code onAdvance} are unspecified and should not be |
753 |
> |
* relied on. Similarly, while it is possible to override this |
754 |
> |
* method to produce side-effects visible to participating tasks, |
755 |
> |
* it is in general safe to do so only in designs in which all |
756 |
> |
* parties register before any arrive, and all {@link |
757 |
> |
* #awaitAdvance} at each phase. |
758 |
|
* |
759 |
|
* <p>The default version returns {@code true} when the number of |
760 |
|
* registered parties is zero. Normally, overrides that arrange |
761 |
|
* termination for other reasons should also preserve this |
762 |
|
* property. |
763 |
|
* |
783 |
– |
* <p>You may override this method to perform an action with side |
784 |
– |
* effects visible to participating tasks, but it is only sensible |
785 |
– |
* to do so in designs where all parties register before any |
786 |
– |
* arrive, and all {@link #awaitAdvance} at each phase. |
787 |
– |
* Otherwise, you cannot ensure lack of interference from other |
788 |
– |
* parties during the invocation of this method. Additionally, |
789 |
– |
* method {@code onAdvance} may be invoked more than once per |
790 |
– |
* transition if registrations are intermixed with arrivals. |
791 |
– |
* |
764 |
|
* @param phase the phase number on entering the barrier |
765 |
|
* @param registeredParties the current number of registered parties |
766 |
|
* @return {@code true} if this barrier should terminate |
801 |
|
volatile boolean wasInterrupted = false; |
802 |
|
volatile Thread thread; // nulled to cancel wait |
803 |
|
QNode next; |
804 |
+ |
|
805 |
|
QNode(Phaser phaser, int phase, boolean interruptible, |
806 |
|
boolean timed, long startTime, long nanos) { |
807 |
|
this.phaser = phaser; |
812 |
|
this.nanos = nanos; |
813 |
|
thread = Thread.currentThread(); |
814 |
|
} |
815 |
+ |
|
816 |
|
public boolean isReleasable() { |
817 |
|
return (thread == null || |
818 |
|
phaser.getPhase() != phase || |
819 |
|
(interruptible && wasInterrupted) || |
820 |
|
(timed && (nanos - (System.nanoTime() - startTime)) <= 0)); |
821 |
|
} |
822 |
+ |
|
823 |
|
public boolean block() { |
824 |
|
if (Thread.interrupted()) { |
825 |
|
wasInterrupted = true; |
836 |
|
} |
837 |
|
return isReleasable(); |
838 |
|
} |
839 |
+ |
|
840 |
|
void signal() { |
841 |
|
Thread t = thread; |
842 |
|
if (t != null) { |
844 |
|
LockSupport.unpark(t); |
845 |
|
} |
846 |
|
} |
847 |
+ |
|
848 |
|
boolean doWait() { |
849 |
|
if (thread != null) { |
850 |
|
try { |
851 |
|
ForkJoinPool.managedBlock(this); |
852 |
|
} catch (InterruptedException ie) { |
853 |
+ |
wasInterrupted = true; // can't currently happen |
854 |
|
} |
855 |
|
} |
856 |
|
return wasInterrupted; |
857 |
|
} |
880 |
– |
|
858 |
|
} |
859 |
|
|
860 |
|
/** |
880 |
|
} |
881 |
|
|
882 |
|
/** |
883 |
+ |
* The number of times to spin before blocking waiting for advance. |
884 |
+ |
*/ |
885 |
+ |
static final int MAX_SPINS = |
886 |
+ |
Runtime.getRuntime().availableProcessors() == 1 ? 0 : 1 << 8; |
887 |
+ |
|
888 |
+ |
/** |
889 |
|
* Enqueues node and waits unless aborted or signalled. |
890 |
|
* |
891 |
|
* @return current phase |
894 |
|
QNode node = null; |
895 |
|
boolean queued = false; |
896 |
|
boolean interrupted = false; |
897 |
+ |
int spins = MAX_SPINS; |
898 |
|
int p; |
899 |
|
while ((p = getPhase()) == phase) { |
900 |
|
if (Thread.interrupted()) |
901 |
|
interrupted = true; |
902 |
+ |
else if (spins > 0) { |
903 |
+ |
if (--spins == 0) |
904 |
+ |
Thread.yield(); |
905 |
+ |
} |
906 |
|
else if (node == null) |
907 |
|
node = new QNode(this, phase, false, false, 0, 0); |
908 |
|
else if (!queued) |
909 |
|
queued = tryEnqueue(node); |
910 |
< |
else |
911 |
< |
interrupted = node.doWait(); |
910 |
> |
else if (node.doWait()) |
911 |
> |
interrupted = true; |
912 |
|
} |
913 |
|
if (node != null) |
914 |
|
node.thread = null; |
926 |
|
QNode node = null; |
927 |
|
boolean queued = false; |
928 |
|
boolean interrupted = false; |
929 |
+ |
int spins = MAX_SPINS; |
930 |
|
int p; |
931 |
|
while ((p = getPhase()) == phase && !interrupted) { |
932 |
|
if (Thread.interrupted()) |
933 |
|
interrupted = true; |
934 |
+ |
else if (spins > 0) { |
935 |
+ |
if (--spins == 0) |
936 |
+ |
Thread.yield(); |
937 |
+ |
} |
938 |
|
else if (node == null) |
939 |
|
node = new QNode(this, phase, true, false, 0, 0); |
940 |
|
else if (!queued) |
941 |
|
queued = tryEnqueue(node); |
942 |
< |
else |
943 |
< |
interrupted = node.doWait(); |
942 |
> |
else if (node.doWait()) |
943 |
> |
interrupted = true; |
944 |
|
} |
945 |
|
if (node != null) |
946 |
|
node.thread = null; |
961 |
|
QNode node = null; |
962 |
|
boolean queued = false; |
963 |
|
boolean interrupted = false; |
964 |
+ |
int spins = MAX_SPINS; |
965 |
|
int p; |
966 |
|
while ((p = getPhase()) == phase && !interrupted) { |
967 |
|
if (Thread.interrupted()) |
968 |
|
interrupted = true; |
969 |
|
else if (nanos - (System.nanoTime() - startTime) <= 0) |
970 |
|
break; |
971 |
+ |
else if (spins > 0) { |
972 |
+ |
if (--spins == 0) |
973 |
+ |
Thread.yield(); |
974 |
+ |
} |
975 |
|
else if (node == null) |
976 |
|
node = new QNode(this, phase, true, true, startTime, nanos); |
977 |
|
else if (!queued) |
978 |
|
queued = tryEnqueue(node); |
979 |
< |
else |
980 |
< |
interrupted = node.doWait(); |
979 |
> |
else if (node.doWait()) |
980 |
> |
interrupted = true; |
981 |
|
} |
982 |
|
if (node != null) |
983 |
|
node.thread = null; |
996 |
|
private static final long stateOffset = |
997 |
|
objectFieldOffset("state", Phaser.class); |
998 |
|
|
1001 |
– |
private final boolean casState(long cmp, long val) { |
1002 |
– |
return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val); |
1003 |
– |
} |
1004 |
– |
|
999 |
|
private static long objectFieldOffset(String field, Class<?> klazz) { |
1000 |
|
try { |
1001 |
|
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |