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 | /** |
paolop | c51bde0 | 2018-06-09 09:40:11 +0000 | [diff] [blame] | 24 | * Defines start options 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 |
paolop | c51bde0 | 2018-06-09 09:40:11 +0000 | [diff] [blame] | 45 | * execution at all, but will complete with an exception. |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 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 |
paolop | c51bde0 | 2018-06-09 09:40:11 +0000 | [diff] [blame] | 59 | * execution at all, but will complete with an exception. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 60 | */ |
| 61 | LAZY, |
| 62 | |
| 63 | /** |
paolop | c51bde0 | 2018-06-09 09:40:11 +0000 | [diff] [blame] | 64 | * Atomically (i.e., 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] | 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 | /** |
paolop | c51bde0 | 2018-06-09 09:40:11 +0000 | [diff] [blame] | 73 | * Immediately executes coroutine until its first suspension point _in the current thread_ as if the |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 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, |
paolop | c51bde0 | 2018-06-09 09:40:11 +0000 | [diff] [blame] | 78 | * but the difference is that it starts executing in the same thread. |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 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. |
| 92 | */ |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 93 | public operator fun <T> invoke(block: suspend () -> T, completion: Continuation<T>) = |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 94 | when (this) { |
| 95 | CoroutineStart.DEFAULT -> block.startCoroutineCancellable(completion) |
| 96 | CoroutineStart.ATOMIC -> block.startCoroutine(completion) |
| 97 | CoroutineStart.UNDISPATCHED -> block.startCoroutineUndispatched(completion) |
| 98 | CoroutineStart.LAZY -> Unit // will start lazily |
| 99 | } |
| 100 | |
| 101 | /** |
Roman Elizarov | 7b10c94 | 2017-05-16 21:02:51 +0300 | [diff] [blame] | 102 | * 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] | 103 | * |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 104 | * * [DEFAULT] uses [startCoroutineCancellable]. |
| 105 | * * [ATOMIC] uses [startCoroutine]. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 106 | * * [UNDISPATCHED] uses [startCoroutineUndispatched]. |
| 107 | * * [LAZY] does nothing. |
| 108 | */ |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 109 | 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] | 110 | when (this) { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 111 | CoroutineStart.DEFAULT -> block.startCoroutineCancellable(receiver, completion) |
| 112 | CoroutineStart.ATOMIC -> block.startCoroutine(receiver, completion) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 113 | CoroutineStart.UNDISPATCHED -> block.startCoroutineUndispatched(receiver, completion) |
| 114 | CoroutineStart.LAZY -> Unit // will start lazily |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns `true` when [LAZY]. |
| 119 | */ |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 120 | public val isLazy: Boolean get() = this === LAZY |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 121 | } |