|
4 | 4 |
|
5 | 5 | @file:JvmMultifileClass
|
6 | 6 | @file:JvmName("BuildersKt")
|
7 |
| -@file:OptIn(ExperimentalContracts::class) |
8 | 7 |
|
9 | 8 | package kotlinx.coroutines
|
10 | 9 |
|
11 | 10 | import kotlinx.atomicfu.*
|
12 | 11 | import kotlinx.coroutines.internal.*
|
13 | 12 | import kotlinx.coroutines.intrinsics.*
|
14 | 13 | import kotlinx.coroutines.selects.*
|
15 |
| -import kotlin.contracts.* |
16 | 14 | import kotlin.coroutines.*
|
17 | 15 | import kotlin.coroutines.intrinsics.*
|
18 | 16 | import kotlin.jvm.*
|
@@ -141,36 +139,31 @@ private class LazyDeferredCoroutine<T>(
|
141 | 139 | public suspend fun <T> withContext(
|
142 | 140 | context: CoroutineContext,
|
143 | 141 | block: suspend CoroutineScope.() -> T
|
144 |
| -): T { |
145 |
| - contract { |
146 |
| - callsInPlace(block, InvocationKind.EXACTLY_ONCE) |
| 142 | +): T = suspendCoroutineUninterceptedOrReturn sc@ { uCont -> |
| 143 | + // compute new context |
| 144 | + val oldContext = uCont.context |
| 145 | + val newContext = oldContext + context |
| 146 | + // always check for cancellation of new context |
| 147 | + newContext.checkCompletion() |
| 148 | + // FAST PATH #1 -- new context is the same as the old one |
| 149 | + if (newContext === oldContext) { |
| 150 | + val coroutine = ScopeCoroutine(newContext, uCont) |
| 151 | + return@sc coroutine.startUndispatchedOrReturn(coroutine, block) |
147 | 152 | }
|
148 |
| - return suspendCoroutineUninterceptedOrReturn sc@ { uCont -> |
149 |
| - // compute new context |
150 |
| - val oldContext = uCont.context |
151 |
| - val newContext = oldContext + context |
152 |
| - // always check for cancellation of new context |
153 |
| - newContext.checkCompletion() |
154 |
| - // FAST PATH #1 -- new context is the same as the old one |
155 |
| - if (newContext === oldContext) { |
156 |
| - val coroutine = ScopeCoroutine(newContext, uCont) |
| 153 | + // FAST PATH #2 -- the new dispatcher is the same as the old one (something else changed) |
| 154 | + // `equals` is used by design (see equals implementation is wrapper context like ExecutorCoroutineDispatcher) |
| 155 | + if (newContext[ContinuationInterceptor] == oldContext[ContinuationInterceptor]) { |
| 156 | + val coroutine = UndispatchedCoroutine(newContext, uCont) |
| 157 | + // There are changes in the context, so this thread needs to be updated |
| 158 | + withCoroutineContext(newContext, null) { |
157 | 159 | return@sc coroutine.startUndispatchedOrReturn(coroutine, block)
|
158 | 160 | }
|
159 |
| - // FAST PATH #2 -- the new dispatcher is the same as the old one (something else changed) |
160 |
| - // `equals` is used by design (see equals implementation is wrapper context like ExecutorCoroutineDispatcher) |
161 |
| - if (newContext[ContinuationInterceptor] == oldContext[ContinuationInterceptor]) { |
162 |
| - val coroutine = UndispatchedCoroutine(newContext, uCont) |
163 |
| - // There are changes in the context, so this thread needs to be updated |
164 |
| - withCoroutineContext(newContext, null) { |
165 |
| - return@sc coroutine.startUndispatchedOrReturn(coroutine, block) |
166 |
| - } |
167 |
| - } |
168 |
| - // SLOW PATH -- use new dispatcher |
169 |
| - val coroutine = DispatchedCoroutine(newContext, uCont) |
170 |
| - coroutine.initParentJob() |
171 |
| - block.startCoroutineCancellable(coroutine, coroutine) |
172 |
| - coroutine.getResult() |
173 | 161 | }
|
| 162 | + // SLOW PATH -- use new dispatcher |
| 163 | + val coroutine = DispatchedCoroutine(newContext, uCont) |
| 164 | + coroutine.initParentJob() |
| 165 | + block.startCoroutineCancellable(coroutine, coroutine) |
| 166 | + coroutine.getResult() |
174 | 167 | }
|
175 | 168 |
|
176 | 169 | /**
|
|
0 commit comments