Files
kotlin-fork/plugins/atomicfu/atomicfu-compiler/testData/box/atomics_basic/LockFreeStackTest.kt
T
mvicsokolova 5c5367d377 [atomicfu-JVM] Preparation for commonization of JVM and K/N transformers
The following updates in the JVM/IR plugin were made:
* Lots of refactoring with preparation for K/N support: commonization of transformations.
* Improved error handling (checks for visibility constraints, appending message about usage constraints in case of an error).
* Explicit requirements for the visibility of atomic properties: to prevent leaking they should be private/internal or be members of private/internal classes.
* Fixed visibility of generated properties: volatile properties are always private and atomic updaters have the same visibility as the original atomic property.
* Volatile fields are generated from scratch and original atomic properties are removed.
* Delegated properties support is fixed (only declaration in the same scope is allowed).
* Non-inline atomic extensions are forbidden.
* For top-level atomics: only one wrapper class per file (with corresponding visibility) is generated.
* Bug fixes.

The corresponding tickets: 
https://github.com/Kotlin/kotlinx-atomicfu/issues/322
KT-60528



Merge-request: KT-MR-10579
Merged-by: Maria Sokolova <maria.sokolova@jetbrains.com>
2023-07-20 13:59:23 +00:00

70 lines
1.6 KiB
Kotlin
Vendored

import kotlinx.atomicfu.*
import kotlin.test.*
class LockFreeStackTest {
fun testClear() {
val s = LockFreeStack<String>()
assertTrue(s.isEmpty())
s.pushLoop("A")
assertTrue(!s.isEmpty())
s.clear()
assertTrue(s.isEmpty())
}
fun testPushPopLoop() {
val s = LockFreeStack<String>()
assertTrue(s.isEmpty())
s.pushLoop("A")
assertTrue(!s.isEmpty())
assertEquals("A", s.popLoop())
assertTrue(s.isEmpty())
}
fun testPushPopUpdate() {
val s = LockFreeStack<String>()
assertTrue(s.isEmpty())
s.pushUpdate("A")
assertTrue(!s.isEmpty())
assertEquals("A", s.popUpdate())
assertTrue(s.isEmpty())
}
}
class LockFreeStack<T> {
private val top = atomic<Node<T>?>(null)
private class Node<T>(val value: T, val next: Node<T>?)
fun isEmpty() = top.value == null
fun clear() { top.value = null }
fun pushLoop(value: T) {
top.loop { cur ->
val upd = Node(value, cur)
if (top.compareAndSet(cur, upd)) return
}
}
fun popLoop(): T? {
top.loop { cur ->
if (cur == null) return null
if (top.compareAndSet(cur, cur.next)) return cur.value
}
}
fun pushUpdate(value: T) {
top.update { cur -> Node(value, cur) }
}
fun popUpdate(): T? =
top.getAndUpdate { cur -> cur?.next } ?.value
}
fun box(): String {
val testClass = LockFreeStackTest()
testClass.testClear()
testClass.testPushPopLoop()
testClass.testPushPopUpdate()
return "OK"
}