Skip to content

Commit 9750b59

Browse files
committed
~small tweaks
1 parent 64163c1 commit 9750b59

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import kotlin.coroutines.*
99
import kotlin.coroutines.jvm.internal.CoroutineStackFrame
1010

1111
/**
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.
1615
* See [DEBUG_PROPERTY_NAME] for description of debugging facilities on JVM.
1716
*/
1817
@ExperimentalCoroutinesApi
@@ -24,7 +23,7 @@ public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext):
2423
}
2524

2625
/**
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].
2827
* @suppress
2928
*/
3029
@InternalCoroutinesApi
@@ -33,13 +32,12 @@ public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineCo
3332
* Fast-path: we only have to copy/merge if 'addedContext' (which typically has one or two elements)
3433
* contains copyable elements.
3534
*/
36-
if (!addedContext.fold(false, hasCopyableElements)) return this + addedContext
35+
if (!addedContext.hasCopyableElements()) return this + addedContext
3736
return foldCopies(this, addedContext, false)
3837
}
3938

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<*> }
4341

4442
/**
4543
* Folds two contexts properly applying [CopyableThreadContextElement] rules when necessary.
@@ -53,8 +51,8 @@ private val hasCopyableElements: (Boolean, CoroutineContext.Element) -> Boolean
5351
*/
5452
private fun foldCopies(originalContext: CoroutineContext, appendContext: CoroutineContext, isNewCoroutine: Boolean): CoroutineContext {
5553
// 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()
5856

5957
// Nothing to fold, so just return the sum of contexts
6058
if (!hasElementsLeft && !hasElementsRight) {
@@ -126,7 +124,7 @@ internal actual inline fun <T> withContinuationContext(continuation: Continuatio
126124
internal fun Continuation<*>.updateUndispatchedCompletion(context: CoroutineContext, oldValue: Any?): UndispatchedCoroutine<*>? {
127125
if (this !is CoroutineStackFrame) return null
128126
/*
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.
130128
*
131129
* Implementation note.
132130
* 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
137135
* Both options should work, but it requires more careful studying of the performance
138136
* and, mostly, maintainability impact.
139137
*/
140-
val potentiallyHasUndispatchedCorotuine = context[UndispatchedMarker] !== null
141-
if (!potentiallyHasUndispatchedCorotuine) return null
138+
val potentiallyHasUndispatchedCoroutine = context[UndispatchedMarker] !== null
139+
if (!potentiallyHasUndispatchedCoroutine) return null
142140
val completion = undispatchedCompletion()
143141
completion?.saveThreadContext(context, oldValue)
144142
return completion

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public interface ThreadContextElement<S> : CoroutineContext.Element {
8080
/**
8181
* A [ThreadContextElement] copied whenever a child coroutine inherits a context containing it.
8282
*
83-
* When an API uses a _mutable_ `ThreadLocal` for consistency, a [CopyableThreadContextElement]
83+
* When an API uses a _mutable_ [ThreadLocal] for consistency, a [CopyableThreadContextElement]
8484
* can give coroutines "coroutine-safe" write access to that `ThreadLocal`.
8585
*
8686
* A write made to a `ThreadLocal` with a matching [CopyableThreadContextElement] by a coroutine
@@ -97,8 +97,9 @@ public interface ThreadContextElement<S> : CoroutineContext.Element {
9797
* is in a coroutine:
9898
*
9999
* ```
100-
* class TraceContextElement(private val traceData: TraceData?) : CopyableThreadContextElement<TraceData?, TraceContextElement> {
100+
* class TraceContextElement(private val traceData: TraceData?) : CopyableThreadContextElement<TraceData?> {
101101
* companion object Key : CoroutineContext.Key<TraceContextElement>
102+
*
102103
* override val key: CoroutineContext.Key<TraceContextElement> = Key
103104
*
104105
* override fun updateThreadContext(context: CoroutineContext): TraceData? {
@@ -118,7 +119,7 @@ public interface ThreadContextElement<S> : CoroutineContext.Element {
118119
* return TraceContextElement(traceThreadLocal.get()?.copy())
119120
* }
120121
*
121-
* override fun mergeForChild(overwritingElement: CoroutineContext.Element): CoroutineContext
122+
* override fun mergeForChild(overwritingElement: CoroutineContext.Element): CoroutineContext {
122123
* // Merge operation defines how to handle situation when both
123124
* // the parent coroutine has an element in the context and
124125
* // an element with the same key was also

0 commit comments

Comments
 (0)