Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +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 | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental |
| 6 | |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.CoroutineStart.* |
| 8 | import kotlinx.coroutines.experimental.intrinsics.* |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 9 | import kotlin.coroutines.experimental.* |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 10 | |
| 11 | /** |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 12 | * Abstract base class for implementation of coroutines in coroutine builders. |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 13 | * |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 14 | * This class implements completion [Continuation], [Job], and [CoroutineScope] interfaces. |
| 15 | * It stores the result of continuation in the state of the job. |
| 16 | * This coroutine waits for children coroutines to finish before completing and |
| 17 | * is cancelled through an intermediate _cancelling_ state. |
| 18 | * |
| 19 | * The following methods are available for override: |
| 20 | * |
| 21 | * * [onStart] is invoked when coroutine is create in not active state and is [started][Job.start]. |
| 22 | * * [onCancellation] is invoked as soon as coroutine is [cancelled][cancel] (becomes _cancelling_) |
| 23 | * or when it completes for any reason. |
| 24 | * * [onCompleted] is invoked when coroutine completes with a value. |
| 25 | * * [onCompletedExceptionally] in invoked when coroutines completes with exception. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 26 | * |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 27 | * @param parentContext context of the parent coroutine. |
| 28 | * @param active when `true` (by default) coroutine is created in _active_ state, when `false` in _new_ state. |
| 29 | * See [Job] for details. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 30 | */ |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 31 | @Suppress("EXPOSED_SUPER_CLASS") |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 32 | public abstract class AbstractCoroutine<in T>( |
| 33 | private val parentContext: CoroutineContext, |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 34 | active: Boolean = true |
| 35 | ) : JobSupport(active), Job, Continuation<T>, CoroutineScope { |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 36 | @Suppress("LeakingThis") |
| 37 | public final override val context: CoroutineContext = parentContext + this |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 38 | @Deprecated("Replaced with context", replaceWith = ReplaceWith("context")) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 39 | public final override val coroutineContext: CoroutineContext get() = context |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 40 | |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 41 | /** |
| 42 | * Initializes parent job from the `parentContext` of this coroutine that was passed to it during construction. |
| 43 | * It shall be invoked at most once after construction after all other initialization. |
| 44 | * |
| 45 | * Invocation of this function may cause this coroutine to become cancelled if parent is already cancelled, |
| 46 | * in which case it synchronously invokes all the corresponding handlers. |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 47 | * @suppress **This is unstable API and it is subject to change.** |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 48 | */ |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 49 | internal fun initParentJob() { |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 50 | initParentJobInternal(parentContext[Job]) |
| 51 | } |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 52 | |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 53 | /** |
| 54 | * This function is invoked once when non-active coroutine (constructed with `active` set to `false) |
| 55 | * is [started][start]. |
| 56 | */ |
| 57 | protected open fun onStart() {} |
| 58 | |
| 59 | internal final override fun onStartInternal() { |
| 60 | onStart() |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * This function is invoked once when this coroutine is cancelled or is completed, |
| 65 | * similarly to [invokeOnCompletion] with `onCancelling` set to `true`. |
| 66 | * |
Roman Elizarov | e89cd68 | 2018-04-25 13:03:40 +0300 | [diff] [blame] | 67 | * The meaning of [cause] parameter: |
| 68 | * * Cause is `null` when job has completed normally. |
| 69 | * * Cause is an instance of [CancellationException] when job was cancelled _normally_. |
| 70 | * **It should not be treated as an error**. In particular, it should not be reported to error logs. |
| 71 | * * Otherwise, the job had _failed_. |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 72 | */ |
| 73 | protected open fun onCancellation(cause: Throwable?) {} |
| 74 | |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 75 | internal override fun onCancellationInternal(exceptionally: CompletedExceptionally?) { |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 76 | onCancellation(exceptionally?.cause) |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * This function is invoked once when job is completed normally with the specified [value]. |
| 81 | */ |
| 82 | protected open fun onCompleted(value: T) {} |
| 83 | |
| 84 | /** |
| 85 | * This function is invoked once when job is completed exceptionally with the specified [exception]. |
| 86 | */ |
| 87 | protected open fun onCompletedExceptionally(exception: Throwable) {} |
| 88 | |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 89 | @Suppress("UNCHECKED_CAST") |
Roman Elizarov | ebc8866 | 2018-01-24 23:58:56 +0300 | [diff] [blame] | 90 | internal override fun onCompletionInternal(state: Any?, mode: Int) { |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 91 | if (state is CompletedExceptionally) |
Vsevolod Tolstopyatov | f5e63ca | 2018-04-12 19:59:56 +0300 | [diff] [blame] | 92 | onCompletedExceptionally(state.cause) |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 93 | else |
| 94 | onCompleted(state as T) |
| 95 | } |
| 96 | |
| 97 | internal open val defaultResumeMode: Int get() = MODE_ATOMIC_DEFAULT |
| 98 | |
| 99 | /** |
| 100 | * Completes execution of this coroutine normally with the specified [value]. |
| 101 | */ |
| 102 | public final override fun resume(value: T) { |
Roman Elizarov | 4d626de | 2018-01-11 22:57:28 +0300 | [diff] [blame] | 103 | makeCompletingOnce(value, defaultResumeMode) |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 104 | } |
| 105 | |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 106 | /** |
| 107 | * Completes execution of this with coroutine exceptionally with the specified [exception]. |
| 108 | */ |
| 109 | public final override fun resumeWithException(exception: Throwable) { |
Roman Elizarov | 4d626de | 2018-01-11 22:57:28 +0300 | [diff] [blame] | 110 | makeCompletingOnce(CompletedExceptionally(exception), defaultResumeMode) |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 111 | } |
| 112 | |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 113 | internal final override fun handleException(exception: Throwable) { |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 114 | handleCoroutineException(parentContext, exception) |
| 115 | } |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 116 | |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 117 | internal override fun nameString(): String { |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 118 | val coroutineName = context.coroutineName ?: return super.nameString() |
| 119 | return "\"$coroutineName\":${super.nameString()}" |
| 120 | } |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 121 | |
| 122 | /** |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 123 | * Starts this coroutine with the given code [block] and [start] strategy. |
| 124 | * This function shall be invoked at most once on this coroutine. |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 125 | * |
| 126 | * First, this function initializes parent job from the `parentContext` of this coroutine that was passed to it |
| 127 | * during construction. Second, it starts the coroutine based on [start] parameter: |
| 128 | * |
| 129 | * * [DEFAULT] uses [startCoroutineCancellable]. |
| 130 | * * [ATOMIC] uses [startCoroutine]. |
| 131 | * * [UNDISPATCHED] uses [startCoroutineUndispatched]. |
| 132 | * * [LAZY] does nothing. |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 133 | */ |
| 134 | public fun start(start: CoroutineStart, block: suspend () -> T) { |
| 135 | initParentJob() |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 136 | start(block, this) |
| 137 | } |
| 138 | |
| 139 | /** |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 140 | * Starts this coroutine with the given code [block] and [start] strategy. |
| 141 | * This function shall be invoked at most once on this coroutine. |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 142 | * |
| 143 | * First, this function initializes parent job from the `parentContext` of this coroutine that was passed to it |
| 144 | * during construction. Second, it starts the coroutine based on [start] parameter: |
| 145 | * |
| 146 | * * [DEFAULT] uses [startCoroutineCancellable]. |
| 147 | * * [ATOMIC] uses [startCoroutine]. |
| 148 | * * [UNDISPATCHED] uses [startCoroutineUndispatched]. |
| 149 | * * [LAZY] does nothing. |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 150 | */ |
| 151 | public fun <R> start(start: CoroutineStart, receiver: R, block: suspend R.() -> T) { |
| 152 | initParentJob() |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 153 | start(block, receiver, this) |
| 154 | } |
Roman Elizarov | dfeba24 | 2018-01-24 23:31:22 +0300 | [diff] [blame] | 155 | |
| 156 | // todo: This workaround for KT-21968, should be removed in the future |
| 157 | override fun invokeOnCompletion( |
| 158 | onCancelling: Boolean, |
| 159 | invokeImmediately: Boolean, |
| 160 | handler: CompletionHandler |
| 161 | ): DisposableHandle = |
| 162 | super.invokeOnCompletion(onCancelling, invokeImmediately, handler) |
| 163 | |
| 164 | // todo: This workaround for KT-21968, should be removed in the future |
| 165 | override fun cancel(cause: Throwable?) = |
| 166 | super.cancel(cause) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 167 | } |
| 168 | |