• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download

<lambda>null1 package kotlinx.coroutines
2 
3 import kotlinx.coroutines.internal.probeCoroutineCreated
4 import kotlinx.coroutines.internal.probeCoroutineResumed
5 import kotlin.coroutines.*
6 import kotlin.coroutines.intrinsics.*
7 
8 suspend fun <T> withEmptyContext(block: suspend () -> T): T = suspendCoroutine { cont ->
9     block.startCoroutineUnintercepted(Continuation(EmptyCoroutineContext) { cont.resumeWith(it) })
10 }
11 
12 /**
13  * Use this function to restart a coroutine directly from inside of [suspendCoroutine],
14  * when the code is already in the context of this coroutine.
15  * It does not use [ContinuationInterceptor] and does not update the context of the current thread.
16  */
startCoroutineUninterceptednull17 fun <T> (suspend () -> T).startCoroutineUnintercepted(completion: Continuation<T>) {
18     val actualCompletion = probeCoroutineCreated(completion)
19     val value = try {
20         probeCoroutineResumed(actualCompletion)
21         startCoroutineUninterceptedOrReturn(actualCompletion)
22     } catch (e: Throwable) {
23         actualCompletion.resumeWithException(e)
24         return
25     }
26     if (value !== COROUTINE_SUSPENDED) {
27         @Suppress("UNCHECKED_CAST")
28         actualCompletion.resume(value as T)
29     }
30 }
31