src/Pure/Concurrent/synchronized_sequential.ML
author wenzelm
Thu, 22 Oct 2009 15:19:44 +0200
changeset 33060 e66b41782cb5
parent 32816 5db89f8d44f3
child 35015 efafb3337ef3
permissions -rw-r--r--
support single-assigment variables -- based on magic RTS operations by David Matthews;

(*  Title:      Pure/Concurrent/synchronized_sequential.ML
    Author:     Makarius

Sequential version of state variables -- plain refs.
*)

structure Synchronized: SYNCHRONIZED =
struct

abstype 'a var = Var of 'a Unsynchronized.ref
with

fun var _ x = Var (Unsynchronized.ref x);
fun value (Var var) = ! var;

fun timed_access (Var var) _ f =
  (case f (! var) of
    SOME (y, x') => (var := x'; SOME y)
  | NONE => Thread.unavailable ());

fun guarded_access var f = the (timed_access var (K NONE) f);

fun readonly_access var f =
  guarded_access var (fn x => (case f x of NONE => NONE | SOME y => SOME (y, x)));

fun change_result var f = guarded_access var (SOME o f);
fun change var f = change_result var (fn x => ((), f x));

val assign = change;

end;
end;