src/HOL/Tools/Sledgehammer/async_manager.ML
author blanchet
Fri, 27 May 2011 10:30:07 +0200
changeset 43005 c96f06bffd90
parent 41042 8275f52ac991
child 43029 3e060b1c844b
permissions -rw-r--r--
merge timeout messages from several ATPs into one message to avoid clutter
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41042
8275f52ac991 load "try" after "Metis" and move "Async_Manager" back to Sledgehammer
blanchet
parents: 40301
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/async_manager.ML
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
     2
    Author:     Fabian Immler, TU Muenchen
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
     3
    Author:     Makarius
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
     4
    Author:     Jasmin Blanchette, TU Muenchen
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
     5
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
     6
Central manager for asynchronous diagnosis tool threads.
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
     7
*)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
     8
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
     9
signature ASYNC_MANAGER =
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    10
sig
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    11
  val implode_desc : string * string -> string
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    12
  val break_into_chunks : string -> string list
37584
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
    13
  val launch :
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    14
    string -> Time.time -> Time.time -> string * string
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    15
    -> (unit -> bool * string) -> unit
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    16
  val kill_threads : string -> string -> unit
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    17
  val running_threads : string -> string -> unit
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    18
  val thread_messages : string -> string -> int option -> unit
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    19
end;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    20
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    21
structure Async_Manager : ASYNC_MANAGER =
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    22
struct
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    23
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    24
(** preferences **)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    25
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    26
val message_store_limit = 20;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    27
val message_display_limit = 5;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    28
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    29
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    30
(** thread management **)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    31
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    32
val implode_desc = op ^ o apfst quote
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    33
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    34
fun implode_message (workers, work) =
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    35
  space_implode " " (ATP_Proof.serial_commas "and" (map quote workers)) ^ work
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    36
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    37
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    38
(* data structures over threads *)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    39
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    40
structure Thread_Heap = Heap
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    41
(
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    42
  type elem = Time.time * Thread.thread;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    43
  fun ord ((a, _), (b, _)) = Time.compare (a, b);
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    44
);
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    45
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    46
fun lookup_thread xs = AList.lookup Thread.equal xs;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    47
fun delete_thread xs = AList.delete Thread.equal xs;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    48
fun update_thread xs = AList.update Thread.equal xs;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    49
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    50
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    51
(* state of thread manager *)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    52
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    53
type state =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    54
  {manager: Thread.thread option,
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    55
   timeout_heap: Thread_Heap.T,
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    56
   active:
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    57
     (Thread.thread
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    58
      * (string * Time.time * Time.time * (string * string))) list,
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    59
   canceling:  (Thread.thread * (string * Time.time * (string * string))) list,
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    60
   messages: (bool * (string * (string * string))) list,
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    61
   store: (string * (string * string)) list}
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    62
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    63
fun make_state manager timeout_heap active canceling messages store : state =
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    64
  {manager = manager, timeout_heap = timeout_heap, active = active,
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    65
   canceling = canceling, messages = messages, store = store}
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    66
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    67
val global_state = Synchronized.var "async_manager"
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    68
  (make_state NONE Thread_Heap.empty [] [] [] []);
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    69
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    70
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    71
(* unregister thread *)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    72
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    73
fun unregister (urgent, message) thread =
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    74
  Synchronized.change global_state
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    75
  (fn state as {manager, timeout_heap, active, canceling, messages, store} =>
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    76
    (case lookup_thread active thread of
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    77
      SOME (tool, _, _, desc as (worker, its_desc)) =>
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    78
        let
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    79
          val active' = delete_thread thread active;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    80
          val now = Time.now ()
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    81
          val canceling' = (thread, (tool, now, desc)) :: canceling
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    82
          val message' = (worker, its_desc ^ "\n" ^ message)
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    83
          val messages' = (urgent, (tool, message')) :: messages
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    84
          val store' = (tool, message') ::
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    85
            (if length store <= message_store_limit then store
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    86
             else #1 (chop message_store_limit store));
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    87
        in make_state manager timeout_heap active' canceling' messages' store' end
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    88
    | NONE => state));
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    89
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    90
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    91
(* main manager thread -- only one may exist *)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    92
40301
bf39a257b3d3 simplified some time constants;
wenzelm
parents: 40132
diff changeset
    93
val min_wait_time = seconds 0.3;
bf39a257b3d3 simplified some time constants;
wenzelm
parents: 40132
diff changeset
    94
val max_wait_time = seconds 10.0;
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    95
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    96
fun replace_all bef aft =
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    97
  let
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    98
    fun aux seen "" = String.implode (rev seen)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
    99
      | aux seen s =
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   100
        if String.isPrefix bef s then
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   101
          aux seen "" ^ aft ^ aux [] (unprefix bef s)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   102
        else
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   103
          aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   104
  in aux [] end
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   105
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   106
(* This is a workaround for Proof General's off-by-a-few sendback display bug,
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   107
   whereby "pr" in "proof" is not highlighted. *)
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   108
val break_into_chunks = space_explode "\000" o replace_all "\n\n" "\000"
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   109
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   110
fun print_new_messages () =
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   111
  Synchronized.change_result global_state
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   112
      (fn {manager, timeout_heap, active, canceling, messages, store} =>
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   113
          messages
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   114
          |> List.partition (fn (urgent, _) => null active orelse urgent)
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   115
          ||> (fn postponed_messages =>
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   116
                  make_state manager timeout_heap active canceling
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   117
                                     postponed_messages store))
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   118
  |> map (fn (_, (tool, (worker, work))) => ((tool, work), worker))
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   119
  |> AList.group (op =)
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   120
  |> List.app (fn ((tool, work), workers) =>
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   121
                  tool ^ ": " ^
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   122
                  implode_message (workers |> sort string_ord, work)
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   123
                  |> break_into_chunks
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   124
                  |> List.app Output.urgent_message)
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   125
39006
a02cb5717677 remove time information in output, since it's confusing anyway
blanchet
parents: 38990
diff changeset
   126
fun check_thread_manager () = Synchronized.change global_state
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   127
  (fn state as {manager, timeout_heap, active, canceling, messages, store} =>
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   128
    if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   129
    else let val manager = SOME (Toplevel.thread false (fn () =>
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   130
      let
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   131
        fun time_limit timeout_heap =
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   132
          (case try Thread_Heap.min timeout_heap of
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   133
            NONE => Time.+ (Time.now (), max_wait_time)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   134
          | SOME (time, _) => time);
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   135
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   136
        (*action: find threads whose timeout is reached, and interrupt canceling threads*)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   137
        fun action {manager, timeout_heap, active, canceling, messages, store} =
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   138
          let val (timeout_threads, timeout_heap') =
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   139
            Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   140
          in
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   141
            if null timeout_threads andalso null canceling then
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   142
              NONE
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   143
            else
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   144
              let
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   145
                val _ = List.app (Simple_Thread.interrupt o #1) canceling
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   146
                val canceling' = filter (Thread.isActive o #1) canceling
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   147
                val state' = make_state manager timeout_heap' active canceling' messages store;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   148
              in SOME (map #2 timeout_threads, state') end
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   149
          end;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   150
      in
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   151
        while Synchronized.change_result global_state
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   152
          (fn state as {timeout_heap, active, canceling, messages, store, ...} =>
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   153
            if null active andalso null canceling andalso null messages
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   154
            then (false, make_state NONE timeout_heap active canceling messages store)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   155
            else (true, state))
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   156
        do
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   157
          (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   158
            |> these
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   159
            |> List.app (unregister (false, "Timed out.\n"));
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   160
            print_new_messages ();
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   161
            (*give threads some time to respond to interrupt*)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   162
            OS.Process.sleep min_wait_time)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   163
      end))
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   164
    in make_state manager timeout_heap active canceling messages store end)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   165
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   166
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   167
(* register thread *)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   168
39006
a02cb5717677 remove time information in output, since it's confusing anyway
blanchet
parents: 38990
diff changeset
   169
fun register tool birth_time death_time desc thread =
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   170
 (Synchronized.change global_state
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   171
    (fn {manager, timeout_heap, active, canceling, messages, store} =>
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   172
      let
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   173
        val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   174
        val active' = update_thread (thread, (tool, birth_time, death_time, desc)) active;
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   175
        val state' = make_state manager timeout_heap' active' canceling messages store;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   176
      in state' end);
39006
a02cb5717677 remove time information in output, since it's confusing anyway
blanchet
parents: 38990
diff changeset
   177
  check_thread_manager ())
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   178
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   179
39006
a02cb5717677 remove time information in output, since it's confusing anyway
blanchet
parents: 38990
diff changeset
   180
fun launch tool birth_time death_time desc f =
37584
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   181
  (Toplevel.thread true
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   182
       (fn () =>
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   183
           let
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   184
             val self = Thread.self ()
39006
a02cb5717677 remove time information in output, since it's confusing anyway
blanchet
parents: 38990
diff changeset
   185
             val _ = register tool birth_time death_time desc self
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   186
           in unregister (f ()) self end);
37584
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   187
   ())
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   188
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   189
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   190
(** user commands **)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   191
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   192
(* kill threads *)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   193
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   194
fun kill_threads tool das_wort_worker = Synchronized.change global_state
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   195
  (fn {manager, timeout_heap, active, canceling, messages, store} =>
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   196
    let
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   197
      val killing =
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   198
        map_filter (fn (th, (tool', _, _, desc)) =>
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   199
                       if tool' = tool then SOME (th, (tool', Time.now (), desc))
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   200
                       else NONE) active
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   201
      val state' = make_state manager timeout_heap [] (killing @ canceling) messages store;
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   202
      val _ =
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   203
        if null killing then ()
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   204
        else Output.urgent_message ("Killed active " ^ das_wort_worker ^ "s.")
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   205
    in state' end);
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   206
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   207
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   208
(* running threads *)
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   209
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   210
fun seconds time = string_of_int (Time.toSeconds time) ^ " s"
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   211
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   212
fun running_threads tool das_wort_worker =
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   213
  let
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   214
    val {active, canceling, ...} = Synchronized.value global_state;
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   215
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   216
    val now = Time.now ();
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   217
    fun running_info (_, (tool', birth_time, death_time, desc)) =
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   218
      if tool' = tool then
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   219
        SOME ("Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   220
              seconds (Time.- (death_time, now)) ^ " to live:\n" ^
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   221
              implode_desc desc)
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   222
      else
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   223
        NONE
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   224
    fun canceling_info (_, (tool', death_time, desc)) =
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   225
      if tool' = tool then
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   226
        SOME ("Trying to interrupt " ^ das_wort_worker ^ " since " ^
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   227
              seconds (Time.- (now, death_time)) ^ ":\n" ^ implode_desc desc)
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   228
      else
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   229
        NONE
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   230
    val running =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   231
      case map_filter running_info active of
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   232
        [] => ["No " ^ das_wort_worker ^ "s running."]
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   233
      | ss => "Running " ^ das_wort_worker ^ "s " :: ss
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   234
    val interrupting =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   235
      case map_filter canceling_info canceling of
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   236
        [] => []
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   237
      | ss => "Interrupting " ^ das_wort_worker ^ "s " :: ss
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39376
diff changeset
   238
  in Output.urgent_message (space_implode "\n\n" (running @ interrupting)) end
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   239
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   240
fun thread_messages tool das_wort_worker opt_limit =
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   241
  let
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   242
    val limit = the_default message_display_limit opt_limit;
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   243
    val tool_store = Synchronized.value global_state
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   244
                     |> #store |> filter (curry (op =) tool o fst)
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   245
    val header =
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   246
      "Recent " ^ das_wort_worker ^ " messages" ^
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   247
        (if length tool_store <= limit then ":"
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   248
         else " (" ^ string_of_int limit ^ " displayed):");
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   249
    val ss = tool_store |> chop limit |> #1 |> map (implode_desc o snd)
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   250
  in List.app Output.urgent_message (header :: maps break_into_chunks ss) end
37583
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   251
9ce2451647d5 factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
diff changeset
   252
end;