Skip to content

Commit 1d02d27

Browse files
committed
Use public API in withContinuationContext
1 parent c02fe59 commit 1d02d27

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

kotlinx-coroutines-core/jvm/src/CoroutineContext.kt

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
6-
75
package kotlinx.coroutines
86

97
import kotlinx.coroutines.internal.*
108
import kotlinx.coroutines.scheduling.*
119
import kotlin.coroutines.*
1210
import kotlin.coroutines.jvm.internal.*
11+
import kotlin.coroutines.jvm.internal.CoroutineStackFrame
1312

1413
internal const val COROUTINES_SCHEDULER_PROPERTY_NAME = "kotlinx.coroutines.scheduler"
1514

@@ -58,7 +57,7 @@ internal actual inline fun <T> withContinuationContext(continuation: Continuatio
5857
val oldValue = updateThreadContext(context, countOrElement)
5958
val undispatchedCompletion = if (oldValue !== NO_THREAD_ELEMENTS) {
6059
// Only if some values were replaced we'll go to the slow path of figuring out where/how to restore them
61-
continuation.undispatchedCompletion()
60+
(continuation as? CoroutineStackFrame)?.undispatchedCompletion()
6261
} else
6362
null // fast path -- don't even try to find undispatchedCompletion as there's nothing to restore in the context
6463
undispatchedCompletion?.saveThreadContext(context, oldValue)
@@ -70,16 +69,14 @@ internal actual inline fun <T> withContinuationContext(continuation: Continuatio
7069
}
7170
}
7271

73-
internal tailrec fun Continuation<*>.undispatchedCompletion(): UndispatchedCoroutine<*>? {
72+
internal tailrec fun CoroutineStackFrame.undispatchedCompletion(): UndispatchedCoroutine<*>? {
7473
// Find direct completion of this continuation
75-
val completion: Continuation<*> = when (this) {
76-
is BaseContinuationImpl -> completion ?: return null // regular suspending function -- direct resume
77-
is DispatchedCoroutine -> return null // dispatches on resume
78-
is ScopeCoroutine -> uCont // other scoped coroutine -- direct resume
79-
else -> return null // something else -- not supported
74+
val completion: CoroutineStackFrame? = when (this) {
75+
is DispatchedCoroutine<*> -> return null // dispatches on resume
76+
else -> callerFrame ?: return null // something else -- not supported
8077
}
8178
if (completion is UndispatchedCoroutine<*>) return completion // found UndispatchedCoroutine!
82-
return completion.undispatchedCompletion() // walk up the call stack with tail call
79+
return completion?.undispatchedCompletion() // walk up the call stack with tail call
8380
}
8481

8582
// Used by withContext when context changes, but dispatcher stays the same

0 commit comments

Comments
 (0)