[K/N] Stabilization of Atomics API

`AtomicInt`, `AtomicLong`, `AtomicReference` and `AtomicNativePtr` classes were moved to `kotlin.concurrent` package. The corresponding classes from `kotlin.native.concurrent` were deprecated with warning since Kotlin 1.9.

In order to prepare for further commonization of Atomics API the following changes were made:
* `kotlin.concurrent.AtomicInt`: 
    * `increment(): Unit` and `decrement(): Unit` methods were deprecated with error
    * New methods were added: `incrementAndGet(): Int` , `decrementAndGet(): Int`, `getAndIncrement(): Int`, `getAndDecrement(): Int`, `getAndSet(newValue: Int): Int` 
* `kotlin.concurrent.AtomicLong`:
    * `increment(): Unit` and `decrement(): Unit` methods were deprecated with error
    * New methods were added: `incrementAndGet(): Long`, `decrementAndGet(): Long`, `getAndIncrement(): Long`, `getAndDecrement(): Long`, `getAndSet(newValue: Long): Long`
    * Deprecated `AtomicLong()` constructor with default parameter value
* For all atomic classes `compareAndSwap` method was renamed to `compareAndExchange`

See KT-58074 for more details.

Merge-request: KT-MR-9272
Merged-by: Maria Sokolova <maria.sokolova@jetbrains.com>
This commit is contained in:
mvicsokolova
2023-04-25 16:55:42 +00:00
committed by Space Team
parent 1c56a71b14
commit 75b4469757
33 changed files with 1162 additions and 493 deletions
@@ -8,6 +8,7 @@ val K = if (true) "K" else "A" // to avoid const init
// FILE: main.kt
import kotlin.native.concurrent.*
import kotlin.concurrent.AtomicInt
val sem = AtomicInt(0)
@@ -18,7 +19,7 @@ fun box() : String {
mode = TransferMode.SAFE,
{ },
{
sem.increment();
sem.incrementAndGet();
while (sem.value != 3) {}
O
}
@@ -27,7 +28,7 @@ fun box() : String {
mode = TransferMode.SAFE,
{ },
{
sem.increment();
sem.incrementAndGet();
while (sem.value != 3) {}
K
}
@@ -38,4 +39,4 @@ fun box() : String {
w1.requestTermination().result
w2.requestTermination().result
return result
}
}
@@ -22,5 +22,5 @@ import kotlin.concurrent.*
fun box() : String {
val o = "O"
val x = Box(o)
return x::value.compareAndSwapField(o, "K") + x.value
return x::value.compareAndExchangeField(o, "K") + x.value
}
+6 -6
View File
@@ -38,28 +38,28 @@ interface RefWrapper<T> : Wrapper<T> {
class IntWrapper(@Volatile var x : Int) : IncWrapper<Int> {
override fun compareAndSwap(expected: Int, new: Int) = this::x.compareAndSwapField(expected, new)
override fun compareAndSwap(expected: Int, new: Int) = this::x.compareAndExchangeField(expected, new)
override fun compareAndSet(expected: Int, new: Int) = this::x.compareAndSetField(expected, new)
override fun getAndSet(new: Int) = this::x.getAndSetField(new)
override fun getAndAdd(delta: Int) = this::x.getAndAddFieldLocal(delta)
}
class LongWrapper(@Volatile var x : Long) : IncWrapper<Long> {
override fun compareAndSwap(expected: Long, new: Long) = this::x.compareAndSwapField(expected, new)
override fun compareAndSwap(expected: Long, new: Long) = this::x.compareAndExchangeField(expected, new)
override fun compareAndSet(expected: Long, new: Long) = this::x.compareAndSetField(expected, new)
override fun getAndSet(new: Long) = this::x.getAndSetField(new)
override fun getAndAdd(delta: Long) = this::x.getAndAddFieldLocal(delta)
}
class ShortWrapper(@Volatile var x : Short) : IncWrapper<Short> {
override fun compareAndSwap(expected: Short, new: Short) = this::x.compareAndSwapField(expected, new)
override fun compareAndSwap(expected: Short, new: Short) = this::x.compareAndExchangeField(expected, new)
override fun compareAndSet(expected: Short, new: Short) = this::x.compareAndSetField(expected, new)
override fun getAndSet(new: Short) = this::x.getAndSetField(new)
override fun getAndAdd(delta: Short) = this::x.getAndAddFieldLocal(delta)
}
class ByteWrapper(@Volatile var x : Byte) : IncWrapper<Byte> {
override fun compareAndSwap(expected: Byte, new: Byte) = this::x.compareAndSwapField(expected, new)
override fun compareAndSwap(expected: Byte, new: Byte) = this::x.compareAndExchangeField(expected, new)
override fun compareAndSet(expected: Byte, new: Byte) = this::x.compareAndSetField(expected, new)
override fun getAndSet(new: Byte) = this::x.getAndSetField(new)
override fun getAndAdd(delta: Byte) = this::x.getAndAddFieldLocal(delta)
@@ -67,13 +67,13 @@ class ByteWrapper(@Volatile var x : Byte) : IncWrapper<Byte> {
class StringWrapper(@Volatile var x : String) : RefWrapper<String> {
override fun compareAndSwap(expected: String, new: String) = this::x.compareAndSwapField(expected, new)
override fun compareAndSwap(expected: String, new: String) = this::x.compareAndExchangeField(expected, new)
override fun compareAndSet(expected: String, new: String) = this::x.compareAndSetField(expected, new)
override fun getAndSet(new: String) = this::x.getAndSetField(new)
}
class GenericWrapper<T>(@Volatile var x : T) : RefWrapper<T> {
override fun compareAndSwap(expected: T, new: T) = this::x.compareAndSwapField(expected, new)
override fun compareAndSwap(expected: T, new: T) = this::x.compareAndExchangeField(expected, new)
override fun compareAndSet(expected: T, new: T) = this::x.compareAndSetField(expected, new)
override fun getAndSet(new: T) = this::x.getAndSetField(new)
}
@@ -35,8 +35,8 @@ val c = "3"
fun box() : String {
if (::x.compareAndSetField(1, 2) != true) return "FAIL Int: 1"
if (::x.compareAndSetField(1, 2) != false) return "FAIL Int: 2"
if (::x.compareAndSwapField(2, 1) != 2) return "FAIL Int: 3"
if (::x.compareAndSwapField(2, 1) != 1) return "FAIL Int: 4"
if (::x.compareAndExchangeField(2, 1) != 2) return "FAIL Int: 3"
if (::x.compareAndExchangeField(2, 1) != 1) return "FAIL Int: 4"
if (::x.getAndSetField(3) != 1) return "FAIL Int: 5"
if (::x.getAndSetField(1) != 3) return "FAIL Int: 6"
if (::x.getAndAddFieldLocal(1) != 1) return "FAIL Int: 7"
@@ -45,8 +45,8 @@ fun box() : String {
if (::y.compareAndSetField(1L, 2L) != true) return "FAIL Long: 1"
if (::y.compareAndSetField(1L, 2L) != false) return "FAIL Long: 2"
if (::y.compareAndSwapField(2L, 1L) != 2L) return "FAIL Long: 3"
if (::y.compareAndSwapField(2L, 1L) != 1L) return "FAIL Long: 4"
if (::y.compareAndExchangeField(2L, 1L) != 2L) return "FAIL Long: 3"
if (::y.compareAndExchangeField(2L, 1L) != 1L) return "FAIL Long: 4"
if (::y.getAndSetField(3L) != 1L) return "FAIL Long: 5"
if (::y.getAndSetField(1L) != 3L) return "FAIL Long: 6"
if (::y.getAndAddFieldLocal(1L) != 1L) return "FAIL Long: 7"
@@ -57,8 +57,8 @@ fun box() : String {
if (isExperimentalMM()) {
if (::z.compareAndSetField(a, b) != true) return "FAIL String: 1"
if (::z.compareAndSetField(a, b) != false) return "FAIL String: 2"
if (::z.compareAndSwapField(b, a) != b) return "FAIL String: 3"
if (::z.compareAndSwapField(b, a) != a) return "FAIL String: 4"
if (::z.compareAndExchangeField(b, a) != b) return "FAIL String: 3"
if (::z.compareAndExchangeField(b, a) != a) return "FAIL String: 4"
if (::z.getAndSetField(c) != a) return "FAIL String: 5"
if (::z.getAndSetField(a) != c) return "FAIL String: 6"
if (z != a) return "FAIL String: 7"
@@ -66,8 +66,8 @@ fun box() : String {
if (::t.compareAndSetField(true, false) != true) return "FAIL Bool: 1"
if (::t.compareAndSetField(true, false) != false) return "FAIL Bool: 2"
if (::t.compareAndSwapField(false, true) != false) return "FAIL Bool: 3"
if (::t.compareAndSwapField(false, true) != true) return "FAIL Bool: 4"
if (::t.compareAndExchangeField(false, true) != false) return "FAIL Bool: 3"
if (::t.compareAndExchangeField(false, true) != true) return "FAIL Bool: 4"
if (::t.getAndSetField(false) != true) return "FAIL Bool: 5"
if (::t.getAndSetField(true) != false) return "FAIL Bool: 6"
if (t != true) return "FAIL Bool: 7"