|
6 | 6 |
|
7 | 7 | package kotlinx.coroutines.experimental
|
8 | 8 |
|
| 9 | +import kotlinx.coroutines.experimental.internal.* |
9 | 10 | import kotlinx.coroutines.experimental.scheduling.*
|
| 11 | +import java.util.* |
| 12 | +import kotlin.coroutines.experimental.* |
10 | 13 |
|
11 | 14 | /**
|
12 | 15 | * Name of the property that defines the maximal number of threads that are used by [Dispatchers.IO] coroutines dispatcher.
|
13 | 16 | */
|
14 | 17 | public const val IO_PARALLELISM_PROPERTY_NAME = "kotlinx.coroutines.io.parallelism"
|
15 | 18 |
|
16 | 19 | /**
|
17 |
| - * The [CoroutineDispatcher] that is designed for offloading blocking IO tasks to a shared pool of threads. |
18 |
| - * |
19 |
| - * Additional threads in this pool are created and are shutdown on demand. |
20 |
| - * The number of threads used by this dispatcher is limited by the value of |
21 |
| - * "`kotlinx.coroutines.io.parallelism`" ([IO_PARALLELISM_PROPERTY_NAME]) system property. |
22 |
| - * It defaults to the limit of 64 threads or the number of cores (whichever is larger). |
23 |
| - * |
24 |
| - * This dispatcher shares threads with a [Default][Dispatchers.Default] dispatcher, so using |
25 |
| - * `withContext(Dispatchers.IO) { ... }` does not lead to an actual switching to another thread — |
26 |
| - * typically execution continues in the same thread. |
| 20 | + * Groups various implementations of [CoroutineDispatcher]. |
27 | 21 | */
|
28 |
| -public val Dispatchers.IO: CoroutineDispatcher |
29 |
| - get() = DefaultScheduler.IO |
| 22 | +actual object Dispatchers { |
| 23 | + |
| 24 | + private val mainDispatcher = loadMainDispatcher() |
| 25 | + |
| 26 | + private fun loadMainDispatcher(): MainCoroutineDispatcher? { |
| 27 | + return MainDispatcherFactory::class.java.let { clz -> |
| 28 | + ServiceLoader.load(clz, clz.classLoader).toList() |
| 29 | + }.maxBy { it.loadPriority }?.createDispatcher() |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * The default [CoroutineDispatcher] that is used by all standard builders like |
| 34 | + * [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc |
| 35 | + * if no dispatcher nor any other [ContinuationInterceptor] is specified in their context. |
| 36 | + * |
| 37 | + * It is backed by a shared pool of threads on JVM. By default, the maximal number of threads used |
| 38 | + * by this dispatcher is equal to the number CPU cores, but is at least two. |
| 39 | + */ |
| 40 | + @JvmStatic |
| 41 | + public actual val Default: CoroutineDispatcher = createDefaultDispatcher() |
| 42 | + |
| 43 | + /** |
| 44 | + * A coroutine dispatcher that is confined to the Main thread operating with UI objects. |
| 45 | + * Usually such dispatcher is single-threaded. |
| 46 | + * |
| 47 | + * Access to this property may throw [IllegalStateException] if no main thread dispatchers are present in the classpath. |
| 48 | + * |
| 49 | + * Depending on platform and classpath it can be mapped to different dispatchers: |
| 50 | + * - On JS and Native it is equivalent of [Default] dispatcher. |
| 51 | + * - On JVM it either Android main thread dispatcher, JavaFx or Swing EDT dispatcher. It is chosen by |
| 52 | + * [`ServiceLoader`](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html). |
| 53 | + * |
| 54 | + * In order to work with `Main` dispatcher, following artifact should be added to project runtime dependencies: |
| 55 | + * - `kotlinx-coroutines-android` for Android Main thread dispatcher |
| 56 | + * - `kotlinx-coroutines-javafx` for JavaFx Application thread dispatcher |
| 57 | + * - `kotlinx-coroutines-swing` for Swing EDT dispatcher |
| 58 | + * |
| 59 | + * Implementation note: [MainCoroutineDispatcher.immediate] is not supported on Native and JS platforms. |
| 60 | + */ |
| 61 | + @JvmStatic |
| 62 | + public actual val Main: MainCoroutineDispatcher get() = mainDispatcher ?: error("Module with Main dispatcher is missing. " + |
| 63 | + "Add dependency with required Main dispatcher, e.g. 'kotlinx-coroutines-android'") |
| 64 | + |
| 65 | + /** |
| 66 | + * A coroutine dispatcher that is not confined to any specific thread. |
| 67 | + * It executes initial continuation of the coroutine _immediately_ in the current call-frame |
| 68 | + * and lets the coroutine resume in whatever thread that is used by the corresponding suspending function, without |
| 69 | + * mandating any specific threading policy. |
| 70 | + * **Note: use with extreme caution, not for general code**. |
| 71 | + * |
| 72 | + * Note, that if you need your coroutine to be confined to a particular thread or a thread-pool after resumption, |
| 73 | + * but still want to execute it in the current call-frame until its first suspension, then you can use |
| 74 | + * an optional [CoroutineStart] parameter in coroutine builders like |
| 75 | + * [launch][CoroutineScope.launch] and [async][CoroutineScope.async] setting it to the |
| 76 | + * the value of [CoroutineStart.UNDISPATCHED]. |
| 77 | + * |
| 78 | + * **Note: This is an experimental api.** |
| 79 | + * Semantics, order of execution, and particular implementation details of this dispatcher may change in the future. |
| 80 | + */ |
| 81 | + @JvmStatic |
| 82 | + @ExperimentalCoroutinesApi |
| 83 | + public actual val Unconfined: CoroutineDispatcher = kotlinx.coroutines.experimental.Unconfined |
| 84 | + |
| 85 | + /** |
| 86 | + * The [CoroutineDispatcher] that is designed for offloading blocking IO tasks to a shared pool of threads. |
| 87 | + * |
| 88 | + * Additional threads in this pool are created and are shutdown on demand. |
| 89 | + * The number of threads used by this dispatcher is limited by the value of |
| 90 | + * "`kotlinx.coroutines.io.parallelism`" ([IO_PARALLELISM_PROPERTY_NAME]) system property. |
| 91 | + * It defaults to the limit of 64 threads or the number of cores (whichever is larger). |
| 92 | + * |
| 93 | + * This dispatcher shares threads with a [Default][Dispatchers.Default] dispatcher, so using |
| 94 | + * `withContext(Dispatchers.IO) { ... }` does not lead to an actual switching to another thread — |
| 95 | + * typically execution continues in the same thread. |
| 96 | + */ |
| 97 | + @JvmStatic |
| 98 | + public val IO: CoroutineDispatcher = DefaultScheduler.IO |
| 99 | +} |
0 commit comments