diff options
| author | Enrico Tassi | 2016-09-05 16:23:09 +0200 |
|---|---|---|
| committer | Enrico Tassi | 2016-09-05 16:37:00 +0200 |
| commit | 6f393734e54146c4d26325aea006368380d97280 (patch) | |
| tree | db447c39ec86206fe8da06692b279f22a6c1b1cc | |
| parent | 3cf399dd17759cc0258295045742f1b50e376d5a (diff) | |
Summary: simpler API for process-local storage
| -rw-r--r-- | library/summary.ml | 25 | ||||
| -rw-r--r-- | library/summary.mli | 11 |
2 files changed, 36 insertions, 0 deletions
diff --git a/library/summary.ml b/library/summary.ml index 4fef06438d..6efa07f388 100644 --- a/library/summary.ml +++ b/library/summary.ml @@ -186,4 +186,29 @@ let ref ?(freeze=fun _ r -> r) ~name x = init_function = (fun () -> r := x) }; r +module Local = struct + +type 'a local_ref = ('a CEphemeron.key * string) ref + +let (:=) r v = r := (CEphemeron.create v, snd !r) + +let (!) r = + let key, name = !r in + try CEphemeron.get key + with CEphemeron.InvalidKey -> + let _, { init_function } = + Int.Map.find (String.hash (mangle name)) !summaries in + init_function (); + CEphemeron.get (fst !r) + +let ref ?(freeze=fun x -> x) ~name init = + let r = Pervasives.ref (CEphemeron.create init, name) in + declare_summary name + { freeze_function = (fun _ -> freeze !r); + unfreeze_function = ((:=) r); + init_function = (fun () -> r := init) }; + r + +end + let dump = Dyn.dump diff --git a/library/summary.mli b/library/summary.mli index 27889cab27..1b57613cb7 100644 --- a/library/summary.mli +++ b/library/summary.mli @@ -42,6 +42,17 @@ val declare_summary : string -> 'a summary_declaration -> unit val ref : ?freeze:(marshallable -> 'a -> 'a) -> name:string -> 'a -> 'a ref +(* As [ref] but the value is local to a process, i.e. not sent to, say, proof + * workers. It is useful to implement a local cache for example. *) +module Local : sig + + type 'a local_ref + val ref : ?freeze:('a -> 'a) -> name:string -> 'a -> 'a local_ref + val (:=) : 'a local_ref -> 'a -> unit + val (!) : 'a local_ref -> 'a + +end + (** Special name for the summary of ML modules. This summary entry is special because its unfreeze may load ML code and hence add summary entries. Thus is has to be recognizable, and handled appropriately *) |
