Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Platform.getAvailableProcessors for K/N Dispatchers.Default #3366

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions kotlinx-coroutines-core/nativeOther/src/Dispatchers.kt
Original file line number Diff line number Diff line change
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