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

commonMain.com.badoo.reaktive.utils.atomic.AtomicReferenceExt.kt Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package com.badoo.reaktive.utils.atomic

import com.badoo.reaktive.utils.InternalReaktiveApi

@Suppress("UNCHECKED_CAST") // https://youtrack.jetbrains.com/issue/KT-57412
@InternalReaktiveApi
inline fun  AtomicReference.getAndChange(update: (T) -> T): T {
    while (true) {
        val prev: Any? = value
        val next: Any? = update(prev as T)

        if (compareAndSet(prev, next as T)) {
            return prev
        }
    }
}

@Suppress("UNCHECKED_CAST") // https://youtrack.jetbrains.com/issue/KT-57412
@InternalReaktiveApi
inline fun  AtomicReference.changeAndGet(update: (T) -> R): R {
    while (true) {
        val prev: Any? = value
        val next: Any? = update(prev as T)

        if (compareAndSet(prev, next as R)) {
            return next
        }
    }
}

@InternalReaktiveApi
inline fun  AtomicReference.change(update: (T) -> T) {
    getAndChange(update)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy