turned unsynchronized ref into synchronized var
authorboehmes
Thu, 01 Oct 2009 15:54:55 +0200
changeset 32821 99843bbfaeb2
parent 32820 02f412281b99
child 32823 b13e04329012
child 32825 314b6a73b55c
turned unsynchronized ref into synchronized var
src/Tools/more_conv.ML
--- a/src/Tools/more_conv.ML	Thu Oct 01 15:19:49 2009 +0200
+++ b/src/Tools/more_conv.ML	Thu Oct 01 15:54:55 2009 +0200
@@ -46,16 +46,18 @@
   Conv.arg_conv (Conv.abs_conv (fn (_, cx) => cv cx) ctxt)
 
 
-fun cache_conv conv =   (* FIXME not thread-safe *)
-  let 
-    val tab = Unsynchronized.ref Termtab.empty
-    fun add_result t thm =
-      let val _ = Unsynchronized.change tab (Termtab.insert Thm.eq_thm (t, thm))
-      in thm end
-    fun cconv ct =  
-      (case Termtab.lookup (!tab) (Thm.term_of ct) of
+fun cache_conv conv =
+  let
+    val cache = Synchronized.var "cache_conv" Termtab.empty
+    fun lookup t =
+      Synchronized.change_result cache (fn tab => (Termtab.lookup tab t, tab))
+    val keep = Synchronized.change cache o Termtab.insert Thm.eq_thm
+    fun keep_result t thm = (keep (t, thm); thm)
+
+    fun cconv ct =
+      (case lookup (Thm.term_of ct) of
         SOME thm => thm
-      | NONE => add_result (Thm.term_of ct) (conv ct))
+      | NONE => keep_result (Thm.term_of ct) (conv ct))
   in cconv end
 
 end