-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathCoroutineContext.kt
40 lines (30 loc) · 1.78 KB
/
CoroutineContext.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import kotlin.coroutines.*
private fun takeEventLoop(): EventLoopImpl =
ThreadLocalEventLoop.currentOrNull() as? EventLoopImpl ?:
error("There is no event loop. Use runBlocking { ... } to start one.")
internal actual object DefaultExecutor : CoroutineDispatcher(), Delay {
override fun dispatch(context: CoroutineContext, block: Runnable) =
takeEventLoop().dispatch(context, block)
override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) =
takeEventLoop().scheduleResumeAfterDelay(timeMillis, continuation)
override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle =
takeEventLoop().invokeOnTimeout(timeMillis, block)
actual fun enqueue(task: Runnable): Unit = loopWasShutDown()
}
internal fun loopWasShutDown(): Nothing = error("Cannot execute task because event loop was shut down")
internal actual fun createDefaultDispatcher(): CoroutineDispatcher =
DefaultExecutor
internal actual val DefaultDelay: Delay = DefaultExecutor
public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
val combined = coroutineContext + context
return if (combined !== DefaultExecutor && combined[ContinuationInterceptor] == null)
combined + DefaultExecutor else combined
}
// No debugging facilities on native
internal actual inline fun <T> withCoroutineContext(context: CoroutineContext, countOrElement: Any?, block: () -> T): T = block()
internal actual fun Continuation<*>.toDebugString(): String = toString()
internal actual val CoroutineContext.coroutineName: String? get() = null // not supported on native