Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 6 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.internal.* |
| 8 | import kotlin.coroutines.* |
Vsevolod Tolstopyatov | 36c3ba1 | 2018-10-14 23:45:28 +0300 | [diff] [blame] | 9 | import kotlin.jvm.* |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 10 | |
| 11 | @Suppress("PrivatePropertyName") |
Vsevolod Tolstopyatov | d8cdc9f | 2018-11-13 13:30:16 +0300 | [diff] [blame] | 12 | @SharedImmutable |
Vsevolod Tolstopyatov | 09b9d6c | 2018-10-17 19:51:50 +0300 | [diff] [blame] | 13 | private val UNDEFINED = Symbol("UNDEFINED") |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 14 | |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 15 | /** |
| 16 | * Executes given [block] as part of current event loop, updating current continuation |
| 17 | * mode and state if continuation is not resumed immediately. |
| 18 | * [doYield] indicates whether current continuation is yielding (to provide fast-path if event-loop is empty). |
| 19 | * Returns `true` if execution of continuation was queued (trampolined) or `false` otherwise. |
| 20 | */ |
| 21 | private inline fun DispatchedContinuation<*>.executeUnconfined( |
| 22 | contState: Any?, mode: Int, doYield: Boolean = false, |
| 23 | block: () -> Unit |
| 24 | ) : Boolean { |
| 25 | val eventLoop = ThreadLocalEventLoop.eventLoop |
| 26 | // If we are yielding and unconfined queue is empty, we can bail out as part of fast path |
| 27 | if (doYield && eventLoop.isUnconfinedQueueEmpty) return false |
| 28 | return if (eventLoop.isUnconfinedLoopActive) { |
| 29 | // When unconfined loop is active -- dispatch continuation for execution to avoid stack overflow |
| 30 | _state = contState |
| 31 | resumeMode = mode |
| 32 | eventLoop.dispatchUnconfined(this) |
| 33 | true // queued into the active loop |
| 34 | } else { |
| 35 | // Was not active -- run event loop until all unconfined tasks are executed |
| 36 | runUnconfinedEventLoop(eventLoop, block = block) |
| 37 | false |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 38 | } |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 39 | } |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 40 | |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 41 | private fun DispatchedTask<*>.resumeUnconfined() { |
| 42 | val eventLoop = ThreadLocalEventLoop.eventLoop |
| 43 | if (eventLoop.isUnconfinedLoopActive) { |
| 44 | // When unconfined loop is active -- dispatch continuation for execution to avoid stack overflow |
| 45 | eventLoop.dispatchUnconfined(this) |
| 46 | } else { |
| 47 | // Was not active -- run event loop until all unconfined tasks are executed |
| 48 | runUnconfinedEventLoop(eventLoop) { |
| 49 | resume(delegate, MODE_UNDISPATCHED) |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 50 | } |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 51 | } |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 52 | } |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 53 | |
Vsevolod Tolstopyatov | 0c8789d | 2019-03-07 15:07:41 +0300 | [diff] [blame] | 54 | private inline fun DispatchedTask<*>.runUnconfinedEventLoop( |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 55 | eventLoop: EventLoop, |
| 56 | block: () -> Unit |
| 57 | ) { |
| 58 | eventLoop.incrementUseCount(unconfined = true) |
| 59 | try { |
| 60 | block() |
| 61 | while (true) { |
| 62 | // break when all unconfined continuations where executed |
| 63 | if (!eventLoop.processUnconfinedEvent()) break |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 64 | } |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 65 | } catch (e: Throwable) { |
| 66 | /* |
Vsevolod Tolstopyatov | 0c8789d | 2019-03-07 15:07:41 +0300 | [diff] [blame] | 67 | * This exception doesn't happen normally, only if we have a bug in implementation. |
| 68 | * Report it as a fatal exception. |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 69 | */ |
Vsevolod Tolstopyatov | 0c8789d | 2019-03-07 15:07:41 +0300 | [diff] [blame] | 70 | handleFatalException(e, null) |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 71 | } finally { |
| 72 | eventLoop.decrementUseCount(unconfined = true) |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 73 | } |
| 74 | } |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 75 | |
| 76 | internal class DispatchedContinuation<in T>( |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 77 | @JvmField val dispatcher: CoroutineDispatcher, |
| 78 | @JvmField val continuation: Continuation<T> |
Vsevolod Tolstopyatov | 675c30c | 2018-11-02 15:55:20 +0300 | [diff] [blame] | 79 | ) : DispatchedTask<T>(MODE_ATOMIC_DEFAULT), CoroutineStackFrame, Continuation<T> by continuation { |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 80 | @JvmField |
| 81 | @Suppress("PropertyName") |
| 82 | internal var _state: Any? = UNDEFINED |
Vsevolod Tolstopyatov | 675c30c | 2018-11-02 15:55:20 +0300 | [diff] [blame] | 83 | override val callerFrame: CoroutineStackFrame? = continuation as? CoroutineStackFrame |
| 84 | override fun getStackTraceElement(): StackTraceElement? = null |
Vsevolod Tolstopyatov | ae85797 | 2018-10-16 15:47:26 +0300 | [diff] [blame] | 85 | @JvmField // pre-cached value to avoid ctx.fold on every resumption |
| 86 | internal val countOrElement = threadContextElements(context) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 87 | |
| 88 | override fun takeState(): Any? { |
| 89 | val state = _state |
| 90 | check(state !== UNDEFINED) // fail-fast if repeatedly invoked |
| 91 | _state = UNDEFINED |
| 92 | return state |
| 93 | } |
| 94 | |
| 95 | override val delegate: Continuation<T> |
| 96 | get() = this |
| 97 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 98 | override fun resumeWith(result: Result<T>) { |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 99 | val context = continuation.context |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 100 | val state = result.toState() |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 101 | if (dispatcher.isDispatchNeeded(context)) { |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 102 | _state = state |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 103 | resumeMode = MODE_ATOMIC_DEFAULT |
| 104 | dispatcher.dispatch(context, this) |
Vsevolod Tolstopyatov | 838b052 | 2018-09-28 16:17:26 +0300 | [diff] [blame] | 105 | } else { |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 106 | executeUnconfined(state, MODE_ATOMIC_DEFAULT) { |
Vsevolod Tolstopyatov | cd37d8e | 2018-10-18 16:59:46 +0300 | [diff] [blame] | 107 | withCoroutineContext(this.context, countOrElement) { |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 108 | continuation.resumeWith(result) |
| 109 | } |
| 110 | } |
Vsevolod Tolstopyatov | 838b052 | 2018-09-28 16:17:26 +0300 | [diff] [blame] | 111 | } |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Suppress("NOTHING_TO_INLINE") // we need it inline to save us an entry on the stack |
| 115 | inline fun resumeCancellable(value: T) { |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 116 | if (dispatcher.isDispatchNeeded(context)) { |
| 117 | _state = value |
| 118 | resumeMode = MODE_CANCELLABLE |
| 119 | dispatcher.dispatch(context, this) |
Vsevolod Tolstopyatov | 838b052 | 2018-09-28 16:17:26 +0300 | [diff] [blame] | 120 | } else { |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 121 | executeUnconfined(value, MODE_CANCELLABLE) { |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 122 | if (!resumeCancelled()) { |
| 123 | resumeUndispatched(value) |
| 124 | } |
Vsevolod Tolstopyatov | 838b052 | 2018-09-28 16:17:26 +0300 | [diff] [blame] | 125 | } |
| 126 | } |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Suppress("NOTHING_TO_INLINE") // we need it inline to save us an entry on the stack |
| 130 | inline fun resumeCancellableWithException(exception: Throwable) { |
| 131 | val context = continuation.context |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 132 | val state = CompletedExceptionally(exception) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 133 | if (dispatcher.isDispatchNeeded(context)) { |
| 134 | _state = CompletedExceptionally(exception) |
| 135 | resumeMode = MODE_CANCELLABLE |
| 136 | dispatcher.dispatch(context, this) |
Vsevolod Tolstopyatov | 838b052 | 2018-09-28 16:17:26 +0300 | [diff] [blame] | 137 | } else { |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 138 | executeUnconfined(state, MODE_CANCELLABLE) { |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 139 | if (!resumeCancelled()) { |
| 140 | resumeUndispatchedWithException(exception) |
| 141 | } |
Vsevolod Tolstopyatov | 838b052 | 2018-09-28 16:17:26 +0300 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Suppress("NOTHING_TO_INLINE") |
| 147 | inline fun resumeCancelled(): Boolean { |
| 148 | val job = context[Job] |
| 149 | if (job != null && !job.isActive) { |
| 150 | resumeWithException(job.getCancellationException()) |
| 151 | return true |
| 152 | } |
| 153 | |
| 154 | return false |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Suppress("NOTHING_TO_INLINE") // we need it inline to save us an entry on the stack |
| 158 | inline fun resumeUndispatched(value: T) { |
Vsevolod Tolstopyatov | ae85797 | 2018-10-16 15:47:26 +0300 | [diff] [blame] | 159 | withCoroutineContext(context, countOrElement) { |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 160 | continuation.resume(value) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @Suppress("NOTHING_TO_INLINE") // we need it inline to save us an entry on the stack |
| 165 | inline fun resumeUndispatchedWithException(exception: Throwable) { |
Vsevolod Tolstopyatov | ae85797 | 2018-10-16 15:47:26 +0300 | [diff] [blame] | 166 | withCoroutineContext(context, countOrElement) { |
Vsevolod Tolstopyatov | 675c30c | 2018-11-02 15:55:20 +0300 | [diff] [blame] | 167 | continuation.resumeWithStackTrace(exception) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | // used by "yield" implementation |
| 172 | internal fun dispatchYield(value: T) { |
| 173 | val context = continuation.context |
| 174 | _state = value |
| 175 | resumeMode = MODE_CANCELLABLE |
Vsevolod Tolstopyatov | 3ac73f6 | 2018-07-26 16:09:33 +0300 | [diff] [blame] | 176 | dispatcher.dispatchYield(context, this) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | override fun toString(): String = |
| 180 | "DispatchedContinuation[$dispatcher, ${continuation.toDebugString()}]" |
| 181 | } |
| 182 | |
| 183 | internal fun <T> Continuation<T>.resumeCancellable(value: T) = when (this) { |
| 184 | is DispatchedContinuation -> resumeCancellable(value) |
| 185 | else -> resume(value) |
| 186 | } |
| 187 | |
| 188 | internal fun <T> Continuation<T>.resumeCancellableWithException(exception: Throwable) = when (this) { |
| 189 | is DispatchedContinuation -> resumeCancellableWithException(exception) |
Vsevolod Tolstopyatov | 675c30c | 2018-11-02 15:55:20 +0300 | [diff] [blame] | 190 | else -> resumeWithStackTrace(exception) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | internal fun <T> Continuation<T>.resumeDirect(value: T) = when (this) { |
| 194 | is DispatchedContinuation -> continuation.resume(value) |
| 195 | else -> resume(value) |
| 196 | } |
| 197 | |
| 198 | internal fun <T> Continuation<T>.resumeDirectWithException(exception: Throwable) = when (this) { |
Vsevolod Tolstopyatov | 675c30c | 2018-11-02 15:55:20 +0300 | [diff] [blame] | 199 | is DispatchedContinuation -> continuation.resumeWithStackTrace(exception) |
| 200 | else -> resumeWithStackTrace(exception) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 201 | } |
| 202 | |
Roman Elizarov | aa374a9 | 2018-11-02 00:10:39 +0300 | [diff] [blame] | 203 | internal abstract class DispatchedTask<in T>( |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 204 | @JvmField public var resumeMode: Int |
Roman Elizarov | aa374a9 | 2018-11-02 00:10:39 +0300 | [diff] [blame] | 205 | ) : SchedulerTask() { |
Roman Elizarov | 2b8218a | 2019-04-11 01:41:22 +0300 | [diff] [blame] | 206 | internal abstract val delegate: Continuation<T> |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 207 | |
Roman Elizarov | 2b8218a | 2019-04-11 01:41:22 +0300 | [diff] [blame] | 208 | internal abstract fun takeState(): Any? |
| 209 | |
| 210 | internal open fun cancelResult(state: Any?, cause: Throwable) {} |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 211 | |
| 212 | @Suppress("UNCHECKED_CAST") |
Roman Elizarov | 2b8218a | 2019-04-11 01:41:22 +0300 | [diff] [blame] | 213 | internal open fun <T> getSuccessfulResult(state: Any?): T = |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 214 | state as T |
| 215 | |
Roman Elizarov | 2b8218a | 2019-04-11 01:41:22 +0300 | [diff] [blame] | 216 | internal fun getExceptionalResult(state: Any?): Throwable? = |
Vsevolod Tolstopyatov | c1092d5 | 2018-04-12 20:22:25 +0300 | [diff] [blame] | 217 | (state as? CompletedExceptionally)?.cause |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 218 | |
Roman Elizarov | aa374a9 | 2018-11-02 00:10:39 +0300 | [diff] [blame] | 219 | public final override fun run() { |
Roman Elizarov | e64d9b7 | 2018-10-22 05:31:23 +0300 | [diff] [blame] | 220 | val taskContext = this.taskContext |
Vsevolod Tolstopyatov | c022ab6 | 2019-05-14 15:10:09 +0300 | [diff] [blame^] | 221 | var fatalException: Throwable? = null |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 222 | try { |
| 223 | val delegate = delegate as DispatchedContinuation<T> |
| 224 | val continuation = delegate.continuation |
| 225 | val context = continuation.context |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 226 | val state = takeState() // NOTE: Must take state in any case, even if cancelled |
Vsevolod Tolstopyatov | ae85797 | 2018-10-16 15:47:26 +0300 | [diff] [blame] | 227 | withCoroutineContext(context, delegate.countOrElement) { |
Vsevolod Tolstopyatov | c022ab6 | 2019-05-14 15:10:09 +0300 | [diff] [blame^] | 228 | val exception = getExceptionalResult(state) |
| 229 | val job = if (resumeMode.isCancellableMode) context[Job] else null |
| 230 | /* |
| 231 | * Check whether continuation was originally resumed with an exception. |
| 232 | * If so, it dominates cancellation, otherwise the original exception |
| 233 | * will be silently lost. |
| 234 | */ |
| 235 | if (exception == null && job != null && !job.isActive) { |
Roman Elizarov | 2b8218a | 2019-04-11 01:41:22 +0300 | [diff] [blame] | 236 | val cause = job.getCancellationException() |
| 237 | cancelResult(state, cause) |
Roman Elizarov | 9e9c9a3 | 2019-04-24 10:54:51 +0300 | [diff] [blame] | 238 | continuation.resumeWithStackTrace(cause) |
Roman Elizarov | 2b8218a | 2019-04-11 01:41:22 +0300 | [diff] [blame] | 239 | } else { |
Vsevolod Tolstopyatov | c022ab6 | 2019-05-14 15:10:09 +0300 | [diff] [blame^] | 240 | if (exception != null) continuation.resumeWithStackTrace(exception) |
| 241 | else continuation.resume(getSuccessfulResult(state)) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } catch (e: Throwable) { |
Vsevolod Tolstopyatov | 0c8789d | 2019-03-07 15:07:41 +0300 | [diff] [blame] | 245 | // This instead of runCatching to have nicer stacktrace and debug experience |
Vsevolod Tolstopyatov | c022ab6 | 2019-05-14 15:10:09 +0300 | [diff] [blame^] | 246 | fatalException = e |
Roman Elizarov | 16e2034 | 2018-10-21 20:08:49 +0300 | [diff] [blame] | 247 | } finally { |
Vsevolod Tolstopyatov | 0c8789d | 2019-03-07 15:07:41 +0300 | [diff] [blame] | 248 | val result = runCatching { taskContext.afterTask() } |
Vsevolod Tolstopyatov | c022ab6 | 2019-05-14 15:10:09 +0300 | [diff] [blame^] | 249 | handleFatalException(fatalException, result.exceptionOrNull()) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 250 | } |
| 251 | } |
Vsevolod Tolstopyatov | 0c8789d | 2019-03-07 15:07:41 +0300 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * Machinery that handles fatal exceptions in kotlinx.coroutines. |
| 255 | * There are two kinds of fatal exceptions: |
| 256 | * |
| 257 | * 1) Exceptions from kotlinx.coroutines code. Such exceptions indicate that either |
| 258 | * the library or the compiler has a bug that breaks internal invariants. |
| 259 | * They usually have specific workarounds, but require careful study of the cause and should |
| 260 | * be reported to the maintainers and fixed on the library's side anyway. |
| 261 | * |
| 262 | * 2) Exceptions from [ThreadContextElement.updateThreadContext] and [ThreadContextElement.restoreThreadContext]. |
| 263 | * While a user code can trigger such exception by providing an improper implementation of [ThreadContextElement], |
| 264 | * we can't ignore it because it may leave coroutine in the inconsistent state. |
| 265 | * If you encounter such exception, you can either disable this context element or wrap it into |
| 266 | * another context element that catches all exceptions and handles it in the application specific manner. |
| 267 | * |
| 268 | * Fatal exception handling can be intercepted with [CoroutineExceptionHandler] element in the context of |
| 269 | * a failed coroutine, but such exceptions should be reported anyway. |
| 270 | */ |
| 271 | internal fun handleFatalException(exception: Throwable?, finallyException: Throwable?) { |
| 272 | if (exception === null && finallyException === null) return |
| 273 | if (exception !== null && finallyException !== null) { |
| 274 | exception.addSuppressedThrowable(finallyException) |
| 275 | } |
| 276 | |
| 277 | val cause = exception ?: finallyException |
Vsevolod Tolstopyatov | 132c1e3 | 2019-03-07 15:15:40 +0300 | [diff] [blame] | 278 | val reason = CoroutinesInternalError("Fatal exception in coroutines machinery for $this. " + |
Vsevolod Tolstopyatov | 0c8789d | 2019-03-07 15:07:41 +0300 | [diff] [blame] | 279 | "Please read KDoc to 'handleFatalException' method and report this incident to maintainers", cause!!) |
Vsevolod Tolstopyatov | 4651b57 | 2019-03-13 14:29:19 +0300 | [diff] [blame] | 280 | handleCoroutineException(this.delegate.context, reason) |
Vsevolod Tolstopyatov | 0c8789d | 2019-03-07 15:07:41 +0300 | [diff] [blame] | 281 | } |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 282 | } |
| 283 | |
Vsevolod Tolstopyatov | bf45576 | 2018-10-23 11:38:14 +0300 | [diff] [blame] | 284 | internal fun DispatchedContinuation<Unit>.yieldUndispatched(): Boolean = |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 285 | executeUnconfined(Unit, MODE_CANCELLABLE, doYield = true) { |
Robert Golusiński | c33ef61 | 2018-10-23 11:29:56 +0200 | [diff] [blame] | 286 | run() |
| 287 | } |
Robert Golusiński | c33ef61 | 2018-10-23 11:29:56 +0200 | [diff] [blame] | 288 | |
Vsevolod Tolstopyatov | 1f7b2d8 | 2018-10-09 15:57:51 +0300 | [diff] [blame] | 289 | internal fun <T> DispatchedTask<T>.dispatch(mode: Int = MODE_CANCELLABLE) { |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 290 | val delegate = this.delegate |
| 291 | if (mode.isDispatchedMode && delegate is DispatchedContinuation<*> && mode.isCancellableMode == resumeMode.isCancellableMode) { |
| 292 | // dispatch directly using this instance's Runnable implementation |
| 293 | val dispatcher = delegate.dispatcher |
| 294 | val context = delegate.context |
Roman Elizarov | 9a0d8ac | 2018-01-11 14:32:19 +0300 | [diff] [blame] | 295 | if (dispatcher.isDispatchNeeded(context)) { |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 296 | dispatcher.dispatch(context, this) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 297 | } else { |
Roman Elizarov | 5173824 | 2018-12-21 16:41:39 +0300 | [diff] [blame] | 298 | resumeUnconfined() |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 299 | } |
Vsevolod Tolstopyatov | faa4774 | 2018-10-18 11:54:56 +0300 | [diff] [blame] | 300 | } else { |
| 301 | resume(delegate, mode) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 302 | } |
Vsevolod Tolstopyatov | faa4774 | 2018-10-18 11:54:56 +0300 | [diff] [blame] | 303 | } |
Vsevolod Tolstopyatov | fd54bc4 | 2018-10-17 18:37:36 +0300 | [diff] [blame] | 304 | |
Vsevolod Tolstopyatov | faa4774 | 2018-10-18 11:54:56 +0300 | [diff] [blame] | 305 | internal fun <T> DispatchedTask<T>.resume(delegate: Continuation<T>, useMode: Int) { |
| 306 | // slow-path - use delegate |
| 307 | val state = takeState() |
| 308 | val exception = getExceptionalResult(state) |
| 309 | if (exception != null) { |
| 310 | delegate.resumeWithExceptionMode(exception, useMode) |
| 311 | } else { |
| 312 | delegate.resumeMode(getSuccessfulResult(state), useMode) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 313 | } |
| 314 | } |
Vsevolod Tolstopyatov | 675c30c | 2018-11-02 15:55:20 +0300 | [diff] [blame] | 315 | |
| 316 | |
| 317 | @Suppress("NOTHING_TO_INLINE") |
Vsevolod Tolstopyatov | 1032f58 | 2018-11-27 18:13:47 +0300 | [diff] [blame] | 318 | internal inline fun Continuation<*>.resumeWithStackTrace(exception: Throwable) { |
| 319 | resumeWith(Result.failure(recoverStackTrace(exception, this))) |
| 320 | } |