arrow
arrow copied to clipboard
Update arrow.atomics to base on kotlin stdlib common atomics
https://github.com/Kotlin/KEEP/issues/398
Just remove platform implementations and keep extension functions?
e.g.
import kotlin.concurrent.atomics.AtomicInt
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
inline fun AtomicInt.loop(action: (Int) -> Unit): Nothing {
contract { callsInPlace(action, InvocationKind.AT_LEAST_ONCE) }
do {
action(load())
} while (true)
}
inline fun <R> AtomicInt.update(function: (Int) -> Int, transform: (old: Int, new: Int) -> R): R {
contract {
callsInPlace(function, InvocationKind.AT_LEAST_ONCE)
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}
loop { cur ->
val upd = function(load())
if (compareAndSet(cur, upd)) return transform(cur, upd)
}
}
inline fun AtomicInt.updateAndGet(function: (Int) -> Int): Int {
contract { callsInPlace(function, InvocationKind.AT_LEAST_ONCE) }
return update(function) { _, new -> new }
}
@serras Btw any possibility to upstream these extension functions to kotlin stdlib?
@revonateB0T I've reported this as an issue in YouTrack.
We've decided to keep our implementation of atomics for the time being (until the new ones are no longer experimental). The update family of functions is showing in 2.2.20, so this is definitely a win, since you can now easily migrate from Arrow atomics into Kotlin ones.