Skip to content

Commit 214f156

Browse files
authored
Leverage polymorphic keys un CoroutineDispatcher and ExecutorCoroutineDispatcher (#1840)
1 parent 8d8a8fb commit 214f156

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

Diff for: kotlinx-coroutines-core/api/kotlinx-coroutines-core.api

+8
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public final class kotlinx/coroutines/CoroutineContextKt {
151151
}
152152

153153
public abstract class kotlinx/coroutines/CoroutineDispatcher : kotlin/coroutines/AbstractCoroutineContextElement, kotlin/coroutines/ContinuationInterceptor {
154+
public static final field Key Lkotlinx/coroutines/CoroutineDispatcher$Key;
154155
public fun <init> ()V
155156
public abstract fun dispatch (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V
156157
public fun dispatchYield (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V
@@ -163,6 +164,9 @@ public abstract class kotlinx/coroutines/CoroutineDispatcher : kotlin/coroutines
163164
public fun toString ()Ljava/lang/String;
164165
}
165166

167+
public final class kotlinx/coroutines/CoroutineDispatcher$Key : kotlin/coroutines/AbstractCoroutineContextKey {
168+
}
169+
166170
public abstract interface class kotlinx/coroutines/CoroutineExceptionHandler : kotlin/coroutines/CoroutineContext$Element {
167171
public static final field Key Lkotlinx/coroutines/CoroutineExceptionHandler$Key;
168172
public abstract fun handleException (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Throwable;)V
@@ -294,11 +298,15 @@ public final class kotlinx/coroutines/ExceptionsKt {
294298
}
295299

296300
public abstract class kotlinx/coroutines/ExecutorCoroutineDispatcher : kotlinx/coroutines/CoroutineDispatcher, java/io/Closeable {
301+
public static final field Key Lkotlinx/coroutines/ExecutorCoroutineDispatcher$Key;
297302
public fun <init> ()V
298303
public abstract fun close ()V
299304
public abstract fun getExecutor ()Ljava/util/concurrent/Executor;
300305
}
301306

307+
public final class kotlinx/coroutines/ExecutorCoroutineDispatcher$Key : kotlin/coroutines/AbstractCoroutineContextKey {
308+
}
309+
302310
public final class kotlinx/coroutines/ExecutorsKt {
303311
public static final fun asExecutor (Lkotlinx/coroutines/CoroutineDispatcher;)Ljava/util/concurrent/Executor;
304312
public static final fun from (Ljava/util/concurrent/Executor;)Lkotlinx/coroutines/CoroutineDispatcher;

Diff for: kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ import kotlin.coroutines.*
3030
public abstract class CoroutineDispatcher :
3131
AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {
3232

33+
/** @suppress */
34+
@ExperimentalStdlibApi
35+
public companion object Key : AbstractCoroutineContextKey<ContinuationInterceptor, CoroutineDispatcher>(
36+
ContinuationInterceptor,
37+
{ it as? CoroutineDispatcher })
38+
3339
/**
3440
* Returns `true` if the execution of the coroutine should be performed with [dispatch] method.
3541
* The default behavior for most dispatchers is to return `true`.

Diff for: kotlinx-coroutines-core/jvm/src/Executors.kt

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package kotlinx.coroutines
66

77
import kotlinx.coroutines.internal.*
8-
import java.io.Closeable
8+
import java.io.*
99
import java.util.concurrent.*
1010
import kotlin.coroutines.*
1111

@@ -17,17 +17,23 @@ import kotlin.coroutines.*
1717
* asynchronous API which requires instance of the [Executor].
1818
*/
1919
public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closeable {
20+
/** @suppress */
21+
@ExperimentalStdlibApi
22+
public companion object Key : AbstractCoroutineContextKey<CoroutineDispatcher, ExecutorCoroutineDispatcher>(
23+
CoroutineDispatcher,
24+
{ it as? ExecutorCoroutineDispatcher })
25+
26+
/**
27+
* Underlying executor of current [CoroutineDispatcher].
28+
*/
29+
public abstract val executor: Executor
30+
2031
/**
2132
* Closes this coroutine dispatcher and shuts down its executor.
2233
*
2334
* It may throw an exception if this dispatcher is global and cannot be closed.
2435
*/
2536
public abstract override fun close()
26-
27-
/**
28-
* Underlying executor of current [CoroutineDispatcher].
29-
*/
30-
public abstract val executor: Executor
3137
}
3238

3339
/**
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2016-2020 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+
import org.junit.Test
8+
import kotlin.coroutines.*
9+
import kotlin.test.*
10+
11+
@UseExperimental(ExperimentalStdlibApi::class)
12+
class DispatcherKeyTest : TestBase() {
13+
14+
companion object CustomInterceptor : AbstractCoroutineContextElement(ContinuationInterceptor),
15+
ContinuationInterceptor {
16+
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> {
17+
return continuation
18+
}
19+
}
20+
21+
private val name = CoroutineName("test")
22+
23+
@Test
24+
fun testDispatcher() {
25+
val context = name + CustomInterceptor
26+
assertNull(context[CoroutineDispatcher])
27+
assertSame(CustomInterceptor, context[ContinuationInterceptor])
28+
29+
val updated = context + Dispatchers.Main
30+
val result: CoroutineDispatcher? = updated[CoroutineDispatcher]
31+
assertSame(Dispatchers.Main, result)
32+
assertSame(Dispatchers.Main, updated[ContinuationInterceptor])
33+
assertEquals(name, updated.minusKey(CoroutineDispatcher))
34+
assertEquals(name, updated.minusKey(ContinuationInterceptor))
35+
}
36+
37+
@Test
38+
fun testExecutorCoroutineDispatcher() {
39+
val context = name + CustomInterceptor
40+
assertNull(context[ExecutorCoroutineDispatcher])
41+
val updated = context + Dispatchers.Main
42+
assertNull(updated[ExecutorCoroutineDispatcher])
43+
val executor = Dispatchers.Default
44+
val updated2 = updated + executor
45+
assertSame(Dispatchers.Default, updated2[ContinuationInterceptor])
46+
assertSame(Dispatchers.Default, updated2[CoroutineDispatcher])
47+
assertSame(Dispatchers.Default as ExecutorCoroutineDispatcher, updated2[ExecutorCoroutineDispatcher])
48+
assertEquals(name, updated2.minusKey(ContinuationInterceptor))
49+
assertEquals(name, updated2.minusKey(CoroutineDispatcher))
50+
assertEquals(name, updated2.minusKey(ExecutorCoroutineDispatcher))
51+
}
52+
}

0 commit comments

Comments
 (0)