Is it possible to have an AtomicReference to a lis...
# kotlin-native
m
Is it possible to have an AtomicReference to a list? I'm getting this error:
Copy code
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.collections.ArrayList@1aa3f68
    at kfun:kotlin.Throwable#<init>(kotlin.String?){} (0x4169a9)
    at kfun:kotlin.Exception#<init>(kotlin.String?){} (0x410c87)
    at kfun:kotlin.RuntimeException#<init>(kotlin.String?){} (0x410d97)
    at kfun:kotlin.native.concurrent.InvalidMutabilityException#<init>(kotlin.String){} (0x433557)
    at ThrowInvalidMutabilityException (0x434a76)
    at Kotlin_AtomicReference_checkIfFrozen (0x523c71)
    at kfun:kotlin.native.concurrent.AtomicReference#<init>(1:0){} (0x4328ae)
    at ...
k
Depends on what you would like to achieve, but maybe you should take a look at the
Stately
library and its collections implementation: https://github.com/touchlab/Stately#stately-iso-collections
d
You can have an atomic reference but you can't mutate the list itself.
m
I'm not mutating the list though, this just happens when doing AtomicReference(someList)
I do swap out the list for different ones but I never modify it
and it seems the Stately collections are too slow for real-time audio ^^'
d
Hmm, will have to see code then.
m
Copy code
open class IIRNode<T>(coeffsA: List<Float>, coeffsB: List<Float>) : MonoNode<T>() {
    private val coeffsA = AtomicReference(coeffsA)
    private val coeffsB = AtomicReference(coeffsB)
    constructor(order: Int) : this(List(order+1) { 1f }, List(order+1) { 1f })

    /* ... */
}
d
That's suspicious. You don't know if those list aren't mutated after construction. You might want to take a defensive copy.
m
Unless K/N mutates them by itself I'm positive they are never modified. Even so, this errors in the AtomicReference constructor
d
Ah I see, odd. Seems youtrack worthy then.
r
Does it work if you freeze manually before adding to atomic ref?
private val coeffsA = AtomicReference(coeffsA.freeze())
etc
m
manually freezing does not solve it, no
r
What does the code look like where you're constructing a
IIRNode
?
m
I just managed to recreate it with just
val x = AtomicReference(List(10) { 1f })
r
val x = AtomicReference(List(10) { 1f }.freeze())
should work
If it doesn't then I have no idea
d
Does the stack trace at least look different?