Source file domain_manager.ml
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
open Std
type ty = [`Domain_mgr]
type 'a t = ([> ty] as 'a) r
module Pi = struct
module type MGR = sig
type t
val run : t -> (cancelled:exn Promise.t -> 'a) -> 'a
val run_raw : t -> (unit -> 'a) -> 'a
end
type (_, _, _) Resource.pi +=
| Mgr : ('t, (module MGR with type t = 't), [> ty]) Resource.pi
let mgr (type t) (module X : MGR with type t = t) =
Resource.handler [H (Mgr, (module X))]
end
let run_raw (Resource.T (t, ops)) fn =
let module X = (val (Resource.get ops Pi.Mgr)) in
X.run_raw t fn
let run (Resource.T (t, ops)) fn =
let module X = (val (Resource.get ops Pi.Mgr)) in
X.run t @@ fun ~cancelled ->
try
Fiber.first
(fun () ->
match Promise.await cancelled with
| Cancel.Cancelled ex -> raise ex
| ex -> raise ex
)
fn
with ex ->
match Promise.peek cancelled with
| Some (Cancel.Cancelled ex2 as cex) when ex == ex2 ->
raise cex
| _ -> raise ex