src/Pure/PIDE/execution.ML
author wenzelm
Fri, 09 Aug 2013 11:18:36 +0200
changeset 52931 ac6648c0c0fb
parent 52787 c143ad7811fc
child 53192 04df1d236e1c
permissions -rw-r--r--
cancel_query via direct access to the exec_id of the running query process;
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
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
     4
Global management of execution.  Unique running execution serves as
52608
wenzelm
parents: 52607
diff changeset
     5
barrier for further exploration of exec fragments.
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
     6
*)
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
     7
52605
a2a805549c74 clarified module name;
wenzelm
parents: 52604
diff changeset
     8
signature EXECUTION =
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
     9
sig
52787
c143ad7811fc pro-forma Execution.reset, despite lack of final join/commit;
wenzelm
parents: 52784
diff changeset
    10
  val reset: unit -> unit
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    11
  val start: unit -> Document_ID.execution
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    12
  val discontinue: unit -> unit
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    13
  val is_running: Document_ID.execution -> bool
52784
4ba2e8b9972f de-assign execs that were not registered as running yet -- observe change of perspective more thoroughly;
wenzelm
parents: 52775
diff changeset
    14
  val is_running_exec: Document_ID.exec -> bool
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    15
  val running: Document_ID.execution -> Document_ID.exec -> bool
52655
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52608
diff changeset
    16
  val cancel: Document_ID.exec -> unit
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52608
diff changeset
    17
  val terminate: Document_ID.exec -> unit
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    18
end;
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    19
52605
a2a805549c74 clarified module name;
wenzelm
parents: 52604
diff changeset
    20
structure Execution: EXECUTION =
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    21
struct
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    22
52787
c143ad7811fc pro-forma Execution.reset, despite lack of final join/commit;
wenzelm
parents: 52784
diff changeset
    23
(* global state *)
c143ad7811fc pro-forma Execution.reset, despite lack of final join/commit;
wenzelm
parents: 52784
diff changeset
    24
c143ad7811fc pro-forma Execution.reset, despite lack of final join/commit;
wenzelm
parents: 52784
diff changeset
    25
type state = Document_ID.execution * Future.group Inttab.table;
c143ad7811fc pro-forma Execution.reset, despite lack of final join/commit;
wenzelm
parents: 52784
diff changeset
    26
c143ad7811fc pro-forma Execution.reset, despite lack of final join/commit;
wenzelm
parents: 52784
diff changeset
    27
val init_state: state = (Document_ID.none, Inttab.empty);
c143ad7811fc pro-forma Execution.reset, despite lack of final join/commit;
wenzelm
parents: 52784
diff changeset
    28
val state = Synchronized.var "Execution.state" init_state;
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    29
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    30
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    31
(* unique running execution *)
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    32
52787
c143ad7811fc pro-forma Execution.reset, despite lack of final join/commit;
wenzelm
parents: 52784
diff changeset
    33
fun reset () = Synchronized.change state (K init_state);
c143ad7811fc pro-forma Execution.reset, despite lack of final join/commit;
wenzelm
parents: 52784
diff changeset
    34
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    35
fun start () =
52602
00170ef1dc39 strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
wenzelm
parents: 52596
diff changeset
    36
  let
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    37
    val execution_id = Document_ID.make ();
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    38
    val _ = Synchronized.change state (apfst (K execution_id));
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    39
  in execution_id end;
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    40
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    41
fun discontinue () =
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    42
  Synchronized.change state (apfst (K Document_ID.none));
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    43
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    44
fun is_running execution_id = execution_id = fst (Synchronized.value state);
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    45
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    46
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    47
(* registered execs *)
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    48
52784
4ba2e8b9972f de-assign execs that were not registered as running yet -- observe change of perspective more thoroughly;
wenzelm
parents: 52775
diff changeset
    49
fun is_running_exec exec_id =
4ba2e8b9972f de-assign execs that were not registered as running yet -- observe change of perspective more thoroughly;
wenzelm
parents: 52775
diff changeset
    50
  Inttab.defined (snd (Synchronized.value state)) exec_id;
4ba2e8b9972f de-assign execs that were not registered as running yet -- observe change of perspective more thoroughly;
wenzelm
parents: 52775
diff changeset
    51
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    52
fun running execution_id exec_id =
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    53
  Synchronized.guarded_access state
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    54
    (fn (execution_id', execs) =>
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    55
      let
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    56
        val continue = execution_id = execution_id';
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    57
        val execs' =
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    58
          if continue then
52607
33a133d50616 clarified execution: maintain running execs only, check "stable" separately via memo (again);
wenzelm
parents: 52606
diff changeset
    59
            Inttab.update_new (exec_id, Future.the_worker_group ()) execs
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    60
              handle Inttab.DUP dup => error ("Duplicate execution " ^ Document_ID.print dup)
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    61
          else execs;
52606
0d68d108d7e0 tuned signature;
wenzelm
parents: 52605
diff changeset
    62
      in SOME (continue, (execution_id', 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
    63
52655
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52608
diff changeset
    64
fun peek_list exec_id =
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52608
diff changeset
    65
  the_list (Inttab.lookup (snd (Synchronized.value state)) exec_id);
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52608
diff changeset
    66
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52608
diff changeset
    67
fun cancel exec_id = List.app Future.cancel_group (peek_list exec_id);
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52608
diff changeset
    68
fun terminate exec_id = List.app Future.terminate (peek_list exec_id);
52604
ff2f0818aebc more explicit type Exec.context;
wenzelm
parents: 52602
diff changeset
    69
52596
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    70
end;
40298d383463 global management of command execution fragments;
wenzelm
parents:
diff changeset
    71