Skip to content

Commit 19666ac

Browse files
authored
Use Platform.getAvailableProcessors for K/N Dispatchers.Default (#3366)
1 parent 845e322 commit 19666ac

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

kotlinx-coroutines-core/nativeOther/src/Dispatchers.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
package kotlinx.coroutines
66

77
import kotlin.coroutines.*
8+
import kotlin.native.*
89

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

1213
internal actual fun createDefaultDispatcher(): CoroutineDispatcher = DefaultDispatcher
1314

1415
private object DefaultDispatcher : CoroutineDispatcher() {
15-
16-
// Delegated, so users won't be able to downcast and call 'close'
17-
// The precise number of threads cannot be obtained until KT-48179 is implemented, 4 is just "good enough" number.
18-
private val ctx = newFixedThreadPoolContext(4, "Dispatchers.Default")
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")
1919

2020
override fun dispatch(context: CoroutineContext, block: Runnable) {
2121
ctx.dispatch(context, block)

0 commit comments

Comments
 (0)