[K/N] Change AtomicReference for the new MM ^KT-50026

Make AtomicReference behave like FreezableAtomicReference with the new MM: not frozen by default, don't require freezing value unless the ref itself is frozen. The behaviour with the old MM is unchanged.

Merge-request: KT-MR-5155
This commit is contained in:
Alexander Shabalin
2021-12-03 17:08:21 +00:00
committed by Space
parent 1d2e60a2af
commit 4fd61ad1b0
10 changed files with 84 additions and 33 deletions
@@ -74,7 +74,7 @@ fun test3(workers: Array<Worker>) {
assertEquals(seen.size, workers.size)
}
fun test4() {
fun test4LegacyMM() {
assertFailsWith<InvalidMutabilityException> {
AtomicReference(Data(1))
}
@@ -83,7 +83,25 @@ fun test4() {
}
}
fun test5() {
fun test4() {
run {
val ref = AtomicReference(Data(1))
assertEquals(1, ref.value.value)
}
run {
val ref = AtomicReference<Data?>(null)
ref.compareAndSwap(null, Data(2))
assertEquals(2, ref.value!!.value)
}
run {
val ref = AtomicReference<Data?>(null).freeze()
assertFailsWith<InvalidMutabilityException> {
ref.compareAndSwap(null, Data(2))
}
}
}
fun test5LegacyMM() {
assertFailsWith<InvalidMutabilityException> {
AtomicReference<Data?>(null).value = Data(2)
}
@@ -94,6 +112,14 @@ fun test5() {
assertEquals(3, ref.value!!.value)
}
fun test5() {
val ref = AtomicReference<Data?>(null)
ref.value = Data(2)
assertEquals(2, ref.value!!.value)
ref.value = Data(3).freeze()
assertEquals(3, ref.value!!.value)
}
fun test6() {
val int = AtomicInt(0)
int.value = 239
@@ -128,8 +154,13 @@ fun test7() {
test1(workers)
test2(workers)
test3(workers)
test4()
test5()
if (Platform.memoryModel == MemoryModel.EXPERIMENTAL) {
test4()
test5()
} else {
test4LegacyMM()
test5LegacyMM()
}
test6()
test7()