author | wenzelm |
Wed, 02 Feb 2011 13:38:09 +0100 | |
changeset 41681 | b5d7b15166bf |
parent 41680 | a4c822915eaa |
child 41682 | 44a2e0db281f |
permissions | -rw-r--r-- |
28165 | 1 |
(* Title: Pure/Concurrent/task_queue.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Ordered queue of grouped tasks. |
|
5 |
*) |
|
6 |
||
7 |
signature TASK_QUEUE = |
|
8 |
sig |
|
29340 | 9 |
type task |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
10 |
val dummy_task: task |
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
11 |
val name_of_task: task -> string |
29121 | 12 |
val pri_of_task: task -> int |
28196 | 13 |
val str_of_task: task -> string |
41680
a4c822915eaa
more informative task timing: some dependency tracking;
wenzelm
parents:
41679
diff
changeset
|
14 |
val timing_of_task: task -> Time.time * Time.time * string list |
29340 | 15 |
type group |
32221 | 16 |
val new_group: group option -> group |
32052 | 17 |
val group_id: group -> int |
29340 | 18 |
val eq_group: group * group -> bool |
32221 | 19 |
val cancel_group: group -> exn -> unit |
20 |
val is_canceled: group -> bool |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
21 |
val group_status: group -> exn list |
28179 | 22 |
val str_of_group: group -> string |
28165 | 23 |
type queue |
24 |
val empty: queue |
|
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
25 |
val all_passive: queue -> bool |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
26 |
val status: queue -> {ready: int, pending: int, running: int, passive: int} |
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
27 |
val cancel: queue -> group -> bool |
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
28 |
val cancel_all: queue -> group list |
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
29 |
val finish: task -> queue -> bool * queue |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
30 |
val enqueue_passive: group -> (unit -> bool) -> queue -> task * queue |
41673 | 31 |
val enqueue: string -> group -> task list -> int -> (bool -> bool) -> |
32 |
queue -> (task * bool) * queue |
|
29365 | 33 |
val extend: task -> (bool -> bool) -> queue -> queue option |
39243
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
34 |
val dequeue_passive: Thread.thread -> task -> queue -> bool * queue |
32249 | 35 |
val dequeue: Thread.thread -> queue -> (task * group * (bool -> bool) list) option * queue |
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
36 |
type deps |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
37 |
val init_deps: task list -> deps |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
38 |
val finished_deps: deps -> bool |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
39 |
val dequeue_deps: Thread.thread -> deps -> queue -> |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
40 |
(((task * group * (bool -> bool) list) option * deps) * queue) |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
41 |
val running: task -> (unit -> 'a) -> 'a |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
42 |
val joining: task -> (unit -> 'a) -> 'a |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
43 |
val waiting: task -> deps -> (unit -> 'a) -> 'a |
28165 | 44 |
end; |
45 |
||
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
46 |
structure Task_Queue: TASK_QUEUE = |
28165 | 47 |
struct |
48 |
||
40450
8eab60e1baeb
private counter, to keep externalized ids a bit smaller;
wenzelm
parents:
39243
diff
changeset
|
49 |
val new_id = Synchronized.counter (); |
8eab60e1baeb
private counter, to keep externalized ids a bit smaller;
wenzelm
parents:
39243
diff
changeset
|
50 |
|
8eab60e1baeb
private counter, to keep externalized ids a bit smaller;
wenzelm
parents:
39243
diff
changeset
|
51 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
52 |
(** grouped tasks **) |
41670 | 53 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
54 |
(* tasks *) |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
55 |
|
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
56 |
type timing = Time.time * Time.time * string list; (*run, wait, wait dependencies*) |
41670 | 57 |
|
58 |
fun new_timing () = |
|
41680
a4c822915eaa
more informative task timing: some dependency tracking;
wenzelm
parents:
41679
diff
changeset
|
59 |
Synchronized.var "timing" ((Time.zeroTime, Time.zeroTime, []): timing); |
41670 | 60 |
|
41673 | 61 |
abstype task = Task of |
62 |
{name: string, |
|
63 |
id: int, |
|
64 |
pri: int option, |
|
65 |
timing: timing Synchronized.var} |
|
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
66 |
with |
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
67 |
|
41673 | 68 |
val dummy_task = Task {name = "", id = ~1, pri = NONE, timing = new_timing ()}; |
69 |
fun new_task name pri = Task {name = name, id = new_id (), pri = pri, timing = new_timing ()}; |
|
41670 | 70 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
71 |
fun name_of_task (Task {name, ...}) = name; |
41673 | 72 |
fun pri_of_task (Task {pri, ...}) = the_default 0 pri; |
73 |
fun str_of_task (Task {name, id, ...}) = |
|
74 |
if name = "" then string_of_int id else string_of_int id ^ " (" ^ name ^ ")"; |
|
28165 | 75 |
|
41673 | 76 |
fun timing_of_task (Task {timing, ...}) = Synchronized.value timing; |
41679 | 77 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
78 |
fun update_timing update (Task {timing, ...}) e = |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
79 |
let |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
80 |
val start = Time.now (); |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
81 |
val result = Exn.capture e (); |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
82 |
val t = Time.- (Time.now (), start); |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
83 |
val _ = Synchronized.change timing (update t); |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
84 |
in Exn.release result end; |
28998 | 85 |
|
41673 | 86 |
fun task_ord (Task {id = id1, pri = pri1, ...}, Task {id = id2, pri = pri2, ...}) = |
87 |
prod_ord (rev_order o option_ord int_ord) int_ord ((pri1, id1), (pri2, id2)); |
|
41670 | 88 |
|
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
89 |
val eq_task = is_equal o task_ord; |
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
90 |
|
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
91 |
end; |
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
92 |
|
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31632
diff
changeset
|
93 |
structure Task_Graph = Graph(type key = task val ord = task_ord); |
28165 | 94 |
|
28998 | 95 |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
96 |
(* nested groups *) |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
97 |
|
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
98 |
abstype group = Group of |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
99 |
{parent: group option, |
40450
8eab60e1baeb
private counter, to keep externalized ids a bit smaller;
wenzelm
parents:
39243
diff
changeset
|
100 |
id: int, |
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
101 |
status: exn list Synchronized.var} |
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
102 |
with |
29121 | 103 |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
104 |
fun make_group (parent, id, status) = Group {parent = parent, id = id, status = status}; |
32052 | 105 |
|
40450
8eab60e1baeb
private counter, to keep externalized ids a bit smaller;
wenzelm
parents:
39243
diff
changeset
|
106 |
fun new_group parent = make_group (parent, new_id (), Synchronized.var "group" []); |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
107 |
|
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
108 |
fun group_id (Group {id, ...}) = id; |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
109 |
fun eq_group (group1, group2) = group_id group1 = group_id group2; |
28551 | 110 |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
111 |
fun group_ancestry (Group {parent, id, ...}) = |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
112 |
id :: (case parent of NONE => [] | SOME group => group_ancestry group); |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
113 |
|
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
114 |
|
32221 | 115 |
(* group status *) |
116 |
||
32251 | 117 |
fun cancel_group (Group {status, ...}) exn = |
118 |
Synchronized.change status |
|
119 |
(fn exns => |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
37854
diff
changeset
|
120 |
if not (Exn.is_interrupt exn) orelse null exns then exn :: exns |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
37854
diff
changeset
|
121 |
else exns); |
29121 | 122 |
|
32251 | 123 |
fun is_canceled (Group {parent, status, ...}) = |
124 |
not (null (Synchronized.value status)) orelse |
|
125 |
(case parent of NONE => false | SOME group => is_canceled group); |
|
32221 | 126 |
|
32251 | 127 |
fun group_status (Group {parent, status, ...}) = |
128 |
Synchronized.value status @ |
|
129 |
(case parent of NONE => [] | SOME group => group_status group); |
|
28165 | 130 |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
131 |
fun str_of_group group = |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
132 |
(is_canceled group ? enclose "(" ")") (string_of_int (group_id group)); |
28179 | 133 |
|
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
134 |
end; |
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
135 |
|
28165 | 136 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
137 |
(** queue of jobs and groups **) |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
138 |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
139 |
(* jobs *) |
28165 | 140 |
|
141 |
datatype job = |
|
29365 | 142 |
Job of (bool -> bool) list | |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
143 |
Running of Thread.thread | |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
144 |
Passive of unit -> bool; |
28165 | 145 |
|
29121 | 146 |
type jobs = (group * job) Task_Graph.T; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
147 |
|
29121 | 148 |
fun get_group (jobs: jobs) task = #1 (Task_Graph.get_node jobs task); |
149 |
fun get_job (jobs: jobs) task = #2 (Task_Graph.get_node jobs task); |
|
29365 | 150 |
fun set_job task job (jobs: jobs) = Task_Graph.map_node task (fn (group, _) => (group, job)) jobs; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
151 |
|
32250 | 152 |
fun add_job task dep (jobs: jobs) = |
153 |
Task_Graph.add_edge (dep, task) jobs handle Task_Graph.UNDEF _ => jobs; |
|
154 |
||
155 |
fun get_deps (jobs: jobs) task = |
|
156 |
Task_Graph.imm_preds jobs task handle Task_Graph.UNDEF _ => []; |
|
157 |
||
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
158 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
159 |
(* queue *) |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
160 |
|
28165 | 161 |
datatype queue = Queue of |
28184
5ed5cb73a2e9
eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents:
28179
diff
changeset
|
162 |
{groups: task list Inttab.table, (*groups with presently active members*) |
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
163 |
jobs: jobs}; (*job dependency graph*) |
28165 | 164 |
|
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
165 |
fun make_queue groups jobs = Queue {groups = groups, jobs = jobs}; |
28204 | 166 |
|
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
167 |
val empty = make_queue Inttab.empty Task_Graph.empty; |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
168 |
|
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
169 |
|
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
170 |
(* job status *) |
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
171 |
|
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
172 |
fun ready_job task ((group, Job list), ([], _)) = SOME (task, group, rev list) |
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
173 |
| ready_job task ((group, Passive abort), ([], _)) = |
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
174 |
if is_canceled group then SOME (task, group, [fn _ => abort ()]) |
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
175 |
else NONE |
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
176 |
| ready_job _ _ = NONE; |
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
177 |
|
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
178 |
fun active_job (_, Job _) = SOME () |
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
179 |
| active_job (_, Running _) = SOME () |
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
180 |
| active_job (group, Passive _) = if is_canceled group then SOME () else NONE; |
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
181 |
|
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
182 |
fun all_passive (Queue {jobs, ...}) = |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
183 |
is_none (Task_Graph.get_first (active_job o #1 o #2) jobs); |
28165 | 184 |
|
185 |
||
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
186 |
(* queue status *) |
32052 | 187 |
|
188 |
fun status (Queue {jobs, ...}) = |
|
189 |
let |
|
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
190 |
val (x, y, z, w) = |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
191 |
Task_Graph.fold (fn (_, ((_, job), (deps, _))) => fn (x, y, z, w) => |
32052 | 192 |
(case job of |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
193 |
Job _ => if null deps then (x + 1, y, z, w) else (x, y + 1, z, w) |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
194 |
| Running _ => (x, y, z + 1, w) |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
195 |
| Passive _ => (x, y, z, w + 1))) |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
196 |
jobs (0, 0, 0, 0); |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
197 |
in {ready = x, pending = y, running = z, passive = w} end; |
32052 | 198 |
|
199 |
||
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
200 |
|
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
201 |
(** task queue operations **) |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
202 |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
203 |
(* cancel -- peers and sub-groups *) |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
204 |
|
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
205 |
fun cancel (Queue {groups, jobs}) group = |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
206 |
let |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
207 |
val _ = cancel_group group Exn.Interrupt; |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
208 |
val tasks = Inttab.lookup_list groups (group_id group); |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
209 |
val running = |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
210 |
fold (get_job jobs #> (fn Running t => insert Thread.equal t | _ => I)) tasks []; |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
35242
diff
changeset
|
211 |
val _ = List.app Simple_Thread.interrupt running; |
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
212 |
in null running end; |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
213 |
|
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
214 |
fun cancel_all (Queue {jobs, ...}) = |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
215 |
let |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
216 |
fun cancel_job (group, job) (groups, running) = |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
217 |
(cancel_group group Exn.Interrupt; |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
218 |
(case job of |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
219 |
Running t => (insert eq_group group groups, insert Thread.equal t running) |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
220 |
| _ => (groups, running))); |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
221 |
val (running_groups, running) = Task_Graph.fold (cancel_job o #1 o #2) jobs ([], []); |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
35242
diff
changeset
|
222 |
val _ = List.app Simple_Thread.interrupt running; |
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
223 |
in running_groups end; |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
224 |
|
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
225 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
226 |
(* finish *) |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
227 |
|
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
228 |
fun finish task (Queue {groups, jobs}) = |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
229 |
let |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
230 |
val group = get_group jobs task; |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
231 |
val groups' = groups |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
232 |
|> fold (fn gid => Inttab.remove_list eq_task (gid, task)) (group_ancestry group); |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
233 |
val jobs' = Task_Graph.del_node task jobs; |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
234 |
val maximal = null (Task_Graph.imm_succs jobs task); |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
235 |
in (maximal, make_queue groups' jobs') end; |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
236 |
|
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
237 |
|
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
238 |
(* enqueue *) |
28165 | 239 |
|
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
240 |
fun enqueue_passive group abort (Queue {groups, jobs}) = |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
241 |
let |
41676 | 242 |
val task = new_task "passive" NONE; |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
243 |
val groups' = groups |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
244 |
|> fold (fn gid => Inttab.cons_list (gid, task)) (group_ancestry group); |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
245 |
val jobs' = jobs |> Task_Graph.new_node (task, (group, Passive abort)); |
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
246 |
in (task, make_queue groups' jobs') end; |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
247 |
|
41673 | 248 |
fun enqueue name group deps pri job (Queue {groups, jobs}) = |
28165 | 249 |
let |
41673 | 250 |
val task = new_task name (SOME pri); |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
251 |
val groups' = groups |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
252 |
|> fold (fn gid => Inttab.cons_list (gid, task)) (group_ancestry group); |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
253 |
val jobs' = jobs |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
254 |
|> Task_Graph.new_node (task, (group, Job [job])) |
32190
4fc7a882b41e
enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents:
32101
diff
changeset
|
255 |
|> fold (add_job task) deps |
4fc7a882b41e
enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents:
32101
diff
changeset
|
256 |
|> fold (fold (add_job task) o get_deps jobs) deps; |
32218
222f26693757
enqueue/finish: return minimal/maximal state of this task;
wenzelm
parents:
32192
diff
changeset
|
257 |
val minimal = null (get_deps jobs' task); |
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
258 |
in ((task, minimal), make_queue groups' jobs') end; |
28165 | 259 |
|
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
260 |
fun extend task job (Queue {groups, jobs}) = |
29365 | 261 |
(case try (get_job jobs) task of |
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
262 |
SOME (Job list) => SOME (make_queue groups (set_job task (Job (job :: list)) jobs)) |
29365 | 263 |
| _ => NONE); |
264 |
||
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
265 |
|
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
266 |
(* dequeue *) |
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
267 |
|
39243
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
268 |
fun dequeue_passive thread task (queue as Queue {groups, jobs}) = |
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
269 |
(case try (get_job jobs) task of |
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
270 |
SOME (Passive _) => |
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
271 |
let val jobs' = set_job task (Running thread) jobs |
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
272 |
in (true, make_queue groups jobs') end |
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
273 |
| _ => (false, queue)); |
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
274 |
|
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
275 |
fun dequeue thread (queue as Queue {groups, jobs}) = |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
276 |
(case Task_Graph.get_first (uncurry ready_job) jobs of |
39243
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
277 |
SOME (result as (task, _, _)) => |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
278 |
let val jobs' = set_job task (Running thread) jobs |
39243
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
279 |
in (SOME result, make_queue groups jobs') end |
307e3d07d19f
Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents:
39232
diff
changeset
|
280 |
| NONE => (NONE, queue)); |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
281 |
|
28165 | 282 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
283 |
(* dequeue wrt. dynamic dependencies *) |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
284 |
|
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
285 |
abstype deps = Deps of task list |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
286 |
with |
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
287 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
288 |
fun init_deps tasks = Deps tasks; |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
289 |
fun finished_deps (Deps tasks) = null tasks; |
32814
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32784
diff
changeset
|
290 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
291 |
fun insert_deps (Deps tasks) = fold (insert (op =) o name_of_task) tasks; |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
292 |
|
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
293 |
fun dequeue_deps thread (Deps deps) (queue as Queue {groups, jobs}) = |
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
294 |
let |
37854
047c96f41455
back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents:
37216
diff
changeset
|
295 |
fun ready task = ready_job task (Task_Graph.get_entry jobs task); |
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
296 |
val tasks = filter (can (Task_Graph.get_node jobs)) deps; |
32192 | 297 |
fun result (res as (task, _, _)) = |
34280
16bf3e9786a3
eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents:
34279
diff
changeset
|
298 |
let val jobs' = set_job task (Running thread) jobs |
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
299 |
in ((SOME res, Deps tasks), make_queue groups jobs') end; |
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
300 |
in |
32093
30996b775a7f
tuned dequeu_towards: try immediate tasks before expensive all_preds;
wenzelm
parents:
32055
diff
changeset
|
301 |
(case get_first ready tasks of |
32192 | 302 |
SOME res => result res |
303 |
| NONE => |
|
32250 | 304 |
(case get_first (get_first ready o get_deps jobs) tasks of |
32192 | 305 |
SOME res => result res |
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
306 |
| NONE => ((NONE, Deps tasks), queue))) |
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
307 |
end; |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
308 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
309 |
end; |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
310 |
|
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
311 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
312 |
(* timing *) |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
313 |
|
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
314 |
fun running task = |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
315 |
update_timing (fn t => fn (a, b, ds) => (Time.+ (a, t), b, ds)) task; |
32221 | 316 |
|
41681
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
317 |
fun joining task = |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
318 |
update_timing (fn t => fn (a, b, ds) => (Time.- (a, t), b, ds)) task; |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
319 |
|
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
320 |
fun waiting task deps = |
b5d7b15166bf
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents:
41680
diff
changeset
|
321 |
update_timing (fn t => fn (a, b, ds) => (Time.- (a, t), Time.+ (b, t), insert_deps deps ds)) task; |
32221 | 322 |
|
28165 | 323 |
end; |