Skip to content

Commit 54f5d2c

Browse files
committed
Updated readme for user-defined extensions on atomics
1 parent 17540bf commit 54f5d2c

File tree

1 file changed

+23
-69
lines changed

1 file changed

+23
-69
lines changed

README.md

Lines changed: 23 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ The idiomatic way to use atomic operations in Kotlin.
1818
* This enables writing [common](#common) Kotlin code with atomics that compiles for JVM, JS, and Native.
1919
* [Gradle](#gradle-build-setup) for all platforms and [Maven](#maven-build-setup) for JVM are supported.
2020
* [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).
2425

2526
## Example
2627

@@ -99,8 +100,9 @@ Building with Gradle is supported for all platforms.
99100

100101
### JVM
101102

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:
104106

105107
```groovy
106108
buildscript {
@@ -114,89 +116,31 @@ buildscript {
114116
apply plugin: 'kotlinx-atomicfu'
115117
```
116118

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-
126119
### JS
127120

128121
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-
```
137122

138123
### Native
139124

140125
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.
142126
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:
149129

150130
```groovy
151131
enableFeaturePreview('GRADLE_METADATA')
152132
```
153133

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).
190135

191136
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`.
195139

196140
### Common
197141

198142
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:
200144

201145
```groovy
202146
dependencies {
@@ -310,6 +254,16 @@ a[i].value = x // set value
310254
a[i].compareAndSet(expect, update) // do atomic operations
311255
```
312256

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+
313267
### Testing lock-free data structures on JVM
314268

315269
You can optionally test lock-freedomness of lock-free data structures using `LockFreedomTestEnvironment` class.

0 commit comments

Comments
 (0)