@@ -17,6 +17,8 @@ internal class DispatchedContinuation<in T>(
17
17
) : Continuation<T> by continuation, DispatchedTask<T> {
18
18
private var _state : Any? = UNDEFINED
19
19
public override var resumeMode: Int = 0
20
+ @JvmField // pre-cached value to avoid ctx.fold on every resumption
21
+ internal val countOrElement = threadContextElements(context)
20
22
21
23
override fun takeState (): Any? {
22
24
val state = _state
@@ -80,21 +82,21 @@ internal class DispatchedContinuation<in T>(
80
82
81
83
@Suppress(" NOTHING_TO_INLINE" ) // we need it inline to save us an entry on the stack
82
84
inline fun resumeUndispatchedWith (result : Result <T >) {
83
- withCoroutineContext(context) {
85
+ withCoroutineContext(context, countOrElement ) {
84
86
continuation.resumeWith(result)
85
87
}
86
88
}
87
89
88
90
@Suppress(" NOTHING_TO_INLINE" ) // we need it inline to save us an entry on the stack
89
91
inline fun resumeUndispatched (value : T ) {
90
- withCoroutineContext(context) {
92
+ withCoroutineContext(context, countOrElement ) {
91
93
continuation.resume(value)
92
94
}
93
95
}
94
96
95
97
@Suppress(" NOTHING_TO_INLINE" ) // we need it inline to save us an entry on the stack
96
98
inline fun resumeUndispatchedWithException (exception : Throwable ) {
97
- withCoroutineContext(context) {
99
+ withCoroutineContext(context, countOrElement ) {
98
100
continuation.resumeWithException(exception)
99
101
}
100
102
}
@@ -151,7 +153,7 @@ internal interface DispatchedTask<in T> : Runnable {
151
153
val context = continuation.context
152
154
val job = if (resumeMode.isCancellableMode) context[Job ] else null
153
155
val state = takeState() // NOTE: Must take state in any case, even if cancelled
154
- withCoroutineContext(context) {
156
+ withCoroutineContext(context, delegate.countOrElement ) {
155
157
if (job != null && ! job.isActive)
156
158
continuation.resumeWithException(job.getCancellationException())
157
159
else {
0 commit comments