Skip to content

Commit

Permalink
Use Platform.getAvailableProcessors for K/N Dispatchers.Default (#3366)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwwdfsad committed Jul 14, 2022
1 parent 845e322 commit 19666ac
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions kotlinx-coroutines-core/nativeOther/src/Dispatchers.kt
Expand Up @@ -5,17 +5,17 @@
package kotlinx.coroutines

import kotlin.coroutines.*
import kotlin.native.*

internal actual fun createMainDispatcher(default: CoroutineDispatcher): MainCoroutineDispatcher =
MissingMainDispatcher

internal actual fun createDefaultDispatcher(): CoroutineDispatcher = DefaultDispatcher

private object DefaultDispatcher : CoroutineDispatcher() {

// Delegated, so users won't be able to downcast and call 'close'
// The precise number of threads cannot be obtained until KT-48179 is implemented, 4 is just "good enough" number.
private val ctx = newFixedThreadPoolContext(4, "Dispatchers.Default")
// Be consistent with JVM -- at least 2 threads to provide some liveness guarantees in case of improper uses
@OptIn(ExperimentalStdlibApi::class)
private val ctx = newFixedThreadPoolContext(Platform.getAvailableProcessors().coerceAtLeast(2), "Dispatchers.Default")

override fun dispatch(context: CoroutineContext, block: Runnable) {
ctx.dispatch(context, block)
Expand Down

0 comments on commit 19666ac

Please sign in to comment.