Skip to content

Commit

Permalink
Fix comparison with 0 on non-JVM platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
zuevmaxim committed Oct 21, 2024
1 parent f407758 commit 8cd9968
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ private val updateState =
return state
}

internal expect fun isZeroCount(countOrElement: Any?): Boolean

// countOrElement is pre-cached in dispatched continuation
// returns NO_THREAD_ELEMENTS if the contest does not have any ThreadContextElements
internal fun updateThreadContext(context: CoroutineContext, countOrElement: Any?): Any? {
@Suppress("NAME_SHADOWING")
val countOrElement = countOrElement ?: threadContextElements(context)
@Suppress("IMPLICIT_BOXING_IN_IDENTITY_EQUALS")
return when {
countOrElement === 0 -> NO_THREAD_ELEMENTS // very fast path when there are no active ThreadContextElements
// ^^^ identity comparison for speed, we know zero always has the same identity
isZeroCount(countOrElement) -> NO_THREAD_ELEMENTS // very fast path when there are no active ThreadContextElements
countOrElement is Int -> {
// slow path for multiple active ThreadContextElements, allocates ThreadState for multiple old values
context.fold(ThreadState(context, countOrElement), updateState)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package kotlinx.coroutines.internal

internal actual fun isZeroCount(countOrElement: Any?): Boolean = countOrElement is Int && countOrElement == 0
4 changes: 3 additions & 1 deletion kotlinx-coroutines-core/jvm/src/internal/ThreadContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package kotlinx.coroutines.internal
import kotlinx.coroutines.*
import kotlin.coroutines.*


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

// top-level data class for a nicer out-of-the-box toString representation and class name
@PublishedApi
Expand Down
3 changes: 3 additions & 0 deletions kotlinx-coroutines-core/native/src/internal/ThreadContext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package kotlinx.coroutines.internal

internal actual fun isZeroCount(countOrElement: Any?): Boolean = countOrElement is Int && countOrElement == 0

0 comments on commit 8cd9968

Please sign in to comment.