7 *) |
7 *) |
8 |
8 |
9 signature ASYNC_MANAGER = |
9 signature ASYNC_MANAGER = |
10 sig |
10 sig |
11 val launch : |
11 val launch : |
12 string -> bool -> Time.time -> Time.time -> string -> (unit -> string) |
12 string -> Time.time -> Time.time -> string -> (unit -> string) -> unit |
13 -> unit |
|
14 val kill_threads : string -> string -> unit |
13 val kill_threads : string -> string -> unit |
15 val running_threads : string -> string -> unit |
14 val running_threads : string -> string -> unit |
16 val thread_messages : string -> string -> int option -> unit |
15 val thread_messages : string -> string -> int option -> unit |
17 end; |
16 end; |
18 |
17 |
58 (make_state NONE Thread_Heap.empty [] [] [] []); |
57 (make_state NONE Thread_Heap.empty [] [] [] []); |
59 |
58 |
60 |
59 |
61 (* unregister thread *) |
60 (* unregister thread *) |
62 |
61 |
63 fun unregister verbose message thread = |
62 fun unregister message thread = |
64 Synchronized.change global_state |
63 Synchronized.change global_state |
65 (fn state as {manager, timeout_heap, active, canceling, messages, store} => |
64 (fn state as {manager, timeout_heap, active, canceling, messages, store} => |
66 (case lookup_thread active thread of |
65 (case lookup_thread active thread of |
67 SOME (tool, birth_time, _, desc) => |
66 SOME (tool, birth_time, _, desc) => |
68 let |
67 let |
69 val active' = delete_thread thread active; |
68 val active' = delete_thread thread active; |
70 val now = Time.now () |
69 val now = Time.now () |
71 val canceling' = (thread, (tool, now, desc)) :: canceling |
70 val canceling' = (thread, (tool, now, desc)) :: canceling |
72 val message' = |
71 val message' = desc ^ "\n" ^ message |
73 desc ^ "\n" ^ message ^ |
|
74 (if verbose then |
|
75 "\nTotal time: " ^ Int.toString (Time.toMilliseconds |
|
76 (Time.- (now, birth_time))) ^ " ms." |
|
77 else |
|
78 "") |
|
79 val messages' = (tool, message') :: messages; |
72 val messages' = (tool, message') :: messages; |
80 val store' = (tool, message') :: |
73 val store' = (tool, message') :: |
81 (if length store <= message_store_limit then store |
74 (if length store <= message_store_limit then store |
82 else #1 (chop message_store_limit store)); |
75 else #1 (chop message_store_limit store)); |
83 in make_state manager timeout_heap active' canceling' messages' store' end |
76 in make_state manager timeout_heap active' canceling' messages' store' end |
113 | msgs as (tool, _) :: _ => |
106 | msgs as (tool, _) :: _ => |
114 let val ss = break_into_chunks msgs in |
107 let val ss = break_into_chunks msgs in |
115 List.app priority (tool ^ ": " ^ hd ss :: tl ss) |
108 List.app priority (tool ^ ": " ^ hd ss :: tl ss) |
116 end |
109 end |
117 |
110 |
118 fun check_thread_manager verbose = Synchronized.change global_state |
111 fun check_thread_manager () = Synchronized.change global_state |
119 (fn state as {manager, timeout_heap, active, canceling, messages, store} => |
112 (fn state as {manager, timeout_heap, active, canceling, messages, store} => |
120 if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state |
113 if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state |
121 else let val manager = SOME (Toplevel.thread false (fn () => |
114 else let val manager = SOME (Toplevel.thread false (fn () => |
122 let |
115 let |
123 fun time_limit timeout_heap = |
116 fun time_limit timeout_heap = |
146 then (false, make_state NONE timeout_heap active canceling messages store) |
139 then (false, make_state NONE timeout_heap active canceling messages store) |
147 else (true, state)) |
140 else (true, state)) |
148 do |
141 do |
149 (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action |
142 (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action |
150 |> these |
143 |> these |
151 |> List.app (unregister verbose "Timed out.\n"); |
144 |> List.app (unregister "Timed out.\n"); |
152 print_new_messages (); |
145 print_new_messages (); |
153 (*give threads some time to respond to interrupt*) |
146 (*give threads some time to respond to interrupt*) |
154 OS.Process.sleep min_wait_time) |
147 OS.Process.sleep min_wait_time) |
155 end)) |
148 end)) |
156 in make_state manager timeout_heap active canceling messages store end) |
149 in make_state manager timeout_heap active canceling messages store end) |
157 |
150 |
158 |
151 |
159 (* register thread *) |
152 (* register thread *) |
160 |
153 |
161 fun register tool verbose birth_time death_time desc thread = |
154 fun register tool birth_time death_time desc thread = |
162 (Synchronized.change global_state |
155 (Synchronized.change global_state |
163 (fn {manager, timeout_heap, active, canceling, messages, store} => |
156 (fn {manager, timeout_heap, active, canceling, messages, store} => |
164 let |
157 let |
165 val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap; |
158 val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap; |
166 val active' = update_thread (thread, (tool, birth_time, death_time, desc)) active; |
159 val active' = update_thread (thread, (tool, birth_time, death_time, desc)) active; |
167 val state' = make_state manager timeout_heap' active' canceling messages store; |
160 val state' = make_state manager timeout_heap' active' canceling messages store; |
168 in state' end); |
161 in state' end); |
169 check_thread_manager verbose); |
162 check_thread_manager ()) |
170 |
163 |
171 |
164 |
172 fun launch tool verbose birth_time death_time desc f = |
165 fun launch tool birth_time death_time desc f = |
173 (Toplevel.thread true |
166 (Toplevel.thread true |
174 (fn () => |
167 (fn () => |
175 let |
168 let |
176 val self = Thread.self () |
169 val self = Thread.self () |
177 val _ = register tool verbose birth_time death_time desc self |
170 val _ = register tool birth_time death_time desc self |
178 val message = f () |
171 val message = f () |
179 in unregister verbose message self end); |
172 in unregister message self end); |
180 ()) |
173 ()) |
181 |
174 |
182 |
175 |
183 (** user commands **) |
176 (** user commands **) |
184 |
177 |