Skip to content
Open
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
4 changes: 4 additions & 0 deletions kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public abstract class CoroutineDispatcher :
AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {

/** @suppress */
@Deprecated("Use ContinuationInterceptor.Key and attempt " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a bit of a nuisance.

I would consider coupling this change with introduction of .dispatcher and .dispatcherOrNull extensions on CoroutineContext (not sure about the CoroutineScope).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean, but this pattern is very niche anyway: https://grep.app/search?f.lang=Kotlin&regexp=true&q=%5B%5E+%5D%5C%5BCoroutineDispatcher%5C%5D

In addition, in most cases, using ContinuationInterceptor instead of CoroutineDispatcher doesn't make any functional difference (or even makes the code a bit more flexible), which means directing the few existing cases to .dispatcher is not ideal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've figured, but still see it as a good idea:

  • That's, generally, an informal respect to backwards compatibility and migration paths (i.e. not how we did it with capitalize) for users who do use that
  • The cost of this addition is negligible
  • In the spirit of the standard library, it is a small but really convenient addition when you need it. I believe even I was in need of that quite a few times (I was also pretty sure I created an issue for that, but I failed to find it)

"casting the context element to CoroutineDispatcher instead",
level = DeprecationLevel.WARNING)
// WARNING since 1.11, ERROR since 1.12, remove in 1.13
@ExperimentalStdlibApi
public companion object Key : AbstractCoroutineContextKey<ContinuationInterceptor, CoroutineDispatcher>(
ContinuationInterceptor,
Expand Down
1 change: 0 additions & 1 deletion kotlinx-coroutines-core/common/src/flow/SharingStarted.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ private class StartedWhileSubscribed(
.dropWhile { it != SharingCommand.START } // don't emit any STOP/RESET_BUFFER to start with, only START
.distinctUntilChanged() // just in case somebody forgets it, don't leak our multiple sending of START

@OptIn(ExperimentalStdlibApi::class)
override fun toString(): String {
val params = buildList(2) {
if (stopTimeout > 0) add("stopTimeout=${stopTimeout}ms")
Expand Down
6 changes: 2 additions & 4 deletions kotlinx-coroutines-core/common/src/selects/SelectOld.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,17 @@ internal suspend inline fun <R> selectUnbiasedOld(crossinline builder: SelectBui
scope.initSelectResult()
}

@OptIn(ExperimentalStdlibApi::class)
private fun <T> CancellableContinuation<T>.resumeUndispatched(result: T) {
val dispatcher = context[CoroutineDispatcher]
val dispatcher = context[ContinuationInterceptor] as? CoroutineDispatcher
if (dispatcher != null) {
dispatcher.resumeUndispatched(result)
} else {
resume(result)
}
}

@OptIn(ExperimentalStdlibApi::class)
private fun CancellableContinuation<*>.resumeUndispatchedWithException(exception: Throwable) {
val dispatcher = context[CoroutineDispatcher]
val dispatcher = context[ContinuationInterceptor] as? CoroutineDispatcher
if (dispatcher != null) {
dispatcher.resumeUndispatchedWithException(exception)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class ShareInTest : TestBase() {
fun testWhileSubscribedCustomAtLeast2() =
testWhileSubscribed(2, SharingStarted.WhileSubscribedAtLeast(2))

@OptIn(ExperimentalStdlibApi::class)
private fun testWhileSubscribed(threshold: Int, started: SharingStarted) = runTest {
expect(1)
val flowState = FlowState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ class SharedFlowScenarioTest : TestBase() {
private data class ResumeCollecting(val job: TestJob) : Action()
private data class Cancelled(val job: TestJob) : Action()

@OptIn(ExperimentalStdlibApi::class)
private class ScenarioDsl<T>(
val sharedFlow: MutableSharedFlow<T>,
coroutineContext: CoroutineContext
Expand Down
4 changes: 4 additions & 0 deletions kotlinx-coroutines-core/jvm/src/Executors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import kotlin.AutoCloseable
*/
public abstract class ExecutorCoroutineDispatcher : CoroutineDispatcher(), Closeable, AutoCloseable {
/** @suppress */
@Deprecated("Use ContinuationInterceptor.Key and attempt " +
"casting the context element to ExecutorCoroutineDispatcher instead",
level = DeprecationLevel.WARNING)
@ExperimentalStdlibApi
@Suppress("DEPRECATION")
public companion object Key : AbstractCoroutineContextKey<CoroutineDispatcher, ExecutorCoroutineDispatcher>(
CoroutineDispatcher,
{ it as? ExecutorCoroutineDispatcher })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ internal object DebugProbesImpl {
* Internal (JVM-public) method used by IDEA debugger as of 1.6.0-RC.
* See KTIJ-24102.
*/
@OptIn(ExperimentalStdlibApi::class)
fun dumpCoroutinesInfoAsJsonAndReferences(): Array<Any> {
val coroutinesInfo = dumpCoroutinesInfo()
val size = coroutinesInfo.size
Expand All @@ -186,7 +185,7 @@ internal object DebugProbesImpl {
for (info in coroutinesInfo) {
val context = info.context
val name = context[CoroutineName.Key]?.name?.toStringRepr()
val dispatcher = context[CoroutineDispatcher.Key]?.toStringRepr()
val dispatcher = context[ContinuationInterceptor.Key]?.toStringRepr()
coroutinesInfoAsJson.add(
"""
{
Expand Down
5 changes: 2 additions & 3 deletions kotlinx-coroutines-core/jvm/test/DispatchersToStringTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
@file:OptIn(ExperimentalStdlibApi::class)

package kotlinx.coroutines

import kotlinx.coroutines.scheduling.CORE_POOL_SIZE
import kotlinx.coroutines.scheduling.MAX_POOL_SIZE
import kotlin.coroutines.ContinuationInterceptor
import kotlin.test.*

class DispatchersToStringTest {
Expand Down Expand Up @@ -44,7 +43,7 @@ class DispatchersToStringTest {
assertEquals("12", limitedNamed.limitedParallelism(12, "12").toString())

runBlocking {
val d = coroutineContext[CoroutineDispatcher]!!
val d = coroutineContext[ContinuationInterceptor] as CoroutineDispatcher
assertContains(d.toString(), "BlockingEventLoop")
val limited = d.limitedParallelism(2)
assertContains(limited.toString(), "BlockingEventLoop")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ internal actual fun ensurePlatformExceptionHandlerLoaded(callback: CoroutineExce
}
}

@OptIn(ExperimentalStdlibApi::class)
internal actual fun propagateExceptionFinalResort(exception: Throwable) {
// log exception
processUnhandledException(exception)
Expand Down
1 change: 0 additions & 1 deletion kotlinx-coroutines-core/nativeOther/src/Dispatchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ internal actual fun createDefaultDispatcher(): CoroutineDispatcher = DefaultDisp

private object DefaultDispatcher : CoroutineDispatcher() {
// Be consistent with JVM -- at least 2 threads to provide some liveness guarantees in case of improper uses
@OptIn(ExperimentalStdlibApi::class)
private val ctx = newFixedThreadPoolContext(Platform.getAvailableProcessors().coerceAtLeast(2), "Dispatchers.Default")

override fun dispatch(context: CoroutineContext, block: Runnable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package kotlinx.coroutines.debug

import kotlinx.coroutines.testing.*
import com.google.gson.*
import kotlinx.coroutines.*
import kotlinx.coroutines.debug.internal.*
import org.junit.Test
import kotlin.coroutines.*
import kotlin.test.*

@ExperimentalStdlibApi
class DumpCoroutineInfoAsJsonAndReferencesTest : DebugTestBase() {
private data class CoroutineInfoFromJson(
val name: String?,
Expand Down Expand Up @@ -93,7 +91,7 @@ class DumpCoroutineInfoAsJsonAndReferencesTest : DebugTestBase() {
val context = info.context
assertEquals(context[CoroutineName.Key]?.name, infoFromJson.name)
assertEquals(context[CoroutineId.Key]?.id, infoFromJson.id)
assertEquals(context[CoroutineDispatcher.Key]?.toString(), infoFromJson.dispatcher)
assertEquals(context[ContinuationInterceptor.Key]?.toString(), infoFromJson.dispatcher)
}
}
}