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