Skip to content

Commit a58ccc0

Browse files
committed
Dispatchers are renamed and grouped in the Dispatchers object
* Dispatchers.Default — a default dispatcher for background asynchronous tasks (currently backed by FJP commonPool, a new dispatcher in the future). * Dispatchers.IO — a dispatcher for blocking background operations (#79). * Dispatchers.Main — a dispatcher for Android Main Thread (#533). * Dispatchers.Swing — a dispatcher for Swing Event Dispatch Thread. * Dispatchers.JavaFx — a dispatcher for JavaFx Application Thread. * Old dispatchers are deprecated, CommonPool is deprecated, too. * awaitPulse() in JavaFx and awaitFrame() in Android are top-level funs. * Introduced HandlerDispatcher, SwingDispatcher, and JavaFxDispatcher types in the corresponding UI modules for type-safety and future extensions Fixes #41 Fixes #533
1 parent b65ea36 commit a58ccc0

File tree

116 files changed

+782
-541
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+782
-541
lines changed

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-android.txt

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@ public final class kotlinx/coroutines/experimental/android/AndroidExceptionPreHa
33
public fun handleException (Lkotlin/coroutines/experimental/CoroutineContext;Ljava/lang/Throwable;)V
44
}
55

6-
public final class kotlinx/coroutines/experimental/android/HandlerContext : kotlinx/coroutines/experimental/CoroutineDispatcher, kotlinx/coroutines/experimental/Delay {
6+
public final class kotlinx/coroutines/experimental/android/HandlerContext : kotlinx/coroutines/experimental/android/HandlerDispatcher, kotlinx/coroutines/experimental/Delay {
77
public fun <init> (Landroid/os/Handler;Ljava/lang/String;)V
88
public synthetic fun <init> (Landroid/os/Handler;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
99
public final fun awaitFrame (Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
10-
public fun delay (JLjava/util/concurrent/TimeUnit;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
1110
public fun dispatch (Lkotlin/coroutines/experimental/CoroutineContext;Ljava/lang/Runnable;)V
1211
public fun equals (Ljava/lang/Object;)Z
13-
public final fun getImmediate ()Lkotlinx/coroutines/experimental/android/HandlerContext;
12+
public fun getImmediate ()Lkotlinx/coroutines/experimental/android/HandlerContext;
13+
public synthetic fun getImmediate ()Lkotlinx/coroutines/experimental/android/HandlerDispatcher;
1414
public fun hashCode ()I
1515
public fun invokeOnTimeout (JLjava/util/concurrent/TimeUnit;Ljava/lang/Runnable;)Lkotlinx/coroutines/experimental/DisposableHandle;
1616
public fun isDispatchNeeded (Lkotlin/coroutines/experimental/CoroutineContext;)Z
1717
public fun scheduleResumeAfterDelay (JLjava/util/concurrent/TimeUnit;Lkotlinx/coroutines/experimental/CancellableContinuation;)V
1818
public fun toString ()Ljava/lang/String;
1919
}
2020

21-
public final class kotlinx/coroutines/experimental/android/HandlerContextKt {
22-
public static final fun asCoroutineDispatcher (Landroid/os/Handler;)Lkotlinx/coroutines/experimental/android/HandlerContext;
21+
public abstract class kotlinx/coroutines/experimental/android/HandlerDispatcher : kotlinx/coroutines/experimental/CoroutineDispatcher, kotlinx/coroutines/experimental/Delay {
22+
public fun delay (JLjava/util/concurrent/TimeUnit;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
23+
public abstract fun getImmediate ()Lkotlinx/coroutines/experimental/android/HandlerDispatcher;
24+
public fun invokeOnTimeout (JLjava/util/concurrent/TimeUnit;Ljava/lang/Runnable;)Lkotlinx/coroutines/experimental/DisposableHandle;
25+
}
26+
27+
public final class kotlinx/coroutines/experimental/android/HandlerDispatcherKt {
28+
public static final synthetic fun asCoroutineDispatcher (Landroid/os/Handler;)Lkotlinx/coroutines/experimental/android/HandlerContext;
29+
public static final fun asCoroutineDispatcher (Landroid/os/Handler;)Lkotlinx/coroutines/experimental/android/HandlerDispatcher;
30+
public static final fun awaitFrame (Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
31+
public static final fun getMain (Lkotlinx/coroutines/experimental/Dispatchers;)Lkotlinx/coroutines/experimental/android/HandlerDispatcher;
2332
public static final fun getUI ()Lkotlinx/coroutines/experimental/android/HandlerContext;
2433
}
2534

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-core.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public final class kotlinx/coroutines/experimental/CoroutineContextKt {
139139
public static final field DEBUG_PROPERTY_VALUE_AUTO Ljava/lang/String;
140140
public static final field DEBUG_PROPERTY_VALUE_OFF Ljava/lang/String;
141141
public static final field DEBUG_PROPERTY_VALUE_ON Ljava/lang/String;
142-
public static final field IO_PARALLELISM_PROPERTY_NAME Ljava/lang/String;
143142
public static final fun getDefaultDispatcher ()Lkotlinx/coroutines/experimental/CoroutineDispatcher;
144143
public static final fun getIO ()Lkotlinx/coroutines/experimental/CoroutineDispatcher;
145144
public static final fun newCoroutineContext (Lkotlin/coroutines/experimental/CoroutineContext;)Lkotlin/coroutines/experimental/CoroutineContext;
@@ -297,6 +296,17 @@ public final class kotlinx/coroutines/experimental/DispatchedTask$DefaultImpls {
297296
public static fun run (Lkotlinx/coroutines/experimental/DispatchedTask;)V
298297
}
299298

299+
public final class kotlinx/coroutines/experimental/Dispatchers {
300+
public static final field Default Lkotlinx/coroutines/experimental/CoroutineDispatcher;
301+
public static final field INSTANCE Lkotlinx/coroutines/experimental/Dispatchers;
302+
public static final field Unconfined Lkotlinx/coroutines/experimental/CoroutineDispatcher;
303+
}
304+
305+
public final class kotlinx/coroutines/experimental/DispatchersKt {
306+
public static final field IO_PARALLELISM_PROPERTY_NAME Ljava/lang/String;
307+
public static final fun getIO (Lkotlinx/coroutines/experimental/Dispatchers;)Lkotlinx/coroutines/experimental/CoroutineDispatcher;
308+
}
309+
300310
public abstract interface class kotlinx/coroutines/experimental/DisposableHandle {
301311
public abstract fun dispose ()V
302312
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
public final class kotlinx/coroutines/experimental/javafx/JavaFx : kotlinx/coroutines/experimental/CoroutineDispatcher, kotlinx/coroutines/experimental/Delay {
1+
public final class kotlinx/coroutines/experimental/javafx/JavaFx : kotlinx/coroutines/experimental/javafx/JavaFxDispatcher, kotlinx/coroutines/experimental/Delay {
22
public static final field INSTANCE Lkotlinx/coroutines/experimental/javafx/JavaFx;
33
public final fun awaitPulse (Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
4-
public fun delay (JLjava/util/concurrent/TimeUnit;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
54
public fun dispatch (Lkotlin/coroutines/experimental/CoroutineContext;Ljava/lang/Runnable;)V
65
public fun invokeOnTimeout (JLjava/util/concurrent/TimeUnit;Ljava/lang/Runnable;)Lkotlinx/coroutines/experimental/DisposableHandle;
76
public fun scheduleResumeAfterDelay (JLjava/util/concurrent/TimeUnit;Lkotlinx/coroutines/experimental/CancellableContinuation;)V
87
public fun toString ()Ljava/lang/String;
98
}
109

10+
public abstract class kotlinx/coroutines/experimental/javafx/JavaFxDispatcher : kotlinx/coroutines/experimental/CoroutineDispatcher, kotlinx/coroutines/experimental/Delay {
11+
public fun delay (JLjava/util/concurrent/TimeUnit;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
12+
public fun invokeOnTimeout (JLjava/util/concurrent/TimeUnit;Ljava/lang/Runnable;)Lkotlinx/coroutines/experimental/DisposableHandle;
13+
}
14+
15+
public final class kotlinx/coroutines/experimental/javafx/JavaFxDispatcherKt {
16+
public static final fun awaitPulse (Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
17+
public static final fun getJavaFx (Lkotlinx/coroutines/experimental/Dispatchers;)Lkotlinx/coroutines/experimental/javafx/JavaFxDispatcher;
18+
}
19+
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
public final class kotlinx/coroutines/experimental/swing/Swing : kotlinx/coroutines/experimental/CoroutineDispatcher, kotlinx/coroutines/experimental/Delay {
1+
public final class kotlinx/coroutines/experimental/swing/Swing : kotlinx/coroutines/experimental/swing/SwingDispatcher, kotlinx/coroutines/experimental/Delay {
22
public static final field INSTANCE Lkotlinx/coroutines/experimental/swing/Swing;
3-
public fun delay (JLjava/util/concurrent/TimeUnit;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
43
public fun dispatch (Lkotlin/coroutines/experimental/CoroutineContext;Ljava/lang/Runnable;)V
54
public fun invokeOnTimeout (JLjava/util/concurrent/TimeUnit;Ljava/lang/Runnable;)Lkotlinx/coroutines/experimental/DisposableHandle;
65
public fun scheduleResumeAfterDelay (JLjava/util/concurrent/TimeUnit;Lkotlinx/coroutines/experimental/CancellableContinuation;)V
76
public fun toString ()Ljava/lang/String;
87
}
98

9+
public abstract class kotlinx/coroutines/experimental/swing/SwingDispatcher : kotlinx/coroutines/experimental/CoroutineDispatcher, kotlinx/coroutines/experimental/Delay {
10+
public fun delay (JLjava/util/concurrent/TimeUnit;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
11+
public fun invokeOnTimeout (JLjava/util/concurrent/TimeUnit;Ljava/lang/Runnable;)Lkotlinx/coroutines/experimental/DisposableHandle;
12+
}
13+
14+
public final class kotlinx/coroutines/experimental/swing/SwingDispatcherKt {
15+
public static final fun getSwing (Lkotlinx/coroutines/experimental/Dispatchers;)Lkotlinx/coroutines/experimental/swing/SwingDispatcher;
16+
}
17+

common/kotlinx-coroutines-core-common/src/Builders.common.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import kotlin.coroutines.experimental.intrinsics.*
1919
* The coroutine is cancelled when the resulting job is [cancelled][Job.cancel].
2020
*
2121
* Coroutine context is inherited from a [CoroutineScope], additional context elements can be specified with [context] argument.
22-
* If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [DefaultDispatcher] is used.
22+
* If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used.
2323
* The parent job is inherited from a [CoroutineScope] as well, but it can also be overridden
2424
* with corresponding [coroutineContext] element.
2525
*
@@ -35,10 +35,10 @@ import kotlin.coroutines.experimental.intrinsics.*
3535
*
3636
* See [newCoroutineContext] for a description of debugging facilities that are available for newly created coroutine.
3737
*
38-
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine
38+
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
3939
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
4040
* @param onCompletion optional completion handler for the coroutine (see [Job.invokeOnCompletion]).
41-
* @param block the coroutine code which will be invoked in the context of the provided scope
41+
* @param block the coroutine code which will be invoked in the context of the provided scope.
4242
**/
4343
public fun CoroutineScope.launch(
4444
context: CoroutineContext = EmptyCoroutineContext,
@@ -64,7 +64,7 @@ public fun CoroutineScope.launch(
6464
replaceWith = ReplaceWith("GlobalScope.launch(context, start, onCompletion, block)", imports = ["kotlinx.coroutines.experimental.*"])
6565
)
6666
public fun launch(
67-
context: CoroutineContext = DefaultDispatcher,
67+
context: CoroutineContext = Dispatchers.Default,
6868
start: CoroutineStart = CoroutineStart.DEFAULT,
6969
onCompletion: CompletionHandler? = null,
7070
block: suspend CoroutineScope.() -> Unit
@@ -80,7 +80,7 @@ public fun launch(
8080
replaceWith = ReplaceWith("GlobalScope.launch(context + parent, start, onCompletion, block)", imports = ["kotlinx.coroutines.experimental.*"])
8181
)
8282
public fun launch(
83-
context: CoroutineContext = DefaultDispatcher,
83+
context: CoroutineContext = Dispatchers.Default,
8484
start: CoroutineStart = CoroutineStart.DEFAULT,
8585
parent: Job? = null, // nullable for binary compatibility
8686
onCompletion: CompletionHandler? = null,
@@ -91,7 +91,7 @@ public fun launch(
9191
/** @suppress **Deprecated**: Binary compatibility */
9292
@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
9393
public fun launch(
94-
context: CoroutineContext = DefaultDispatcher,
94+
context: CoroutineContext = Dispatchers.Default,
9595
start: CoroutineStart = CoroutineStart.DEFAULT,
9696
parent: Job? = null,
9797
block: suspend CoroutineScope.() -> Unit
@@ -101,7 +101,7 @@ public fun launch(
101101
/** @suppress **Deprecated**: Binary compatibility */
102102
@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
103103
public fun launch(
104-
context: CoroutineContext = DefaultDispatcher,
104+
context: CoroutineContext = Dispatchers.Default,
105105
start: CoroutineStart = CoroutineStart.DEFAULT,
106106
block: suspend CoroutineScope.() -> Unit
107107
): Job =

common/kotlinx-coroutines-core-common/src/CoroutineContext.common.kt

+6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ import kotlin.coroutines.experimental.*
99
public expect fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext
1010

1111
@Suppress("PropertyName")
12+
@Deprecated(
13+
message = "Use Dispatchers.Default",
14+
replaceWith = ReplaceWith("Dispatchers.Default",
15+
imports = ["kotlinx.coroutines.experimental.Dispatchers"]))
1216
public expect val DefaultDispatcher: CoroutineDispatcher
1317

18+
internal expect fun createDefaultDispatcher(): CoroutineDispatcher
19+
1420
@Suppress("PropertyName")
1521
internal expect val DefaultDelay: Delay
1622

common/kotlinx-coroutines-core-common/src/CoroutineDispatcher.kt

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ import kotlin.coroutines.experimental.*
99
/**
1010
* Base class that shall be extended by all coroutine dispatcher implementations.
1111
*
12-
* The following standard implementations are provided by `kotlinx.coroutines`:
12+
* The following standard implementations are provided by `kotlinx.coroutines` as properties on
13+
* [Dispatchers] objects:
1314
*
14-
* * [DefaultDispatcher] -- is used by all standard builder if no dispatcher nor any other [ContinuationInterceptor]
15-
* is specified in their context. It is currently equal to [CommonPool] (subject to change in the future).
15+
* * [Dispatchers.Default] -- is used by all standard builder if no dispatcher nor any other [ContinuationInterceptor]
16+
* is specified in their context. It uses a common pool of shared background threads.
1617
* This is an appropriate choice for compute-intensive coroutines that consume CPU resources.
17-
* * [CommonPool] -- schedules coroutine execution to a common pool of shared background threads designed
18-
* to be used for compute-intensive code.
19-
* * [IO] -- uses a shared pool of on-demand created threads and is designed for offloading of IO-intensive _blocking_
18+
* * [Dispatchers.IO] -- uses a shared pool of on-demand created threads and is designed for offloading of IO-intensive _blocking_
2019
* operations (like file I/O and blocking socket I/O).
21-
* * [Unconfined] -- starts coroutine execution in the current call-frame until the first suspension.
20+
* * [Dispatchers.Unconfined] -- starts coroutine execution in the current call-frame until the first suspension.
2221
* On first suspension the coroutine builder function returns.
2322
* The coroutine resumes in whatever thread that is used by the
2423
* corresponding suspending function, without confining it to any specific thread or pool.

common/kotlinx-coroutines-core-common/src/CoroutineScope.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public val CoroutineScope.isActive: Boolean
111111
*
112112
* Global scope is used to launch top-level coroutines which are operating on the whole application lifetime
113113
* and are not cancelled prematurely.
114-
* Another use of the global scope is [Unconfined] operators, which don't have any job associated with them.
114+
* Another use of the global scope is operators running in [Dispatchers.Unconfined], which don't have any job associated with them.
115115
*
116116
* Application code usually should use application-defined [CoroutineScope], using
117117
* [async][CoroutineScope.async] or [launch][CoroutineScope.launch]
@@ -120,7 +120,7 @@ public val CoroutineScope.isActive: Boolean
120120
* Usage of this interface may look like this:
121121
*
122122
* ```
123-
* fun ReceiveChannel<Int>.sqrt(): ReceiveChannel<Double> = GlobalScope.produce(Unconfined) {
123+
* fun ReceiveChannel<Int>.sqrt(): ReceiveChannel<Double> = GlobalScope.produce(Dispatchers.Unconfined) {
124124
* for (number in this) {
125125
* send(Math.sqrt(number))
126126
* }

common/kotlinx-coroutines-core-common/src/CoroutineStart.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public enum class CoroutineStart {
2626
* function as most dispatchers do, then the coroutine code is dispatched for execution later, while the code that
2727
* invoked the coroutine builder continues execution.
2828
*
29-
* Note, that [Unconfined] dispatcher always returns `false` from its [CoroutineDispatcher.isDispatchNeeded]
30-
* function, so starting coroutine with [Unconfined] dispatcher by [DEFAULT] is the same as using [UNDISPATCHED].
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].
3131
*
3232
* If coroutine [Job] is cancelled before it even had a chance to start executing, then it will not start its
3333
* execution at all, but will complete with an exception.
@@ -59,7 +59,7 @@ public enum class CoroutineStart {
5959

6060
/**
6161
* Immediately executes coroutine until its first suspension point _in the current thread_ as if the
62-
* coroutine was started using [Unconfined] dispatcher. However, when coroutine is resumed from suspension
62+
* coroutine was started using [Dispatchers.Unconfined]. However, when coroutine is resumed from suspension
6363
* it is dispatched according to the [CoroutineDispatcher] in its context.
6464
*
6565
* This is similar to [ATOMIC] in the sense that coroutine starts executing even if it was already cancelled,

common/kotlinx-coroutines-core-common/src/Deferred.kt

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package kotlinx.coroutines.experimental
66

7-
import kotlinx.coroutines.experimental.internal.*
87
import kotlinx.coroutines.experimental.intrinsics.*
98
import kotlinx.coroutines.experimental.selects.*
109
import kotlin.coroutines.experimental.*
@@ -128,7 +127,7 @@ public interface Deferred<out T> : Job {
128127
* The running coroutine is cancelled when the resulting deferred is [cancelled][Job.cancel].
129128
*
130129
* Coroutine context is inherited from a [CoroutineScope], additional context elements can be specified with [context] argument.
131-
* If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [DefaultDispatcher] is used.
130+
* If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used.
132131
* The parent job is inherited from a [CoroutineScope] as well, but it can also be overridden
133132
* with corresponding [coroutineContext] element.
134133
*
@@ -138,7 +137,7 @@ public interface Deferred<out T> : Job {
138137
* the resulting [Deferred] is created in _new_ state. It can be explicitly started with [start][Job.start]
139138
* function and will be started implicitly on the first invocation of [join][Job.join], [await][Deferred.await] or [awaitAll].
140139
*
141-
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine
140+
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
142141
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
143142
* @param onCompletion optional completion handler for the coroutine (see [Job.invokeOnCompletion]).
144143
* @param block the coroutine code.
@@ -167,7 +166,7 @@ public fun <T> CoroutineScope.async(
167166
replaceWith = ReplaceWith("GlobalScope.async(context, start, onCompletion, block)", imports = ["kotlinx.coroutines.experimental.*"])
168167
)
169168
public fun <T> async(
170-
context: CoroutineContext = DefaultDispatcher,
169+
context: CoroutineContext = Dispatchers.Default,
171170
start: CoroutineStart = CoroutineStart.DEFAULT,
172171
onCompletion: CompletionHandler? = null,
173172
block: suspend CoroutineScope.() -> T
@@ -183,7 +182,7 @@ public fun <T> async(
183182
replaceWith = ReplaceWith("GlobalScope.async(context + parent, start, onCompletion, block)", imports = ["kotlinx.coroutines.experimental.*"])
184183
)
185184
public fun <T> async(
186-
context: CoroutineContext = DefaultDispatcher,
185+
context: CoroutineContext = Dispatchers.Default,
187186
start: CoroutineStart = CoroutineStart.DEFAULT,
188187
parent: Job? = null,
189188
onCompletion: CompletionHandler? = null,
@@ -194,7 +193,7 @@ public fun <T> async(
194193
/** @suppress **Deprecated**: Binary compatibility */
195194
@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
196195
public fun <T> async(
197-
context: CoroutineContext = DefaultDispatcher,
196+
context: CoroutineContext = Dispatchers.Default,
198197
start: CoroutineStart = CoroutineStart.DEFAULT,
199198
parent: Job? = null,
200199
block: suspend CoroutineScope.() -> T
@@ -204,7 +203,7 @@ public fun <T> async(
204203
/** @suppress **Deprecated**: Binary compatibility */
205204
@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
206205
public fun <T> async(
207-
context: CoroutineContext = DefaultDispatcher,
206+
context: CoroutineContext = Dispatchers.Default,
208207
start: CoroutineStart = CoroutineStart.DEFAULT,
209208
block: suspend CoroutineScope.() -> T
210209
): Deferred<T> =

0 commit comments

Comments
 (0)