-
-
Save Timshel/5983890 to your computer and use it in GitHub Desktop.
Atomic Sugar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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