Skip to content

Commit 23fb728

Browse files
committed
AbsractCoroutine documentation & some more common (shared) code;
instrincs package is made public.
1 parent c12123e commit 23fb728

File tree

15 files changed

+67
-262
lines changed

15 files changed

+67
-262
lines changed

common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/AbstractCoroutine.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ import kotlin.coroutines.experimental.*
2323
/**
2424
* Abstract base class for implementation of coroutines in coroutine builders.
2525
*
26-
* * Coroutines implement completion [Continuation], [Job], and [CoroutineScope] interfaces.
27-
* * Coroutine stores the result of continuation in the state of the job.
28-
* * Coroutine waits for children coroutines to finish before completing.
29-
* * Coroutines are cancelled through an intermediate _cancelling_ state.
26+
* This class implements completion [Continuation], [Job], and [CoroutineScope] interfaces.
27+
* It stores the result of continuation in the state of the job.
28+
* This coroutine waits for children coroutines to finish before completing and
29+
* is cancelled through an intermediate _cancelling_ state.
30+
*
31+
* The following methods are available for override:
32+
*
33+
* * [onStart] is invoked when coroutine is create in not active state and is [started][Job.start].
34+
* * [onCancellation] is invoked as soon as coroutine is [cancelled][cancel] (becomes _cancelling_)
35+
* or when it completes for any reason.
36+
* * [onCompleted] is invoked when coroutine completes with a value.
37+
* * [onCompletedExceptionally] in invoked when coroutines completes with exception.
3038
*
3139
* @param parentContext context of the parent coroutine.
3240
* @param active when `true` (by default) coroutine is created in _active_ state, when `false` in _new_ state.
@@ -47,6 +55,7 @@ public abstract class AbstractCoroutine<in T>(
4755
*
4856
* Invocation of this function may cause this coroutine to become cancelled if parent is already cancelled,
4957
* in which case it synchronously invokes all the corresponding handlers.
58+
* @suppress **This is unstable API and it is subject to change.**
5059
*/
5160
internal fun initParentJob() {
5261
initParentJobInternal(parentContext[Job])
@@ -124,7 +133,8 @@ public abstract class AbstractCoroutine<in T>(
124133
}
125134

126135
/**
127-
* Starts the corresponding block as a coroutine with this coroutine start strategy.
136+
* Starts this coroutine with the given code [block] and [start] strategy.
137+
* This function shall be invoked at most once on this coroutine.
128138
*
129139
* First, this function initializes parent job from the `parentContext` of this coroutine that was passed to it
130140
* during construction. Second, it starts the coroutine based on [start] parameter:
@@ -133,8 +143,6 @@ public abstract class AbstractCoroutine<in T>(
133143
* * [ATOMIC] uses [startCoroutine].
134144
* * [UNDISPATCHED] uses [startCoroutineUndispatched].
135145
* * [LAZY] does nothing.
136-
*
137-
* This function shall be invoked at most once.
138146
*/
139147
public fun start(start: CoroutineStart, block: suspend () -> T) {
140148
initParentJob()
@@ -143,7 +151,8 @@ public abstract class AbstractCoroutine<in T>(
143151
}
144152

145153
/**
146-
* Starts the corresponding block with receiver as a coroutine with this coroutine start strategy.
154+
* Starts this coroutine with the given code [block] and [start] strategy.
155+
* This function shall be invoked at most once on this coroutine.
147156
*
148157
* First, this function initializes parent job from the `parentContext` of this coroutine that was passed to it
149158
* during construction. Second, it starts the coroutine based on [start] parameter:
@@ -152,8 +161,6 @@ public abstract class AbstractCoroutine<in T>(
152161
* * [ATOMIC] uses [startCoroutine].
153162
* * [UNDISPATCHED] uses [startCoroutineUndispatched].
154163
* * [LAZY] does nothing.
155-
*
156-
* This function shall be invoked at most once.
157164
*/
158165
public fun <R> start(start: CoroutineStart, receiver: R, block: suspend R.() -> T) {
159166
initParentJob()

common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/CommonCoroutineStart.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CoroutineStart.kt renamed to common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/CoroutineStart.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@ import kotlin.coroutines.experimental.*
2222

2323
/**
2424
* Defines start option for coroutines builders.
25-
* It is used in `start` parameter of [launch], [async], and [actor][kotlinx.coroutines.experimental.channels.actor]
26-
* coroutine builder functions.
25+
* It is used in `start` parameter of [launch], [async], and other coroutine builder functions.
2726
*
2827
* The summary of coroutine start options is:
2928
* * [DEFAULT] -- immediately schedules coroutine for execution according to its context;
3029
* * [LAZY] -- starts coroutine lazily, only when it is needed;
31-
* * [ATOMIC] -- atomically (non-cancellably) schedules coroutine for execution according to its context;
30+
* * [ATOMIC] -- atomically (in a non-cancellable way) schedules coroutine for execution according to its context;
3231
* * [UNDISPATCHED] -- immediately executes coroutine until its first suspension point _in the current thread_.
3332
*/
34-
public actual enum class CoroutineStart {
33+
public enum class CoroutineStart {
3534
/**
3635
* Default -- immediately schedules coroutine for execution according to its context.
3736
*
@@ -53,16 +52,16 @@ public actual enum class CoroutineStart {
5352
/**
5453
* Starts coroutine lazily, only when it is needed.
5554
*
56-
* See the documentation for the corresponding coroutine builders for details:
57-
* [launch], [async], and [actor][kotlinx.coroutines.experimental.channels.actor].
55+
* See the documentation for the corresponding coroutine builders for details
56+
* (like [launch] and [async]).
5857
*
5958
* If coroutine [Job] is cancelled before it even had a chance to start executing, then it will not start its
6059
* execution at all, but complete with an exception.
6160
*/
6261
LAZY,
6362

6463
/**
65-
* Atomically (non-cancellably) schedules coroutine for execution according to its context.
64+
* Atomically (in non-cancellable way) schedules coroutine for execution according to its context.
6665
* This is similar to [DEFAULT], but the coroutine cannot be cancelled before it starts executing.
6766
*
6867
* Cancellability of coroutine at suspension points depends on the particular implementation details of
@@ -94,7 +93,7 @@ public actual enum class CoroutineStart {
9493
* @suppress **Deprecated**: Use [AbstractCoroutine.start]
9594
*/
9695
@Deprecated(message = "Use AbstractCoroutine.start") // todo: make it internal & rename
97-
public actual operator fun <T> invoke(block: suspend () -> T, completion: Continuation<T>) =
96+
public operator fun <T> invoke(block: suspend () -> T, completion: Continuation<T>) =
9897
when (this) {
9998
CoroutineStart.DEFAULT -> block.startCoroutineCancellable(completion)
10099
CoroutineStart.ATOMIC -> block.startCoroutine(completion)
@@ -113,7 +112,7 @@ public actual enum class CoroutineStart {
113112
* @suppress **Deprecated**: Use [AbstractCoroutine.start]
114113
*/
115114
@Deprecated(message = "Use AbstractCoroutine.start") // todo: make it internal & rename
116-
public actual operator fun <R, T> invoke(block: suspend R.() -> T, receiver: R, completion: Continuation<T>) =
115+
public operator fun <R, T> invoke(block: suspend R.() -> T, receiver: R, completion: Continuation<T>) =
117116
when (this) {
118117
CoroutineStart.DEFAULT -> block.startCoroutineCancellable(receiver, completion)
119118
CoroutineStart.ATOMIC -> block.startCoroutine(receiver, completion)
@@ -124,5 +123,5 @@ public actual enum class CoroutineStart {
124123
/**
125124
* Returns `true` when [LAZY].
126125
*/
127-
public actual val isLazy: Boolean get() = this === LAZY
126+
public val isLazy: Boolean get() = this === LAZY
128127
}

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Cancellable.kt renamed to common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/intrinsics/Cancellable.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17-
package kotlinx.coroutines.experimental
17+
package kotlinx.coroutines.experimental.intrinsics
1818

19-
import kotlin.coroutines.experimental.Continuation
20-
import kotlin.coroutines.experimental.intrinsics.createCoroutineUnchecked
19+
import kotlinx.coroutines.experimental.*
20+
import kotlin.coroutines.experimental.*
21+
import kotlin.coroutines.experimental.intrinsics.*
2122

2223
/**
2324
* Use this function to start coroutine in a cancellable way, so that it can be cancelled
2425
* while waiting to be dispatched.
2526
*/
26-
internal fun <T> (suspend () -> T).startCoroutineCancellable(completion: Continuation<T>) =
27+
public fun <T> (suspend () -> T).startCoroutineCancellable(completion: Continuation<T>) =
2728
createCoroutineUnchecked(completion).resumeCancellable(Unit)
2829

2930
/**
3031
* Use this function to start coroutine in a cancellable way, so that it can be cancelled
3132
* while waiting to be dispatched.
3233
*/
33-
internal fun <R, T> (suspend (R) -> T).startCoroutineCancellable(receiver: R, completion: Continuation<T>) =
34+
public fun <R, T> (suspend (R) -> T).startCoroutineCancellable(receiver: R, completion: Continuation<T>) =
3435
createCoroutineUnchecked(receiver, completion).resumeCancellable(Unit)
35-

common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/intrinsics/Undispatched.kt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ import kotlin.coroutines.experimental.*
2121
import kotlin.coroutines.experimental.intrinsics.*
2222

2323
/**
24-
* Use this function to restart coroutine directly from inside of [suspendCoroutine].
25-
*
26-
* @suppress **This is unstable API and it is subject to change.**
24+
* Use this function to restart coroutine directly from inside of [suspendCoroutine] in the same context.
2725
*/
2826
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
29-
internal fun <T> (suspend () -> T).startCoroutineUndispatched(completion: Continuation<T>) {
27+
public fun <T> (suspend () -> T).startCoroutineUndispatched(completion: Continuation<T>) {
3028
val value = try {
3129
startCoroutineUninterceptedOrReturn(completion)
3230
} catch (e: Throwable) {
@@ -38,12 +36,10 @@ internal fun <T> (suspend () -> T).startCoroutineUndispatched(completion: Contin
3836
}
3937

4038
/**
41-
* Use this function to restart coroutine directly from inside of [suspendCoroutine].
42-
*
43-
* @suppress **This is unstable API and it is subject to change.**
39+
* Use this function to restart coroutine directly from inside of [suspendCoroutine] in the same context.
4440
*/
4541
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
46-
internal fun <R, T> (suspend (R) -> T).startCoroutineUndispatched(receiver: R, completion: Continuation<T>) {
42+
public fun <R, T> (suspend (R) -> T).startCoroutineUndispatched(receiver: R, completion: Continuation<T>) {
4743
val value = try {
4844
startCoroutineUninterceptedOrReturn(receiver, completion)
4945
} catch (e: Throwable) {
@@ -55,29 +51,25 @@ internal fun <R, T> (suspend (R) -> T).startCoroutineUndispatched(receiver: R, c
5551
}
5652

5753
/**
58-
* Starts the corresponding block as a coroutine with this coroutine start strategy.
54+
* Starts this coroutine with the given code [block] in the same context and returns result when it
55+
* completes without suspnesion.
56+
* This function shall be invoked at most once on this coroutine.
5957
*
6058
* First, this function initializes parent job from the `parentContext` of this coroutine that was passed to it
6159
* during construction. Second, it starts the coroutine using [startCoroutineUninterceptedOrReturn].
62-
*
63-
* This function shall be invoked at most once.
64-
*
65-
* @suppress **This is unstable API and it is subject to change.**
6660
*/
6761
public fun <T> AbstractCoroutine<T>.startUndispatchedOrReturn(block: suspend () -> T): Any? {
6862
initParentJob()
6963
return undispatchedResult { block.startCoroutineUninterceptedOrReturn(this) }
7064
}
7165

7266
/**
73-
* Starts the corresponding block with receiver as a coroutine with this coroutine start strategy.
67+
* Starts this coroutine with the given code [block] in the same context and returns result when it
68+
* completes without suspnesion.
69+
* This function shall be invoked at most once on this coroutine.
7470
*
7571
* First, this function initializes parent job from the `parentContext` of this coroutine that was passed to it
7672
* during construction. Second, it starts the coroutine using [startCoroutineUninterceptedOrReturn].
77-
*
78-
* This function shall be invoked at most once.
79-
*
80-
* @suppress **This is unstable API and it is subject to change.**
8173
*/
8274
public fun <T, R> AbstractCoroutine<T>.startUndispatchedOrReturn(receiver: R, block: suspend R.() -> T): Any? {
8375
initParentJob()

core/kotlinx-coroutines-core/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ Channels -- non-blocking primitives for communicating a stream of elements betwe
8383

8484
Select expression to perform multiple suspending operations simultaneously until one of them succeeds.
8585

86+
# Package kotlinx.coroutines.experimental.intrinsics
87+
88+
Low-level primitives for finer-grained control of coroutines.
89+
8690
<!--- MODULE kotlinx-coroutines-core -->
8791
<!--- INDEX kotlinx.coroutines.experimental -->
8892
[launch]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/launch.html

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Builders.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package kotlinx.coroutines.experimental
1818

19+
import kotlinx.coroutines.experimental.intrinsics.*
1920
import java.util.concurrent.locks.*
2021
import kotlin.coroutines.experimental.*
2122
import kotlin.coroutines.experimental.intrinsics.*

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Deferred.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package kotlinx.coroutines.experimental
1818

19+
import kotlinx.coroutines.experimental.intrinsics.*
1920
import kotlinx.coroutines.experimental.selects.*
2021
import kotlin.coroutines.experimental.*
2122

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Job.kt

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,13 @@
1616

1717
package kotlinx.coroutines.experimental
1818

19-
import kotlinx.atomicfu.atomic
20-
import kotlinx.atomicfu.loop
21-
import kotlinx.coroutines.experimental.internal.LockFreeLinkedListHead
22-
import kotlinx.coroutines.experimental.internal.LockFreeLinkedListNode
23-
import kotlinx.coroutines.experimental.internal.OpDescriptor
24-
import kotlinx.coroutines.experimental.internal.unwrap
25-
import kotlinx.coroutines.experimental.intrinsics.startCoroutineUndispatched
26-
import kotlinx.coroutines.experimental.selects.SelectClause0
27-
import kotlinx.coroutines.experimental.selects.SelectInstance
28-
import kotlinx.coroutines.experimental.selects.select
29-
import java.util.concurrent.Future
30-
import kotlin.coroutines.experimental.Continuation
31-
import kotlin.coroutines.experimental.CoroutineContext
32-
import kotlin.coroutines.experimental.buildSequence
33-
import kotlin.coroutines.experimental.intrinsics.suspendCoroutineOrReturn
19+
import kotlinx.atomicfu.*
20+
import kotlinx.coroutines.experimental.internal.*
21+
import kotlinx.coroutines.experimental.intrinsics.*
22+
import kotlinx.coroutines.experimental.selects.*
23+
import java.util.concurrent.*
24+
import kotlin.coroutines.experimental.*
25+
import kotlin.coroutines.experimental.intrinsics.*
3426

3527
// --------------- core job interfaces ---------------
3628

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/Actor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package kotlinx.coroutines.experimental.channels
1818

1919
import kotlinx.coroutines.experimental.*
20+
import kotlinx.coroutines.experimental.intrinsics.*
2021
import kotlinx.coroutines.experimental.selects.*
2122
import kotlin.coroutines.experimental.*
2223

0 commit comments

Comments
 (0)