|
| 1 | +/* |
| 2 | + * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package kotlinx.coroutines.experimental |
| 18 | + |
| 19 | +import kotlinx.coroutines.experimental.internal.* |
| 20 | +import kotlin.coroutines.experimental.* |
| 21 | + |
| 22 | +@Suppress("PrivatePropertyName") |
| 23 | +private val UNDEFINED = Symbol("UNDEFINED") |
| 24 | + |
| 25 | +internal class DispatchedContinuation<in T>( |
| 26 | + val dispatcher: CoroutineDispatcher, |
| 27 | + val continuation: Continuation<T> |
| 28 | +) : Continuation<T> by continuation, DispatchedTask<T> { |
| 29 | + private var _state: Any? = UNDEFINED |
| 30 | + public override var resumeMode: Int = 0 |
| 31 | + |
| 32 | + override fun takeState(): Any? { |
| 33 | + val state = _state |
| 34 | + check(state !== UNDEFINED) // fail-fast if repeatedly invoked |
| 35 | + _state = UNDEFINED |
| 36 | + return state |
| 37 | + } |
| 38 | + |
| 39 | + override val delegate: Continuation<T> |
| 40 | + get() = this |
| 41 | + |
| 42 | + override fun resume(value: T) { |
| 43 | + val context = continuation.context |
| 44 | + if (dispatcher.isDispatchNeeded(context)) { |
| 45 | + _state = value |
| 46 | + resumeMode = MODE_ATOMIC_DEFAULT |
| 47 | + dispatcher.dispatch(context, this) |
| 48 | + } else |
| 49 | + resumeUndispatched(value) |
| 50 | + } |
| 51 | + |
| 52 | + override fun resumeWithException(exception: Throwable) { |
| 53 | + val context = continuation.context |
| 54 | + if (dispatcher.isDispatchNeeded(context)) { |
| 55 | + _state = CompletedExceptionally(exception) |
| 56 | + resumeMode = MODE_ATOMIC_DEFAULT |
| 57 | + dispatcher.dispatch(context, this) |
| 58 | + } else |
| 59 | + resumeUndispatchedWithException(exception) |
| 60 | + } |
| 61 | + |
| 62 | + @Suppress("NOTHING_TO_INLINE") // we need it inline to save us an entry on the stack |
| 63 | + inline fun resumeCancellable(value: T) { |
| 64 | + val context = continuation.context |
| 65 | + if (dispatcher.isDispatchNeeded(context)) { |
| 66 | + _state = value |
| 67 | + resumeMode = MODE_CANCELLABLE |
| 68 | + dispatcher.dispatch(context, this) |
| 69 | + } else |
| 70 | + resumeUndispatched(value) |
| 71 | + } |
| 72 | + |
| 73 | + @Suppress("NOTHING_TO_INLINE") // we need it inline to save us an entry on the stack |
| 74 | + inline fun resumeCancellableWithException(exception: Throwable) { |
| 75 | + val context = continuation.context |
| 76 | + if (dispatcher.isDispatchNeeded(context)) { |
| 77 | + _state = CompletedExceptionally(exception) |
| 78 | + resumeMode = MODE_CANCELLABLE |
| 79 | + dispatcher.dispatch(context, this) |
| 80 | + } else |
| 81 | + resumeUndispatchedWithException(exception) |
| 82 | + } |
| 83 | + |
| 84 | + @Suppress("NOTHING_TO_INLINE") // we need it inline to save us an entry on the stack |
| 85 | + inline fun resumeUndispatched(value: T) { |
| 86 | + withCoroutineContext(context) { |
| 87 | + continuation.resume(value) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Suppress("NOTHING_TO_INLINE") // we need it inline to save us an entry on the stack |
| 92 | + inline fun resumeUndispatchedWithException(exception: Throwable) { |
| 93 | + withCoroutineContext(context) { |
| 94 | + continuation.resumeWithException(exception) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // used by "yield" implementation |
| 99 | + internal fun dispatchYield(value: T) { |
| 100 | + val context = continuation.context |
| 101 | + _state = value |
| 102 | + resumeMode = MODE_CANCELLABLE |
| 103 | + dispatcher.dispatch(context, this) |
| 104 | + } |
| 105 | + |
| 106 | + override fun toString(): String = |
| 107 | + "DispatchedContinuation[$dispatcher, ${continuation.toDebugString()}]" |
| 108 | +} |
| 109 | + |
| 110 | +internal fun <T> Continuation<T>.resumeCancellable(value: T) = when (this) { |
| 111 | + is DispatchedContinuation -> resumeCancellable(value) |
| 112 | + else -> resume(value) |
| 113 | +} |
| 114 | + |
| 115 | +internal fun <T> Continuation<T>.resumeCancellableWithException(exception: Throwable) = when (this) { |
| 116 | + is DispatchedContinuation -> resumeCancellableWithException(exception) |
| 117 | + else -> resumeWithException(exception) |
| 118 | +} |
| 119 | + |
| 120 | +internal fun <T> Continuation<T>.resumeDirect(value: T) = when (this) { |
| 121 | + is DispatchedContinuation -> continuation.resume(value) |
| 122 | + else -> resume(value) |
| 123 | +} |
| 124 | + |
| 125 | +internal fun <T> Continuation<T>.resumeDirectWithException(exception: Throwable) = when (this) { |
| 126 | + is DispatchedContinuation -> continuation.resumeWithException(exception) |
| 127 | + else -> resumeWithException(exception) |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * @suppress **This is unstable API and it is subject to change.** |
| 132 | + */ |
| 133 | +public interface DispatchedTask<in T> : Runnable { |
| 134 | + public val delegate: Continuation<T> |
| 135 | + public val resumeMode: Int get() = MODE_CANCELLABLE |
| 136 | + |
| 137 | + public fun takeState(): Any? |
| 138 | + |
| 139 | + @Suppress("UNCHECKED_CAST") |
| 140 | + public fun <T> getSuccessfulResult(state: Any?): T = |
| 141 | + state as T |
| 142 | + |
| 143 | + public fun getExceptionalResult(state: Any?): Throwable? = |
| 144 | + (state as? CompletedExceptionally)?.exception |
| 145 | + |
| 146 | + public override fun run() { |
| 147 | + try { |
| 148 | + val delegate = delegate as DispatchedContinuation<T> |
| 149 | + val continuation = delegate.continuation |
| 150 | + val context = continuation.context |
| 151 | + val job = if (resumeMode.isCancellableMode) context[Job] else null |
| 152 | + val state = takeState() // NOTE: Must take state in any case, even if cancelled |
| 153 | + withCoroutineContext(context) { |
| 154 | + if (job != null && !job.isActive) |
| 155 | + continuation.resumeWithException(job.getCancellationException()) |
| 156 | + else { |
| 157 | + val exception = getExceptionalResult(state) |
| 158 | + if (exception != null) |
| 159 | + continuation.resumeWithException(exception) |
| 160 | + else |
| 161 | + continuation.resume(getSuccessfulResult(state)) |
| 162 | + } |
| 163 | + } |
| 164 | + } catch (e: Throwable) { |
| 165 | + throw DispatchException("Unexpected exception running $this", e) |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +/** |
| 171 | + * @suppress **This is unstable API and it is subject to change.** |
| 172 | + */ |
| 173 | +public fun <T> DispatchedTask<T>.dispatch(mode: Int = MODE_CANCELLABLE) { |
| 174 | + var useMode = mode |
| 175 | + val delegate = this.delegate |
| 176 | + if (mode.isDispatchedMode && delegate is DispatchedContinuation<*> && mode.isCancellableMode == resumeMode.isCancellableMode) { |
| 177 | + // dispatch directly using this instance's Runnable implementation |
| 178 | + val dispatcher = delegate.dispatcher |
| 179 | + val context = delegate.context |
| 180 | + if (dispatcher.isDispatchNeeded(context) |
| 181 | + ) { |
| 182 | + dispatcher.dispatch(context, this) |
| 183 | + return // and that's it -- dispatched via fast-path |
| 184 | + } else { |
| 185 | + useMode = MODE_UNDISPATCHED |
| 186 | + } |
| 187 | + } |
| 188 | + // slow-path - use delegate |
| 189 | + val state = takeState() |
| 190 | + val exception = getExceptionalResult(state) |
| 191 | + if (exception != null) { |
| 192 | + delegate.resumeWithExceptionMode(exception, useMode) |
| 193 | + } else { |
| 194 | + delegate.resumeMode(getSuccessfulResult(state), useMode) |
| 195 | + } |
| 196 | +} |
0 commit comments