All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nativeCommonMain.co.touchlab.stately.concurrency.GuardedStableRef.kt Maven / Gradle / Ivy

package co.touchlab.stately.concurrency

import kotlinx.cinterop.StableRef

class GuardedStableRef(t: T) {
    private val stableRef: StableRef = StableRef.create(t)
    private val threadRef = ThreadRef()
    internal val disposed = AtomicBoolean(false)

    public val isDisposed
        get() = disposed.value

    val state: T
        get() {
            checkStateAccessValid()
            return stableRef.get()
        }

    fun dispose() {
        checkStateAccessValid()
        stableRef.dispose()
        disposed.value = true
    }

    private fun checkStateAccessValid() {
        if (!threadRef.same()) {
            throw IllegalStateException("StableRef can only be accessed from the thread it was created with")
        }

        if (disposed.value) {
            throw IllegalStateException("StableRef already disposed")
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy