or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

atomic-arrays.mdatomic-operations.mdindex.mdlock-free-synchronization.mdplugin-configuration.md
tile.json

tessl/maven-org-jetbrains-kotlin--kotlin-atomicfu-compiler-plugin

Atomicfu is a multiplatform Kotlin compiler plugin and library for atomic operations across JVM, Native, JS, and Wasm platforms

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.jetbrains.kotlin/kotlin-atomicfu-compiler-plugin@2.2.x

To install, run

npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-atomicfu-compiler-plugin@2.2.0

index.mddocs/

Atomicfu

Atomicfu 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.

Package Information

  • Package Name: org.jetbrains.kotlin:kotlin-atomicfu-compiler-plugin
  • Package Type: maven
  • Language: Kotlin
  • Installation:
    • Library dependency: implementation("org.jetbrains.kotlinx:atomicfu:0.25.0")
    • Compiler plugin: id("kotlinx-atomicfu") in build.gradle.kts

Core Imports

import kotlinx.atomicfu.*

Basic Usage

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 10

Architecture

Atomicfu is built around several key components:

  • Compiler Plugin: Transforms high-level atomic operations into platform-specific implementations
  • Factory Functions: atomic() functions create appropriate atomic wrappers for different types
  • Atomic Types: Type-safe wrappers (AtomicInt, AtomicLong, AtomicBoolean, AtomicRef) with comprehensive operation sets
  • Array Types: Atomic arrays for bulk operations (AtomicIntArray, AtomicLongArray, etc.)
  • Platform Optimization: JVM uses java.util.concurrent.atomic.*, Native uses intrinsics, JS/Wasm use runtime implementations
  • Gradle Plugin: Configuration for enabling transformations across different platforms

Capabilities

Atomic Operations

Core 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)

Atomic Operations

Atomic Arrays

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?>

Atomic Arrays

Gradle Plugin Configuration

Configuration options for enabling atomicfu transformations across different Kotlin platforms.

class AtomicfuKotlinGradleExtension {
    var isJsIrTransformationEnabled: Boolean
    var isJvmIrTransformationEnabled: Boolean
    var isNativeIrTransformationEnabled: Boolean
}

Plugin Configuration

Lock-Free Synchronization

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
}

Lock-Free Synchronization