blob: 8ac8d54a8b50144cf1513541d5620bd49b58563d [file] [log] [blame]
/*
* Copyright 2016-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 kotlinx.coroutines.experimental
import kotlin.coroutines.experimental.Continuation
import kotlin.coroutines.experimental.CoroutineContext
/**
* Receiver interface for generic coroutine builders, so that the code inside coroutine has a convenient access
* to its [context] and cancellation status via [isActive].
*/
public interface CoroutineScope {
/**
* Returns `true` when this coroutine is still active (was not cancelled).
*/
public val isActive: Boolean
/**
* Returns the context of this coroutine.
*/
public val context: CoroutineContext
}
/**
* Abstract class to simplify writing of coroutine completion objects that
* implements [Continuation] and [Job] interfaces.
* It stores the result of continuation in the state of the job.
*/
@Suppress("LeakingThis")
internal abstract class AbstractCoroutine<in T>(context: CoroutineContext) : JobSupport(), Continuation<T>, CoroutineScope {
override val context: CoroutineContext = context + this // merges this job into this context
final override fun resume(value: T) {
while (true) { // lock-free loop on state
val state = getState() // atomic read
when (state) {
is Active -> if (updateState(state, value)) return
is Cancelled -> return // ignore resumes on cancelled continuation
else -> throw IllegalStateException("Already resumed, but got value $value")
}
}
}
final override fun resumeWithException(exception: Throwable) {
while (true) { // lock-free loop on state
val state = getState() // atomic read
when (state) {
is Active -> if (updateState(state, CompletedExceptionally(exception))) return
is Cancelled -> {
// ignore resumes on cancelled continuation, but handle exception if a different one is here
if (exception != state.exception) handleCoroutineException(context, exception)
return
}
else -> throw IllegalStateException("Already resumed, but got exception $exception", exception)
}
}
}
final override fun handleCompletionException(closeException: Throwable) {
handleCoroutineException(context, closeException)
}
}