Skip to content

Commit

Permalink
~ Refactor to Channel.onElementCancel lambda
Browse files Browse the repository at this point in the history
Fixes #1936
  • Loading branch information
elizarov committed Apr 22, 2020
1 parent b19b38a commit 77a1543
Show file tree
Hide file tree
Showing 31 changed files with 199 additions and 210 deletions.
20 changes: 5 additions & 15 deletions kotlinx-coroutines-core/api/kotlinx-coroutines-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract interface class kotlinx/coroutines/CancellableContinuation : kot
public abstract fun resumeUndispatched (Lkotlinx/coroutines/CoroutineDispatcher;Ljava/lang/Object;)V
public abstract fun resumeUndispatchedWithException (Lkotlinx/coroutines/CoroutineDispatcher;Ljava/lang/Throwable;)V
public abstract fun tryResume (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
public abstract fun tryResumeAtomic (Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public abstract fun tryResume (Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public abstract fun tryResumeWithException (Ljava/lang/Throwable;)Ljava/lang/Object;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public class kotlinx/coroutines/CancellableContinuationImpl : kotlin/coroutines/
public fun resumeWith (Ljava/lang/Object;)V
public fun toString ()Ljava/lang/String;
public fun tryResume (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
public fun tryResumeAtomic (Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public fun tryResume (Ljava/lang/Object;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public fun tryResumeWithException (Ljava/lang/Throwable;)Ljava/lang/Object;
}

Expand Down Expand Up @@ -271,11 +271,6 @@ public final class kotlinx/coroutines/DelayKt {
public static final fun delay-p9JZ4hM (DLkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class kotlinx/coroutines/DispatchedContinuationKt {
public static final fun resumeCancellableWith (Lkotlin/coroutines/Continuation;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V
public static synthetic fun resumeCancellableWith$default (Lkotlin/coroutines/Continuation;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
}

public final class kotlinx/coroutines/Dispatchers {
public static final field INSTANCE Lkotlinx/coroutines/Dispatchers;
public static final fun getDefault ()Lkotlinx/coroutines/CoroutineDispatcher;
Expand Down Expand Up @@ -487,13 +482,6 @@ public final class kotlinx/coroutines/ParentJob$DefaultImpls {
public static fun plus (Lkotlinx/coroutines/ParentJob;Lkotlinx/coroutines/Job;)Lkotlinx/coroutines/Job;
}

public final class kotlinx/coroutines/Resource {
public fun <init> (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V
public final fun cancel ()V
public final fun getValue ()Ljava/lang/Object;
public final fun isCancelled ()Z
}

public final class kotlinx/coroutines/RunnableKt {
public static final fun Runnable (Lkotlin/jvm/functions/Function0;)Ljava/lang/Runnable;
}
Expand Down Expand Up @@ -613,8 +601,10 @@ public final class kotlinx/coroutines/channels/ChannelIterator$DefaultImpls {
}

public final class kotlinx/coroutines/channels/ChannelKt {
public static final fun Channel (I)Lkotlinx/coroutines/channels/Channel;
public static final synthetic fun Channel (I)Lkotlinx/coroutines/channels/Channel;
public static final fun Channel (ILkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/channels/Channel;
public static synthetic fun Channel$default (IILjava/lang/Object;)Lkotlinx/coroutines/channels/Channel;
public static synthetic fun Channel$default (ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/coroutines/channels/Channel;
}

public final class kotlinx/coroutines/channels/ChannelsKt {
Expand Down
4 changes: 2 additions & 2 deletions kotlinx-coroutines-core/common/src/CancellableContinuation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public interface CancellableContinuation<in T> : Continuation<T> {
* guaranteed can be provided by having a cancellation fallback.
*/
@InternalCoroutinesApi
public fun tryResumeAtomic(value: T, idempotent: Any?, onCancellation: ((cause: Throwable) -> Unit)?): Any?
public fun tryResume(value: T, idempotent: Any?, onCancellation: ((cause: Throwable) -> Unit)?): Any?

/**
* Tries to resume this continuation with the specified [exception] and returns a non-null object token if successful,
Expand Down Expand Up @@ -197,7 +197,7 @@ public interface CancellableContinuation<in T> : Continuation<T> {
* There is no guarantee on the execution context of its invocation.
*/
@ExperimentalCoroutinesApi // since 1.2.0, tentatively graduates in 1.3.0
public fun resume(value: T, onCancellation: (cause: Throwable) -> Unit)
public fun resume(value: T, onCancellation: ((cause: Throwable) -> Unit)?)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ internal open class CancellableContinuationImpl<in T>(
}
}
else -> {
// completed normally without marker class, promote to CompletedContinuation to synchronize cancellation
// completed normally without marker class, promote to CompletedContinuation in case
// if invokeOnCancellation if called later
if (_state.compareAndSet(state, CompletedContinuation(state, cancelCause = cause))) {
cancelResourceIfNeeded(state) { context }
return // done
}
}
Expand Down Expand Up @@ -288,7 +288,7 @@ internal open class CancellableContinuationImpl<in T>(
override fun resumeWith(result: Result<T>) =
resumeImpl(result.toState(this), resumeMode)

override fun resume(value: T, onCancellation: (cause: Throwable) -> Unit) =
override fun resume(value: T, onCancellation: ((cause: Throwable) -> Unit)?) =
resumeImpl(value, resumeMode, onCancellation)

public override fun invokeOnCancellation(handler: CompletionHandler) {
Expand Down Expand Up @@ -398,8 +398,6 @@ internal open class CancellableContinuationImpl<in T>(
if (state.makeResumed()) { // check if trying to resume one (otherwise error)
// call onCancellation
onCancellation?.let { callOnCancellation(it, state.cause) }
// cancel resource
cancelResourceIfNeeded(state) { context }
return // done
}
}
Expand Down Expand Up @@ -460,9 +458,9 @@ internal open class CancellableContinuationImpl<in T>(

// Note: Always returns RESUME_TOKEN | null
override fun tryResume(value: T, idempotent: Any?): Any? =
tryResumeImpl(value, idempotent = idempotent, onCancellation = null)
tryResumeImpl(value, idempotent, onCancellation = null)

override fun tryResumeAtomic(value: T, idempotent: Any?, onCancellation: ((cause: Throwable) -> Unit)?): Any? =
override fun tryResume(value: T, idempotent: Any?, onCancellation: ((cause: Throwable) -> Unit)?): Any? =
tryResumeImpl(value, idempotent, onCancellation)

override fun tryResumeWithException(exception: Throwable): Any? =
Expand Down Expand Up @@ -532,6 +530,5 @@ private data class CompletedContinuation(
fun invokeHandlers(cont: CancellableContinuationImpl<*>, cause: Throwable) {
cancelHandler?.let { cont.callCancelHandler(it, cause) }
onCancellation?.let { cont.callOnCancellation(it, cause) }
cancelResourceIfNeeded(result) { cont.context }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ internal fun <T> Result<T>.toState(caller: CancellableContinuation<*>): Any? = f
onFailure = { CompletedExceptionally(recoverStackTrace(it, caller)) }
)

internal fun DispatchedContinuation<*>.cancelState(state: Any?, cause: Throwable) {
when (state) {
is CompletedWithCancellation -> {
cancelResourceIfNeeded(state.result) { context }
state.onCancellation(cause)
}
else -> cancelResourceIfNeeded(state) { context }
}
}

@Suppress("RESULT_CLASS_IN_RETURN_TYPE", "UNCHECKED_CAST")
internal fun <T> recoverResult(state: Any?, uCont: Continuation<T>): Result<T> =
if (state is CompletedExceptionally)
Expand Down
1 change: 1 addition & 0 deletions kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package kotlinx.coroutines

import kotlinx.coroutines.internal.*
import kotlin.coroutines.*

/**
Expand Down
44 changes: 0 additions & 44 deletions kotlinx-coroutines-core/common/src/Resource.common.kt.kt

This file was deleted.

1 change: 1 addition & 0 deletions kotlinx-coroutines-core/common/src/Yield.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package kotlinx.coroutines

import kotlinx.coroutines.internal.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*

Expand Down

0 comments on commit 77a1543

Please sign in to comment.