@@ -56,8 +56,6 @@ public final class Fiber implements Runnable, Future<Void>, ComponentRegistry {
5656 private static final int NOT_COMPLETE = 0 ;
5757 private static final int DONE = 1 ;
5858 private static final int CANCELLED = 2 ;
59- private static final ExitCallback PLACEHOLDER = () -> {
60- };
6159 private static final ThreadLocal <Fiber > CURRENT_FIBER = new ThreadLocal <Fiber >();
6260 /** Used to allocate unique number for each fiber. */
6361 private static final AtomicInteger iotaGen = new AtomicInteger ();
@@ -546,15 +544,15 @@ private boolean doRun() {
546544
547545 private void triggerExitCallback () {
548546 synchronized (this ) {
549- if (exitCallback != null && exitCallback != PLACEHOLDER ) {
547+ if (exitCallback != null ) {
550548
551549 if (LOGGER .isFinerEnabled ()) {
552550 LOGGER .finer ("{0} triggering exit callback" , new Object [] {getName ()});
553551 }
554552
555553 exitCallback .onExit ();
556554 }
557- exitCallback = PLACEHOLDER ;
555+ exitCallback = null ;
558556 }
559557 }
560558
@@ -686,7 +684,7 @@ public void setCompletionCallback(CompletionCallback completionCallback) {
686684 }
687685
688686 /**
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
690688 * processing this fiber. Since the fiber will now be cancelled or done, no thread will re-enter
691689 * this fiber. If the return value is true, then there is a current thread processing in this
692690 * 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
715713 count .incrementAndGet ();
716714 }
717715
716+ ExitCallback preexistingExitCallback = this .exitCallback ;
718717 ExitCallback myCallback =
719718 () -> {
720719 if (count .decrementAndGet () == 0 ) {
720+ if (preexistingExitCallback != null ) {
721+ preexistingExitCallback .onExit ();
722+ }
721723 exitCallback .onExit ();
722724 }
723725 };
@@ -732,9 +734,6 @@ public boolean cancelAndExitCallback(boolean mayInterrupt, ExitCallback exitCall
732734
733735 boolean isWillCall = count .get () > 1 ; // more calls outstanding then our initial buffer count
734736 if (isWillCall ) {
735- if (this .exitCallback != null || this .exitCallback == PLACEHOLDER ) {
736- throw new IllegalStateException ();
737- }
738737 this .exitCallback = myCallback ;
739738 myCallback .onExit (); // remove the buffer count
740739 }
0 commit comments