소스 검색

Use a mutex

Rudy Ges 1 개월 전
부모
커밋
1132888f96
1개의 변경된 파일13개의 추가작업 그리고 13개의 파일을 삭제
  1. 13 13
      src/core/ds/atomicLazy.ml

+ 13 - 13
src/core/ds/atomicLazy.ml

@@ -1,21 +1,21 @@
 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 =
-  { value = None; computed = Atomic.make false; compute = (fun () -> f()) }
+	{ value = None; mutex = Mutex.create (); compute = (fun () -> f()) }
 
 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"