|
1 | 1 | /*
|
2 |
| - * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 2 | + * Copyright 2016-2019 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,71 @@ 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 = continuation.undispatchedCompletion() |
| 60 | + undispatchedCompletion?.saveThreadContext(context, oldValue) |
| 61 | + try { |
| 62 | + return block() |
| 63 | + } finally { |
| 64 | + if (undispatchedCompletion == null || undispatchedCompletion.clearThreadContext()) |
| 65 | + restoreThreadContext(context, oldValue) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +internal tailrec fun Continuation<*>.undispatchedCompletion(): UndispatchedCoroutine<*>? { |
| 70 | + val completion = directCompletion() ?: return null |
| 71 | + if (completion is UndispatchedCoroutine<*>) return completion |
| 72 | + return completion.undispatchedCompletion() |
| 73 | +} |
| 74 | + |
| 75 | +private fun Continuation<*>.directCompletion(): Continuation<*>? = when (this) { |
| 76 | + is BaseContinuationImpl -> completion // regular suspending function -- direct resume |
| 77 | + is DispatchedCoroutine -> null // dispatches on resume |
| 78 | + is ScopeCoroutine -> uCont // other scoped coroutine -- direct resume |
| 79 | + else -> null |
| 80 | +} |
| 81 | + |
| 82 | +// Used by withContext when context changes, but dispatcher stays the same |
| 83 | +internal actual class UndispatchedCoroutine<in T> actual constructor( |
| 84 | + context: CoroutineContext, |
| 85 | + uCont: Continuation<T> |
| 86 | +) : ScopeCoroutine<T>(context, uCont) { |
| 87 | + private var savedContext: CoroutineContext? = null |
| 88 | + private var savedOldValue: Any? = null |
| 89 | + |
| 90 | + fun saveThreadContext(context: CoroutineContext, oldValue: Any?) { |
| 91 | + savedContext = context |
| 92 | + savedOldValue = oldValue |
| 93 | + } |
| 94 | + |
| 95 | + fun clearThreadContext(): Boolean { |
| 96 | + if (savedContext == null) return false |
| 97 | + savedContext = null |
| 98 | + savedOldValue = null |
| 99 | + return true |
| 100 | + } |
| 101 | + |
| 102 | + override fun afterResume(state: Any?) { |
| 103 | + savedContext?.let { context -> |
| 104 | + restoreThreadContext(context, savedOldValue) |
| 105 | + savedContext = null |
| 106 | + savedOldValue = null |
| 107 | + } |
| 108 | + // resume undispatched -- update context but stay on the same dispatcher |
| 109 | + val result = recoverResult(state, uCont) |
| 110 | + withContinuationContext(uCont, null) { |
| 111 | + uCont.resumeWith(result) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | + |
51 | 118 | internal actual val CoroutineContext.coroutineName: String? get() {
|
52 | 119 | if (!DEBUG) return null
|
53 | 120 | val coroutineId = this[CoroutineId] ?: return null
|
|
0 commit comments