@@ -194,16 +194,16 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
194
194
// Finalizes Finishing -> Completed (terminal state) transition.
195
195
// ## IMPORTANT INVARIANT: Only one thread can be concurrently invoking this method.
196
196
// Returns final state that was created and updated to
197
- private fun tryFinalizeFinishingState (state : Finishing , proposedUpdate : Any? ): Any? {
197
+ private fun finalizeFinishingState (state : Finishing , proposedUpdate : Any? ): Any? {
198
198
/*
199
199
* Note: proposed state can be Incomplete, e.g.
200
200
* async {
201
201
* something.invokeOnCompletion {} // <- returns handle which implements Incomplete under the hood
202
202
* }
203
203
*/
204
- require( this .state == = state) // consistency check -- it cannot change
205
- require( ! state.isSealed) // consistency check -- cannot be sealed yet
206
- require( state.isCompleting) // consistency check -- must be marked as completing
204
+ assert { this .state == = state } // consistency check -- it cannot change
205
+ assert { ! state.isSealed } // consistency check -- cannot be sealed yet
206
+ assert { state.isCompleting } // consistency check -- must be marked as completing
207
207
val proposedException = (proposedUpdate as ? CompletedExceptionally )?.cause
208
208
// Create the final exception and seal the state so that no more exceptions can be added
209
209
var wasCancelling = false // KLUDGE: we cannot have contract for our own expect fun synchronized
@@ -233,7 +233,8 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
233
233
if (! wasCancelling) onCancelling(finalException)
234
234
onCompletionInternal(finalState)
235
235
// Then CAS to completed state -> it must succeed
236
- require(_state .compareAndSet(state, finalState.boxIncomplete())) { " Unexpected state: ${_state .value} , expected: $state , update: $finalState " }
236
+ val casSuccess = _state .compareAndSet(state, finalState.boxIncomplete())
237
+ assert { casSuccess }
237
238
// And process all post-completion actions
238
239
completeStateFinalization(state, finalState)
239
240
return finalState
@@ -269,7 +270,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
269
270
}
270
271
271
272
// fast-path method to finalize normally completed coroutines without children
272
- // returns true if complete, and afterCompletionInternal (update, mode ) shall be called
273
+ // returns true if complete, and afterCompletion (update) shall be called
273
274
private fun tryFinalizeSimpleState (state : Incomplete , update : Any? ): Boolean {
274
275
assert { state is Empty || state is JobNode <* > } // only simple state without lists where children can concurrently add
275
276
assert { update !is CompletedExceptionally } // only for normal completion
@@ -495,10 +496,10 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
495
496
496
497
private fun makeNode (handler : CompletionHandler , onCancelling : Boolean ): JobNode <* > {
497
498
return if (onCancelling)
498
- (handler as ? JobCancellingNode <* >)?.also { require( it.job == = this ) }
499
+ (handler as ? JobCancellingNode <* >)?.also { assert { it.job == = this } }
499
500
? : InvokeOnCancelling (this , handler)
500
501
else
501
- (handler as ? JobNode <* >)?.also { require( it.job == = this && it !is JobCancellingNode ) }
502
+ (handler as ? JobNode <* >)?.also { assert { it.job == = this && it !is JobCancellingNode } }
502
503
? : InvokeOnCompletion (this , handler)
503
504
}
504
505
@@ -653,7 +654,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
653
654
finalState == = COMPLETING_WAITING_CHILDREN -> true
654
655
finalState == = TOO_LATE_TO_CANCEL -> false
655
656
else -> {
656
- afterCompletionInternal (finalState, MODE_ATOMIC_DEFAULT )
657
+ afterCompletion (finalState)
657
658
true
658
659
}
659
660
}
@@ -663,7 +664,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
663
664
// It contains a loop and never returns COMPLETING_RETRY, can return
664
665
// COMPLETING_ALREADY -- if already completed/completing
665
666
// COMPLETING_WAITING_CHILDREN -- if started waiting for children
666
- // final state -- when completed, for call to afterCompletionInternal
667
+ // final state -- when completed, for call to afterCompletion
667
668
private fun cancelMakeCompleting (cause : Any? ): Any? {
668
669
loopOnState { state ->
669
670
if (state !is Incomplete || state is Finishing && state.isCompleting) {
@@ -703,7 +704,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
703
704
// COMPLETING_ALREADY -- if already completing or successfully made cancelling, added exception
704
705
// COMPLETING_WAITING_CHILDREN -- if started waiting for children, added exception
705
706
// TOO_LATE_TO_CANCEL -- too late to cancel, did not add exception
706
- // final state -- when completed, for call to afterCompletionInternal
707
+ // final state -- when completed, for call to afterCompletion
707
708
private fun makeCancelling (cause : Any? ): Any? {
708
709
var causeExceptionCache: Throwable ? = null // lazily init result of createCauseException(cause)
709
710
loopOnState { state ->
@@ -786,7 +787,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
786
787
finalState == = COMPLETING_WAITING_CHILDREN -> return true
787
788
finalState == = COMPLETING_RETRY -> return @loopOnState
788
789
else -> {
789
- afterCompletionInternal (finalState, MODE_ATOMIC_DEFAULT )
790
+ afterCompletion (finalState)
790
791
return true
791
792
}
792
793
}
@@ -798,7 +799,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
798
799
* It throws [IllegalStateException] on repeated invocation (when this job is already completing).
799
800
* Returns:
800
801
* * [COMPLETING_WAITING_CHILDREN] if started waiting for children.
801
- * * Final state otherwise (caller should do [afterCompletionInternal ])
802
+ * * Final state otherwise (caller should do [afterCompletion ])
802
803
*/
803
804
internal fun makeCompletingOnce (proposedUpdate : Any? ): Any? {
804
805
loopOnState { state ->
@@ -819,7 +820,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
819
820
// COMPLETING_ALREADY -- when already complete or completing
820
821
// COMPLETING_RETRY -- when need to retry due to interference
821
822
// COMPLETING_WAITING_CHILDREN -- when made completing and is waiting for children
822
- // final state -- when completed, for call to afterCompletionInternal
823
+ // final state -- when completed, for call to afterCompletion
823
824
private fun tryMakeCompleting (state : Any? , proposedUpdate : Any? ): Any? {
824
825
if (state !is Incomplete )
825
826
return COMPLETING_ALREADY
@@ -844,7 +845,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
844
845
// COMPLETING_ALREADY -- when already complete or completing
845
846
// COMPLETING_RETRY -- when need to retry due to interference
846
847
// COMPLETING_WAITING_CHILDREN -- when made completing and is waiting for children
847
- // final state -- when completed, for call to afterCompletionInternal
848
+ // final state -- when completed, for call to afterCompletion
848
849
private fun tryMakeCompletingSlowPath (state : Incomplete , proposedUpdate : Any? ): Any? {
849
850
// get state's list or else promote to list to correctly operate on child lists
850
851
val list = getOrPromoteCancellingList(state) ? : return COMPLETING_RETRY
@@ -866,7 +867,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
866
867
if (! _state .compareAndSet(state, finishing)) return COMPLETING_RETRY
867
868
}
868
869
// ## IMPORTANT INVARIANT: Only one thread (that had set isCompleting) can go past this point
869
- require( ! finishing.isSealed) // cannot be sealed
870
+ assert { ! finishing.isSealed } // cannot be sealed
870
871
// add new proposed exception to the finishing state
871
872
val wasCancelling = finishing.isCancelling
872
873
(proposedUpdate as ? CompletedExceptionally )?.let { finishing.addExceptionLocked(it.cause) }
@@ -880,7 +881,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
880
881
if (child != null && tryWaitForChild(finishing, child, proposedUpdate))
881
882
return COMPLETING_WAITING_CHILDREN
882
883
// otherwise -- we have not children left (all were already cancelled?)
883
- return tryFinalizeFinishingState (finishing, proposedUpdate)
884
+ return finalizeFinishingState (finishing, proposedUpdate)
884
885
}
885
886
886
887
private val Any? .exceptionOrNull: Throwable ?
@@ -903,14 +904,14 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
903
904
904
905
// ## IMPORTANT INVARIANT: Only one thread can be concurrently invoking this method.
905
906
private fun continueCompleting (state : Finishing , lastChild : ChildHandleNode , proposedUpdate : Any? ) {
906
- require( this .state == = state) // consistency check -- it cannot change while we are waiting for children
907
+ assert { this .state == = state } // consistency check -- it cannot change while we are waiting for children
907
908
// figure out if we need to wait for next child
908
909
val waitChild = lastChild.nextChild()
909
910
// try wait for next child
910
911
if (waitChild != null && tryWaitForChild(state, waitChild, proposedUpdate)) return // waiting for next child
911
912
// no more children to wait -- try update state
912
- val finalState = tryFinalizeFinishingState (state, proposedUpdate)
913
- afterCompletionInternal (finalState, MODE_ATOMIC_DEFAULT )
913
+ val finalState = finalizeFinishingState (state, proposedUpdate)
914
+ afterCompletion (finalState)
914
915
}
915
916
916
917
private fun LockFreeLinkedListNode.nextChild (): ChildHandleNode ? {
@@ -1014,14 +1015,13 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
1014
1015
protected open fun onCompletionInternal (state : Any? ) {}
1015
1016
1016
1017
/* *
1017
- * Override for the very last action on job's completion to resume the rest of the code in scoped coroutines.
1018
- *
1019
- * @param state the final state.
1020
- * @param mode completion mode.
1018
+ * Override for the very last action on job's completion to resume the rest of the code in
1019
+ * scoped coroutines. It is called when this job is externally completed in an unknown
1020
+ * context and thus should resume with a default mode.
1021
1021
*
1022
1022
* @suppress **This is unstable API and it is subject to change.**
1023
1023
*/
1024
- protected open fun afterCompletionInternal (state : Any? , mode : Int ) {}
1024
+ protected open fun afterCompletion (state : Any? ) {}
1025
1025
1026
1026
// for nicer debugging
1027
1027
public override fun toString (): String =
0 commit comments