Debian Codegraph @

stm/workerPool.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 worker_id = string
12
13 type 'a cpanel = {
14 exit : unit -> unit; (* called by manager to exit instead of Thread.exit *)
15 cancelled : unit -> bool; (* manager checks for a request of termination *)
16 extra : 'a; (* extra stuff to pass to the manager *)
17 }
18
19 module type PoolModel = sig
20 (* this shall come from a Spawn.* model *)
21 type process
22 val spawn : spawn_args:string list -> int -> CoqworkmgrApi.priority -> worker_id * process * CThread.thread_ic * out_channel
23
24 (* this defines the main loop of the manager *)
25 type extra
26 val manager :
27 extra cpanel -> worker_id * process * CThread.thread_ic * out_channel -> unit
28 end
29
30 module Make(Model : PoolModel) = struct
31
32 type worker = {
33 name : worker_id;
34 cancel : bool ref;
35 manager : Thread.t;
36 process : Model.process;
37 }
38
39 type pre_pool = {
40 workers : worker list ref;
41 count : int ref;
42 extra_arg : Model.extra;
43 }
44
45 type pool = { lock : Mutex.t; pool : pre_pool }
46
47 let magic_no = 17
48
49 let master_handshake worker_id ic oc =
50 try
51 Marshal.to_channel oc magic_no []; flush oc;
52 let n = (CThread.thread_friendly_input_value ic : int) in
53 if n <> magic_no then begin
54 Printf.eprintf "Handshake with %s failed: protocol mismatch\n" worker_id;
55 exit 1;
56 end
57 with e when CErrors.noncritical e ->
58 Printf.eprintf "Handshake with %s failed: %s\n"
59 worker_id (Printexc.to_string e);
60 exit 1
61
62 let worker_handshake slave_ic slave_oc =
63 try
64 let v = (CThread.thread_friendly_input_value slave_ic : int) in
65 if v <> magic_no then begin
66 prerr_endline "Handshake failed: protocol mismatch\n";
67 exit 1;
68 end;
69 Marshal.to_channel slave_oc v []; flush slave_oc;
70 with e when CErrors.noncritical e ->
71 prerr_endline ("Handshake failed: " ^ Printexc.to_string e);
72 exit 1
73
74 let locking { lock; pool = p } f =
75 CThread.with_lock lock ~scope:(fun () -> f p)
76
77 let rec create_worker ~spawn_args extra pool priority id =
78 let cancel = ref false in
79 let name, process, ic, oc as worker = Model.spawn ~spawn_args id priority in
80 master_handshake name ic oc;
81 let exit () =
82 cancel := true;
83 cleanup ~spawn_args pool priority;
84 (Thread.exit [@warning "-3"]) ()
85 in
86 let cancelled () = !cancel in
87 let cpanel = { exit; cancelled; extra } in
88 let manager = CThread.create (Model.manager cpanel) worker in
89 { name; cancel; manager; process }
90
91 and cleanup ~spawn_args x priority = locking x begin fun { workers; count; extra_arg } ->
92 workers := List.map (function
93 | { cancel } as w when !cancel = false -> w
94 | _ -> let n = !count in incr count; create_worker ~spawn_args extra_arg x priority n)
95 !workers
96 end
97
98 let n_workers x = locking x begin fun { workers } ->
99 List.length !workers
100 end
101
102 let is_empty x = locking x begin fun { workers } -> !workers = [] end
103
104 let create ~spawn_args extra_arg ~size priority = let x = {
105 lock = Mutex.create ();
106 pool = {
107 extra_arg;
108 workers = ref [];
109 count = ref size;
110 }} in
111 locking x begin fun { workers } ->
112 workers := CList.init size (create_worker ~spawn_args extra_arg x priority)
113 end;
114 x
115
116 let cancel n x = locking x begin fun { workers } ->
117 List.iter (fun { name; cancel } -> if n = name then cancel := true) !workers
118 end
119
120 let cancel_all x = locking x begin fun { workers } ->
121 List.iter (fun { cancel } -> cancel := true) !workers
122 end
123
124 let destroy x = locking x begin fun { workers } ->
125 List.iter (fun { cancel } -> cancel := true) !workers;
126 workers := []
127 end
128
129 end