|
@@ -1,21 +1,21 @@
|
|
open Atomic
|
|
open Atomic
|
|
|
|
|
|
-type 'a atomic_lazy = {
|
|
|
|
- mutable value: 'a option;
|
|
|
|
- computed: bool Atomic.t;
|
|
|
|
- compute: unit->'a
|
|
|
|
|
|
+type 'a t = {
|
|
|
|
+ mutable value: 'a option;
|
|
|
|
+ mutex: Mutex.t;
|
|
|
|
+ compute: unit->'a
|
|
}
|
|
}
|
|
|
|
|
|
let from_fun f =
|
|
let from_fun f =
|
|
- { value = None; computed = Atomic.make false; compute = (fun () -> f()) }
|
|
|
|
|
|
+ { value = None; mutex = Mutex.create (); compute = (fun () -> f()) }
|
|
|
|
|
|
let force lazy_val =
|
|
let force lazy_val =
|
|
- if not (Atomic.get lazy_val.computed) then begin
|
|
|
|
- let result = lazy_val.compute () in
|
|
|
|
- lazy_val.value <- Some result;
|
|
|
|
- Atomic.set lazy_val.computed true;
|
|
|
|
- end;
|
|
|
|
- match lazy_val.value with
|
|
|
|
- | Some v -> v
|
|
|
|
- | None -> failwith "Value not computed"
|
|
|
|
|
|
+ if Option.is_none lazy_val.value then
|
|
|
|
+ Mutex.protect lazy_val.mutex (fun () ->
|
|
|
|
+ let result = lazy_val.compute () in
|
|
|
|
+ lazy_val.value <- Some result;
|
|
|
|
+ );
|
|
|
|
+ match lazy_val.value with
|
|
|
|
+ | Some v -> v
|
|
|
|
+ | None -> failwith "Value not computed"
|
|
|
|
|