Atomicfu is a multiplatform Kotlin compiler plugin and library for atomic operations across JVM, Native, JS, and Wasm platforms
—
Configuration options for enabling atomicfu transformations across different Kotlin platforms.
Apply and configure the atomicfu compiler plugin in your Kotlin multiplatform project.
/**
* Atomicfu Kotlin Gradle Subplugin for configuring compiler transformations
*/
class AtomicfuKotlinGradleSubplugin : KotlinCompilerPluginSupportPlugin {
companion object {
/** Artifact name for the atomicfu compiler plugin */
const val ATOMICFU_ARTIFACT_NAME = "kotlin-atomicfu-compiler-plugin-embeddable"
}
/** Returns the compiler plugin ID */
override fun getCompilerPluginId(): String
/** Returns the plugin artifact configuration */
override fun getPluginArtifact(): SubpluginArtifact
/** Determines if the plugin is applicable to a given compilation */
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean
}Configure transformation settings for different platforms.
/**
* Extension class for configuring atomicfu plugin behavior
*/
class AtomicfuKotlinGradleExtension {
/** Enable JavaScript IR transformation */
var isJsIrTransformationEnabled: Boolean
/** Enable JVM IR transformation */
var isJvmIrTransformationEnabled: Boolean
/** Enable Native IR transformation */
var isNativeIrTransformationEnabled: Boolean
}Usage Examples:
// build.gradle.kts
plugins {
id("kotlinx-atomicfu")
}
atomicfuCompilerPlugin {
isJvmIrTransformationEnabled = true
isJsIrTransformationEnabled = true
isNativeIrTransformationEnabled = true
}Configure atomicfu transformations for specific Kotlin platforms.
JVM Configuration:
// build.gradle.kts
kotlin {
jvm {
// JVM-specific configuration
}
}
atomicfuCompilerPlugin {
// Enable JVM IR transformations
isJvmIrTransformationEnabled = true
}JavaScript Configuration:
// build.gradle.kts
kotlin {
js {
browser()
nodejs()
}
}
atomicfuCompilerPlugin {
// Enable JS IR transformations
isJsIrTransformationEnabled = true
}Native Configuration:
// build.gradle.kts
kotlin {
linuxX64()
macosX64()
mingwX64()
}
atomicfuCompilerPlugin {
// Enable Native IR transformations
isNativeIrTransformationEnabled = true
}Multiplatform Configuration:
// build.gradle.kts
kotlin {
jvm()
js {
browser()
nodejs()
}
linuxX64()
macosX64()
mingwX64()
sourceSets {
val commonMain by getting {
dependencies {
// Add atomicfu library dependency
implementation("org.jetbrains.kotlinx:atomicfu:0.25.0")
}
}
}
}
atomicfuCompilerPlugin {
// Enable transformations for all platforms
isJvmIrTransformationEnabled = true
isJsIrTransformationEnabled = true
isNativeIrTransformationEnabled = true
}The atomicfu compiler plugin transforms high-level atomic operations into platform-specific implementations:
Transformation Process:
Platform Transformations:
java.util.concurrent.atomic.* classes for maximum performanceExample Transformation:
// Source code (common)
val counter = atomic(0)
counter.compareAndSet(0, 1)
// JVM transformation
var counter = java.util.concurrent.atomic.AtomicInteger(0)
counter.compareAndSet(0, 1)
// JS transformation
var counter = 0
atomicfu_compareAndSet(0, 1, { counter }, { v -> counter = v })
// Native transformation
var counter = kotlin.native.concurrent.AtomicInt(0)
counter.compareAndSet(0, 1)Minimal Configuration:
// build.gradle.kts
plugins {
kotlin("multiplatform")
id("kotlinx-atomicfu")
}
// Plugin automatically detects targets and enables appropriate transformationsSelective Platform Configuration:
// build.gradle.kts
atomicfuCompilerPlugin {
// Only enable transformations for specific platforms
isJvmIrTransformationEnabled = true
isJsIrTransformationEnabled = false // Use library fallback for JS
isNativeIrTransformationEnabled = true
}Debug Configuration:
// build.gradle.kts
atomicfuCompilerPlugin {
isJvmIrTransformationEnabled = false // Disable for debugging
isJsIrTransformationEnabled = false // Use library implementations
isNativeIrTransformationEnabled = false
}
// This configuration will use the library's runtime implementations
// which may be slower but easier to debugInstall with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-atomicfu-compiler-plugin