Atomicfu is a multiplatform Kotlin compiler plugin and library for atomic operations across JVM, Native, JS, and Wasm platforms
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-atomicfu-compiler-plugin@2.2.0Atomicfu is a multiplatform Kotlin compiler plugin and library that provides atomic operations across JVM, Native, JS, and Wasm platforms. It enables developers to write idiomatic atomic operations like boxed values while running them efficiently in production with platform-specific optimizations.
implementation("org.jetbrains.kotlinx:atomicfu:0.25.0")id("kotlinx-atomicfu") in build.gradle.ktsimport kotlinx.atomicfu.*import kotlinx.atomicfu.*
class Counter {
private val count = atomic(0)
fun increment(): Int = count.incrementAndGet()
fun get(): Int = count.value
fun compareAndSet(expected: Int, newValue: Int): Boolean =
count.compareAndSet(expected, newValue)
}
// Usage example
val counter = Counter()
counter.increment() // Returns 1
counter.compareAndSet(1, 10) // Returns true, sets value to 10Atomicfu is built around several key components:
atomic() functions create appropriate atomic wrappers for different typesAtomicInt, AtomicLong, AtomicBoolean, AtomicRef) with comprehensive operation setsAtomicIntArray, AtomicLongArray, etc.)java.util.concurrent.atomic.*, Native uses intrinsics, JS/Wasm use runtime implementationsCore atomic operations for thread-safe programming with compare-and-swap, arithmetic operations, high-level update patterns, tracing, and delegated properties.
fun atomic(value: Int): AtomicInt
fun atomic(value: Long): AtomicLong
fun atomic(value: Boolean): AtomicBoolean
fun <T> atomic(value: T): AtomicRef<T>
// With tracing support
fun atomic(value: Int, trace: Trace): AtomicInt
fun <T> atomic(value: T, trace: Trace): AtomicRef<T>
// Delegated property support
var property: T by atomic(initialValue)Arrays of atomic values for bulk operations and indexed atomic access patterns.
class AtomicIntArray(size: Int)
class AtomicLongArray(size: Int)
class AtomicBooleanArray(size: Int)
fun <T> atomicArrayOfNulls(size: Int): AtomicArray<T?>Configuration options for enabling atomicfu transformations across different Kotlin platforms.
class AtomicfuKotlinGradleExtension {
var isJsIrTransformationEnabled: Boolean
var isJvmIrTransformationEnabled: Boolean
var isNativeIrTransformationEnabled: Boolean
}Reentrant locks and lock-free synchronization primitives for thread-safe coordination without blocking.
fun reentrantLock(): ReentrantLock
class ReentrantLock {
fun lock(): Unit
fun unlock(): Unit
fun tryLock(): Boolean
inline fun <T> withLock(action: () -> T): T
}