commonMain.com.badoo.reaktive.utils.atomic.AtomicReferenceExt.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-internal-jvm Show documentation
Show all versions of utils-internal-jvm Show documentation
Kotlin multi-platform implementation of Reactive Extensions
package com.badoo.reaktive.utils.atomic
import com.badoo.reaktive.utils.InternalReaktiveApi
@Suppress("UNCHECKED_CAST") // https://youtrack.jetbrains.com/issue/KT-57412
@InternalReaktiveApi
inline fun AtomicReference.getAndChange(update: (T) -> T): T {
while (true) {
val prev: Any? = value
val next: Any? = update(prev as T)
if (compareAndSet(prev, next as T)) {
return prev
}
}
}
@Suppress("UNCHECKED_CAST") // https://youtrack.jetbrains.com/issue/KT-57412
@InternalReaktiveApi
inline fun AtomicReference.changeAndGet(update: (T) -> R): R {
while (true) {
val prev: Any? = value
val next: Any? = update(prev as T)
if (compareAndSet(prev, next as R)) {
return next
}
}
}
@InternalReaktiveApi
inline fun AtomicReference.change(update: (T) -> T) {
getAndChange(update)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy