Skip to content

Commit 2d4cabc

Browse files
elizarovmvicsokolova
authored andcommitted
Docs for locks
1 parent cab9a43 commit 2d4cabc

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The idiomatic way to use atomic operations in Kotlin.
2121
* [JDK9 VarHandle](#varhandles-with-java-9).
2222
* [Arrays of atomic values](#arrays-of-atomic-values).
2323
* [User-defined extensions on atomics](#user-defined-extensions-on-atomics)
24+
* [Locks](#locks)
2425
* [Testing of lock-free data structures](#testing-lock-free-data-structures-on-jvm).
2526

2627
## Example
@@ -124,7 +125,7 @@ Configure add apply plugin just like for [JVM](#jvm).
124125
### Native
125126

126127
This library is available for Kotlin/Native (`atomicfu-native`).
127-
Kotlin/Native uses Gradle metadata and needs Gradle version 5.3 or later
128+
Kotlin/Native uses Gradle metadata and needs Gradle version 5.3 or later.
128129
See [Gradle Metadata 1.0 announcement](https://blog.gradle.org/gradle-metadata-1.0) for more details.
129130
Apply the corresponding plugin just like for [JVM](#jvm).
130131

@@ -279,6 +280,26 @@ be public and be used outside of the module they are defined in. For example:
279280
private inline fun AtomicBoolean.tryAcquire(): Boolean = compareAndSet(false, true)
280281
```
281282

283+
### Locks
284+
285+
This project includes `kotlinx.atomicfu.locks` package providing multiplatform locking primitives that
286+
require no additional runtime dependencies on Kotlin/JVM and Kotlin/JS with a library implementation for
287+
Kotlin/Native.
288+
289+
* `SynchronizedObject` is designed for inheritance. You write `class MyClass : SynchronizedObject()` and then
290+
use `synchronized(instance) { ... }` extension function similarly to the
291+
[synchronized](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/synchronized.html)
292+
function from the standard library that is available for JVM. The `SynchronizedObject` superclass gets erased
293+
(transformed to `Any`) on JVM and JS, with `synchronized` leaving no trace in the code on JS and getting
294+
replaced with built-in monitors for locking on JVM.
295+
296+
* `ReentrantLock` is designed for delegation. You write `val lock = reentrantLock()` to construct its instance and
297+
use `lock`/`tryLock`/`unlock` functions or `lock.withLock { ... }` extension function similarly to the way
298+
[jucl.ReentrantLock](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantLock.html)
299+
is used on JVM. On JVM it is a typealias to the later class, erased on JS.
300+
301+
Condition variables (`notify`/`wait` and `signal`/`await`) are not supported.
302+
282303
### Testing lock-free data structures on JVM
283304

284305
You can optionally test lock-freedomness of lock-free data structures using

0 commit comments

Comments
 (0)