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

Leverage polymorphic keys un CoroutineDispatcher and ExecutorCoroutin… #1840

Merged
merged 1 commit into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
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
Leverage polymorphic keys un CoroutineDispatcher and ExecutorCoroutin…
…eDispatcher
  • Loading branch information
qwwdfsad committed Mar 4, 2020
commit cc6e22c90c61e93054b6a714959c0255b8473d0a
8 changes: 8 additions & 0 deletions kotlinx-coroutines-core/api/kotlinx-coroutines-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public final class kotlinx/coroutines/CoroutineContextKt {
}

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

public final class kotlinx/coroutines/CoroutineDispatcher$Key : kotlin/coroutines/AbstractCoroutineContextKey {
}

public abstract interface class kotlinx/coroutines/CoroutineExceptionHandler : kotlin/coroutines/CoroutineContext$Element {
public static final field Key Lkotlinx/coroutines/CoroutineExceptionHandler$Key;
public abstract fun handleException (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Throwable;)V
Expand Down Expand Up @@ -294,11 +298,15 @@ public final class kotlinx/coroutines/ExceptionsKt {
}

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

public final class kotlinx/coroutines/ExecutorCoroutineDispatcher$Key : kotlin/coroutines/AbstractCoroutineContextKey {
}

public final class kotlinx/coroutines/ExecutorsKt {
public static final fun asExecutor (Lkotlinx/coroutines/CoroutineDispatcher;)Ljava/util/concurrent/Executor;
public static final fun from (Ljava/util/concurrent/Executor;)Lkotlinx/coroutines/CoroutineDispatcher;
Expand Down
6 changes: 6 additions & 0 deletions kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ import kotlin.coroutines.*
public abstract class CoroutineDispatcher :
AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {

/** @suppress */
@ExperimentalStdlibApi
public companion object Key : AbstractCoroutineContextKey<ContinuationInterceptor, CoroutineDispatcher>(
ContinuationInterceptor,
{ it as? CoroutineDispatcher })

/**
* Returns `true` if the execution of the coroutine should be performed with [dispatch] method.
* The default behavior for most dispatchers is to return `true`.
Expand Down
18 changes: 12 additions & 6 deletions kotlinx-coroutines-core/jvm/src/Executors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package kotlinx.coroutines

import kotlinx.coroutines.internal.*
import java.io.Closeable
import java.io.*
import java.util.concurrent.*
import kotlin.coroutines.*

Expand All @@ -17,17 +17,23 @@ import kotlin.coroutines.*
* asynchronous API which requires instance of the [Executor].
*/
public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closeable {
/** @suppress */
@ExperimentalStdlibApi
public companion object Key : AbstractCoroutineContextKey<CoroutineDispatcher, ExecutorCoroutineDispatcher>(
CoroutineDispatcher,
{ it as? ExecutorCoroutineDispatcher })

/**
* Underlying executor of current [CoroutineDispatcher].
*/
public abstract val executor: Executor

/**
* Closes this coroutine dispatcher and shuts down its executor.
*
* It may throw an exception if this dispatcher is global and cannot be closed.
*/
public abstract override fun close()

/**
* Underlying executor of current [CoroutineDispatcher].
*/
public abstract val executor: Executor
}

/**
Expand Down
52 changes: 52 additions & 0 deletions kotlinx-coroutines-core/jvm/test/DispatcherKeyTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines

import org.junit.Test
import kotlin.coroutines.*
import kotlin.test.*

@UseExperimental(ExperimentalStdlibApi::class)
class DispatcherKeyTest : TestBase() {

companion object CustomInterceptor : AbstractCoroutineContextElement(ContinuationInterceptor),
ContinuationInterceptor {
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> {
return continuation
}
}

private val name = CoroutineName("test")

@Test
fun testDispatcher() {
val context = name + CustomInterceptor
assertNull(context[CoroutineDispatcher])
assertSame(CustomInterceptor, context[ContinuationInterceptor])

val updated = context + Dispatchers.Main
val result: CoroutineDispatcher? = updated[CoroutineDispatcher]
assertSame(Dispatchers.Main, result)
assertSame(Dispatchers.Main, updated[ContinuationInterceptor])
assertEquals(name, updated.minusKey(CoroutineDispatcher))
assertEquals(name, updated.minusKey(ContinuationInterceptor))
}

@Test
fun testExecutorCoroutineDispatcher() {
val context = name + CustomInterceptor
assertNull(context[ExecutorCoroutineDispatcher])
val updated = context + Dispatchers.Main
assertNull(updated[ExecutorCoroutineDispatcher])
val executor = Dispatchers.Default
val updated2 = updated + executor
assertSame(Dispatchers.Default, updated2[ContinuationInterceptor])
assertSame(Dispatchers.Default, updated2[CoroutineDispatcher])
assertSame(Dispatchers.Default as ExecutorCoroutineDispatcher, updated2[ExecutorCoroutineDispatcher])
assertEquals(name, updated2.minusKey(ContinuationInterceptor))
assertEquals(name, updated2.minusKey(CoroutineDispatcher))
assertEquals(name, updated2.minusKey(ExecutorCoroutineDispatcher))
}
}