Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 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 | |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.CoroutineStart.* |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 20 | import kotlinx.coroutines.experimental.intrinsics.* |
| 21 | import kotlin.coroutines.experimental.* |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Defines start option for coroutines builders. |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame^] | 25 | * It is used in `start` parameter of [launch], [async], and other coroutine builder functions. |
Roman Elizarov | 7b10c94 | 2017-05-16 21:02:51 +0300 | [diff] [blame] | 26 | * |
| 27 | * The summary of coroutine start options is: |
| 28 | * * [DEFAULT] -- immediately schedules coroutine for execution according to its context; |
| 29 | * * [LAZY] -- starts coroutine lazily, only when it is needed; |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame^] | 30 | * * [ATOMIC] -- atomically (in a non-cancellable way) schedules coroutine for execution according to its context; |
Roman Elizarov | 7b10c94 | 2017-05-16 21:02:51 +0300 | [diff] [blame] | 31 | * * [UNDISPATCHED] -- immediately executes coroutine until its first suspension point _in the current thread_. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 32 | */ |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame^] | 33 | public enum class CoroutineStart { |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 34 | /** |
Roman Elizarov | 7b10c94 | 2017-05-16 21:02:51 +0300 | [diff] [blame] | 35 | * Default -- immediately schedules coroutine for execution according to its context. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 36 | * |
| 37 | * If the [CoroutineDispatcher] of the coroutine context returns `true` from [CoroutineDispatcher.isDispatchNeeded] |
| 38 | * function as most dispatchers do, then the coroutine code is dispatched for execution later, while the code that |
| 39 | * invoked the coroutine builder continues execution. |
| 40 | * |
| 41 | * Note, that [Unconfined] dispatcher always returns `false` from its [CoroutineDispatcher.isDispatchNeeded] |
| 42 | * function, so starting coroutine with [Unconfined] dispatcher by [DEFAULT] is the same as using [UNDISPATCHED]. |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 43 | * |
| 44 | * If coroutine [Job] is cancelled before it even had a chance to start executing, then it will not start its |
| 45 | * execution at all, but complete with an exception. |
| 46 | * |
| 47 | * Cancellability of coroutine at suspension points depends on the particular implementation details of |
| 48 | * suspending functions. Use [suspendCancellableCoroutine] to implement cancellable suspending functions. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 49 | */ |
| 50 | DEFAULT, |
| 51 | |
| 52 | /** |
| 53 | * Starts coroutine lazily, only when it is needed. |
| 54 | * |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame^] | 55 | * See the documentation for the corresponding coroutine builders for details |
| 56 | * (like [launch] and [async]). |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 57 | * |
| 58 | * If coroutine [Job] is cancelled before it even had a chance to start executing, then it will not start its |
| 59 | * execution at all, but complete with an exception. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 60 | */ |
| 61 | LAZY, |
| 62 | |
| 63 | /** |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame^] | 64 | * Atomically (in non-cancellable way) schedules coroutine for execution according to its context. |
Roman Elizarov | 7b10c94 | 2017-05-16 21:02:51 +0300 | [diff] [blame] | 65 | * This is similar to [DEFAULT], but the coroutine cannot be cancelled before it starts executing. |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 66 | * |
| 67 | * Cancellability of coroutine at suspension points depends on the particular implementation details of |
| 68 | * suspending functions as in [DEFAULT]. |
| 69 | */ |
| 70 | ATOMIC, |
| 71 | |
| 72 | /** |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 73 | * Immediately executes coroutine until its first suspension point _in the current thread_ as if it the |
| 74 | * coroutine was started using [Unconfined] dispatcher. However, when coroutine is resumed from suspension |
| 75 | * it is dispatched according to the [CoroutineDispatcher] in its context. |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 76 | * |
| 77 | * This is similar to [ATOMIC] in the sense that coroutine starts executing even if it was already cancelled, |
| 78 | * but the difference is that it start executing in the same thread. |
| 79 | * |
| 80 | * Cancellability of coroutine at suspension points depends on the particular implementation details of |
| 81 | * suspending functions as in [DEFAULT]. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 82 | */ |
| 83 | UNDISPATCHED; |
| 84 | |
| 85 | /** |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 86 | * Starts the corresponding block as a coroutine with this coroutine start strategy. |
| 87 | * |
| 88 | * * [DEFAULT] uses [startCoroutineCancellable]. |
| 89 | * * [ATOMIC] uses [startCoroutine]. |
| 90 | * * [UNDISPATCHED] uses [startCoroutineUndispatched]. |
| 91 | * * [LAZY] does nothing. |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 92 | * |
| 93 | * @suppress **Deprecated**: Use [AbstractCoroutine.start] |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 94 | */ |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 95 | @Deprecated(message = "Use AbstractCoroutine.start") // todo: make it internal & rename |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame^] | 96 | public operator fun <T> invoke(block: suspend () -> T, completion: Continuation<T>) = |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 97 | when (this) { |
| 98 | CoroutineStart.DEFAULT -> block.startCoroutineCancellable(completion) |
| 99 | CoroutineStart.ATOMIC -> block.startCoroutine(completion) |
| 100 | CoroutineStart.UNDISPATCHED -> block.startCoroutineUndispatched(completion) |
| 101 | CoroutineStart.LAZY -> Unit // will start lazily |
| 102 | } |
| 103 | |
| 104 | /** |
Roman Elizarov | 7b10c94 | 2017-05-16 21:02:51 +0300 | [diff] [blame] | 105 | * Starts the corresponding block with receiver as a coroutine with this coroutine start strategy. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 106 | * |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 107 | * * [DEFAULT] uses [startCoroutineCancellable]. |
| 108 | * * [ATOMIC] uses [startCoroutine]. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 109 | * * [UNDISPATCHED] uses [startCoroutineUndispatched]. |
| 110 | * * [LAZY] does nothing. |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 111 | * |
| 112 | * @suppress **Deprecated**: Use [AbstractCoroutine.start] |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 113 | */ |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 114 | @Deprecated(message = "Use AbstractCoroutine.start") // todo: make it internal & rename |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame^] | 115 | public operator fun <R, T> invoke(block: suspend R.() -> T, receiver: R, completion: Continuation<T>) = |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 116 | when (this) { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 117 | CoroutineStart.DEFAULT -> block.startCoroutineCancellable(receiver, completion) |
| 118 | CoroutineStart.ATOMIC -> block.startCoroutine(receiver, completion) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 119 | CoroutineStart.UNDISPATCHED -> block.startCoroutineUndispatched(receiver, completion) |
| 120 | CoroutineStart.LAZY -> Unit // will start lazily |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns `true` when [LAZY]. |
| 125 | */ |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame^] | 126 | public val isLazy: Boolean get() = this === LAZY |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 127 | } |