Skip to content

Commit

Permalink
Remove internal 'checkCompletion' function and replace it with the id…
Browse files Browse the repository at this point in the history
…entical 'ensureActive' extension to reduce code duplication (#2467)
  • Loading branch information
qwwdfsad authored Apr 7, 2021
1 parent 86a0658 commit d23dd3d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 8 deletions.
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/common/src/Builders.common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public suspend fun <T> withContext(
val oldContext = uCont.context
val newContext = oldContext + context
// always check for cancellation of new context
newContext.checkCompletion()
newContext.ensureActive()
// FAST PATH #1 -- new context is the same as the old one
if (newContext === oldContext) {
val coroutine = ScopeCoroutine(newContext, uCont)
Expand Down
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/common/src/JobSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren

public final override suspend fun join() {
if (!joinInternal()) { // fast-path no wait
coroutineContext.checkCompletion()
coroutineContext.ensureActive()
return // do not suspend
}
return joinSuspend() // slow-path wait
Expand Down
7 changes: 1 addition & 6 deletions kotlinx-coroutines-core/common/src/Yield.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import kotlin.coroutines.intrinsics.*
*/
public suspend fun yield(): Unit = suspendCoroutineUninterceptedOrReturn sc@ { uCont ->
val context = uCont.context
context.checkCompletion()
context.ensureActive()
val cont = uCont.intercepted() as? DispatchedContinuation<Unit> ?: return@sc Unit
if (cont.dispatcher.isDispatchNeeded(context)) {
// this is a regular dispatcher -- do simple dispatchYield
Expand All @@ -50,8 +50,3 @@ public suspend fun yield(): Unit = suspendCoroutineUninterceptedOrReturn sc@ { u
}
COROUTINE_SUSPENDED
}

internal fun CoroutineContext.checkCompletion() {
val job = get(Job)
if (job != null && !job.isActive) throw job.getCancellationException()
}

0 comments on commit d23dd3d

Please sign in to comment.