Skip to content

Commit 547fe3a

Browse files
committed
Add optional name parameter to .limitedParallelism
Fixes #4023
1 parent dd890c9 commit 547fe3a

File tree

12 files changed

+40
-23
lines changed

12 files changed

+40
-23
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ public abstract class kotlinx/coroutines/CoroutineDispatcher : kotlin/coroutines
170170
public fun get (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext$Element;
171171
public final fun interceptContinuation (Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
172172
public fun isDispatchNeeded (Lkotlin/coroutines/CoroutineContext;)Z
173-
public fun limitedParallelism (I)Lkotlinx/coroutines/CoroutineDispatcher;
173+
public synthetic fun limitedParallelism (I)Lkotlinx/coroutines/CoroutineDispatcher;
174+
public fun limitedParallelism (ILjava/lang/String;)Lkotlinx/coroutines/CoroutineDispatcher;
175+
public static synthetic fun limitedParallelism$default (Lkotlinx/coroutines/CoroutineDispatcher;ILjava/lang/String;ILjava/lang/Object;)Lkotlinx/coroutines/CoroutineDispatcher;
174176
public fun minusKey (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext;
175177
public final fun plus (Lkotlinx/coroutines/CoroutineDispatcher;)Lkotlinx/coroutines/CoroutineDispatcher;
176178
public final fun releaseInterceptedContinuation (Lkotlin/coroutines/Continuation;)V
@@ -502,7 +504,7 @@ public class kotlinx/coroutines/JobSupport : kotlinx/coroutines/ChildJob, kotlin
502504
public abstract class kotlinx/coroutines/MainCoroutineDispatcher : kotlinx/coroutines/CoroutineDispatcher {
503505
public fun <init> ()V
504506
public abstract fun getImmediate ()Lkotlinx/coroutines/MainCoroutineDispatcher;
505-
public fun limitedParallelism (I)Lkotlinx/coroutines/CoroutineDispatcher;
507+
public fun limitedParallelism (ILjava/lang/String;)Lkotlinx/coroutines/CoroutineDispatcher;
506508
public fun toString ()Ljava/lang/String;
507509
protected final fun toStringInternalImpl ()Ljava/lang/String;
508510
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ abstract class kotlinx.coroutines/CoroutineDispatcher : kotlin.coroutines/Abstra
5959
open fun dispatchYield(kotlin.coroutines/CoroutineContext, kotlinx.coroutines/Runnable) // kotlinx.coroutines/CoroutineDispatcher.dispatchYield|dispatchYield(kotlin.coroutines.CoroutineContext;kotlinx.coroutines.Runnable){}[0]
6060
open fun isDispatchNeeded(kotlin.coroutines/CoroutineContext): kotlin/Boolean // kotlinx.coroutines/CoroutineDispatcher.isDispatchNeeded|isDispatchNeeded(kotlin.coroutines.CoroutineContext){}[0]
6161
open fun limitedParallelism(kotlin/Int): kotlinx.coroutines/CoroutineDispatcher // kotlinx.coroutines/CoroutineDispatcher.limitedParallelism|limitedParallelism(kotlin.Int){}[0]
62+
open fun limitedParallelism(kotlin/Int, kotlin/String? =...): kotlinx.coroutines/CoroutineDispatcher // kotlinx.coroutines/CoroutineDispatcher.limitedParallelism|limitedParallelism(kotlin.Int;kotlin.String?){}[0]
6263
open fun toString(): kotlin/String // kotlinx.coroutines/CoroutineDispatcher.toString|toString(){}[0]
6364
}
6465
abstract class kotlinx.coroutines/MainCoroutineDispatcher : kotlinx.coroutines/CoroutineDispatcher { // kotlinx.coroutines/MainCoroutineDispatcher|null[0]
6566
abstract val immediate // kotlinx.coroutines/MainCoroutineDispatcher.immediate|{}immediate[0]
6667
abstract fun <get-immediate>(): kotlinx.coroutines/MainCoroutineDispatcher // kotlinx.coroutines/MainCoroutineDispatcher.immediate.<get-immediate>|<get-immediate>(){}[0]
6768
constructor <init>() // kotlinx.coroutines/MainCoroutineDispatcher.<init>|<init>(){}[0]
6869
final fun toStringInternalImpl(): kotlin/String? // kotlinx.coroutines/MainCoroutineDispatcher.toStringInternalImpl|toStringInternalImpl(){}[0]
69-
open fun limitedParallelism(kotlin/Int): kotlinx.coroutines/CoroutineDispatcher // kotlinx.coroutines/MainCoroutineDispatcher.limitedParallelism|limitedParallelism(kotlin.Int){}[0]
70+
open fun limitedParallelism(kotlin/Int, kotlin/String?): kotlinx.coroutines/CoroutineDispatcher // kotlinx.coroutines/MainCoroutineDispatcher.limitedParallelism|limitedParallelism(kotlin.Int;kotlin.String?){}[0]
7071
open fun toString(): kotlin/String // kotlinx.coroutines/MainCoroutineDispatcher.toString|toString(){}[0]
7172
}
7273
abstract fun interface <#A: in kotlin/Any?> kotlinx.coroutines.flow/FlowCollector { // kotlinx.coroutines.flow/FlowCollector|null[0]

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public abstract class CoroutineDispatcher :
105105
* is established between them:
106106
*
107107
* ```
108-
* val confined = Dispatchers.Default.limitedParallelism(1)
108+
* val confined = Dispatchers.Default.limitedParallelism(1, "incrementDispatcher")
109109
* var counter = 0
110110
*
111111
* // Invoked from arbitrary coroutines
@@ -135,15 +135,23 @@ public abstract class CoroutineDispatcher :
135135
* Implementations of this method are allowed to return `this` if the current dispatcher already satisfies the parallelism requirement.
136136
* For example, `Dispatchers.Main.limitedParallelism(1)` returns `Dispatchers.Main`, because the main dispatcher is already single-threaded.
137137
*
138+
* @param name optional name for the resulting dispatcher string representation if a new dispatcher was created
138139
* @throws IllegalArgumentException if the given [parallelism] is non-positive
139140
* @throws UnsupportedOperationException if the current dispatcher does not support limited parallelism views
140141
*/
141142
@ExperimentalCoroutinesApi
142-
public open fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
143+
public open fun limitedParallelism(parallelism: Int, name: String? = null): CoroutineDispatcher {
143144
parallelism.checkParallelism()
144-
return LimitedDispatcher(this, parallelism)
145+
return LimitedDispatcher(this, parallelism, name)
145146
}
146147

148+
// Was experimental since 1.6.0, deprecated since 1.8.x
149+
@Deprecated("Deprecated for good. Override 'limitedParallelism(parallelism: Int, name: String?)' instead",
150+
level = DeprecationLevel.HIDDEN,
151+
replaceWith = ReplaceWith("limitedParallelism(parallelism, null)")
152+
)
153+
public open fun limitedParallelism(parallelism: Int): CoroutineDispatcher = limitedParallelism(parallelism, null)
154+
147155
/**
148156
* Requests execution of a runnable [block].
149157
* The dispatcher guarantees that [block] will eventually execute, typically by dispatching it to a thread pool,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ internal abstract class EventLoop : CoroutineDispatcher() {
111111
}
112112
}
113113

114-
final override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
114+
final override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher {
115115
parallelism.checkParallelism()
116116
return this
117117
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public abstract class MainCoroutineDispatcher : CoroutineDispatcher() {
4949
*/
5050
override fun toString(): String = toStringInternalImpl() ?: "$classSimpleName@$hexAddress"
5151

52-
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
52+
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher {
5353
parallelism.checkParallelism()
5454
// MainCoroutineDispatcher is single-threaded -- short-circuit any attempts to limit it
5555
return this

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import kotlin.jvm.*
99
internal object Unconfined : CoroutineDispatcher() {
1010

1111
@ExperimentalCoroutinesApi
12-
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
12+
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher {
1313
throw UnsupportedOperationException("limitedParallelism is not supported for Dispatchers.Unconfined")
1414
}
1515

kotlinx-coroutines-core/common/src/internal/LimitedDispatcher.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import kotlin.coroutines.*
2121
*/
2222
internal class LimitedDispatcher(
2323
private val dispatcher: CoroutineDispatcher,
24-
private val parallelism: Int
24+
private val parallelism: Int,
25+
private val name: String?
2526
) : CoroutineDispatcher(), Delay by (dispatcher as? Delay ?: DefaultDelay) {
2627

2728
// Atomic is necessary here for the sake of K/N memory ordering,
@@ -34,10 +35,10 @@ internal class LimitedDispatcher(
3435
private val workerAllocationLock = SynchronizedObject()
3536

3637
@ExperimentalCoroutinesApi
37-
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
38+
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher {
3839
parallelism.checkParallelism()
3940
if (parallelism >= this.parallelism) return this
40-
return super.limitedParallelism(parallelism)
41+
return super.limitedParallelism(parallelism, name)
4142
}
4243

4344
override fun dispatch(context: CoroutineContext, block: Runnable) {
@@ -95,7 +96,7 @@ internal class LimitedDispatcher(
9596
}
9697
}
9798

98-
override fun toString() = "$dispatcher.limitedParallelism($parallelism)"
99+
override fun toString() = name ?: "$dispatcher.limitedParallelism($parallelism)"
99100

100101
/**
101102
* A worker that polls the queue and runs tasks until there are no more of them.

kotlinx-coroutines-core/jsAndWasmShared/src/internal/JSDispatcher.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal abstract class SetTimeoutBasedDispatcher: CoroutineDispatcher(), Delay
3030

3131
abstract fun scheduleQueueProcessing()
3232

33-
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
33+
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher {
3434
parallelism.checkParallelism()
3535
return this
3636
}

kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private class MissingMainCoroutineDispatcher(
9191
override fun isDispatchNeeded(context: CoroutineContext): Boolean =
9292
missing()
9393

94-
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher =
94+
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher =
9595
missing()
9696

9797
override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle =

kotlinx-coroutines-core/jvm/src/scheduling/Dispatcher.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ internal object DefaultScheduler : SchedulerCoroutineDispatcher(
1212
) {
1313

1414
@ExperimentalCoroutinesApi
15-
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
15+
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher {
1616
parallelism.checkParallelism()
1717
if (parallelism >= CORE_POOL_SIZE) return this
18-
return super.limitedParallelism(parallelism)
18+
return super.limitedParallelism(parallelism, name)
1919
}
2020

2121
// Shuts down the dispatcher, used only by Dispatchers.shutdown()
@@ -44,10 +44,10 @@ private object UnlimitedIoScheduler : CoroutineDispatcher() {
4444
}
4545

4646
@ExperimentalCoroutinesApi
47-
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
47+
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher {
4848
parallelism.checkParallelism()
4949
if (parallelism >= MAX_POOL_SIZE) return this
50-
return super.limitedParallelism(parallelism)
50+
return super.limitedParallelism(parallelism, name)
5151
}
5252

5353
// This name only leaks to user code as part of .limitedParallelism machinery
@@ -72,9 +72,9 @@ internal object DefaultIoScheduler : ExecutorCoroutineDispatcher(), Executor {
7272
override fun execute(command: java.lang.Runnable) = dispatch(EmptyCoroutineContext, command)
7373

7474
@ExperimentalCoroutinesApi
75-
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
75+
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher {
7676
// See documentation to Dispatchers.IO for the rationale
77-
return UnlimitedIoScheduler.limitedParallelism(parallelism)
77+
return UnlimitedIoScheduler.limitedParallelism(parallelism, name)
7878
}
7979

8080
override fun dispatch(context: CoroutineContext, block: Runnable) {

kotlinx-coroutines-core/jvm/test/DispatchersToStringTest.kt

+5
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@ class DispatchersToStringTest {
1818
assertEquals("Dispatchers.Default.limitedParallelism(2)", Dispatchers.Default.limitedParallelism(2).toString())
1919
// Not overridden at all, limited parallelism returns `this`
2020
assertEquals("DefaultExecutor", (DefaultDelay as CoroutineDispatcher).limitedParallelism(42).toString())
21+
22+
assertEquals("filesDispatcher", Dispatchers.IO.limitedParallelism(1, "filesDispatcher").toString())
23+
assertEquals("json", Dispatchers.Default.limitedParallelism(2, "json").toString())
24+
// Not overridden at all, limited parallelism returns `this`
25+
assertEquals("DefaultExecutor", (DefaultDelay as CoroutineDispatcher).limitedParallelism(42, "ignored").toString())
2126
}
2227
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ internal object DefaultIoScheduler : CoroutineDispatcher() {
2828
private val io = unlimitedPool.limitedParallelism(64) // Default JVM size
2929

3030
@ExperimentalCoroutinesApi
31-
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
31+
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher {
3232
// See documentation to Dispatchers.IO for the rationale
33-
return unlimitedPool.limitedParallelism(parallelism)
33+
return unlimitedPool.limitedParallelism(parallelism, name)
3434
}
3535

3636
override fun dispatch(context: CoroutineContext, block: Runnable) {

0 commit comments

Comments
 (0)