jvmAndroidMain.com.lehaine.littlekt.log.KtAtomicRef.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-jvm Show documentation
Show all versions of core-jvm Show documentation
Kotlin Multiplatform 2D Game Framework
package com.lehaine.littlekt.log
import java.util.concurrent.atomic.AtomicReference
/**
* @author Colton Daily
* @date 11/25/2021
*/
internal actual class KtAtomicRef actual constructor(initial: T) {
private val ref = AtomicReference(initial)
actual var value: T
get() = ref.get()
set(value) {
ref.set(value)
}
actual inline fun update(block: (T) -> T) {
do {
val old = ref.get()
val new = block(old)
} while (!ref.compareAndSet(old, new))
}
}