Debian Codegraph @

clib/memprof_coq.std.ml

raw symbols SCIP
1 let is_interrupted _ = false [@@inline]
2
3 module Resource_bind = struct
4 let ( let& ) f scope = f ~scope
5 end
6
7 (* We do our own Mutex_aux for OCaml 5.x *)
8 module Mutex_aux = Mutex_aux
9
10 module Thread_map_core = struct
11 open Resource_bind
12
13 module IMap = Map.Make (
14 struct
15 type t = int
16 let compare = Stdlib.compare
17 end)
18
19 type 'a t = { mutex : Mutex.t ; mutable map : 'a IMap.t }
20
21 let create () = { mutex = Mutex.create () ; map = IMap.empty }
22
23 let current_thread () = Thread.id (Thread.self ())
24
25 let get s =
26 (* Concurrent threads do not alter the value for the current
27 thread, so we do not need a lock. *)
28 IMap.find_opt (current_thread ()) s.map
29
30 (* For set and clear we need a lock *)
31
32 let set s v =
33 let& () = Mutex_aux.with_lock s.mutex in
34 let new_map = match v with
35 | None -> IMap.remove (current_thread ()) s.map
36 | Some v -> IMap.add (current_thread ()) v s.map
37 in
38 s.map <- new_map
39
40 let _clear s =
41 let& () = Mutex_aux.with_lock s.mutex in
42 s.map <- IMap.empty
43 end
44
45 module Masking = struct
46
47 (* There's no mechanism to block OCaml's async exceptions,
48 so without memprof there is nothing interesting to do. *)
49 let with_resource ~acquire arg ~scope ~(release : _ -> unit) =
50 let r = acquire arg in
51 Fun.protect ~finally:(fun () -> release r) (fun () -> scope r)
52
53 end
54
55 module Thread_map = struct
56 include Thread_map_core
57
58 let with_value tls ~value ~scope =
59 let old_value = get tls in
60 (* FIXME: needs proper masking here as there is a race between
61 resources and asynchronous exceptions. For now, it is
62 exception-safe only for exceptions arising from Memprof_callbacks. *)
63 Masking.with_resource
64 ~acquire:(fun () -> set tls (Some value)) ()
65 ~scope
66 ~release:(fun () -> set tls old_value)
67
68 end