Move coroutine intrinsics to kotlin.coroutine.intrinsics package

Also rename val SUSPENDED to SUSPENDED_MARKER

 #KT-15698 Fixed
This commit is contained in:
Denis Zharkov
2017-01-13 15:43:50 +03:00
parent e0fa11b0c1
commit 2cb9d3a8ad
119 changed files with 446 additions and 348 deletions
@@ -60,40 +60,3 @@ public interface ContinuationDispatcher {
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.BINARY)
public annotation class RestrictsSuspension
/**
* Contains intrinsic functions for coroutines.
*/
@SinceKotlin("1.1")
public object CoroutineIntrinsics {
/**
* Obtains the current continuation instance inside suspend functions and either suspend
* currently running coroutine or return result immediately without suspension.
*
* If the [block] returns the special [SUSPENDED] value, it means that suspend function did suspend the execution and will
* not return any result immediately. In this case, the [Continuation] provided to the [block] shall be invoked at some moment in the
* future when the result becomes available to resume the computation.
*
* Otherwise, the return value of the [block] must have a type assignable to [T] and represents the result of this suspend function.
* It means that the execution was not suspended and the [Continuation] provided to the [block] shall not be invoked.
* As the result type of the [block] is declared as `Any?` and cannot be correctly type-checked,
* its proper return type remains on the conscience of the suspend function's author.
*
* Note that it is not recommended to call either [Continuation.resume] nor [Continuation.resumeWithException] functions synchronously
* in the same stackframe where suspension function is run. Use [suspendCoroutine] as a safer way to obtain current
* continuation instance.
*/
public inline suspend fun <T> suspendCoroutineOrReturn(
block: (Continuation<T>) -> Any?
): T = suspendWithCurrentContinuation(block)
/**
* This value is used as a return value of [suspendCoroutineOrReturn] `block` argument to state that
* the execution was suspended and will not return any result immediately.
*/
@SinceKotlin("1.1")
public val SUSPENDED: Any = Any()
}
@PublishedApi
internal inline suspend fun <T> suspendWithCurrentContinuation(body: (Continuation<T>) -> Any?): T = null!!
@@ -0,0 +1,47 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.coroutines.intrinsics
import kotlin.coroutines.Continuation
/**
* Obtains the current continuation instance inside suspend functions and either suspend
* currently running coroutine or return result immediately without suspension.
*
* If the [block] returns the special [SUSPENDED_MARKER] value, it means that suspend function did suspend the execution and will
* not return any result immediately. In this case, the [Continuation] provided to the [block] shall be invoked at some moment in the
* future when the result becomes available to resume the computation.
*
* Otherwise, the return value of the [block] must have a type assignable to [T] and represents the result of this suspend function.
* It means that the execution was not suspended and the [Continuation] provided to the [block] shall not be invoked.
* As the result type of the [block] is declared as `Any?` and cannot be correctly type-checked,
* its proper return type remains on the conscience of the suspend function's author.
*
* Note that it is not recommended to call either [Continuation.resume] nor [Continuation.resumeWithException] functions synchronously
* in the same stackframe where suspension function is run. Use [suspendCoroutine] as a safer way to obtain current
* continuation instance.
*/
@SinceKotlin("1.1")
public inline suspend fun <T> suspendCoroutineOrReturn(block: (Continuation<T>) -> Any?): T = null!!
/**
* This value is used as a return value of [suspendCoroutineOrReturn] `block` argument to state that
* the execution was suspended and will not return any result immediately.
*/
@SinceKotlin("1.1")
public val SUSPENDED_MARKER: Any = Any()
@@ -58,6 +58,7 @@ public abstract class KotlinBuiltIns {
private static final FqName ANNOTATION_PACKAGE_FQ_NAME = BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier("annotation"));
public static final FqName COLLECTIONS_PACKAGE_FQ_NAME = BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier("collections"));
public static final FqName COROUTINES_PACKAGE_FQ_NAME = BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier("coroutines"));
private static final FqName COROUTINES_INTRINSICS_PACKAGE_FQ_NAME = COROUTINES_PACKAGE_FQ_NAME.child(Name.identifier("intrinsics"));
public static final FqName RANGES_PACKAGE_FQ_NAME = BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier("ranges"));
public static final FqName TEXT_PACKAGE_FQ_NAME = BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier("text"));
@@ -68,6 +69,7 @@ public abstract class KotlinBuiltIns {
ANNOTATION_PACKAGE_FQ_NAME,
ReflectionTypesKt.getKOTLIN_REFLECT_FQ_NAME(),
COROUTINES_PACKAGE_FQ_NAME,
COROUTINES_INTRINSICS_PACKAGE_FQ_NAME,
BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier("internal"))
);
@@ -97,10 +99,12 @@ public abstract class KotlinBuiltIns {
createPackage(provider, nameToFragment, RANGES_PACKAGE_FQ_NAME);
PackageFragmentDescriptor kotlinAnnotation = createPackage(provider, nameToFragment, ANNOTATION_PACKAGE_FQ_NAME);
PackageFragmentDescriptor coroutinePackage = createPackage(provider, null, COROUTINES_PACKAGE_FQ_NAME);
PackageFragmentDescriptor coroutineIntrinsicsPackage =
createPackage(provider, null, COROUTINES_INTRINSICS_PACKAGE_FQ_NAME);
Set<PackageFragmentDescriptor> allImportedByDefault = new LinkedHashSet<PackageFragmentDescriptor>(nameToFragment.values());
return new PackageFragments(kotlin, kotlinCollections, kotlinAnnotation, coroutinePackage, allImportedByDefault);
return new PackageFragments(kotlin, kotlinCollections, kotlinAnnotation, coroutinePackage, coroutineIntrinsicsPackage, allImportedByDefault);
}
});
@@ -249,6 +253,7 @@ public abstract class KotlinBuiltIns {
public final PackageFragmentDescriptor collectionsPackageFragment;
public final PackageFragmentDescriptor annotationPackageFragment;
public final PackageFragmentDescriptor coroutinePackageFragment;
public final PackageFragmentDescriptor coroutineIntrinsicsPackage;
public final Set<PackageFragmentDescriptor> allImportedByDefaultBuiltInsPackageFragments;
private PackageFragments(
@@ -256,12 +261,14 @@ public abstract class KotlinBuiltIns {
@NotNull PackageFragmentDescriptor collectionsPackageFragment,
@NotNull PackageFragmentDescriptor annotationPackageFragment,
@NotNull PackageFragmentDescriptor coroutinePackageFragment,
@NotNull PackageFragmentDescriptor coroutineIntrinsicsPackage,
@NotNull Set<PackageFragmentDescriptor> allImportedByDefaultBuiltInsPackageFragments
) {
this.builtInsPackageFragment = builtInsPackageFragment;
this.collectionsPackageFragment = collectionsPackageFragment;
this.annotationPackageFragment = annotationPackageFragment;
this.coroutinePackageFragment = coroutinePackageFragment;
this.coroutineIntrinsicsPackage = coroutineIntrinsicsPackage;
this.allImportedByDefaultBuiltInsPackageFragments = allImportedByDefaultBuiltInsPackageFragments;
}
}
@@ -392,8 +399,8 @@ public abstract class KotlinBuiltIns {
}
@NotNull
public PackageFragmentDescriptor getBuiltInsCoroutinePackageFragment() {
return packageFragments.invoke().coroutinePackageFragment;
public PackageFragmentDescriptor getBuiltInsCoroutineIntrinsicsPackageFragment() {
return packageFragments.invoke().coroutineIntrinsicsPackage;
}
@NotNull
@@ -17,6 +17,7 @@
package kotlin.jvm.internal
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.SUSPENDED_MARKER
private const val INTERCEPT_BIT_SET = 1 shl 31
private const val INTERCEPT_BIT_CLEAR = INTERCEPT_BIT_SET.inv()
@@ -73,7 +74,7 @@ abstract class RestrictedCoroutineImpl : Lambda, Continuation<Any?> {
override fun resume(value: Any?) {
try {
val result = doResume(value, null)
if (result != CoroutineIntrinsics.SUSPENDED)
if (result != SUSPENDED_MARKER)
completion!!.resume(result)
} catch (e: Throwable) {
completion!!.resumeWithException(e)
@@ -83,7 +84,7 @@ abstract class RestrictedCoroutineImpl : Lambda, Continuation<Any?> {
override fun resumeWithException(exception: Throwable) {
try {
val result = doResume(null, exception)
if (result != CoroutineIntrinsics.SUSPENDED)
if (result != SUSPENDED_MARKER)
completion!!.resume(result)
} catch (e: Throwable) {
completion!!.resumeWithException(e)