Skip to content

Commit 8cd9968

Browse files
committed
Fix comparison with 0 on non-JVM platforms
1 parent f407758 commit 8cd9968

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

Diff for: kotlinx-coroutines-core/common/src/internal/ThreadContext.common.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ private val updateState =
5353
return state
5454
}
5555

56+
internal expect fun isZeroCount(countOrElement: Any?): Boolean
57+
5658
// countOrElement is pre-cached in dispatched continuation
5759
// returns NO_THREAD_ELEMENTS if the contest does not have any ThreadContextElements
5860
internal fun updateThreadContext(context: CoroutineContext, countOrElement: Any?): Any? {
5961
@Suppress("NAME_SHADOWING")
6062
val countOrElement = countOrElement ?: threadContextElements(context)
61-
@Suppress("IMPLICIT_BOXING_IN_IDENTITY_EQUALS")
6263
return when {
63-
countOrElement === 0 -> NO_THREAD_ELEMENTS // very fast path when there are no active ThreadContextElements
64-
// ^^^ identity comparison for speed, we know zero always has the same identity
64+
isZeroCount(countOrElement) -> NO_THREAD_ELEMENTS // very fast path when there are no active ThreadContextElements
6565
countOrElement is Int -> {
6666
// slow path for multiple active ThreadContextElements, allocates ThreadState for multiple old values
6767
context.fold(ThreadState(context, countOrElement), updateState)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package kotlinx.coroutines.internal
2+
3+
internal actual fun isZeroCount(countOrElement: Any?): Boolean = countOrElement is Int && countOrElement == 0

Diff for: kotlinx-coroutines-core/jvm/src/internal/ThreadContext.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package kotlinx.coroutines.internal
33
import kotlinx.coroutines.*
44
import kotlin.coroutines.*
55

6-
6+
// identity comparison for speed, we know zero always has the same identity
7+
@Suppress("IMPLICIT_BOXING_IN_IDENTITY_EQUALS", "KotlinConstantConditions")
8+
internal actual fun isZeroCount(countOrElement: Any?): Boolean = countOrElement === 0
79

810
// top-level data class for a nicer out-of-the-box toString representation and class name
911
@PublishedApi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package kotlinx.coroutines.internal
2+
3+
internal actual fun isZeroCount(countOrElement: Any?): Boolean = countOrElement is Int && countOrElement == 0

0 commit comments

Comments
 (0)