Skip to content

Commit e79bc62

Browse files
committed
Promote Dispatchers.Unconfined to stable API
1 parent 9942de6 commit e79bc62

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,30 @@ public expect object Dispatchers {
4747
* mandating any specific threading policy. Nested coroutines launched in this dispatcher form an event-loop to avoid
4848
* stack overflows.
4949
*
50+
* ### Event loop
51+
* Event loop semantics is a purely internal concept and have no guarantees on the order of execution
52+
* except that all queued coroutines will be executed on the current thread in the lexical scope of the outermost
53+
* unconfined coroutine.
54+
*
55+
* For example, the following code:
56+
* ```
57+
* withContext(Dispatcher.Unconfined) {
58+
* println(1)
59+
* withContext(Dispatcher.Unconfined) { // Nested unconfined
60+
* println(2)
61+
* }
62+
* println(3)
63+
* }
64+
* println("Done")
65+
* ```
66+
* Can print both "1 2 3" and "1 3 2", this is an implementation detail that can be changed.
67+
* But it is guaranteed that "Done" will be printed only when both `withContext` are completed.
68+
*
5069
* Note, that if you need your coroutine to be confined to a particular thread or a thread-pool after resumption,
5170
* but still want to execute it in the current call-frame until its first suspension, then you can use
5271
* an optional [CoroutineStart] parameter in coroutine builders like
5372
* [launch][CoroutineScope.launch] and [async][CoroutineScope.async] setting it to the
5473
* the value of [CoroutineStart.UNDISPATCHED].
55-
*
56-
* **Note: This is an experimental api.**
57-
* Semantics, order of execution, and particular implementation details of this dispatcher may change in the future.
5874
*/
59-
@ExperimentalCoroutinesApi
6075
public val Unconfined: CoroutineDispatcher
6176
}

kotlinx-coroutines-core/jvm/src/Dispatchers.kt

+23-7
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,38 @@ public actual object Dispatchers {
5656

5757
/**
5858
* A coroutine dispatcher that is not confined to any specific thread.
59-
* It executes initial continuation of the coroutine _immediately_ in the current call-frame
59+
* It executes initial continuation of the coroutine in the current call-frame
6060
* and lets the coroutine resume in whatever thread that is used by the corresponding suspending function, without
61-
* mandating any specific threading policy.
62-
* **Note: use with extreme caution, not for general code**.
61+
* mandating any specific threading policy. Nested coroutines launched in this dispatcher form an event-loop to avoid
62+
* stack overflows.
63+
*
64+
* ### Event loop
65+
* Event loop semantics is a purely internal concept and have no guarantees on the order of execution
66+
* except that all queued coroutines will be executed on the current thread in the lexical scope of the outermost
67+
* unconfined coroutine.
68+
*
69+
* For example, the following code:
70+
* ```
71+
* withContext(Dispatcher.Unconfined) {
72+
* println(1)
73+
* withContext(Dispatcher.Unconfined) { // Nested unconfined
74+
* println(2)
75+
* }
76+
* println(3)
77+
* }
78+
* println("Done")
79+
* ```
80+
* Can print both "1 2 3" and "1 3 2", this is an implementation detail that can be changed.
81+
* But it is guaranteed that "Done" will be printed only when both `withContext` are completed.
82+
*
6383
*
6484
* Note, that if you need your coroutine to be confined to a particular thread or a thread-pool after resumption,
6585
* but still want to execute it in the current call-frame until its first suspension, then you can use
6686
* an optional [CoroutineStart] parameter in coroutine builders like
6787
* [launch][CoroutineScope.launch] and [async][CoroutineScope.async] setting it to the
6888
* the value of [CoroutineStart.UNDISPATCHED].
69-
*
70-
* **Note: This is an experimental api.**
71-
* Semantics, order of execution, and particular implementation details of this dispatcher may change in the future.
7289
*/
7390
@JvmStatic
74-
@ExperimentalCoroutinesApi
7591
public actual val Unconfined: CoroutineDispatcher = kotlinx.coroutines.Unconfined
7692

7793
/**

0 commit comments

Comments
 (0)