src/HOL/Tools/Sledgehammer/async_manager_legacy.ML
author wenzelm
Thu, 29 Jan 2015 16:35:29 +0100
changeset 59471 ca459352d8c5
parent 59468 src/HOL/Tools/Sledgehammer/async_manager.ML@fe6651760643
child 60764 b610ba36e02c
permissions -rw-r--r--
more explicit indication of Async_Manager_Legacy as Proof General legacy;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59471
ca459352d8c5 more explicit indication of Async_Manager_Legacy as Proof General legacy;
wenzelm
parents: 59468
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/async_manager_legacy.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.
58894
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
     7
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
     8
Proof General legacy!
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
     9
*)
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
59471
ca459352d8c5 more explicit indication of Async_Manager_Legacy as Proof General legacy;
wenzelm
parents: 59468
diff changeset
    11
signature ASYNC_MANAGER_LEGACY =
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
    12
sig
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    13
  val break_into_chunks : string -> string list
57556
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
    14
  val thread : string -> Time.time -> Time.time -> string * string -> (unit -> bool * string) ->
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
    15
    unit
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    16
  val kill_threads : string -> string -> unit
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    17
  val has_running_threads : string -> bool
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    18
  val running_threads : string -> string -> unit
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    19
  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
    20
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
    21
59471
ca459352d8c5 more explicit indication of Async_Manager_Legacy as Proof General legacy;
wenzelm
parents: 59468
diff changeset
    22
structure Async_Manager_Legacy : ASYNC_MANAGER_LEGACY =
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
    23
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
    24
58894
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
    25
fun make_thread interrupts body =
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
    26
  Thread.fork
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
    27
    (fn () =>
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
    28
      Runtime.debugging NONE body () handle exn =>
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
    29
        if Exn.is_interrupt exn then ()
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
    30
        else writeln ("## INTERNAL ERROR ##\n" ^ Runtime.exn_message exn),
59468
fe6651760643 explicit threads_stack_limit (for recent Poly/ML SVN versions), which leads to soft interrupt instead of exhaustion of virtual memory, which is particularly relevant for the bigger address space of x86_64;
wenzelm
parents: 58894
diff changeset
    31
      Simple_Thread.attributes {stack_limit = NONE, interrupts = interrupts});
58894
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
    32
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    33
val message_store_limit = 20
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    34
val message_display_limit = 10
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
    35
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    36
fun implode_message (workers, work) =
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    37
  space_implode " " (Try.serial_commas "and" workers) ^ work
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    38
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
    39
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
    40
(
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    41
  type elem = Time.time * Thread.thread
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    42
  fun ord ((a, _), (b, _)) = Time.compare (a, b)
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    43
)
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
    44
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    45
fun lookup_thread xs = AList.lookup Thread.equal xs
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    46
fun delete_thread xs = AList.delete Thread.equal xs
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    47
fun update_thread xs = AList.update Thread.equal xs
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
    48
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
type state =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    50
  {manager: Thread.thread option,
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    51
   timeout_heap: Thread_Heap.T,
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    52
   active:
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    53
     (Thread.thread
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    54
      * (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
    55
   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
    56
   messages: (bool * (string * (string * string))) list,
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    57
   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
    58
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
    59
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
    60
  {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
    61
   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
    62
57556
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
    63
val global_state = Synchronized.var "async_manager" (make_state NONE Thread_Heap.empty [] [] [] [])
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
    64
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    65
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
    66
  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
    67
  (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
    68
    (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
    69
      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
    70
        let
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    71
          val active' = delete_thread thread 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
    72
          val now = Time.now ()
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    73
          val canceling' = (thread, (tool, now, desc)) :: canceling
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    74
          val message' =
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    75
            (worker, its_desc ^ (if message = "" then "" else "\n" ^ message))
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    76
          val messages' = (urgent, (tool, message')) :: messages
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    77
          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
    78
            (if length store <= message_store_limit then store
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    79
             else #1 (chop message_store_limit store))
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
    80
        in make_state manager timeout_heap active' canceling' messages' store' end
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    81
    | NONE => 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
    82
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    83
val min_wait_time = seconds 0.3
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
    84
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
    85
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
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
    87
  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
    88
    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
    89
      | 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
    90
        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
    91
          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
    92
        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
    93
          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
    94
  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
    95
57556
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
    96
(* This is a workaround for Proof General's off-by-a-few sendback display bug, whereby "pr" in
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
    97
   "proof" is not highlighted. *)
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
    98
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
    99
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   100
fun print_new_messages () =
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   101
  Synchronized.change_result global_state
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   102
      (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
   103
          messages
43055
6e0cb8044734 better merging of similar outputs
blanchet
parents: 43029
diff changeset
   104
          |> List.partition
6e0cb8044734 better merging of similar outputs
blanchet
parents: 43029
diff changeset
   105
                 (fn (urgent, _) =>
6e0cb8044734 better merging of similar outputs
blanchet
parents: 43029
diff changeset
   106
                     (null active andalso null canceling) orelse urgent)
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   107
          ||> (fn postponed_messages =>
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   108
                  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
   109
                                     postponed_messages store))
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   110
  |> 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
   111
  |> AList.group (op =)
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   112
  |> List.app (fn ((_, ""), _) => ()
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   113
                | ((tool, work), workers) =>
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   114
                  tool ^ ": " ^
47945
blanchet
parents: 47529
diff changeset
   115
                  implode_message (workers |> sort_distinct string_ord, work)
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   116
                  |> break_into_chunks
58843
521cea5fa777 discontinued obsolete Output.urgent_message;
wenzelm
parents: 57556
diff changeset
   117
                  |> List.app writeln)
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
   118
39006
a02cb5717677 remove time information in output, since it's confusing anyway
blanchet
parents: 38990
diff changeset
   119
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
   120
  (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
   121
    if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
58894
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
   122
    else let val manager = SOME (make_thread false (fn () =>
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
   123
      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
   124
        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
   125
          (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
   126
            NONE => Time.+ (Time.now (), max_wait_time)
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   127
          | SOME (time, _) => time)
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
   128
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
        (*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
   130
        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
   131
          let val (timeout_threads, timeout_heap') =
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   132
            Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap
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
   133
          in
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   134
            if null timeout_threads andalso null canceling then
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   135
              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
   136
            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
   137
              let
44112
ef876972fdc1 more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
wenzelm
parents: 43055
diff changeset
   138
                val _ = List.app (Simple_Thread.interrupt_unsynchronized o #1) canceling
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
   139
                val canceling' = filter (Thread.isActive o #1) canceling
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   140
                val state' = make_state manager timeout_heap' active canceling' messages store
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
   141
              in SOME (map #2 timeout_threads, state') end
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   142
          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
   143
      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
   144
        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
   145
          (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
   146
            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
   147
            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
   148
            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
   149
        do
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   150
          (Synchronized.timed_access global_state
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   151
               (SOME o time_limit o #timeout_heap) action
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   152
           |> these
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   153
           |> List.app (unregister (false, "Timed out."));
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   154
           print_new_messages ();
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   155
           (* give threads some time to respond to interrupt *)
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   156
           OS.Process.sleep min_wait_time)
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
   157
      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
   158
    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
   159
39006
a02cb5717677 remove time information in output, since it's confusing anyway
blanchet
parents: 38990
diff changeset
   160
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
   161
 (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
   162
    (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
   163
      let
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   164
        val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   165
        val active' = update_thread (thread, (tool, birth_time, death_time, desc)) active
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   166
        val state' = make_state manager timeout_heap' active' canceling messages store
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
   167
      in state' end);
39006
a02cb5717677 remove time information in output, since it's confusing anyway
blanchet
parents: 38990
diff changeset
   168
  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
   169
52048
9003b293e775 tuned signature -- emphasize thread creation here;
wenzelm
parents: 48319
diff changeset
   170
fun thread tool birth_time death_time desc f =
58894
447972249785 clarified legacy code;
wenzelm
parents: 58843
diff changeset
   171
  (make_thread true
37584
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   172
       (fn () =>
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   173
           let
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   174
             val self = Thread.self ()
39006
a02cb5717677 remove time information in output, since it's confusing anyway
blanchet
parents: 38990
diff changeset
   175
             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
   176
           in unregister (f ()) self end);
37584
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   177
   ())
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   178
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   179
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
   180
  (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
   181
    let
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   182
      val killing =
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   183
        map_filter (fn (th, (tool', _, _, desc)) =>
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   184
                       if tool' = tool then SOME (th, (tool', Time.now (), desc))
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   185
                       else NONE) active
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   186
      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
   187
      val _ =
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   188
        if null killing then ()
58843
521cea5fa777 discontinued obsolete Output.urgent_message;
wenzelm
parents: 57556
diff changeset
   189
        else writeln ("Interrupted active " ^ das_wort_worker ^ "s.")
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   190
    in state' 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
   191
57556
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
   192
fun str_of_time 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
   193
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   194
fun has_running_threads tool =
57556
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
   195
  exists (fn (_, (tool', _, _, _)) => tool' = tool) (#active (Synchronized.value global_state))
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   196
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   197
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
   198
  let
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   199
    val {active, canceling, ...} = Synchronized.value global_state
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   200
    val now = Time.now ()
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   201
    fun running_info (_, (tool', birth_time, death_time, desc)) =
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   202
      if tool' = tool then
57556
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
   203
        SOME ("Running: " ^ str_of_time (Time.- (now, birth_time)) ^ " -- " ^
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
   204
              str_of_time (Time.- (death_time, now)) ^ " to live:\n" ^ op ^ desc)
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   205
      else
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   206
        NONE
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   207
    fun canceling_info (_, (tool', death_time, desc)) =
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   208
      if tool' = tool then
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   209
        SOME ("Trying to interrupt " ^ das_wort_worker ^ " since " ^
57556
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
   210
              str_of_time (Time.- (now, death_time)) ^ ":\n" ^ op ^ desc)
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   211
      else
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   212
        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
   213
    val running =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   214
      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
   215
        [] => ["No " ^ das_wort_worker ^ "s running."]
57556
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
   216
      | 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
   217
    val interrupting =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   218
      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
   219
        [] => []
57556
6180d81be977 tuned whitespace (also in strings)
blanchet
parents: 56303
diff changeset
   220
      | ss => "Interrupting " ^ das_wort_worker ^ "s" :: ss
58843
521cea5fa777 discontinued obsolete Output.urgent_message;
wenzelm
parents: 57556
diff changeset
   221
  in writeln (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
   222
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   223
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
   224
  let
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   225
    val limit = the_default message_display_limit opt_limit
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   226
    val tool_store = Synchronized.value global_state
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   227
                     |> #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
   228
    val header =
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 41042
diff changeset
   229
      "Recent " ^ das_wort_worker ^ " messages" ^
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   230
        (if length tool_store <= limit then ":"
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   231
         else " (" ^ string_of_int limit ^ " displayed):")
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 47945
diff changeset
   232
    val ss = tool_store |> chop limit |> #1 |> map (op ^ o snd)
58843
521cea5fa777 discontinued obsolete Output.urgent_message;
wenzelm
parents: 57556
diff changeset
   233
  in List.app writeln (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
   234
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
   235
end;