src/Pure/PIDE/execution.ML
author wenzelm
Fri, 12 Jul 2013 11:07:02 +0200
changeset 52605 a2a805549c74
parent 52604 src/Pure/PIDE/exec.ML@ff2f0818aebc
child 52606 0d68d108d7e0
permissions -rw-r--r--
clarified module name;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52605
a2a805549c74 clarified module name;
wenzelm
parents: 52604
diff changeset
     1
(*  Title:      Pure/PIDE/execution.ML
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
     3
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
     4
Global management of command execution fragments.
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
     5
*)
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
     6
52605
a2a805549c74 clarified module name;
wenzelm
parents: 52604
diff changeset
     7
signature EXECUTION =
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
     8
sig
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
     9
  type context
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    10
  val no_context: context
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    11
  val drop_context: context -> unit
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    12
  val fresh_context: unit -> context
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    13
  val is_running: context -> bool
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    14
  val running: context -> Document_ID.exec -> bool
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    15
  val finished: Document_ID.exec -> bool -> unit
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    16
  val is_stable: Document_ID.exec -> bool
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    17
  val peek_running: Document_ID.exec -> Future.group option
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    18
  val purge_canceled: unit -> unit
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    19
  val terminate_all: unit -> unit
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    20
end;
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    21
52605
a2a805549c74 clarified module name;
wenzelm
parents: 52604
diff changeset
    22
structure Execution: EXECUTION =
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    23
struct
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    24
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    25
(* global state *)
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    26
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    27
datatype context = Context of Document_ID.generic;
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    28
val no_context = Context Document_ID.none;
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    29
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    30
type status = Future.group option;  (*SOME group: running, NONE: canceled*)
52605
a2a805549c74 clarified module name;
wenzelm
parents: 52604
diff changeset
    31
val state = Synchronized.var "Execution.state" (no_context, Inttab.empty: status Inttab.table);
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    32
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    33
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    34
(* unique execution context *)
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    35
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    36
fun drop_context context =
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    37
  Synchronized.change state
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    38
    (apfst (fn context' => if context = context' then no_context else context'));
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    39
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    40
fun fresh_context () =
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    41
  let
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    42
    val context = Context (Document_ID.make ());
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    43
    val _ = Synchronized.change state (apfst (K context));
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    44
  in context end;
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    45
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    46
fun is_running context = context = fst (Synchronized.value state);
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    47
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    48
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    49
(* registered execs *)
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    50
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    51
fun running context exec_id =
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    52
  Synchronized.guarded_access state
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    53
    (fn (current_context, execs) =>
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    54
      let
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    55
        val cont = context = current_context;
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    56
        val execs' =
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    57
          if cont then
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    58
            Inttab.update_new (exec_id, SOME (Future.the_worker_group ())) execs
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    59
              handle Inttab.DUP dup => error ("Duplicate execution " ^ Document_ID.print dup)
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    60
          else execs;
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    61
      in SOME (cont, (current_context, execs')) end);
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    62
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    63
fun finished exec_id stable =
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    64
  Synchronized.change state
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    65
    (apsnd (fn execs =>
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    66
      if not (Inttab.defined execs exec_id) then
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    67
        error ("Attempt to finish unknown execution: " ^ Document_ID.print exec_id)
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    68
      else if stable then Inttab.delete exec_id execs
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    69
      else Inttab.update (exec_id, NONE) execs));
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    70
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    71
fun is_stable exec_id =
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    72
  not (Par_Exn.is_interrupted (Future.join_results (Goal.peek_futures exec_id))) andalso
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    73
  (case Inttab.lookup (snd (Synchronized.value state)) exec_id of
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    74
    NONE => true
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    75
  | SOME status => is_some status);
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    76
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    77
fun peek_running exec_id =
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    78
  (case Inttab.lookup (snd (Synchronized.value state)) exec_id of
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    79
    SOME (SOME group) => SOME group
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    80
  | _ => NONE);
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    81
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    82
fun purge_canceled () =
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    83
  Synchronized.guarded_access state
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    84
    (fn (context, execs) =>
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    85
      let
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    86
        val canceled = Inttab.fold (fn (exec_id, NONE) => cons exec_id | _ => I) execs [];
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    87
        val execs' = fold Inttab.delete canceled execs;
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    88
      in SOME ((), (context, execs')) end);
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    89
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    90
fun terminate_all () =
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    91
  let
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    92
    val groups =
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    93
      Inttab.fold (fn (_, SOME group) => cons group | _ => I)
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    94
        (snd (Synchronized.value state)) [];
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    95
    val _ = List.app Future.cancel_group groups;
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    96
    val _ = List.app Future.terminate groups;
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    97
  in () end;
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    98
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    99
end;
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
   100