@@ -27,16 +27,22 @@ public suspend fun yield(): Unit = suspendCoroutineUninterceptedOrReturn sc@ { u
27
27
val context = uCont.context
28
28
context.checkCompletion()
29
29
val cont = uCont.intercepted() as ? DispatchedContinuation <Unit > ? : return @sc Unit
30
- // This code detects the Unconfined dispatcher even if it was wrapped into another dispatcher
31
- val yieldContext = YieldContext ()
32
- cont.dispatchYield(context + yieldContext, Unit )
33
- // Special case for the unconfined dispatcher that can yield only in existing unconfined loop
34
- if (yieldContext.dispatcherWasUnconfined) {
35
- // Means that the Unconfined dispatcher got the call, but did not do anything.
36
- // See also code of "Unconfined.dispatch" function.
37
- return @sc if (cont.yieldUndispatched()) COROUTINE_SUSPENDED else Unit
30
+ if (cont.dispatcher.isDispatchNeeded(context)) {
31
+ // this is a regular dispatcher -- do simple dispatchYield
32
+ cont.dispatchYield(context, Unit )
33
+ } else {
34
+ // This is either an "immediate" dispatcher or the Unconfined dispatcher
35
+ // This code detects the Unconfined dispatcher even if it was wrapped into another dispatcher
36
+ val yieldContext = YieldContext ()
37
+ cont.dispatchYield(context + yieldContext, Unit )
38
+ // Special case for the unconfined dispatcher that can yield only in existing unconfined loop
39
+ if (yieldContext.dispatcherWasUnconfined) {
40
+ // Means that the Unconfined dispatcher got the call, but did not do anything.
41
+ // See also code of "Unconfined.dispatch" function.
42
+ return @sc if (cont.yieldUndispatched()) COROUTINE_SUSPENDED else Unit
43
+ }
44
+ // Otherwise, it was some other dispatcher that successfully dispatched the coroutine
38
45
}
39
- // It was some other dispatcher that successfully dispatched the coroutine
40
46
COROUTINE_SUSPENDED
41
47
}
42
48
0 commit comments