src/HOL/Tools/ATP/async_manager.ML
author blanchet
Wed, 28 Jul 2010 19:01:34 +0200
changeset 38046 6659c15e7421
parent 37960 src/HOL/Tools/ATP_Manager/async_manager.ML@e557d511c791
child 38047 9033c03cc214
permissions -rw-r--r--
rename directory
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
     1
(*  Title:      HOL/Tools/ATP_Manager/async_manager.ML
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
37584
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
    11
  val launch :
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
    12
    string -> bool -> Time.time -> Time.time -> string -> (unit -> string)
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
    13
    -> unit
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    14
  val kill_threads : string -> string -> unit
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    15
  val running_threads : string -> string -> unit
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    16
  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
    17
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
    18
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
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
    20
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
    21
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
(** 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
    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
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
    25
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
    26
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
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
(** 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
    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
(* 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
    31
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
    32
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
    33
(
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
    34
  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
    35
  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
    36
);
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
    37
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
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
    39
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
    40
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
    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
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
(* 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
    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
type state =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    46
  {manager: Thread.thread option,
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    47
   timeout_heap: Thread_Heap.T,
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    48
   active: (Thread.thread * (string * Time.time * Time.time * string)) list,
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    49
   canceling: (Thread.thread * (string * Time.time * string)) list,
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    50
   messages: (string * string) list,
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    51
   store: (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
    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
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
    54
  {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
    55
   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
    56
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
    57
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
    58
  (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
    59
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
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
(* 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
    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 unregister verbose message 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
    64
  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
    65
  (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
    66
    (case lookup_thread active thread of
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    67
      SOME (tool, birth_time, _, 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
    68
        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
    69
          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
    70
          val now = Time.now ()
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    71
          val canceling' = (thread, (tool, now, desc)) :: 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
    72
          val message' =
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
    73
            desc ^ "\n" ^ message ^
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
            (if verbose 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
    75
               "Total time: " ^ Int.toString (Time.toMilliseconds
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
                                          (Time.- (now, birth_time))) ^ " ms.\n"
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
    77
             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
    78
               "")
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    79
          val messages' = (tool, message') :: messages;
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
    80
          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
    81
            (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
    82
             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
    83
        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
    84
    | 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
    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
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
(* 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
    88
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
val min_wait_time = Time.fromMilliseconds 300;
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
val max_wait_time = Time.fromSeconds 10;
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
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
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
    93
  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
    94
    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
    95
      | 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
    96
        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
    97
          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
    98
        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
    99
          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
   100
  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
   101
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
(* 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
   103
   whereby "pr" in "proof" is not highlighted. *)
37960
e557d511c791 fix polymorphic "val"
blanchet
parents: 37585
diff changeset
   104
fun break_into_chunks xs =
e557d511c791 fix polymorphic "val"
blanchet
parents: 37585
diff changeset
   105
  maps (space_explode "\000" o replace_all "\n\n" "\000" o snd) 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
   106
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   107
fun 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
   108
  case 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
   109
         (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
   110
             (messages, make_state manager timeout_heap active 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
   111
                                   store)) 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
   112
    [] => ()
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   113
  | msgs as (tool, _) :: _ =>
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   114
    let val ss = break_into_chunks msgs in
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   115
      List.app priority (tool ^ ": " ^ hd ss :: tl ss)
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   116
    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
   117
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   118
fun check_thread_manager verbose = 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
   119
  (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
   120
    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
   121
    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
   122
      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
   123
        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
   124
          (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
   125
            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
   126
          | 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
   127
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
        (*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
   129
        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
   130
          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
   131
            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
   132
          in
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   133
            if null timeout_threads andalso null canceling then
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   134
              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
   135
            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
   136
              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
   137
                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
   138
                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
   139
                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
   140
              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
   141
          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
   142
      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
   143
        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
   144
          (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
   145
            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
   146
            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
   147
            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
   148
        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
   149
          (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
   150
            |> these
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
            |> List.app (unregister verbose "Timed out.\n");
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   152
            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
   153
            (*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
   154
            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
   155
      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
   156
    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
   157
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
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
(* 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
   160
37584
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   161
fun register tool verbose 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
   162
 (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
   163
    (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
   164
      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
   165
        val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   166
        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
   167
        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
   168
      in state' end);
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   169
  check_thread_manager verbose);
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
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
37584
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   172
fun launch tool verbose birth_time death_time desc f =
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   173
  (Toplevel.thread true
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   174
       (fn () =>
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   175
           let
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   176
             val self = Thread.self ()
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   177
             val _ = register tool verbose birth_time death_time desc self
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   178
             val message = f ()
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   179
           in unregister verbose message self end);
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   180
   ())
2e289dc13615 factor out thread creation
blanchet
parents: 37583
diff changeset
   181
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
   182
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
   183
(** 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
   184
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
   185
(* 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
   186
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   187
fun kill_threads tool workers = 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
   188
  (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
   189
    let
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   190
      val killing =
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   191
        map_filter (fn (th, (tool', _, _, desc)) =>
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   192
                       if tool' = tool then SOME (th, (tool', Time.now (), desc))
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   193
                       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
   194
      val state' = make_state manager timeout_heap [] (killing @ canceling) messages store;
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   195
      val _ = if null killing then () else priority ("Killed active " ^ workers ^ ".")
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
   196
    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
   197
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
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
   199
(* 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
   200
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   201
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
   202
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   203
fun running_threads tool workers =
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
   204
  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
   205
    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
   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
    val now = Time.now ();
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   208
    fun running_info (_, (tool', birth_time, death_time, desc)) =
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   209
      if tool' = tool then
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   210
        SOME ("Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   211
              seconds (Time.- (death_time, now)) ^ " to live:\n" ^ desc)
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   212
      else
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   213
        NONE
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   214
    fun canceling_info (_, (tool', death_time, desc)) =
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   215
      if tool' = tool then
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   216
        SOME ("Trying to interrupt thread since " ^
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   217
              seconds (Time.- (now, death_time)) ^ ":\n" ^ desc)
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   218
      else
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   219
        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
   220
    val running =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   221
      case map_filter running_info active of
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   222
        [] => ["No " ^ workers ^ " running."]
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   223
      | ss => "Running " ^ workers ^ ":" :: 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
   224
    val interrupting =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   225
      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
   226
        [] => []
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   227
      | ss => "Trying to interrupt the following " ^ workers ^ ":" :: 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
   228
  in priority (space_implode "\n\n" (running @ interrupting)) 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
   229
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   230
fun thread_messages tool 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
   231
  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
   232
    val limit = the_default message_display_limit opt_limit;
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   233
    val tool_store = Synchronized.value global_state
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   234
                     |> #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
   235
    val header =
37585
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   236
      "Recent " ^ worker ^ " messages" ^
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   237
        (if length tool_store <= limit then ":"
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   238
         else " (" ^ string_of_int limit ^ " displayed):");
c2ed8112ce57 multiplexing
blanchet
parents: 37584
diff changeset
   239
  in List.app priority (header :: break_into_chunks (#1 (chop limit tool_store))) 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
   240
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
end;