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