Skip to content

Commit

Permalink
Use new Atomic types from Kotlin 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed Oct 19, 2023
1 parent e2be719 commit 9860d51
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
@file:OptIn(FreezingIsDeprecated::class)
package arrow.atomic

import kotlin.native.concurrent.AtomicReference
import kotlin.native.concurrent.freeze
import kotlin.native.concurrent.isFrozen
import kotlin.concurrent.AtomicReference

public actual class Atomic<V> actual constructor(initialValue: V) {
private val inner = AtomicReference(initialValue.freeze())
private val inner = AtomicReference(initialValue)

public actual fun get(): V = inner.value

public actual fun set(value: V) {
inner.value = value.freeze()
inner.value = value
}

public actual fun compareAndSet(expected: V, new: V): Boolean =
inner.compareAndSet(expected, new.freeze())
inner.compareAndSet(expected, new)

public actual fun getAndSet(value: V): V {
if (inner.isFrozen) value.freeze()
while (true) {
val cur = inner.value
if (cur === value) return cur
if (inner.compareAndSwap(cur, value) === cur) return cur
}
}
public actual fun getAndSet(value: V): V =
inner.getAndSet(value)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package arrow.atomic

import kotlin.native.concurrent.AtomicInt
import kotlin.native.concurrent.freeze
import kotlin.native.concurrent.isFrozen
import kotlin.concurrent.AtomicInt

public actual class AtomicInt actual constructor(initialValue: Int) {
private val inner = AtomicInt(initialValue)
Expand All @@ -14,23 +12,17 @@ public actual class AtomicInt actual constructor(initialValue: Int) {
}

public actual fun incrementAndGet(): Int =
inner.addAndGet(1)
inner.incrementAndGet()

public actual fun decrementAndGet(): Int =
inner.addAndGet(-1)
inner.decrementAndGet()

public actual fun addAndGet(delta: Int): Int =
inner.addAndGet(delta)

public actual fun compareAndSet(expected: Int, new: Int): Boolean =
inner.compareAndSet(expected, new)

public actual fun getAndSet(value: Int): Int {
if (inner.isFrozen) value.freeze()
while (true) {
val cur = inner.value
if (cur == value) return cur
if (inner.compareAndSwap(cur, value) == cur) return cur
}
}
public actual fun getAndSet(value: Int): Int =
inner.getAndSet(value)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package arrow.atomic

import kotlin.native.concurrent.AtomicLong
import kotlin.native.concurrent.freeze
import kotlin.native.concurrent.isFrozen
import kotlin.concurrent.AtomicLong

public actual class AtomicLong actual constructor(initialValue: Long) {
private val inner = AtomicLong(initialValue)
Expand All @@ -14,24 +12,18 @@ public actual class AtomicLong actual constructor(initialValue: Long) {
}

public actual fun incrementAndGet(): Long =
inner.addAndGet(1)
inner.incrementAndGet()

public actual fun decrementAndGet(): Long =
inner.addAndGet(-1)
inner.decrementAndGet()

public actual fun addAndGet(delta: Long): Long =
inner.addAndGet(delta)

public actual fun compareAndSet(expected: Long, new: Long): Boolean =
inner.compareAndSet(expected, new)

public actual fun getAndSet(value: Long): Long {
if (inner.isFrozen) value.freeze()
while (true) {
val cur = inner.value
if (cur == value) return cur
if (inner.compareAndSwap(cur, value) == cur) return cur
}
}
public actual fun getAndSet(value: Long): Long =
inner.getAndSet(value)
}

0 comments on commit 9860d51

Please sign in to comment.