forked from ahrefs/devkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmVar.ml
More file actions
21 lines (13 loc) · 655 Bytes
/
mVar.ml
File metadata and controls
21 lines (13 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
open ExtThread
type 'a t = { mutex : Mutex.t; cond : Condition.t; mutable v : 'a option; }
let create () = { mutex = Mutex.create (); cond = Condition.create (); v = None; }
let set t x = locked t.mutex (fun () -> t.v <- Some x; Condition.signal t.cond)
let clear t = locked t.mutex (fun () -> t.v <- None)
let rec wait t =
match t.v with
| None -> Condition.wait t.cond t.mutex; wait t
| Some x -> x
let get t = locked t.mutex (fun () -> wait t)
let grab t = locked t.mutex (fun () -> let x = wait t in t.v <- None; x)
let try_get t = locked t.mutex (fun () -> t.v)
let try_grab t = locked t.mutex (fun () -> let x = t.v in t.v <- None; x)