|
2 | 2 | * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
|
3 | 3 | */
|
4 | 4 |
|
| 5 | +@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") |
| 6 | + |
5 | 7 | package kotlinx.coroutines
|
6 | 8 |
|
7 | 9 | import kotlinx.coroutines.internal.*
|
8 | 10 | import kotlinx.coroutines.scheduling.*
|
9 |
| -import java.util.concurrent.atomic.* |
10 | 11 | import kotlin.coroutines.*
|
| 12 | +import kotlin.coroutines.jvm.internal.* |
11 | 13 |
|
12 | 14 | internal const val COROUTINES_SCHEDULER_PROPERTY_NAME = "kotlinx.coroutines.scheduler"
|
13 | 15 |
|
@@ -48,6 +50,72 @@ internal actual inline fun <T> withCoroutineContext(context: CoroutineContext, c
|
48 | 50 | }
|
49 | 51 | }
|
50 | 52 |
|
| 53 | +/** |
| 54 | + * Executes a block using a context of a given continuation. |
| 55 | + */ |
| 56 | +internal actual inline fun <T> withContinuationContext(continuation: Continuation<*>, countOrElement: Any?, block: () -> T): T { |
| 57 | + val context = continuation.context |
| 58 | + val oldValue = updateThreadContext(context, countOrElement) |
| 59 | + val undispatchedCompletion = if (oldValue !== NO_THREAD_ELEMENTS) { |
| 60 | + // Only if some values were replaced we'll go to the slow path of figuring out where/how to restore them |
| 61 | + continuation.undispatchedCompletion() |
| 62 | + } else |
| 63 | + null // fast path -- don't even try to find undispatchedCompletion as there's nothing to restore in the context |
| 64 | + undispatchedCompletion?.saveThreadContext(context, oldValue) |
| 65 | + try { |
| 66 | + return block() |
| 67 | + } finally { |
| 68 | + if (undispatchedCompletion == null || undispatchedCompletion.clearThreadContext()) |
| 69 | + restoreThreadContext(context, oldValue) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +internal tailrec fun Continuation<*>.undispatchedCompletion(): UndispatchedCoroutine<*>? { |
| 74 | + // 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 |
| 80 | + } |
| 81 | + if (completion is UndispatchedCoroutine<*>) return completion // found UndispatchedCoroutine! |
| 82 | + return completion.undispatchedCompletion() // walk up the call stack with tail call |
| 83 | +} |
| 84 | + |
| 85 | +// Used by withContext when context changes, but dispatcher stays the same |
| 86 | +internal actual class UndispatchedCoroutine<in T> actual constructor( |
| 87 | + context: CoroutineContext, |
| 88 | + uCont: Continuation<T> |
| 89 | +) : ScopeCoroutine<T>(context, uCont) { |
| 90 | + private var savedContext: CoroutineContext? = null |
| 91 | + private var savedOldValue: Any? = null |
| 92 | + |
| 93 | + fun saveThreadContext(context: CoroutineContext, oldValue: Any?) { |
| 94 | + savedContext = context |
| 95 | + savedOldValue = oldValue |
| 96 | + } |
| 97 | + |
| 98 | + fun clearThreadContext(): Boolean { |
| 99 | + if (savedContext == null) return false |
| 100 | + savedContext = null |
| 101 | + savedOldValue = null |
| 102 | + return true |
| 103 | + } |
| 104 | + |
| 105 | + override fun afterResume(state: Any?) { |
| 106 | + savedContext?.let { context -> |
| 107 | + restoreThreadContext(context, savedOldValue) |
| 108 | + savedContext = null |
| 109 | + savedOldValue = null |
| 110 | + } |
| 111 | + // resume undispatched -- update context but stay on the same dispatcher |
| 112 | + val result = recoverResult(state, uCont) |
| 113 | + withContinuationContext(uCont, null) { |
| 114 | + uCont.resumeWith(result) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
51 | 119 | internal actual val CoroutineContext.coroutineName: String? get() {
|
52 | 120 | if (!DEBUG) return null
|
53 | 121 | val coroutineId = this[CoroutineId] ?: return null
|
|
0 commit comments