@@ -18,9 +18,10 @@ The idiomatic way to use atomic operations in Kotlin.
18
18
* This enables writing [ common] ( #common ) Kotlin code with atomics that compiles for JVM, JS, and Native.
19
19
* [ Gradle] ( #gradle-build-setup ) for all platforms and [ Maven] ( #maven-build-setup ) for JVM are supported.
20
20
* [ Additional features] ( #additional-features ) include:
21
- * Support for [ JDK9 VarHandle] ( #varhandles-with-java-9 ) .
22
- * Support for [ arrays of atomic values] ( #arrays-of-atomic-values ) .
23
- * Support for [ testing of lock-free data structures] ( #testing-lock-free-data-structures-on-jvm ) .
21
+ * [ JDK9 VarHandle] ( #varhandles-with-java-9 ) .
22
+ * [ Arrays of atomic values] ( #arrays-of-atomic-values ) .
23
+ * [ User-defined extensions on atomics] ( #user-defined-extensions-on-atomics )
24
+ * [ Testing of lock-free data structures] ( #testing-lock-free-data-structures-on-jvm ) .
24
25
25
26
## Example
26
27
@@ -99,8 +100,9 @@ Building with Gradle is supported for all platforms.
99
100
100
101
### JVM
101
102
102
- You will need Gradle 4.0 or later for the following snippets to work.
103
- Add and apply AtomicFU plugin:
103
+ You will need Gradle 4.10 or later.
104
+ Add and apply AtomicFU plugin. It adds all the corresponding dependencies
105
+ and transformations automatically:
104
106
105
107
``` groovy
106
108
buildscript {
@@ -114,89 +116,31 @@ buildscript {
114
116
apply plugin: 'kotlinx-atomicfu'
115
117
```
116
118
117
- Add compile-only dependency on AtomicFU library and run-time dependency for tests:
118
-
119
- ``` groovy
120
- dependencies {
121
- compileOnly "org.jetbrains.kotlinx:atomicfu:$atomicfu_version"
122
- testRuntime "org.jetbrains.kotlinx:atomicfu:$atomicfu_version"
123
- }
124
- ```
125
-
126
119
### JS
127
120
128
121
Configure add apply plugin just like for [ JVM] ( #jvm ) .
129
- The only change is that JS version of the library (` atomicfu-js ` ) shall be used in dependencies:
130
-
131
- ``` groovy
132
- dependencies {
133
- compileOnly "org.jetbrains.kotlinx:atomicfu-js:$atomicfu_version"
134
- testRuntime "org.jetbrains.kotlinx:atomicfu-js:$atomicfu_version"
135
- }
136
- ```
137
122
138
123
### Native
139
124
140
125
This library is available for Kotlin/Native (` atomicfu-native ` ).
141
- It is a regular library and you should declare a normal dependency, no plugin is needed nor available.
142
126
Only single-threaded code (JS-style) is currently supported.
143
-
144
- Kotlin/Native supports only Gradle version 4.7
145
- and you should use ` kotlin-platform-native ` plugin.
146
-
147
- First of all, you'll need to enable Gradle metadata in your
148
- ` settings.gradle ` file:
127
+ Kotlin/Native supports only Gradle version 4.10.
128
+ You'll need to enable Gradle metadata in your ` settings.gradle ` file:
149
129
150
130
``` groovy
151
131
enableFeaturePreview('GRADLE_METADATA')
152
132
```
153
133
154
- Then, you'll need to apply the corresponding plugin and add appropriate dependencies in your
155
- ` build.gradle ` file:
156
-
157
- ``` groovy
158
- buildscript {
159
- repositories {
160
- jcenter()
161
- maven { url 'https://plugins.gradle.org/m2/' }
162
- maven { url 'https://dl.bintray.com/jetbrains/kotlin-native-dependencies' }
163
- }
164
-
165
- dependencies {
166
- classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:$kotlin_native_version"
167
- }
168
-
169
- }
170
-
171
- apply plugin: 'kotlin-platform-native'
172
-
173
- repositories {
174
- jcenter()
175
- }
176
-
177
- dependencies {
178
- implementation 'org.jetbrains.kotlinx:atomicfu-native:0.12.2'
179
- }
180
-
181
- sourceSets {
182
- main {
183
- component {
184
- targets = ["ios_arm64", "ios_arm32", "ios_x64", "macos_x64", "linux_x64", "mingw_x64"]
185
- outputKinds = [EXECUTABLE]
186
- }
187
- }
188
- }
189
- ```
134
+ Then, you'll need to apply the corresponding plugin just like for [ JVM] ( #jvm ) .
190
135
191
136
Since Kotlin/Native does not generally provide binary compatibility between versions,
192
- you should use the same version of Kotlin/Native compiler as was used to build AtomicFU.
193
- Add an appropriate ` kotlin_native_version ` to your ` gradle.properties ` file.
194
- See [ gradle.properties] ( gradle.properties ) in AtomicFU project.
137
+ you should use the same version of Kotlin compiler as was used to build AtomicFU.
138
+ See [ gradle.properties] ( gradle.properties ) in AtomicFU project for its ` kotlin_version ` .
195
139
196
140
### Common
197
141
198
142
If you write a common code that should get compiled or different platforms, add ` org.jetbrains.kotlinx:atomicfu-common `
199
- to your common code dependencies:
143
+ to your common code dependencies or apply ` kotlinx-atomicfu ` plugin that adds this dependency automatically :
200
144
201
145
``` groovy
202
146
dependencies {
@@ -310,6 +254,16 @@ a[i].value = x // set value
310
254
a[i].compareAndSet(expect, update) // do atomic operations
311
255
```
312
256
257
+ ### User-defined extensions on atomics
258
+
259
+ You can define you own extension functions on ` AtomicXxx ` types but they must be ` inline ` and they cannot
260
+ be public and be used outside of the module they are defined in. For example:
261
+
262
+ ``` kotlin
263
+ @Suppress(" NOTHING_TO_INLINE" )
264
+ private inline fun AtomicBoolean.tryAcquire (): Boolean = compareAndSet(false , true )
265
+ ```
266
+
313
267
### Testing lock-free data structures on JVM
314
268
315
269
You can optionally test lock-freedomness of lock-free data structures using ` LockFreedomTestEnvironment ` class.
0 commit comments