src/Pure/System/session.ML
author wenzelm
Fri, 07 Dec 2012 23:14:39 +0100
changeset 51445 702278df3b57
parent 51136 97d2b77313a0
child 51722 5b2bf7611662
permissions -rw-r--r--
make double-sure that the future scheduler is properly shutdown, otherwise its threads are made persistent and will deadlock with the fresh instance after reloading the image (NB: Present.finish involves another Par_List.map over document_variants and thus might fork again);
wenzelm@30174
     1
(*  Title:      Pure/System/session.ML
wenzelm@6346
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6346
     3
wenzelm@6346
     4
Session management -- maintain state of logic images.
wenzelm@6346
     5
*)
wenzelm@6346
     6
wenzelm@6346
     7
signature SESSION =
wenzelm@6346
     8
sig
wenzelm@25840
     9
  val id: unit -> string list
wenzelm@11509
    10
  val name: unit -> string
wenzelm@6346
    11
  val welcome: unit -> string
wenzelm@49472
    12
  val finish: unit -> unit
wenzelm@49820
    13
  val init: bool -> bool -> bool -> string -> string -> bool -> string -> (string * string) list ->
wenzelm@51136
    14
    string -> string -> bool * string -> string -> bool -> unit
wenzelm@49472
    15
  val with_timing: string -> bool -> ('a -> 'b) -> 'a -> 'b
wenzelm@49460
    16
  val use_dir: string -> string -> bool -> string list -> bool -> bool -> string ->
wenzelm@31690
    17
    string -> bool -> string list -> string -> string -> bool * string ->
wenzelm@42577
    18
    string -> int -> bool -> bool -> int -> int -> int -> int -> unit
wenzelm@6346
    19
end;
wenzelm@6346
    20
wenzelm@6346
    21
structure Session: SESSION =
wenzelm@6346
    22
struct
wenzelm@6346
    23
wenzelm@6346
    24
(* session state *)
wenzelm@6346
    25
wenzelm@32738
    26
val session = Unsynchronized.ref ([Context.PureN]: string list);
wenzelm@32738
    27
val session_finished = Unsynchronized.ref false;
wenzelm@49557
    28
wenzelm@49557
    29
fun id () = ! session;
wenzelm@49557
    30
fun name () = "Isabelle/" ^ List.last (! session);
wenzelm@9414
    31
wenzelm@9414
    32
wenzelm@9414
    33
(* access path *)
wenzelm@6346
    34
wenzelm@49557
    35
val session_path = Unsynchronized.ref ([]: string list);
wenzelm@49557
    36
val remote_path = Unsynchronized.ref (NONE: Url.T option);
wenzelm@49557
    37
wenzelm@6346
    38
fun path () = ! session_path;
wenzelm@6346
    39
wenzelm@30174
    40
wenzelm@30174
    41
(* welcome *)
wenzelm@30174
    42
wenzelm@26109
    43
fun welcome () =
wenzelm@26134
    44
  if Distribution.is_official then
wenzelm@26134
    45
    "Welcome to " ^ name () ^ " (" ^ Distribution.version ^ ")"
wenzelm@50005
    46
  else "Unofficial version of " ^ name () ^ " (" ^ Distribution.version ^ ")";
wenzelm@6346
    47
wenzelm@6346
    48
wenzelm@9414
    49
(* add_path *)
wenzelm@9414
    50
wenzelm@9414
    51
fun add_path reset s =
wenzelm@9414
    52
  let val sess = ! session @ [s] in
wenzelm@18964
    53
    (case duplicates (op =) sess of
wenzelm@9414
    54
      [] => (session := sess; session_path := ((if reset then [] else ! session_path) @ [s]))
wenzelm@49557
    55
    | dups => error ("Duplicate session identifiers " ^ commas_quote dups))
wenzelm@9414
    56
  end;
wenzelm@9414
    57
wenzelm@9414
    58
wenzelm@49472
    59
(* init_name *)
wenzelm@6346
    60
wenzelm@49472
    61
fun init_name reset parent name =
wenzelm@20664
    62
  if not (member (op =) (! session) parent) orelse not (! session_finished) then
wenzelm@6346
    63
    error ("Unfinished parent session " ^ quote parent ^ " for " ^ quote name)
wenzelm@6346
    64
  else (add_path reset name; session_finished := false);
wenzelm@6346
    65
wenzelm@6346
    66
wenzelm@6346
    67
(* finish *)
wenzelm@6346
    68
wenzelm@50946
    69
fun finish_futures () =
wenzelm@50946
    70
  (case map_filter Task_Queue.group_status (Goal.reset_futures ()) of
wenzelm@50946
    71
    [] => ()
wenzelm@50946
    72
  | exns => raise Par_Exn.make exns);
wenzelm@50946
    73
wenzelm@6346
    74
fun finish () =
wenzelm@50926
    75
 (Future.shutdown ();
wenzelm@50946
    76
  finish_futures ();
wenzelm@50926
    77
  Thy_Info.finish ();
wenzelm@50926
    78
  Present.finish ();
wenzelm@50926
    79
  Keyword.status ();
wenzelm@50926
    80
  Outer_Syntax.check_syntax ();
wenzelm@50926
    81
  Options.reset_default ();
wenzelm@51445
    82
  Future.shutdown ();
wenzelm@50926
    83
  session_finished := true);
wenzelm@6346
    84
wenzelm@6346
    85
wenzelm@6346
    86
(* use_dir *)
wenzelm@6346
    87
wenzelm@49472
    88
fun with_timing _ false f x = f x
wenzelm@49472
    89
  | with_timing item true f x =
wenzelm@49472
    90
      let
wenzelm@49472
    91
        val start = Timing.start ();
wenzelm@49472
    92
        val y = f x;
wenzelm@49472
    93
        val timing = Timing.result start;
wenzelm@49472
    94
        val factor = Time.toReal (#cpu timing) / Time.toReal (#elapsed timing)
wenzelm@49472
    95
          |> Real.fmt (StringCvt.FIX (SOME 2));
wenzelm@49472
    96
        val _ =
wenzelm@49472
    97
          Output.physical_stderr ("Timing " ^ item ^ " (" ^
wenzelm@49472
    98
            string_of_int (Multithreading.max_threads_value ()) ^ " threads, " ^
wenzelm@49472
    99
            Timing.message timing ^ ", factor " ^ factor ^ ")\n");
wenzelm@49472
   100
      in y end;
wenzelm@49472
   101
wenzelm@49531
   102
fun get_rpath rpath =
wenzelm@49531
   103
  (if rpath = "" then () else
wenzelm@49531
   104
     if is_some (! remote_path) then
wenzelm@49531
   105
       error "Path for remote theory browsing information may only be set once"
wenzelm@49531
   106
     else
wenzelm@49531
   107
       remote_path := SOME (Url.explode rpath);
wenzelm@49531
   108
   (! remote_path, rpath <> ""));
wenzelm@49531
   109
wenzelm@49820
   110
fun init build reset info info_path doc doc_graph doc_output doc_variants
wenzelm@49820
   111
    parent name doc_dump rpath verbose =
wenzelm@49472
   112
 (init_name reset parent name;
wenzelm@49820
   113
  Present.init build info info_path (if doc = "false" then "" else doc) doc_graph doc_output
wenzelm@49820
   114
    doc_variants (path ()) name doc_dump (get_rpath rpath) verbose
wenzelm@49482
   115
    (map Thy_Info.get_theory (Thy_Info.get_names ())));
wenzelm@49472
   116
wenzelm@49531
   117
local
wenzelm@49531
   118
wenzelm@49819
   119
fun read_variants strs =
wenzelm@49819
   120
  rev (distinct (eq_fst (op =)) (rev (("document", "") :: map Present.read_variant strs)))
wenzelm@49819
   121
  |> filter_out (fn (_, s) => s = "-");
wenzelm@49819
   122
wenzelm@49531
   123
in
wenzelm@49531
   124
wenzelm@49460
   125
fun use_dir item root build modes reset info info_path doc doc_graph doc_variants parent
wenzelm@51136
   126
    name doc_dump rpath level timing verbose max_threads trace_threads
wenzelm@42577
   127
    parallel_proofs parallel_proofs_threshold =
wenzelm@23972
   128
  ((fn () =>
wenzelm@50926
   129
    let
wenzelm@50926
   130
      val _ =
wenzelm@50926
   131
        Output.physical_stderr
wenzelm@50926
   132
          "### Legacy feature: old \"isabelle usedir\" -- use \"isabelle build\" instead!\n";
wenzelm@50926
   133
      val _ =
wenzelm@50926
   134
        init build reset info info_path doc doc_graph "" (read_variants doc_variants) parent name
wenzelm@51136
   135
          doc_dump rpath verbose;
wenzelm@50926
   136
      val res1 = (use |> with_timing item timing |> Exn.capture) root;
wenzelm@50926
   137
      val res2 = Exn.capture finish ();
wenzelm@50926
   138
    in ignore (Par_Exn.release_all [res1, res2]) end)
wenzelm@39862
   139
    |> Unsynchronized.setmp Proofterm.proofs level
wenzelm@39862
   140
    |> Unsynchronized.setmp print_mode (modes @ print_mode_value ())
wenzelm@39862
   141
    |> Unsynchronized.setmp Goal.parallel_proofs parallel_proofs
wenzelm@42577
   142
    |> Unsynchronized.setmp Goal.parallel_proofs_threshold parallel_proofs_threshold
wenzelm@39862
   143
    |> Unsynchronized.setmp Multithreading.trace trace_threads
wenzelm@39862
   144
    |> Unsynchronized.setmp Multithreading.max_threads
wenzelm@23979
   145
      (if Multithreading.available then max_threads
wenzelm@23979
   146
       else (if max_threads = 1 then () else warning "Multithreading support unavailable"; 1))) ()
wenzelm@31478
   147
  handle exn => (Output.error_msg (ML_Compiler.exn_message exn); exit 1);
wenzelm@6346
   148
wenzelm@6346
   149
end;
wenzelm@49531
   150
wenzelm@49531
   151
end;