src/Pure/Concurrent/volatile.scala
author wenzelm
Sun, 20 May 2012 11:34:33 +0200
changeset 47884 21c42b095c84
parent 46712 8650d9a95736
child 56685 535d59d4ed12
permissions -rw-r--r--
try to avoid races again (cf. 8c37cb84065f and fd3a36e48b09);

/*  Title:      Pure/Concurrent/volatile.scala
    Module:     PIDE
    Author:     Makarius

Volatile variables.
*/

package isabelle


object Volatile
{
  def apply[A](init: A): Volatile[A] = new Volatile(init)
}


final class Volatile[A] private(init: A)
{
  @volatile private var state: A = init
  def apply(): A = state
  def >> (f: A => A) { state = f(state) }
  def >>>[B] (f: A => (B, A)): B =
  {
    val (result, new_state) = f(state)
    state = new_state
    result
  }
}