-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathCoroutineContext.kt
49 lines (37 loc) · 2 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
41
42
43
44
45
46
47
48
49
/*
* 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.*
internal val currentEventLoop = ArrayList<BlockingEventLoop>()
private fun takeEventLoop(): BlockingEventLoop =
currentEventLoop.firstOrNull() ?: error("There is no event loop. Use runBlocking { ... } to start one.")
internal object DefaultExecutor : CoroutineDispatcher(), Delay {
override fun dispatch(context: CoroutineContext, block: Runnable) =
takeEventLoop().dispatch(context, block)
override fun scheduleResumeAfterDelay(time: Long, continuation: CancellableContinuation<Unit>) =
takeEventLoop().scheduleResumeAfterDelay(time, continuation)
override fun invokeOnTimeout(time: Long, block: Runnable): DisposableHandle =
takeEventLoop().invokeOnTimeout(time, block)
fun execute(task: Runnable) {
error("Cannot execute task because event loop was shut down")
}
fun schedule(delayedTask: EventLoopBase.DelayedTask) {
error("Cannot schedule task because event loop was shut down")
}
fun removeDelayedImpl(delayedTask: EventLoopBase.DelayedTask) {
error("Cannot happen")
}
}
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 !== kotlinx.coroutines.DefaultExecutor && combined[ContinuationInterceptor] == null)
combined + kotlinx.coroutines.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