Skip to content

Commit

Permalink
Remove some constant parameters and simplify the implementation (#4183)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhalanskyjb authored Jul 29, 2024
1 parent b28426e commit ab279a7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 42 deletions.
18 changes: 3 additions & 15 deletions kotlinx-coroutines-core/common/src/CompletionState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@ import kotlinx.coroutines.internal.*
import kotlin.coroutines.*
import kotlin.jvm.*

internal fun <T> Result<T>.toState(
onCancellation: ((cause: Throwable) -> Unit)? = null
): Any? = fold(
onSuccess = { if (onCancellation != null) CompletedWithCancellation(it, onCancellation) else it },
onFailure = { CompletedExceptionally(it) }
)
internal fun <T> Result<T>.toState(): Any? = getOrElse { CompletedExceptionally(it) }

internal fun <T> Result<T>.toState(caller: CancellableContinuation<*>): Any? = fold(
onSuccess = { it },
onFailure = { CompletedExceptionally(recoverStackTrace(it, caller)) }
)
internal fun <T> Result<T>.toState(caller: CancellableContinuation<*>): Any? =
getOrElse { CompletedExceptionally(recoverStackTrace(it, caller)) }

@Suppress("RESULT_CLASS_IN_RETURN_TYPE", "UNCHECKED_CAST")
internal fun <T> recoverResult(state: Any?, uCont: Continuation<T>): Result<T> =
Expand All @@ -24,11 +17,6 @@ internal fun <T> recoverResult(state: Any?, uCont: Continuation<T>): Result<T> =
else
Result.success(state as T)

internal data class CompletedWithCancellation(
@JvmField val result: Any?,
@JvmField val onCancellation: (cause: Throwable) -> Unit
)

/**
* Class for an internal state of a job that was cancelled (completed exceptionally).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,14 @@ internal class DispatchedContinuation<in T>(
get() = this

override fun resumeWith(result: Result<T>) {
val context = continuation.context
val state = result.toState()
if (dispatcher.isDispatchNeeded(context)) {
_state = state
resumeMode = MODE_ATOMIC
dispatcher.dispatch(context, this)
} else {
executeUnconfined(state, MODE_ATOMIC) {
withCoroutineContext(this.context, countOrElement) {
withCoroutineContext(context, countOrElement) {
continuation.resumeWith(result)
}
}
Expand All @@ -204,11 +203,8 @@ internal class DispatchedContinuation<in T>(
// We inline it to save an entry on the stack in cases where it shows (unconfined dispatcher)
// It is used only in Continuation<T>.resumeCancellableWith
@Suppress("NOTHING_TO_INLINE")
internal inline fun resumeCancellableWith(
result: Result<T>,
noinline onCancellation: ((cause: Throwable) -> Unit)?
) {
val state = result.toState(onCancellation)
internal inline fun resumeCancellableWith(result: Result<T>) {
val state = result.toState()
if (dispatcher.isDispatchNeeded(context)) {
_state = state
resumeMode = MODE_CANCELLABLE
Expand All @@ -222,15 +218,6 @@ internal class DispatchedContinuation<in T>(
}
}

// takeState had already cleared the state so we cancel takenState here
override fun cancelCompletedResult(takenState: Any?, cause: Throwable) {
// It is Ok to call onCancellation here without try/catch around it, since this function only faces
// a "bound" cancellation handler that performs the safe call to the user-specified code.
if (takenState is CompletedWithCancellation) {
takenState.onCancellation(cause)
}
}

// inline here is to save us an entry on the stack for the sake of better stacktraces
@Suppress("NOTHING_TO_INLINE")
internal inline fun resumeCancelled(state: Any?): Boolean {
Expand Down Expand Up @@ -271,9 +258,8 @@ internal class DispatchedContinuation<in T>(
@InternalCoroutinesApi
public fun <T> Continuation<T>.resumeCancellableWith(
result: Result<T>,
onCancellation: ((cause: Throwable) -> Unit)? = null
): Unit = when (this) {
is DispatchedContinuation -> resumeCancellableWith(result, onCancellation)
is DispatchedContinuation -> resumeCancellableWith(result)
else -> resumeWith(result)
}

Expand Down
8 changes: 3 additions & 5 deletions kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ public fun <T> (suspend () -> T).startCoroutineCancellable(completion: Continuat
*/
internal fun <R, T> (suspend (R) -> T).startCoroutineCancellable(
receiver: R, completion: Continuation<T>,
onCancellation: ((cause: Throwable) -> Unit)? = null
) =
runSafely(completion) {
createCoroutineUnintercepted(receiver, completion).intercepted().resumeCancellableWith(Result.success(Unit), onCancellation)
}
) = runSafely(completion) {
createCoroutineUnintercepted(receiver, completion).intercepted().resumeCancellableWith(Result.success(Unit))
}

/**
* Similar to [startCoroutineCancellable], but for already created coroutine.
Expand Down
4 changes: 0 additions & 4 deletions kotlinx-coroutines-debug/test/CoroutinesDumpTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class CoroutinesDumpTest : DebugTestBase() {
"\tat _COROUTINE._CREATION._(CoroutineDebugging.kt)\n" +
"\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt)\n" +
"\tat kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt)\n" +
"\tat kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable\$default(Cancellable.kt)\n" +
"\tat kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt)\n",
ignoredCoroutine = "BlockingCoroutine"
) {
Expand Down Expand Up @@ -60,7 +59,6 @@ class CoroutinesDumpTest : DebugTestBase() {
"\tat _COROUTINE._CREATION._(CoroutineDebugging.kt)\n" +
"\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt)\n" +
"\tat kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt)\n" +
"\tat kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable\$default(Cancellable.kt)\n" +
"\tat kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt)\n" +
"\tat kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt)\n" +
"\tat kotlinx.coroutines.BuildersKt__Builders_commonKt.async(Builders.common.kt)\n" +
Expand Down Expand Up @@ -91,7 +89,6 @@ class CoroutinesDumpTest : DebugTestBase() {
"\tat _COROUTINE._CREATION._(CoroutineDebugging.kt)\n" +
"\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt)\n" +
"\tat kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt)\n" +
"\tat kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable\$default(Cancellable.kt)\n" +
"\tat kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt)\n" +
"\tat kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt)\n" +
"\tat kotlinx.coroutines.BuildersKt__Builders_commonKt.async(Builders.common.kt)\n" +
Expand Down Expand Up @@ -145,7 +142,6 @@ class CoroutinesDumpTest : DebugTestBase() {
val expected =
"kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt)\n" +
"kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt)\n" +
"kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable\$default(Cancellable.kt)\n" +
"kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt)\n" +
"kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt)\n" +
"kotlinx.coroutines.BuildersKt__Builders_commonKt.async(Builders.common.kt)\n" +
Expand Down

0 comments on commit ab279a7

Please sign in to comment.