5c5367d377
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>
57 lines
1.3 KiB
Kotlin
Vendored
57 lines
1.3 KiB
Kotlin
Vendored
import kotlinx.atomicfu.*
|
|
import kotlin.test.*
|
|
|
|
class LockFreeIntBitsTest {
|
|
fun testBasic() {
|
|
val bs = LockFreeIntBits()
|
|
assertTrue(!bs[0])
|
|
assertTrue(bs.bitSet(0))
|
|
assertTrue(bs[0])
|
|
assertTrue(!bs.bitSet(0))
|
|
|
|
assertTrue(!bs[1])
|
|
assertTrue(bs.bitSet(1))
|
|
assertTrue(bs[1])
|
|
assertTrue(!bs.bitSet(1))
|
|
assertTrue(!bs.bitSet(0))
|
|
|
|
assertTrue(bs[0])
|
|
assertTrue(bs.bitClear(0))
|
|
assertTrue(!bs.bitClear(0))
|
|
|
|
assertTrue(bs[1])
|
|
}
|
|
}
|
|
|
|
class LockFreeIntBits {
|
|
private val bits = atomic(0)
|
|
|
|
private fun Int.mask() = 1 shl this
|
|
|
|
operator fun get(index: Int): Boolean = bits.value and index.mask() != 0
|
|
|
|
// User-defined private inline function
|
|
private inline fun bitUpdate(check: (Int) -> Boolean, upd: (Int) -> Int): Boolean {
|
|
bits.update {
|
|
if (check(it)) return false
|
|
upd(it)
|
|
}
|
|
return true
|
|
}
|
|
|
|
fun bitSet(index: Int): Boolean {
|
|
val mask = index.mask()
|
|
return bitUpdate({ it and mask != 0 }, { it or mask })
|
|
}
|
|
|
|
fun bitClear(index: Int): Boolean {
|
|
val mask = index.mask()
|
|
return bitUpdate({ it and mask == 0 }, { it and mask.inv() })
|
|
}
|
|
}
|
|
|
|
fun box(): String {
|
|
val testClass = LockFreeIntBitsTest()
|
|
testClass.testBasic()
|
|
return "OK"
|
|
} |