Skip to content

Commit 1ed19c8

Browse files
authored
Introduce first version of Dispatchers.IO for K/N (#3576)
* Emulate expect declaration refinement via extension property as the only way to do it in a backwards-compatible manner: in the current model, it is impossible to have common 'expect' Dispatchers declaration, then refined 'concurrent' Dispatchers declaration with 'expect val IO' and then JVM declaration with JVM-specific members. Current solutions seems to be the less intrusive one Fixes #3205
1 parent bf03c48 commit 1ed19c8

File tree

7 files changed

+87
-5
lines changed

7 files changed

+87
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ See [Contributing Guidelines](CONTRIBUTING.md).
212212
[MainScope()]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-scope.html
213213
[SupervisorJob()]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-supervisor-job.html
214214
[CoroutineExceptionHandler]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-exception-handler/index.html
215-
[Dispatchers.IO]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-i-o.html
215+
[Dispatchers.IO]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-i-o.html
216216
[asCoroutineDispatcher]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/as-coroutine-dispatcher.html
217217
[Promise.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/await.html
218218
[promise]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/promise.html

kotlinx-coroutines-core/api/kotlinx-coroutines-core.api

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ public final class kotlinx/coroutines/Dispatchers {
302302

303303
public final class kotlinx/coroutines/DispatchersKt {
304304
public static final field IO_PARALLELISM_PROPERTY_NAME Ljava/lang/String;
305+
public static final synthetic fun getIO (Lkotlinx/coroutines/Dispatchers;)Lkotlinx/coroutines/CoroutineDispatcher;
305306
}
306307

307308
public abstract interface class kotlinx/coroutines/DisposableHandle {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import kotlin.coroutines.*
1212
public expect object Dispatchers {
1313
/**
1414
* The default [CoroutineDispatcher] that is used by all standard builders like
15-
* [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc
15+
* [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc.
1616
* if neither a dispatcher nor any other [ContinuationInterceptor] is specified in their context.
1717
*
1818
* It is backed by a shared pool of threads on JVM and Native. By default, the maximum number of threads used
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016-2023 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+
/**
8+
* The [CoroutineDispatcher] that is designed for offloading blocking IO tasks to a shared pool of threads.
9+
* Additional threads in this pool are created on demand.
10+
* Default IO pool size is `64`; on JVM it can be configured using JVM-specific mechanisms,
11+
* please refer to `Dispatchers.IO` documentation on JVM platform.
12+
*
13+
* ### Elasticity for limited parallelism
14+
*
15+
* `Dispatchers.IO` has a unique property of elasticity: its views
16+
* obtained with [CoroutineDispatcher.limitedParallelism] are
17+
* not restricted by the `Dispatchers.IO` parallelism. Conceptually, there is
18+
* a dispatcher backed by an unlimited pool of threads, and both `Dispatchers.IO`
19+
* and views of `Dispatchers.IO` are actually views of that dispatcher. In practice
20+
* this means that, despite not abiding by `Dispatchers.IO`'s parallelism
21+
* restrictions, its views share threads and resources with it.
22+
*
23+
* In the following example
24+
* ```
25+
* // 100 threads for MySQL connection
26+
* val myMysqlDbDispatcher = Dispatchers.IO.limitedParallelism(100)
27+
* // 60 threads for MongoDB connection
28+
* val myMongoDbDispatcher = Dispatchers.IO.limitedParallelism(60)
29+
* ```
30+
* the system may have up to `64 + 100 + 60` threads dedicated to blocking tasks during peak loads,
31+
* but during its steady state there is only a small number of threads shared
32+
* among `Dispatchers.IO`, `myMysqlDbDispatcher` and `myMongoDbDispatcher`
33+
*/
34+
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
35+
public expect val Dispatchers.IO: CoroutineDispatcher
36+
37+

kotlinx-coroutines-core/concurrent/test/DefaultDispatcherConcurrencyTest.kt renamed to kotlinx-coroutines-core/concurrent/test/DefaultDispatchersConcurrencyTest.kt

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ package kotlinx.coroutines
66
class DefaultDispatcherConcurrencyTest : AbstractDispatcherConcurrencyTest() {
77
override val dispatcher: CoroutineDispatcher = Dispatchers.Default
88
}
9+
10+
class IoDispatcherConcurrencyTest : AbstractDispatcherConcurrencyTest() {
11+
override val dispatcher: CoroutineDispatcher = Dispatchers.IO
12+
}

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/*
2-
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
@file:Suppress("unused")
6-
75
package kotlinx.coroutines
86

97
import kotlinx.coroutines.internal.*
@@ -97,3 +95,13 @@ public actual object Dispatchers {
9795
DefaultScheduler.shutdown()
9896
}
9997
}
98+
99+
/**
100+
* `actual` counterpart of the corresponding `expect` declaration.
101+
* Should never be used directly from JVM sources, all accesses
102+
* to `Dispatchers.IO` should be resolved to the corresponding member of [Dispatchers] object.
103+
* @suppress
104+
*/
105+
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
106+
@Deprecated(message = "Should not be used directly", level = DeprecationLevel.HIDDEN)
107+
public actual val Dispatchers.IO: CoroutineDispatcher get() = Dispatchers.IO

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

+32
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
package kotlinx.coroutines
66

7+
import kotlinx.coroutines.internal.*
8+
import kotlin.coroutines.*
9+
710

811
public actual object Dispatchers {
912
public actual val Default: CoroutineDispatcher = createDefaultDispatcher()
@@ -19,6 +22,35 @@ public actual object Dispatchers {
1922
internal fun injectMain(dispatcher: MainCoroutineDispatcher) {
2023
injectedMainDispatcher = dispatcher
2124
}
25+
26+
internal val IO: CoroutineDispatcher = DefaultIoScheduler
2227
}
2328

29+
internal object DefaultIoScheduler : CoroutineDispatcher() {
30+
// 2048 is an arbitrary KMP-friendly constant
31+
private val unlimitedPool = newFixedThreadPoolContext(2048, "Dispatchers.IO")
32+
private val io = unlimitedPool.limitedParallelism(64) // Default JVM size
33+
34+
@ExperimentalCoroutinesApi
35+
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
36+
// See documentation to Dispatchers.IO for the rationale
37+
return unlimitedPool.limitedParallelism(parallelism)
38+
}
39+
40+
override fun dispatch(context: CoroutineContext, block: Runnable) {
41+
io.dispatch(context, block)
42+
}
43+
44+
@InternalCoroutinesApi
45+
override fun dispatchYield(context: CoroutineContext, block: Runnable) {
46+
io.dispatchYield(context, block)
47+
}
48+
49+
override fun toString(): String = "Dispatchers.IO"
50+
}
51+
52+
53+
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
54+
public actual val Dispatchers.IO: CoroutineDispatcher get() = IO
55+
2456
internal expect fun createMainDispatcher(default: CoroutineDispatcher): MainCoroutineDispatcher

0 commit comments

Comments
 (0)