Skip to content

Commit ec3ba2f

Browse files
committed
~avoid unnecessary lookup of the UndispatchedMarker, also make it JVM-only
1 parent acf9dac commit ec3ba2f

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

kotlinx-coroutines-core/common/src/Builders.common.kt

-13
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,6 @@ public suspend fun <T> withContext(
173173
}
174174
}
175175

176-
/**
177-
* Undispatched marker required to properly locate undispatched switch points and restore thread-local context to its initial value.
178-
* Is not added as is as job to avoid being overridden.
179-
* Is hidden as private class so it's not possible to extract this element easily.
180-
*
181-
* `var` is required to avoid cyclic initialization problem with `this`. It's late-binded once in the
182-
* [UndispatchedCoroutine] ctor
183-
*/
184-
internal class UndispatchedMarker(@JvmField var coroutine: UndispatchedCoroutine<*>?) :
185-
AbstractCoroutineContextElement(Key) {
186-
companion object Key : CoroutineContext.Key<UndispatchedMarker>
187-
}
188-
189176
/**
190177
* Calls the specified suspending block with the given [CoroutineDispatcher], suspends until it
191178
* completes, and returns the result.

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

+22-3
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,36 @@ internal fun updateUndispatchedCompletion(context: CoroutineContext, oldValue: A
7676
return undispatched
7777
}
7878

79+
/**
80+
* Undispatched marker required to properly locate undispatched switch points and restore thread-local context to its initial value.
81+
* Is not added as is as job to avoid being overridden.
82+
*
83+
* `var` is required to avoid cyclic initialization problem with `this`. It's late-binded once in the
84+
* [UndispatchedCoroutine] ctor.
85+
*/
86+
internal class UndispatchedMarker(
87+
@JvmField var coroutine: UndispatchedCoroutine<*>?
88+
) : AbstractCoroutineContextElement(Key) {
89+
companion object Key : CoroutineContext.Key<UndispatchedMarker>
90+
}
7991

8092
// Used by withContext when context changes, but dispatcher stays the same
81-
internal actual class UndispatchedCoroutine<in T> actual constructor(
93+
internal actual class UndispatchedCoroutine<in T>(
8294
context: CoroutineContext,
95+
marker: UndispatchedMarker,
8396
uCont: Continuation<T>
84-
) : ScopeCoroutine<T>(context + UndispatchedMarker(null), uCont) {
97+
) : ScopeCoroutine<T>(context + marker, uCont) {
98+
99+
actual constructor(
100+
context: CoroutineContext,
101+
uCont: Continuation<T>
102+
) : this(context, UndispatchedMarker(null), uCont)
103+
85104
private var savedContext: CoroutineContext? = null
86105
private var savedOldValue: Any? = null
87106

88107
init {
89-
parentContext[UndispatchedMarker]!!.coroutine = this
108+
marker.coroutine = this
90109
}
91110

92111
fun saveThreadContext(context: CoroutineContext, oldValue: Any?) {

0 commit comments

Comments
 (0)