author | wenzelm |
Fri, 09 Aug 2013 11:18:36 +0200 | |
changeset 52931 | ac6648c0c0fb |
parent 52787 | c143ad7811fc |
child 53192 | 04df1d236e1c |
permissions | -rw-r--r-- |
52605 | 1 |
(* Title: Pure/PIDE/execution.ML |
52596 | 2 |
Author: Makarius |
3 |
||
52606 | 4 |
Global management of execution. Unique running execution serves as |
52608 | 5 |
barrier for further exploration of exec fragments. |
52596 | 6 |
*) |
7 |
||
52605 | 8 |
signature EXECUTION = |
52596 | 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 | 11 |
val start: unit -> Document_ID.execution |
12 |
val discontinue: unit -> unit |
|
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 | 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 | 18 |
end; |
19 |
||
52605 | 20 |
structure Execution: EXECUTION = |
52596 | 21 |
struct |
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 | 30 |
|
52606 | 31 |
(* unique running execution *) |
52604 | 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 | 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 | 37 |
val execution_id = Document_ID.make (); |
38 |
val _ = Synchronized.change state (apfst (K execution_id)); |
|
39 |
in execution_id end; |
|
52604 | 40 |
|
52606 | 41 |
fun discontinue () = |
42 |
Synchronized.change state (apfst (K Document_ID.none)); |
|
43 |
||
44 |
fun is_running execution_id = execution_id = fst (Synchronized.value state); |
|
52604 | 45 |
|
46 |
||
47 |
(* registered execs *) |
|
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 | 52 |
fun running execution_id exec_id = |
52604 | 53 |
Synchronized.guarded_access state |
52606 | 54 |
(fn (execution_id', execs) => |
52604 | 55 |
let |
52606 | 56 |
val continue = execution_id = execution_id'; |
52604 | 57 |
val execs' = |
52606 | 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 | 60 |
handle Inttab.DUP dup => error ("Duplicate execution " ^ Document_ID.print dup) |
61 |
else execs; |
|
52606 | 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 | 69 |
|
52596 | 70 |
end; |
71 |