Skip to content

Add getAndUpdate() method to AtomicReference #91

@volo-droid

Description

@volo-droid

On JVM the AtomicReference class contains the getAndUpdate() method. It'd be convenient to have it for other targets/common code as well. The non-JVM implementation can look like:

fun getAndUpdate(updateFunction: (V) -> V): V {
    while(true) {
        val prev = get()
        val next = updateFunction(prev)
        if (compareAndSet(prev, next)) return prev
    }
}

The signature of the corresponding JVM method is:

public final V getAndUpdate(UnaryOperator<V> updateFunction)

where UnaryOperator is a functional interface:

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T>

That should place nicely with the SAM conversion

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions