blob: 4ce9df4f402c5fa5ecdc63501859d647d08bf4c3 [file] [log] [blame]
Roman Elizarovecda27f2017-04-06 23:06:26 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarovecda27f2017-04-06 23:06:26 +03003 */
4
5package kotlinx.coroutines.experimental
6
Roman Elizarov4eae2a82017-05-17 20:55:27 +03007import kotlinx.coroutines.experimental.CoroutineStart.*
Roman Elizarov2adf8bc2018-01-24 20:09:57 +03008import kotlinx.coroutines.experimental.intrinsics.*
9import kotlin.coroutines.experimental.*
Roman Elizarovecda27f2017-04-06 23:06:26 +030010
11/**
paolopc51bde02018-06-09 09:40:11 +000012 * Defines start options for coroutines builders.
Roman Elizarovc32579e2018-09-09 19:21:43 +030013 * It is used in `start` parameter of [launch][CoroutineScope.launch], [async][CoroutineScope.async], and other coroutine builder functions.
Roman Elizarov7b10c942017-05-16 21:02:51 +030014 *
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 Elizarov23fb7282018-01-24 23:09:42 +030018 * * [ATOMIC] -- atomically (in a non-cancellable way) schedules coroutine for execution according to its context;
Roman Elizarov7b10c942017-05-16 21:02:51 +030019 * * [UNDISPATCHED] -- immediately executes coroutine until its first suspension point _in the current thread_.
Roman Elizarovecda27f2017-04-06 23:06:26 +030020 */
Roman Elizarov23fb7282018-01-24 23:09:42 +030021public enum class CoroutineStart {
Roman Elizarovecda27f2017-04-06 23:06:26 +030022 /**
Roman Elizarov7b10c942017-05-16 21:02:51 +030023 * Default -- immediately schedules coroutine for execution according to its context.
Roman Elizarovecda27f2017-04-06 23:06:26 +030024 *
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 Elizarovdc29b072018-09-11 18:42:03 +030029 * 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 Elizarova74eb5f2017-05-11 20:15:18 +030031 *
32 * If coroutine [Job] is cancelled before it even had a chance to start executing, then it will not start its
paolopc51bde02018-06-09 09:40:11 +000033 * execution at all, but will complete with an exception.
Roman Elizarova74eb5f2017-05-11 20:15:18 +030034 *
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 Elizarovecda27f2017-04-06 23:06:26 +030037 */
38 DEFAULT,
39
40 /**
41 * Starts coroutine lazily, only when it is needed.
42 *
Roman Elizarov23fb7282018-01-24 23:09:42 +030043 * See the documentation for the corresponding coroutine builders for details
Roman Elizarovc32579e2018-09-09 19:21:43 +030044 * (like [launch][CoroutineScope.launch] and [async][CoroutineScope.async]).
Roman Elizarova74eb5f2017-05-11 20:15:18 +030045 *
46 * If coroutine [Job] is cancelled before it even had a chance to start executing, then it will not start its
paolopc51bde02018-06-09 09:40:11 +000047 * execution at all, but will complete with an exception.
Roman Elizarovecda27f2017-04-06 23:06:26 +030048 */
49 LAZY,
50
51 /**
paolopc51bde02018-06-09 09:40:11 +000052 * Atomically (i.e., in a non-cancellable way) schedules coroutine for execution according to its context.
Roman Elizarov7b10c942017-05-16 21:02:51 +030053 * This is similar to [DEFAULT], but the coroutine cannot be cancelled before it starts executing.
Roman Elizarova74eb5f2017-05-11 20:15:18 +030054 *
55 * Cancellability of coroutine at suspension points depends on the particular implementation details of
56 * suspending functions as in [DEFAULT].
Roman Elizarov27b8f452018-09-20 21:23:41 +030057 *
58 * @suppress **This an internal API and should not be used from general code.**
Roman Elizarova74eb5f2017-05-11 20:15:18 +030059 */
Roman Elizarov27b8f452018-09-20 21:23:41 +030060 @InternalCoroutinesApi
Roman Elizarova74eb5f2017-05-11 20:15:18 +030061 ATOMIC,
62
63 /**
paolopc51bde02018-06-09 09:40:11 +000064 * Immediately executes coroutine until its first suspension point _in the current thread_ as if the
Roman Elizarovdc29b072018-09-11 18:42:03 +030065 * coroutine was started using [Dispatchers.Unconfined]. However, when coroutine is resumed from suspension
Roman Elizarovecda27f2017-04-06 23:06:26 +030066 * it is dispatched according to the [CoroutineDispatcher] in its context.
Roman Elizarov4eae2a82017-05-17 20:55:27 +030067 *
68 * This is similar to [ATOMIC] in the sense that coroutine starts executing even if it was already cancelled,
paolopc51bde02018-06-09 09:40:11 +000069 * but the difference is that it starts executing in the same thread.
Roman Elizarov4eae2a82017-05-17 20:55:27 +030070 *
71 * Cancellability of coroutine at suspension points depends on the particular implementation details of
72 * suspending functions as in [DEFAULT].
Roman Elizarov27b8f452018-09-20 21:23:41 +030073 *
74 * **Note: This is an experimental api.** Execution semantics of coroutines may change in the future when this mode is used.
Roman Elizarovecda27f2017-04-06 23:06:26 +030075 */
Roman Elizarov27b8f452018-09-20 21:23:41 +030076 @ExperimentalCoroutinesApi
Roman Elizarovecda27f2017-04-06 23:06:26 +030077 UNDISPATCHED;
78
79 /**
Roman Elizarov4eae2a82017-05-17 20:55:27 +030080 * 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 Elizarov27b8f452018-09-20 21:23:41 +030086 *
87 * @suppress **This an internal API and should not be used from general code.**
Roman Elizarov4eae2a82017-05-17 20:55:27 +030088 */
Roman Elizarov27b8f452018-09-20 21:23:41 +030089 @InternalCoroutinesApi
Roman Elizarov23fb7282018-01-24 23:09:42 +030090 public operator fun <T> invoke(block: suspend () -> T, completion: Continuation<T>) =
Roman Elizarov4eae2a82017-05-17 20:55:27 +030091 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 Elizarov7b10c942017-05-16 21:02:51 +030099 * Starts the corresponding block with receiver as a coroutine with this coroutine start strategy.
Roman Elizarovecda27f2017-04-06 23:06:26 +0300100 *
Roman Elizarova74eb5f2017-05-11 20:15:18 +0300101 * * [DEFAULT] uses [startCoroutineCancellable].
102 * * [ATOMIC] uses [startCoroutine].
Roman Elizarovecda27f2017-04-06 23:06:26 +0300103 * * [UNDISPATCHED] uses [startCoroutineUndispatched].
104 * * [LAZY] does nothing.
Roman Elizarov27b8f452018-09-20 21:23:41 +0300105 *
106 * @suppress **This an internal API and should not be used from general code.**
Roman Elizarovecda27f2017-04-06 23:06:26 +0300107 */
Roman Elizarov27b8f452018-09-20 21:23:41 +0300108 @InternalCoroutinesApi
Roman Elizarov23fb7282018-01-24 23:09:42 +0300109 public operator fun <R, T> invoke(block: suspend R.() -> T, receiver: R, completion: Continuation<T>) =
Roman Elizarovecda27f2017-04-06 23:06:26 +0300110 when (this) {
Roman Elizarova74eb5f2017-05-11 20:15:18 +0300111 CoroutineStart.DEFAULT -> block.startCoroutineCancellable(receiver, completion)
112 CoroutineStart.ATOMIC -> block.startCoroutine(receiver, completion)
Roman Elizarovecda27f2017-04-06 23:06:26 +0300113 CoroutineStart.UNDISPATCHED -> block.startCoroutineUndispatched(receiver, completion)
114 CoroutineStart.LAZY -> Unit // will start lazily
115 }
116
117 /**
118 * Returns `true` when [LAZY].
Roman Elizarov27b8f452018-09-20 21:23:41 +0300119 *
120 * @suppress **This an internal API and should not be used from general code.**
Roman Elizarovecda27f2017-04-06 23:06:26 +0300121 */
Roman Elizarov27b8f452018-09-20 21:23:41 +0300122 @InternalCoroutinesApi
Roman Elizarov23fb7282018-01-24 23:09:42 +0300123 public val isLazy: Boolean get() = this === LAZY
Roman Elizarovecda27f2017-04-06 23:06:26 +0300124}