Files
kotlin-fork/compiler/testData/codegen/boxModernJdk/testsWithJava11/varHandle.kt
T
Ivan Kylchik c7435ba760 Replace all occurrences of WITH_RUNTIME with WITH_STDLIB
We are going to deprecate `WITH_RUNTIME` directive. The main reason
behind this change is that `WITH_STDLIB` directive better describes
its meaning, specifically it will add kotlin stdlib to test's classpath.
2021-11-17 15:26:38 +03:00

33 lines
856 B
Kotlin
Vendored

// !LANGUAGE: +PolymorphicSignature
// TARGET_BACKEND: JVM
// SKIP_JDK6
// WITH_STDLIB
import java.lang.invoke.MethodHandles
import kotlin.concurrent.thread
fun box(): String {
val handle = MethodHandles.arrayElementVarHandle(ByteArray::class.java)
val array = ByteArray(10)
val index = 0
// Check that we don't consider non-Object return type of a signature-polymorphic method to be polymorphic.
handle.weakCompareAndSetPlain(array, index, 0.toByte(), 21.toByte()) as Comparable<*>
val oldValue = 42.toByte()
val newValue = (-74).toByte()
thread {
Thread.sleep(400L)
handle.setVolatile(array, index, oldValue)
}
while (!handle.compareAndSet(array, index, oldValue, newValue)) {
Thread.sleep(10L)
}
return if (handle.getVolatile(array, index) == newValue) "OK" else "Fail"
}