Skip to content

Instantly share code, notes, and snippets.

@Timshel
Forked from playxamplez-admin/CODE
Last active December 20, 2015 01:29
Show Gist options
  • Save Timshel/6049563 to your computer and use it in GitHub Desktop.
Save Timshel/6049563 to your computer and use it in GitHub Desktop.
#scala #atomic sugar from https://gist.github.com/piotrga/1986175
import annotation.tailrec
import java.util.concurrent.atomic.AtomicReference
class Atomic[T](val atomic : AtomicReference[T]) {
@tailrec
final def update(f: T => T) : T = {
val oldValue = atomic.get()
val newValue = f(oldValue)
if (atomic.compareAndSet(oldValue, newValue)) newValue else update(f)
}
}
object Atomic {
def apply[T]( obj : T ) = new Atomic(new AtomicReference(obj))
implicit def toAtomic[T]( ref : AtomicReference[T]) : Atomic[T] = new Atomic(ref)
implicit def delegateToAtomicReference[T]( a: Atomic[T] ) = a.atomic
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment