Debian Codegraph @

lib/control.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (*s interruption *)
12
13 let interrupt = ref false
14
15 let steps = ref 0
16
17 let enable_thread_delay = ref false
18
19 exception Timeout
20
21 let check_for_interrupt () =
22 if !interrupt then begin interrupt := false; raise Sys.Break end;
23 if !enable_thread_delay then begin
24 incr steps;
25 if !steps = 1000 then begin
26 Thread.delay 0.001;
27 steps := 0;
28 end
29 end
30
31 (** This function does not work on windows, sigh... *)
32 (* This function assumes it is the only function calling [setitimer] *)
33 let unix_timeout n f x =
34 let open Unix in
35 let timeout_handler _ = raise Timeout in
36 let old_timer = getitimer ITIMER_REAL in
37 (* Here we assume that the existing timer will also interrupt us. *)
38 if old_timer.it_value > 0. && old_timer.it_value <= n then Ok (f x) else
39 let psh = Sys.signal Sys.sigalrm (Sys.Signal_handle timeout_handler) in
40 let restore_timeout () =
41 let timer_status = getitimer ITIMER_REAL in
42 let old_timer_value = old_timer.it_value -. n +. timer_status.it_value in
43 (* We want to make sure that the parent timer triggers, even if somehow the parent timeout
44 has already passed. This should not happen, but due to timer imprecision it can happen in practice *)
45 let old_timer_value = if old_timer.it_value <= 0. then 0. else
46 (if old_timer_value <= 0. then epsilon_float else old_timer_value) in
47 let _ = setitimer ITIMER_REAL { old_timer with it_value = old_timer_value } in
48 Sys.set_signal Sys.sigalrm psh
49 in
50 let open Memprof_coq.Resource_bind in
51 let& () = Memprof_coq.Masking.with_resource ~acquire:(fun () -> ()) () ~release:restore_timeout in
52 let _ = setitimer ITIMER_REAL {it_interval = 0.; it_value = n} in
53 try Ok (f x)
54 with Timeout as exn ->
55 let _, info = Exninfo.capture exn in
56 Error info
57
58 let windows_timeout n f x =
59 let killed = ref false in
60 let exited = ref false in
61 let thread init =
62 while not !killed do
63 let cur = Unix.gettimeofday () in
64 if n <= cur -. init then begin
65 interrupt := true;
66 exited := true;
67 Thread.exit () [@ocaml.warning "-3"]
68 end;
69 Thread.delay 0.5
70 done
71 in
72 let init = Unix.gettimeofday () in
73 let _id = CThread.create thread init in
74 try
75 let res = f x in
76 let () = killed := true in
77 let cur = Unix.gettimeofday () in
78 (* The thread did not interrupt, but the computation took longer than
79 expected. *)
80 let () = if n <= cur -. init then begin
81 exited := true;
82 raise Sys.Break
83 end in
84 Ok res
85 with
86 | Sys.Break ->
87 let _, info as e = Exninfo.capture Sys.Break in
88 (* Just in case, it could be a regular Ctrl+C *)
89 if not !exited then begin killed := true; Exninfo.iraise e end
90 else Error info
91 | e ->
92 let e = Exninfo.capture e in
93 let () = killed := true in
94 Exninfo.iraise e
95
96 type timeout = { timeout : 'a 'b. float -> ('a -> 'b) -> 'a -> ('b, Exninfo.info) result }
97
98 let timeout_fun = match Sys.os_type with
99 | "Unix" | "Cygwin" -> { timeout = unix_timeout }
100 | _ -> { timeout = windows_timeout }
101
102 let timeout_fun_ref = ref timeout_fun
103 let set_timeout f = timeout_fun_ref := f
104
105 let timeout n f = !timeout_fun_ref.timeout n f
106
107 let protect_sigalrm f x =
108 let timed_out = ref false in
109 let timeout_handler _ = timed_out := true in
110 try
111 let old_handler = Sys.signal Sys.sigalrm (Sys.Signal_handle timeout_handler) in
112 try
113 let res = f x in
114 Sys.set_signal Sys.sigalrm old_handler;
115 match !timed_out, old_handler with
116 | true, Sys.Signal_handle f -> f Sys.sigalrm; res
117 | _, _ -> res
118 with e ->
119 let e = Exninfo.capture e in
120 Sys.set_signal Sys.sigalrm old_handler;
121 Exninfo.iraise e
122 with Invalid_argument _ -> (* This happens on Windows, as handling SIGALRM does not seem supported *)
123 f x