|
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.*
|
@@ -136,36 +134,31 @@ private class LazyDeferredCoroutine<T>(
|
136 | 134 | public suspend fun <T> withContext(
|
137 | 135 | context: CoroutineContext,
|
138 | 136 | block: suspend CoroutineScope.() -> T
|
139 |
| -): T { |
140 |
| - contract { |
141 |
| - callsInPlace(block, InvocationKind.EXACTLY_ONCE) |
| 137 | +): T = suspendCoroutineUninterceptedOrReturn sc@ { uCont -> |
| 138 | + // compute new context |
| 139 | + val oldContext = uCont.context |
| 140 | + val newContext = oldContext + context |
| 141 | + // always check for cancellation of new context |
| 142 | + newContext.checkCompletion() |
| 143 | + // FAST PATH #1 -- new context is the same as the old one |
| 144 | + if (newContext === oldContext) { |
| 145 | + val coroutine = ScopeCoroutine(newContext, uCont) |
| 146 | + return@sc coroutine.startUndispatchedOrReturn(coroutine, block) |
142 | 147 | }
|
143 |
| - return suspendCoroutineUninterceptedOrReturn sc@ { uCont -> |
144 |
| - // compute new context |
145 |
| - val oldContext = uCont.context |
146 |
| - val newContext = oldContext + context |
147 |
| - // always check for cancellation of new context |
148 |
| - newContext.checkCompletion() |
149 |
| - // FAST PATH #1 -- new context is the same as the old one |
150 |
| - if (newContext === oldContext) { |
151 |
| - val coroutine = ScopeCoroutine(newContext, uCont) |
| 148 | + // FAST PATH #2 -- the new dispatcher is the same as the old one (something else changed) |
| 149 | + // `equals` is used by design (see equals implementation is wrapper context like ExecutorCoroutineDispatcher) |
| 150 | + if (newContext[ContinuationInterceptor] == oldContext[ContinuationInterceptor]) { |
| 151 | + val coroutine = UndispatchedCoroutine(newContext, uCont) |
| 152 | + // There are changes in the context, so this thread needs to be updated |
| 153 | + withCoroutineContext(newContext, null) { |
152 | 154 | return@sc coroutine.startUndispatchedOrReturn(coroutine, block)
|
153 | 155 | }
|
154 |
| - // FAST PATH #2 -- the new dispatcher is the same as the old one (something else changed) |
155 |
| - // `equals` is used by design (see equals implementation is wrapper context like ExecutorCoroutineDispatcher) |
156 |
| - if (newContext[ContinuationInterceptor] == oldContext[ContinuationInterceptor]) { |
157 |
| - val coroutine = UndispatchedCoroutine(newContext, uCont) |
158 |
| - // There are changes in the context, so this thread needs to be updated |
159 |
| - withCoroutineContext(newContext, null) { |
160 |
| - return@sc coroutine.startUndispatchedOrReturn(coroutine, block) |
161 |
| - } |
162 |
| - } |
163 |
| - // SLOW PATH -- use new dispatcher |
164 |
| - val coroutine = DispatchedCoroutine(newContext, uCont) |
165 |
| - coroutine.initParentJob() |
166 |
| - block.startCoroutineCancellable(coroutine, coroutine) |
167 |
| - coroutine.getResult() |
168 | 156 | }
|
| 157 | + // SLOW PATH -- use new dispatcher |
| 158 | + val coroutine = DispatchedCoroutine(newContext, uCont) |
| 159 | + coroutine.initParentJob() |
| 160 | + block.startCoroutineCancellable(coroutine, coroutine) |
| 161 | + coroutine.getResult() |
169 | 162 | }
|
170 | 163 |
|
171 | 164 | /**
|
|
0 commit comments