-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use new Atomic types from Kotlin 1.9
- Loading branch information
Showing
3 changed files
with
16 additions
and
41 deletions.
There are no files selected for viewing
21 changes: 6 additions & 15 deletions
21
arrow-libs/core/arrow-atomic/src/nativeMain/kotlin/arrow/atomic/Atomic.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters