Debian Codegraph @

clib/cThread.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 type thread_ic = in_channel
12
13 let prepare_in_channel_for_thread_friendly_io ic = ic
14
15 let thread_friendly_read_fd fd s ~off ~len =
16 let rec loop () =
17 try Unix.read fd s off len
18 with Unix.Unix_error(Unix.EINTR,_,_) -> loop ()
19 in
20 loop ()
21
22 let thread_friendly_read ic s ~off ~len =
23 try
24 let fd = Unix.descr_of_in_channel ic in
25 thread_friendly_read_fd fd s ~off ~len
26 with Unix.Unix_error _ -> 0
27
28 let really_read_fd fd s off len =
29 let i = ref 0 in
30 while !i < len do
31 let off = off + !i in
32 let len = len - !i in
33 let r = thread_friendly_read_fd fd s ~off ~len in
34 if r = 0 then raise End_of_file;
35 i := !i + r
36 done
37
38 let really_read_fd_2_oc fd oc len =
39 let i = ref 0 in
40 let size = 4096 in
41 let s = Bytes.create size in
42 while !i < len do
43 let len = len - !i in
44 let r = thread_friendly_read_fd fd s ~off:0 ~len:(min len size) in
45 if r = 0 then raise End_of_file;
46 i := !i + r;
47 output oc s 0 r;
48 done
49
50 let thread_friendly_really_read ic s ~off ~len =
51 try
52 let fd = Unix.descr_of_in_channel ic in
53 really_read_fd fd s off len
54 with Unix.Unix_error _ -> raise End_of_file
55
56 let thread_friendly_really_read_line ic =
57 try
58 let fd = Unix.descr_of_in_channel ic in
59 let b = Buffer.create 1024 in
60 let s = Bytes.make 1 '\000' in
61 let endl = Bytes.of_string "\n" in
62 while not (Bytes.equal s endl) do
63 let n = thread_friendly_read_fd fd s ~off:0 ~len:1 in
64 if n = 0 then raise End_of_file;
65 if not (Bytes.equal s endl) then Buffer.add_bytes b s;
66 done;
67 Buffer.contents b
68 with Unix.Unix_error _ -> raise End_of_file
69
70 let thread_friendly_input_value ic =
71 try
72 let fd = Unix.descr_of_in_channel ic in
73 let header = Bytes.create Marshal.header_size in
74 really_read_fd fd header 0 Marshal.header_size;
75 let body_size = Marshal.data_size header 0 in
76 let desired_size = body_size + Marshal.header_size in
77 if desired_size <= Sys.max_string_length then begin
78 let msg = Bytes.create desired_size in
79 Bytes.blit header 0 msg 0 Marshal.header_size;
80 really_read_fd fd msg Marshal.header_size body_size;
81 Marshal.from_bytes msg 0
82 end else begin
83 (* Workaround for 32 bit systems and data > 16M *)
84 let name, oc =
85 Filename.open_temp_file ~mode:[Open_binary] "coq" "marshal" in
86 try
87 output oc header 0 Marshal.header_size;
88 really_read_fd_2_oc fd oc body_size;
89 close_out oc;
90 let ic = open_in_bin name in
91 let data = Marshal.from_channel ic in
92 close_in ic;
93 Sys.remove name;
94 data
95 with e -> Sys.remove name; raise e
96 end
97 with Unix.Unix_error _ | Sys_error _ -> raise End_of_file
98
99 (* On the ocaml runtime used in some opam-for-windows version the
100 * [Thread.sigmask] API raises Invalid_argument "not implemented",
101 * hence we protect the call and turn the exception into a no-op *)
102 let mask_sigalrm f x =
103 begin try ignore(Thread.sigmask Unix.SIG_BLOCK [Sys.sigalrm])
104 with Invalid_argument _ -> () end;
105 f x
106
107 let create f x =
108 Thread.create (mask_sigalrm f) x
109
110 let with_lock = Memprof_coq.Mutex_aux.with_lock