github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/example/routeguide/scala/AtomicRef.scala (about) 1 package example.routeguide.scala 2 3 import java.util.concurrent.atomic.AtomicReference 4 5 import scala.annotation.tailrec 6 7 class AtomicRef[T](initial: T) { 8 private val ref: AtomicReference[T] = new AtomicReference[T](initial) 9 10 def get: T = ref.get() 11 12 @tailrec final def updateAndGet(update: T => T): T = { 13 val oldValue = ref.get 14 val newValue = update(oldValue) 15 if (ref.compareAndSet(oldValue, newValue)) { 16 newValue 17 } else { 18 updateAndGet(update) 19 } 20 } 21 }