Debian Codegraph @

clib/memprof_coq.memprof.ml

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