9ff0e0b046
* `nativeTest` task now allows to provide compiler plugins that may be enabled during test compilation * test sets for JVM and K/N backends are equal KT-60800 describes all the issues with native tests that were solved in this commit. Co-authored-by: Dmitriy Dolovov <Dmitriy.Dolovov@jetbrains.com> Merge-request: KT-MR-11401 Merged-by: Maria Sokolova <maria.sokolova@jetbrains.com>
36 lines
828 B
Kotlin
Vendored
36 lines
828 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
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun box() {
|
|
val testClass = SimpleLockTest()
|
|
testClass.withLock()
|
|
} |