Reformat stdlib: new coroutines
This commit is contained in:
@@ -16,7 +16,7 @@ import kotlin.coroutines.Continuation
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public expect inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(
|
||||
completion: Continuation<T>
|
||||
completion: Continuation<T>
|
||||
): Any?
|
||||
|
||||
/**
|
||||
@@ -28,17 +28,17 @@ public expect inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrRetu
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public expect inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedOrReturn(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Any?
|
||||
|
||||
@SinceKotlin("1.3")
|
||||
public expect fun <T> (suspend () -> T).createCoroutineUnchecked(
|
||||
completion: Continuation<T>
|
||||
completion: Continuation<T>
|
||||
): Continuation<Unit>
|
||||
|
||||
@SinceKotlin("1.3")
|
||||
public expect fun <R, T> (suspend R.() -> T).createCoroutineUnchecked(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Continuation<Unit>
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.coroutines
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater
|
||||
@@ -13,8 +12,8 @@ import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
|
||||
@SinceKotlin("1.3")
|
||||
internal actual class SafeContinuation<in T>
|
||||
internal actual constructor(
|
||||
private val delegate: Continuation<T>,
|
||||
initialResult: Any?
|
||||
private val delegate: Continuation<T>,
|
||||
initialResult: Any?
|
||||
) : Continuation<T> {
|
||||
|
||||
@PublishedApi
|
||||
@@ -33,7 +32,8 @@ internal actual constructor(
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@JvmStatic
|
||||
private val RESULT = AtomicReferenceFieldUpdater.newUpdater<SafeContinuation<*>, Any?>(
|
||||
SafeContinuation::class.java, Any::class.java as Class<Any?>, "result")
|
||||
SafeContinuation::class.java, Any::class.java as Class<Any?>, "result"
|
||||
)
|
||||
}
|
||||
|
||||
private class Fail(val exception: Throwable)
|
||||
@@ -55,7 +55,7 @@ internal actual constructor(
|
||||
actual override fun resumeWithException(exception: Throwable) {
|
||||
while (true) { // lock-free loop
|
||||
val result = this.result // atomic read
|
||||
when {
|
||||
when {
|
||||
result === UNDECIDED -> if (RESULT.compareAndSet(this, UNDECIDED, Fail(exception))) return
|
||||
result === COROUTINE_SUSPENDED -> if (RESULT.compareAndSet(this, COROUTINE_SUSPENDED, RESUMED)) {
|
||||
delegate.resumeWithException(exception)
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
@file:kotlin.jvm.JvmName("IntrinsicsKt")
|
||||
@file:kotlin.jvm.JvmMultifileClass
|
||||
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||
|
||||
package kotlin.coroutines.intrinsics
|
||||
|
||||
import kotlin.coroutines.*
|
||||
@@ -21,7 +22,7 @@ import kotlin.coroutines.*
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@kotlin.internal.InlineOnly
|
||||
public actual inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(
|
||||
completion: Continuation<T>
|
||||
completion: Continuation<T>
|
||||
): Any? = (this as Function1<Continuation<T>, Any?>).invoke(completion)
|
||||
|
||||
/**
|
||||
@@ -35,8 +36,8 @@ public actual inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrRetu
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@kotlin.internal.InlineOnly
|
||||
public actual inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedOrReturn(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Any? = (this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
|
||||
|
||||
|
||||
@@ -54,15 +55,15 @@ public actual inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedO
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public actual fun <T> (suspend () -> T).createCoroutineUnchecked(
|
||||
completion: Continuation<T>
|
||||
completion: Continuation<T>
|
||||
): Continuation<Unit> =
|
||||
if (this !is kotlin.coroutines.jvm.internal.CoroutineImpl)
|
||||
buildContinuationByInvokeCall(completion) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(this as Function1<Continuation<T>, Any?>).invoke(completion)
|
||||
}
|
||||
else
|
||||
(this.create(completion) as kotlin.coroutines.jvm.internal.CoroutineImpl).facade
|
||||
if (this !is kotlin.coroutines.jvm.internal.CoroutineImpl)
|
||||
buildContinuationByInvokeCall(completion) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(this as Function1<Continuation<T>, Any?>).invoke(completion)
|
||||
}
|
||||
else
|
||||
(this.create(completion) as kotlin.coroutines.jvm.internal.CoroutineImpl).facade
|
||||
|
||||
/**
|
||||
* Creates a coroutine with receiver type [R] and result type [T].
|
||||
@@ -76,36 +77,36 @@ public actual fun <T> (suspend () -> T).createCoroutineUnchecked(
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public actual fun <R, T> (suspend R.() -> T).createCoroutineUnchecked(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Continuation<Unit> =
|
||||
if (this !is kotlin.coroutines.jvm.internal.CoroutineImpl)
|
||||
buildContinuationByInvokeCall(completion) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
|
||||
}
|
||||
else
|
||||
(this.create(receiver, completion) as kotlin.coroutines.jvm.internal.CoroutineImpl).facade
|
||||
if (this !is kotlin.coroutines.jvm.internal.CoroutineImpl)
|
||||
buildContinuationByInvokeCall(completion) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
|
||||
}
|
||||
else
|
||||
(this.create(receiver, completion) as kotlin.coroutines.jvm.internal.CoroutineImpl).facade
|
||||
|
||||
// INTERNAL DEFINITIONS
|
||||
|
||||
private inline fun <T> buildContinuationByInvokeCall(
|
||||
completion: Continuation<T>,
|
||||
crossinline block: () -> Any?
|
||||
completion: Continuation<T>,
|
||||
crossinline block: () -> Any?
|
||||
): Continuation<Unit> {
|
||||
val continuation =
|
||||
object : Continuation<Unit> {
|
||||
override val context: CoroutineContext
|
||||
get() = completion.context
|
||||
val continuation =
|
||||
object : Continuation<Unit> {
|
||||
override val context: CoroutineContext
|
||||
get() = completion.context
|
||||
|
||||
override fun resume(value: Unit) {
|
||||
processBareContinuationResume(completion, block)
|
||||
}
|
||||
override fun resume(value: Unit) {
|
||||
processBareContinuationResume(completion, block)
|
||||
}
|
||||
|
||||
override fun resumeWithException(exception: Throwable) {
|
||||
completion.resumeWithException(exception)
|
||||
}
|
||||
}
|
||||
override fun resumeWithException(exception: Throwable) {
|
||||
completion.resumeWithException(exception)
|
||||
}
|
||||
}
|
||||
|
||||
return kotlin.coroutines.jvm.internal.interceptContinuationIfNeeded(completion.context, continuation)
|
||||
return kotlin.coroutines.jvm.internal.interceptContinuationIfNeeded(completion.context, continuation)
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@ import kotlin.jvm.internal.Lambda
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public abstract class CoroutineImpl(
|
||||
arity: Int,
|
||||
@JvmField
|
||||
protected var completion: Continuation<Any?>?
|
||||
arity: Int,
|
||||
@JvmField
|
||||
protected var completion: Continuation<Any?>?
|
||||
) : Lambda(arity), Continuation<Any?> {
|
||||
|
||||
// label == -1 when coroutine cannot be started (it is just a factory object) or has already finished execution
|
||||
|
||||
+4
-3
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
@file:JvmName("CoroutineIntrinsics")
|
||||
|
||||
package kotlin.coroutines.jvm.internal
|
||||
|
||||
import kotlin.coroutines.Continuation
|
||||
@@ -15,10 +16,10 @@ import kotlin.coroutines.CoroutineContext
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public fun <T> normalizeContinuation(continuation: Continuation<T>): Continuation<T> =
|
||||
(continuation as? CoroutineImpl)?.facade ?: continuation
|
||||
(continuation as? CoroutineImpl)?.facade ?: continuation
|
||||
|
||||
@SinceKotlin("1.3")
|
||||
internal fun <T> interceptContinuationIfNeeded(
|
||||
context: CoroutineContext,
|
||||
continuation: Continuation<T>
|
||||
context: CoroutineContext,
|
||||
continuation: Continuation<T>
|
||||
) = context[ContinuationInterceptor]?.interceptContinuation(continuation) ?: continuation
|
||||
|
||||
@@ -30,19 +30,19 @@ public interface CoroutineContext {
|
||||
* The elements from this context with the same key as in the other one are dropped.
|
||||
*/
|
||||
public operator fun plus(context: CoroutineContext): CoroutineContext =
|
||||
if (context === EmptyCoroutineContext) this else // fast path -- avoid lambda creation
|
||||
context.fold(this) { acc, element ->
|
||||
val removed = acc.minusKey(element.key)
|
||||
if (removed === EmptyCoroutineContext) element else {
|
||||
// make sure interceptor is always last in the context (and thus is fast to get when present)
|
||||
val interceptor = removed[ContinuationInterceptor]
|
||||
if (interceptor == null) CombinedContext(removed, element) else {
|
||||
val left = removed.minusKey(ContinuationInterceptor)
|
||||
if (left === EmptyCoroutineContext) CombinedContext(element, interceptor) else
|
||||
CombinedContext(CombinedContext(left, element), interceptor)
|
||||
}
|
||||
if (context === EmptyCoroutineContext) this else // fast path -- avoid lambda creation
|
||||
context.fold(this) { acc, element ->
|
||||
val removed = acc.minusKey(element.key)
|
||||
if (removed === EmptyCoroutineContext) element else {
|
||||
// make sure interceptor is always last in the context (and thus is fast to get when present)
|
||||
val interceptor = removed[ContinuationInterceptor]
|
||||
if (interceptor == null) CombinedContext(removed, element) else {
|
||||
val left = removed.minusKey(ContinuationInterceptor)
|
||||
if (left === EmptyCoroutineContext) CombinedContext(element, interceptor) else
|
||||
CombinedContext(CombinedContext(left, element), interceptor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a context containing elements from this context, but without an element with
|
||||
@@ -62,13 +62,13 @@ public interface CoroutineContext {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public override operator fun <E : Element> get(key: Key<E>): E? =
|
||||
if (this.key === key) this as E else null
|
||||
if (this.key === key) this as E else null
|
||||
|
||||
public override fun <R> fold(initial: R, operation: (R, Element) -> R): R =
|
||||
operation(initial, this)
|
||||
operation(initial, this)
|
||||
|
||||
public override fun minusKey(key: Key<*>): CoroutineContext =
|
||||
if (this.key === key) EmptyCoroutineContext else this
|
||||
if (this.key === key) EmptyCoroutineContext else this
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,7 +46,7 @@ internal class CombinedContext(val left: CoroutineContext, val element: Element)
|
||||
}
|
||||
|
||||
public override fun <R> fold(initial: R, operation: (R, Element) -> R): R =
|
||||
operation(left.fold(initial, operation), element)
|
||||
operation(left.fold(initial, operation), element)
|
||||
|
||||
public override fun minusKey(key: Key<*>): CoroutineContext {
|
||||
element[key]?.let { return left }
|
||||
@@ -59,10 +59,10 @@ internal class CombinedContext(val left: CoroutineContext, val element: Element)
|
||||
}
|
||||
|
||||
private fun size(): Int =
|
||||
if (left is CombinedContext) left.size() + 1 else 2
|
||||
if (left is CombinedContext) left.size() + 1 else 2
|
||||
|
||||
private fun contains(element: Element): Boolean =
|
||||
get(element.key) == element
|
||||
get(element.key) == element
|
||||
|
||||
private fun containsAll(context: CombinedContext): Boolean {
|
||||
var cur = context
|
||||
@@ -78,12 +78,12 @@ internal class CombinedContext(val left: CoroutineContext, val element: Element)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
this === other || other is CombinedContext && other.size() == size() && other.containsAll(this)
|
||||
this === other || other is CombinedContext && other.size() == size() && other.containsAll(this)
|
||||
|
||||
override fun hashCode(): Int = left.hashCode() + element.hashCode()
|
||||
|
||||
override fun toString(): String =
|
||||
"[" + fold("") { acc, element ->
|
||||
if (acc.isEmpty()) element.toString() else acc + ", " + element
|
||||
} + "]"
|
||||
"[" + fold("") { acc, element ->
|
||||
if (acc.isEmpty()) element.toString() else acc + ", " + element
|
||||
} + "]"
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||
@file:kotlin.jvm.JvmName("CoroutinesKt")
|
||||
|
||||
package kotlin.coroutines
|
||||
|
||||
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
|
||||
@@ -20,8 +21,8 @@ import kotlin.internal.InlineOnly
|
||||
@SinceKotlin("1.3")
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public fun <R, T> (suspend R.() -> T).startCoroutine(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
) {
|
||||
createCoroutineUnchecked(receiver, completion).resume(Unit)
|
||||
}
|
||||
@@ -33,8 +34,8 @@ public fun <R, T> (suspend R.() -> T).startCoroutine(
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public fun <T> (suspend () -> T).startCoroutine(
|
||||
completion: Continuation<T>
|
||||
public fun <T> (suspend () -> T).startCoroutine(
|
||||
completion: Continuation<T>
|
||||
) {
|
||||
createCoroutineUnchecked(completion).resume(Unit)
|
||||
}
|
||||
@@ -50,8 +51,8 @@ public fun <T> (suspend () -> T).startCoroutine(
|
||||
@SinceKotlin("1.3")
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Continuation<Unit> = SafeContinuation(createCoroutineUnchecked(receiver, completion), COROUTINE_SUSPENDED)
|
||||
|
||||
/**
|
||||
@@ -65,7 +66,7 @@ public fun <R, T> (suspend R.() -> T).createCoroutine(
|
||||
@SinceKotlin("1.3")
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public fun <T> (suspend () -> T).createCoroutine(
|
||||
completion: Continuation<T>
|
||||
completion: Continuation<T>
|
||||
): Continuation<Unit> = SafeContinuation(createCoroutineUnchecked(completion), COROUTINE_SUSPENDED)
|
||||
|
||||
/**
|
||||
@@ -78,11 +79,11 @@ public fun <T> (suspend () -> T).createCoroutine(
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public suspend inline fun <T> suspendCoroutine(crossinline block: (Continuation<T>) -> Unit): T =
|
||||
suspendCoroutineOrReturn { c: Continuation<T> ->
|
||||
val safe = SafeContinuation(c)
|
||||
block(safe)
|
||||
safe.getResult()
|
||||
}
|
||||
suspendCoroutineOrReturn { c: Continuation<T> ->
|
||||
val safe = SafeContinuation(c)
|
||||
block(safe)
|
||||
safe.getResult()
|
||||
}
|
||||
|
||||
/**
|
||||
* Continuation context of current coroutine.
|
||||
|
||||
@@ -32,7 +32,7 @@ import kotlin.coroutines.*
|
||||
@kotlin.internal.InlineOnly
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
public suspend inline fun <T> suspendCoroutineOrReturn(crossinline block: (Continuation<T>) -> Any?): T =
|
||||
suspendCoroutineUninterceptedOrReturn { cont -> block(cont.intercepted()) }
|
||||
suspendCoroutineUninterceptedOrReturn { cont -> block(cont.intercepted()) }
|
||||
|
||||
/**
|
||||
* Obtains the current continuation instance inside suspend functions and either suspends
|
||||
@@ -43,7 +43,7 @@ public suspend inline fun <T> suspendCoroutineOrReturn(crossinline block: (Conti
|
||||
@SinceKotlin("1.3")
|
||||
@kotlin.internal.InlineOnly
|
||||
public suspend inline fun <T> suspendCoroutineUninterceptedOrReturn(crossinline block: (Continuation<T>) -> Any?): T =
|
||||
throw NotImplementedError("Implementation of suspendCoroutineUninterceptedOrReturn is intrinsic")
|
||||
throw NotImplementedError("Implementation of suspendCoroutineUninterceptedOrReturn is intrinsic")
|
||||
|
||||
/**
|
||||
* Intercept continuation with [ContinuationInterceptor].
|
||||
@@ -51,7 +51,7 @@ public suspend inline fun <T> suspendCoroutineUninterceptedOrReturn(crossinline
|
||||
@SinceKotlin("1.3")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Continuation<T>.intercepted(): Continuation<T> =
|
||||
throw NotImplementedError("Implementation of intercepted is intrinsic")
|
||||
throw NotImplementedError("Implementation of intercepted is intrinsic")
|
||||
|
||||
/**
|
||||
* This value is used as a return value of [suspendCoroutineOrReturn] `block` argument to state that
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
@file:kotlin.jvm.JvmMultifileClass
|
||||
@file:kotlin.jvm.JvmName("SequenceBuilderKt")
|
||||
|
||||
package kotlin.sequences
|
||||
|
||||
import kotlin.*
|
||||
@@ -85,6 +86,7 @@ public abstract class SequenceBuilder<in T> internal constructor() {
|
||||
}
|
||||
|
||||
private typealias State = Int
|
||||
|
||||
private const val State_NotReady: State = 0
|
||||
private const val State_ManyNotReady: State = 1
|
||||
private const val State_ManyReady: State = 2
|
||||
@@ -130,7 +132,7 @@ private class SequenceBuilderIterator<T> : SequenceBuilder<T>(), Iterator<T>, Co
|
||||
}
|
||||
State_Ready -> {
|
||||
state = State_NotReady
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val result = nextValue as T
|
||||
nextValue = null
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user