@@ -9,10 +9,9 @@ import kotlin.coroutines.*
9
9
import kotlin.coroutines.jvm.internal.CoroutineStackFrame
10
10
11
11
/* *
12
- * Creates context for the new coroutine. It installs [Dispatchers.Default] when no other dispatcher nor
13
- * [ContinuationInterceptor] is specified and adds support for
14
- * copyable thread context [elements][CopyableThreadContextElement] and debugging facilities (optionally, when turned on).
15
- *
12
+ * Creates a context for a new coroutine. It installs [Dispatchers.Default] when no other dispatcher or
13
+ * [ContinuationInterceptor] is specified and adds optional support for debugging facilities (when turned on)
14
+ * and copyable-thread-local facilities on JVM.
16
15
* See [DEBUG_PROPERTY_NAME] for description of debugging facilities on JVM.
17
16
*/
18
17
@ExperimentalCoroutinesApi
@@ -24,7 +23,7 @@ public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext):
24
23
}
25
24
26
25
/* *
27
- * Creates context for coroutine builder functions that do not launch a new coroutine, namely [withContext].
26
+ * Creates a context for coroutine builder functions that do not launch a new coroutine, e.g. [withContext].
28
27
* @suppress
29
28
*/
30
29
@InternalCoroutinesApi
@@ -33,13 +32,12 @@ public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineCo
33
32
* Fast-path: we only have to copy/merge if 'addedContext' (which typically has one or two elements)
34
33
* contains copyable elements.
35
34
*/
36
- if (! addedContext.fold( false , hasCopyableElements)) return this + addedContext
35
+ if (! addedContext.hasCopyableElements( )) return this + addedContext
37
36
return foldCopies(this , addedContext, false )
38
37
}
39
38
40
- private val hasCopyableElements: (Boolean , CoroutineContext .Element ) -> Boolean = { result, it ->
41
- result || it is CopyableThreadContextElement <* >
42
- }
39
+ private fun CoroutineContext.hasCopyableElements (): Boolean =
40
+ fold(false ) { result, it -> result || it is CopyableThreadContextElement <* > }
43
41
44
42
/* *
45
43
* Folds two contexts properly applying [CopyableThreadContextElement] rules when necessary.
@@ -53,8 +51,8 @@ private val hasCopyableElements: (Boolean, CoroutineContext.Element) -> Boolean
53
51
*/
54
52
private fun foldCopies (originalContext : CoroutineContext , appendContext : CoroutineContext , isNewCoroutine : Boolean ): CoroutineContext {
55
53
// Do we have something to copy left-hand side?
56
- val hasElementsLeft = originalContext.fold( false , hasCopyableElements)
57
- val hasElementsRight = appendContext.fold( false , hasCopyableElements)
54
+ val hasElementsLeft = originalContext.hasCopyableElements( )
55
+ val hasElementsRight = appendContext.hasCopyableElements( )
58
56
59
57
// Nothing to fold, so just return the sum of contexts
60
58
if (! hasElementsLeft && ! hasElementsRight) {
@@ -126,7 +124,7 @@ internal actual inline fun <T> withContinuationContext(continuation: Continuatio
126
124
internal fun Continuation <* >.updateUndispatchedCompletion (context : CoroutineContext , oldValue : Any? ): UndispatchedCoroutine <* >? {
127
125
if (this !is CoroutineStackFrame ) return null
128
126
/*
129
- * Fast-path to detect whether we have unispatched coroutine at all in our stack.
127
+ * Fast-path to detect whether we have undispatched coroutine at all in our stack.
130
128
*
131
129
* Implementation note.
132
130
* If we ever find that stackwalking for thread-locals is way too slow, here is another idea:
@@ -137,8 +135,8 @@ internal fun Continuation<*>.updateUndispatchedCompletion(context: CoroutineCont
137
135
* Both options should work, but it requires more careful studying of the performance
138
136
* and, mostly, maintainability impact.
139
137
*/
140
- val potentiallyHasUndispatchedCorotuine = context[UndispatchedMarker ] != = null
141
- if (! potentiallyHasUndispatchedCorotuine ) return null
138
+ val potentiallyHasUndispatchedCoroutine = context[UndispatchedMarker ] != = null
139
+ if (! potentiallyHasUndispatchedCoroutine ) return null
142
140
val completion = undispatchedCompletion()
143
141
completion?.saveThreadContext(context, oldValue)
144
142
return completion
0 commit comments