1 package kotlinx.coroutines 2 3 import kotlin.coroutines.* 4 import kotlin.native.* 5 createMainDispatchernull6internal actual fun createMainDispatcher(default: CoroutineDispatcher): MainCoroutineDispatcher = 7 MissingMainDispatcher 8 9 internal actual fun createDefaultDispatcher(): CoroutineDispatcher = DefaultDispatcher 10 11 private object DefaultDispatcher : CoroutineDispatcher() { 12 // Be consistent with JVM -- at least 2 threads to provide some liveness guarantees in case of improper uses 13 @OptIn(ExperimentalStdlibApi::class) 14 private val ctx = newFixedThreadPoolContext(Platform.getAvailableProcessors().coerceAtLeast(2), "Dispatchers.Default") 15 16 override fun dispatch(context: CoroutineContext, block: Runnable) { 17 ctx.dispatch(context, block) 18 } 19 } 20 21 private object MissingMainDispatcher : MainCoroutineDispatcher() { 22 override val immediate: MainCoroutineDispatcher 23 get() = notImplemented() dispatchnull24 override fun dispatch(context: CoroutineContext, block: Runnable) = notImplemented() 25 override fun isDispatchNeeded(context: CoroutineContext): Boolean = notImplemented() 26 override fun dispatchYield(context: CoroutineContext, block: Runnable) = notImplemented() 27 28 private fun notImplemented(): Nothing = TODO("Dispatchers.Main is missing on the current platform") 29 } 30 31 internal actual inline fun platformAutoreleasePool(crossinline block: () -> Unit) = block() 32