1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
open Import
type 'a t =
{ m : Mutex.t
; cv : Condition.t
; mutable cell : 'a option
}
let create () = { m = Mutex.create (); cv = Condition.create (); cell = None }
let get t =
let rec await_value t =
match t.cell with
| None ->
Condition.wait t.cv t.m;
await_value t
| Some v ->
t.cell <- None;
v
in
with_mutex t.m ~f:(fun () -> await_value t)
let set t v =
with_mutex t.m ~f:(fun () -> t.cell <- Some v);
Condition.signal t.cv