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