1 (* Title: HOL/Tools/Sledgehammer/async_manager.ML
2 Author: Fabian Immler, TU Muenchen
4 Author: Jasmin Blanchette, TU Muenchen
6 Central manager for asynchronous diagnosis tool threads.
9 signature ASYNC_MANAGER =
11 val implode_desc : string * string -> string
12 val break_into_chunks : string -> string list
14 string -> Time.time -> Time.time -> string * string
15 -> (unit -> bool * string) -> unit
16 val kill_threads : string -> string -> unit
17 val running_threads : string -> string -> unit
18 val thread_messages : string -> string -> int option -> unit
21 structure Async_Manager : ASYNC_MANAGER =
26 val message_store_limit = 20;
27 val message_display_limit = 5;
30 (** thread management **)
32 val implode_desc = op ^ o apfst quote
34 fun implode_message (workers, work) =
35 space_implode " " (Try.serial_commas "and" (map quote workers)) ^ work
38 (* data structures over threads *)
40 structure Thread_Heap = Heap
42 type elem = Time.time * Thread.thread;
43 fun ord ((a, _), (b, _)) = Time.compare (a, b);
46 fun lookup_thread xs = AList.lookup Thread.equal xs;
47 fun delete_thread xs = AList.delete Thread.equal xs;
48 fun update_thread xs = AList.update Thread.equal xs;
51 (* state of thread manager *)
54 {manager: Thread.thread option,
55 timeout_heap: Thread_Heap.T,
58 * (string * Time.time * Time.time * (string * string))) list,
59 canceling: (Thread.thread * (string * Time.time * (string * string))) list,
60 messages: (bool * (string * (string * string))) list,
61 store: (string * (string * string)) list}
63 fun make_state manager timeout_heap active canceling messages store : state =
64 {manager = manager, timeout_heap = timeout_heap, active = active,
65 canceling = canceling, messages = messages, store = store}
67 val global_state = Synchronized.var "async_manager"
68 (make_state NONE Thread_Heap.empty [] [] [] []);
71 (* unregister thread *)
73 fun unregister (urgent, message) thread =
74 Synchronized.change global_state
75 (fn state as {manager, timeout_heap, active, canceling, messages, store} =>
76 (case lookup_thread active thread of
77 SOME (tool, _, _, desc as (worker, its_desc)) =>
79 val active' = delete_thread thread active;
81 val canceling' = (thread, (tool, now, desc)) :: canceling
82 val message' = (worker, its_desc ^ "\n" ^ message)
83 val messages' = (urgent, (tool, message')) :: messages
84 val store' = (tool, message') ::
85 (if length store <= message_store_limit then store
86 else #1 (chop message_store_limit store));
87 in make_state manager timeout_heap active' canceling' messages' store' end
91 (* main manager thread -- only one may exist *)
93 val min_wait_time = seconds 0.3;
94 val max_wait_time = seconds 10.0;
96 fun replace_all bef aft =
98 fun aux seen "" = String.implode (rev seen)
100 if String.isPrefix bef s then
101 aux seen "" ^ aft ^ aux [] (unprefix bef s)
103 aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
106 (* This is a workaround for Proof General's off-by-a-few sendback display bug,
107 whereby "pr" in "proof" is not highlighted. *)
108 val break_into_chunks = space_explode "\000" o replace_all "\n\n" "\000"
110 fun print_new_messages () =
111 Synchronized.change_result global_state
112 (fn {manager, timeout_heap, active, canceling, messages, store} =>
116 (null active andalso null canceling) orelse urgent)
117 ||> (fn postponed_messages =>
118 make_state manager timeout_heap active canceling
119 postponed_messages store))
120 |> map (fn (_, (tool, (worker, work))) => ((tool, work), worker))
121 |> AList.group (op =)
122 |> List.app (fn ((tool, work), workers) =>
124 implode_message (workers |> sort string_ord, work)
126 |> List.app Output.urgent_message)
128 fun check_thread_manager () = Synchronized.change global_state
129 (fn state as {manager, timeout_heap, active, canceling, messages, store} =>
130 if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
131 else let val manager = SOME (Toplevel.thread false (fn () =>
133 fun time_limit timeout_heap =
134 (case try Thread_Heap.min timeout_heap of
135 NONE => Time.+ (Time.now (), max_wait_time)
136 | SOME (time, _) => time);
138 (*action: find threads whose timeout is reached, and interrupt canceling threads*)
139 fun action {manager, timeout_heap, active, canceling, messages, store} =
140 let val (timeout_threads, timeout_heap') =
141 Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
143 if null timeout_threads andalso null canceling then
147 val _ = List.app (Simple_Thread.interrupt_unsynchronized o #1) canceling
148 val canceling' = filter (Thread.isActive o #1) canceling
149 val state' = make_state manager timeout_heap' active canceling' messages store;
150 in SOME (map #2 timeout_threads, state') end
153 while Synchronized.change_result global_state
154 (fn state as {timeout_heap, active, canceling, messages, store, ...} =>
155 if null active andalso null canceling andalso null messages
156 then (false, make_state NONE timeout_heap active canceling messages store)
159 (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
161 |> List.app (unregister (false, "Timed out."));
162 print_new_messages ();
163 (*give threads some time to respond to interrupt*)
164 OS.Process.sleep min_wait_time)
166 in make_state manager timeout_heap active canceling messages store end)
169 (* register thread *)
171 fun register tool birth_time death_time desc thread =
172 (Synchronized.change global_state
173 (fn {manager, timeout_heap, active, canceling, messages, store} =>
175 val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
176 val active' = update_thread (thread, (tool, birth_time, death_time, desc)) active;
177 val state' = make_state manager timeout_heap' active' canceling messages store;
179 check_thread_manager ())
182 fun launch tool birth_time death_time desc f =
183 (Toplevel.thread true
186 val self = Thread.self ()
187 val _ = register tool birth_time death_time desc self
188 in unregister (f ()) self end);
192 (** user commands **)
196 fun kill_threads tool das_wort_worker = Synchronized.change global_state
197 (fn {manager, timeout_heap, active, canceling, messages, store} =>
200 map_filter (fn (th, (tool', _, _, desc)) =>
201 if tool' = tool then SOME (th, (tool', Time.now (), desc))
203 val state' = make_state manager timeout_heap [] (killing @ canceling) messages store;
205 if null killing then ()
206 else Output.urgent_message ("Interrupted active " ^ das_wort_worker ^ "s.")
210 (* running threads *)
212 fun seconds time = string_of_int (Time.toSeconds time) ^ " s"
214 fun running_threads tool das_wort_worker =
216 val {active, canceling, ...} = Synchronized.value global_state;
218 val now = Time.now ();
219 fun running_info (_, (tool', birth_time, death_time, desc)) =
221 SOME ("Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
222 seconds (Time.- (death_time, now)) ^ " to live:\n" ^
226 fun canceling_info (_, (tool', death_time, desc)) =
228 SOME ("Trying to interrupt " ^ das_wort_worker ^ " since " ^
229 seconds (Time.- (now, death_time)) ^ ":\n" ^ implode_desc desc)
233 case map_filter running_info active of
234 [] => ["No " ^ das_wort_worker ^ "s running."]
235 | ss => "Running " ^ das_wort_worker ^ "s " :: ss
237 case map_filter canceling_info canceling of
239 | ss => "Interrupting " ^ das_wort_worker ^ "s " :: ss
240 in Output.urgent_message (space_implode "\n\n" (running @ interrupting)) end
242 fun thread_messages tool das_wort_worker opt_limit =
244 val limit = the_default message_display_limit opt_limit;
245 val tool_store = Synchronized.value global_state
246 |> #store |> filter (curry (op =) tool o fst)
248 "Recent " ^ das_wort_worker ^ " messages" ^
249 (if length tool_store <= limit then ":"
250 else " (" ^ string_of_int limit ^ " displayed):");
251 val ss = tool_store |> chop limit |> #1 |> map (implode_desc o snd)
252 in List.app Output.urgent_message (header :: maps break_into_chunks ss) end