@@ -56,8 +56,6 @@ public final class Fiber implements Runnable, Future<Void>, ComponentRegistry {
56
56
private static final int NOT_COMPLETE = 0 ;
57
57
private static final int DONE = 1 ;
58
58
private static final int CANCELLED = 2 ;
59
- private static final ExitCallback PLACEHOLDER = () -> {
60
- };
61
59
private static final ThreadLocal <Fiber > CURRENT_FIBER = new ThreadLocal <Fiber >();
62
60
/** Used to allocate unique number for each fiber. */
63
61
private static final AtomicInteger iotaGen = new AtomicInteger ();
@@ -546,15 +544,15 @@ private boolean doRun() {
546
544
547
545
private void triggerExitCallback () {
548
546
synchronized (this ) {
549
- if (exitCallback != null && exitCallback != PLACEHOLDER ) {
547
+ if (exitCallback != null ) {
550
548
551
549
if (LOGGER .isFinerEnabled ()) {
552
550
LOGGER .finer ("{0} triggering exit callback" , new Object [] {getName ()});
553
551
}
554
552
555
553
exitCallback .onExit ();
556
554
}
557
- exitCallback = PLACEHOLDER ;
555
+ exitCallback = null ;
558
556
}
559
557
}
560
558
@@ -686,7 +684,7 @@ public void setCompletionCallback(CompletionCallback completionCallback) {
686
684
}
687
685
688
686
/**
689
- * Cancels the current thread and accepts a callback for when the current thread, if any, exits
687
+ * Cancels this fiber and accepts a callback for when the current thread, if any, exits
690
688
* processing this fiber. Since the fiber will now be cancelled or done, no thread will re-enter
691
689
* this fiber. If the return value is true, then there is a current thread processing in this
692
690
* fiber and the caller can expect a callback; however, if the return value is false, then there
@@ -715,9 +713,13 @@ public boolean cancelAndExitCallback(boolean mayInterrupt, ExitCallback exitCall
715
713
count .incrementAndGet ();
716
714
}
717
715
716
+ ExitCallback preexistingExitCallback = this .exitCallback ;
718
717
ExitCallback myCallback =
719
718
() -> {
720
719
if (count .decrementAndGet () == 0 ) {
720
+ if (preexistingExitCallback != null ) {
721
+ preexistingExitCallback .onExit ();
722
+ }
721
723
exitCallback .onExit ();
722
724
}
723
725
};
@@ -732,9 +734,6 @@ public boolean cancelAndExitCallback(boolean mayInterrupt, ExitCallback exitCall
732
734
733
735
boolean isWillCall = count .get () > 1 ; // more calls outstanding then our initial buffer count
734
736
if (isWillCall ) {
735
- if (this .exitCallback != null || this .exitCallback == PLACEHOLDER ) {
736
- throw new IllegalStateException ();
737
- }
738
737
this .exitCallback = myCallback ;
739
738
myCallback .onExit (); // remove the buffer count
740
739
}
0 commit comments