Files
kotlin-fork/plugins/atomicfu/atomicfu-compiler/testData/box/SimpleLockTest.kt
T
mvicsokolova 93561a1a55 kotlinx.atomicfu compiler plugin for JS_IR backend (#4581)
* kotlinx.atomicfu compiler plugin for JS_IR

Support transformations of atomic operations introduced by the kotlinx.atomicfu library for the JS_IR backend. Compiler plugin is applied externally by the kotlinx.atomicfu gradle plugin.

* Apply compiler plugin for JS platform only

* New plugin test structure

* testGroupOutputDirPrefix changed
2021-12-01 22:33:13 +03:00

36 lines
846 B
Kotlin
Vendored

import kotlinx.atomicfu.*
import kotlin.test.*
class SimpleLockTest {
fun withLock() {
val lock = SimpleLock()
val result = lock.withLock {
"OK"
}
assertEquals("OK", result)
}
}
class SimpleLock {
private val _locked = atomic(0)
fun <T> withLock(block: () -> T): T {
// this contrieves construct triggers Kotlin compiler to reuse local variable slot #2 for
// the exception in `finally` clause
try {
_locked.loop { locked ->
check(locked == 0)
if (!_locked.compareAndSet(0, 1)) return@loop // continue
return block()
}
} finally {
_locked.value = 0
}
}
}
fun box(): String {
val testClass = SimpleLockTest()
testClass.withLock()
return "OK"
}