releaseInterceptedContinuation and few other minor fixes

This commit is contained in:
Roman Elizarov
2018-06-22 18:34:06 +03:00
committed by Denis Zharkov
parent 8e2fae3322
commit b06d626b21
4 changed files with 13 additions and 7 deletions
@@ -154,6 +154,7 @@ public actual fun <T> Continuation<T>.intercepted(): Continuation<T> =
// INTERNAL DEFINITIONS
@SinceKotlin("1.3")
private inline fun <T> createCoroutineFromSuspendFunction(
completion: Continuation<T>,
crossinline block: () -> Any?
@@ -81,18 +81,18 @@ internal abstract class ContinuationImpl protected constructor(
try {
val outcome = invokeSuspend(result)
if (outcome === CoroutineSingletons.COROUTINE_SUSPENDED) return
disposeIntercepted()
releaseIntercepted()
completion.resume(outcome)
} catch (exception: Throwable) {
disposeIntercepted()
releaseIntercepted()
completion.resumeWithException(exception)
}
}
private fun disposeIntercepted() {
private fun releaseIntercepted() {
val intercepted = intercepted
if (intercepted != null && intercepted != this) {
context[ContinuationInterceptor]!!.disposeContinuation(intercepted)
context[ContinuationInterceptor]!!.releaseInterceptedContinuation(intercepted)
}
this.intercepted = CompletedContinuation // just in case
}
@@ -113,6 +113,8 @@ internal object CompletedContinuation : Continuation<Any?> {
}
}
@SinceKotlin("1.3")
// Restricted suspension lambdas inherit from this class
internal abstract class RestrictedSuspendLambda protected constructor(
private val arity: Int,
completion: Continuation<Any?>?
@@ -128,6 +130,8 @@ internal abstract class RestrictedSuspendLambda protected constructor(
super.toString() // this is continuation
}
@SinceKotlin("1.3")
// Suspension lambdas inherit from this class
internal abstract class SuspendLambda protected constructor(
private val arity: Int,
completion: Continuation<Any?>?
@@ -3,6 +3,7 @@
* that can be found in the license/LICENSE.txt file.
*/
// todo: Figure out how to avoid supressing error, move suppressions where they are needed.
@file:Suppress(
"UNCHECKED_CAST",
"RedundantVisibilityModifier",
@@ -22,9 +22,9 @@ public interface ContinuationInterceptor : CoroutineContext.Element {
* This function is invoked by coroutines framework when needed and the resulting continuations are
* cached internally per each instance of the original [continuation].
*
* This function may simply return `this` if it does not want to intercept this particular continuation.
* This function may simply return original [continuation] if it does not want to intercept this particular continuation.
*
* When the original [continuation] completes coroutine framework invokes [disposeContinuation]
* When the original [continuation] completes coroutine framework invokes [releaseInterceptedContinuation]
* with the resulting continuation if it was intercepted.
*/
public fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T>
@@ -37,7 +37,7 @@ public interface ContinuationInterceptor : CoroutineContext.Element {
*
* @param continuation Continuation instance returned by this interceptor's [interceptContinuation] invocation.
*/
public fun disposeContinuation(continuation: Continuation<*>) {
public fun releaseInterceptedContinuation(continuation: Continuation<*>) {
/* do nothing by default */
}
}