0
# Plugin Configuration
1
2
Configuration options for enabling atomicfu transformations across different Kotlin platforms.
3
4
## Capabilities
5
6
### Gradle Plugin Application
7
8
Apply and configure the atomicfu compiler plugin in your Kotlin multiplatform project.
9
10
```kotlin { .api }
11
/**
12
* Atomicfu Kotlin Gradle Subplugin for configuring compiler transformations
13
*/
14
class AtomicfuKotlinGradleSubplugin : KotlinCompilerPluginSupportPlugin {
15
companion object {
16
/** Artifact name for the atomicfu compiler plugin */
17
const val ATOMICFU_ARTIFACT_NAME = "kotlin-atomicfu-compiler-plugin-embeddable"
18
}
19
20
/** Returns the compiler plugin ID */
21
override fun getCompilerPluginId(): String
22
23
/** Returns the plugin artifact configuration */
24
override fun getPluginArtifact(): SubpluginArtifact
25
26
/** Determines if the plugin is applicable to a given compilation */
27
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean
28
}
29
```
30
31
### Plugin Extension Configuration
32
33
Configure transformation settings for different platforms.
34
35
```kotlin { .api }
36
/**
37
* Extension class for configuring atomicfu plugin behavior
38
*/
39
class AtomicfuKotlinGradleExtension {
40
/** Enable JavaScript IR transformation */
41
var isJsIrTransformationEnabled: Boolean
42
43
/** Enable JVM IR transformation */
44
var isJvmIrTransformationEnabled: Boolean
45
46
/** Enable Native IR transformation */
47
var isNativeIrTransformationEnabled: Boolean
48
}
49
```
50
51
**Usage Examples:**
52
53
```kotlin
54
// build.gradle.kts
55
plugins {
56
id("kotlinx-atomicfu")
57
}
58
59
atomicfuCompilerPlugin {
60
isJvmIrTransformationEnabled = true
61
isJsIrTransformationEnabled = true
62
isNativeIrTransformationEnabled = true
63
}
64
```
65
66
### Platform-Specific Configuration
67
68
Configure atomicfu transformations for specific Kotlin platforms.
69
70
**JVM Configuration:**
71
72
```kotlin
73
// build.gradle.kts
74
kotlin {
75
jvm {
76
// JVM-specific configuration
77
}
78
}
79
80
atomicfuCompilerPlugin {
81
// Enable JVM IR transformations
82
isJvmIrTransformationEnabled = true
83
}
84
```
85
86
**JavaScript Configuration:**
87
88
```kotlin
89
// build.gradle.kts
90
kotlin {
91
js {
92
browser()
93
nodejs()
94
}
95
}
96
97
atomicfuCompilerPlugin {
98
// Enable JS IR transformations
99
isJsIrTransformationEnabled = true
100
}
101
```
102
103
**Native Configuration:**
104
105
```kotlin
106
// build.gradle.kts
107
kotlin {
108
linuxX64()
109
macosX64()
110
mingwX64()
111
}
112
113
atomicfuCompilerPlugin {
114
// Enable Native IR transformations
115
isNativeIrTransformationEnabled = true
116
}
117
```
118
119
**Multiplatform Configuration:**
120
121
```kotlin
122
// build.gradle.kts
123
kotlin {
124
jvm()
125
js {
126
browser()
127
nodejs()
128
}
129
linuxX64()
130
macosX64()
131
mingwX64()
132
133
sourceSets {
134
val commonMain by getting {
135
dependencies {
136
// Add atomicfu library dependency
137
implementation("org.jetbrains.kotlinx:atomicfu:0.25.0")
138
}
139
}
140
}
141
}
142
143
atomicfuCompilerPlugin {
144
// Enable transformations for all platforms
145
isJvmIrTransformationEnabled = true
146
isJsIrTransformationEnabled = true
147
isNativeIrTransformationEnabled = true
148
}
149
```
150
151
### Plugin Behavior
152
153
The atomicfu compiler plugin transforms high-level atomic operations into platform-specific implementations:
154
155
**Transformation Process:**
156
157
1. **Source Analysis**: The plugin scans Kotlin source code for atomicfu imports and usage
158
2. **Platform Detection**: Determines target platform (JVM, JS, Native, Wasm)
159
3. **Code Generation**: Replaces atomic operations with platform-specific equivalents
160
4. **Optimization**: Applies platform-specific optimizations for best performance
161
162
**Platform Transformations:**
163
164
- **JVM**: Uses `java.util.concurrent.atomic.*` classes for maximum performance
165
- **JavaScript**: Generates runtime implementations with lock-free algorithms
166
- **Native**: Leverages Kotlin/Native atomic intrinsics for direct memory operations
167
- **Wasm**: Provides appropriate runtime implementations for WebAssembly
168
169
**Example Transformation:**
170
171
```kotlin
172
// Source code (common)
173
val counter = atomic(0)
174
counter.compareAndSet(0, 1)
175
176
// JVM transformation
177
var counter = java.util.concurrent.atomic.AtomicInteger(0)
178
counter.compareAndSet(0, 1)
179
180
// JS transformation
181
var counter = 0
182
atomicfu_compareAndSet(0, 1, { counter }, { v -> counter = v })
183
184
// Native transformation
185
var counter = kotlin.native.concurrent.AtomicInt(0)
186
counter.compareAndSet(0, 1)
187
```
188
189
### Configuration Examples
190
191
**Minimal Configuration:**
192
193
```kotlin
194
// build.gradle.kts
195
plugins {
196
kotlin("multiplatform")
197
id("kotlinx-atomicfu")
198
}
199
200
// Plugin automatically detects targets and enables appropriate transformations
201
```
202
203
**Selective Platform Configuration:**
204
205
```kotlin
206
// build.gradle.kts
207
atomicfuCompilerPlugin {
208
// Only enable transformations for specific platforms
209
isJvmIrTransformationEnabled = true
210
isJsIrTransformationEnabled = false // Use library fallback for JS
211
isNativeIrTransformationEnabled = true
212
}
213
```
214
215
**Debug Configuration:**
216
217
```kotlin
218
// build.gradle.kts
219
atomicfuCompilerPlugin {
220
isJvmIrTransformationEnabled = false // Disable for debugging
221
isJsIrTransformationEnabled = false // Use library implementations
222
isNativeIrTransformationEnabled = false
223
}
224
225
// This configuration will use the library's runtime implementations
226
// which may be slower but easier to debug
227
```