author | haftmann |
Tue, 20 Jul 2010 06:35:29 +0200 | |
changeset 37891 | c26f9d06e82c |
parent 37216 | 3165bc303f66 |
child 37854 | 047c96f41455 |
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 |
29340 | 13 |
type group |
32221 | 14 |
val new_group: group option -> group |
32052 | 15 |
val group_id: group -> int |
29340 | 16 |
val eq_group: group * group -> bool |
32221 | 17 |
val cancel_group: group -> exn -> unit |
18 |
val is_canceled: group -> bool |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
19 |
val group_status: group -> exn list |
28179 | 20 |
val str_of_group: group -> string |
28165 | 21 |
type queue |
22 |
val empty: queue |
|
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
23 |
val all_passive: queue -> bool |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
24 |
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
|
25 |
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
|
26 |
val cancel_all: queue -> group list |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
27 |
val enqueue_passive: group -> queue -> task * queue |
32218
222f26693757
enqueue/finish: return minimal/maximal state of this task;
wenzelm
parents:
32192
diff
changeset
|
28 |
val enqueue: group -> task list -> int -> (bool -> bool) -> queue -> (task * bool) * queue |
29365 | 29 |
val extend: task -> (bool -> bool) -> queue -> queue option |
32249 | 30 |
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
|
31 |
val depend: task -> task list -> queue -> queue |
32249 | 32 |
val dequeue_towards: Thread.thread -> task list -> queue -> |
32224 | 33 |
(((task * group * (bool -> bool) list) option * task list) * queue) |
32221 | 34 |
val finish: task -> queue -> bool * queue |
28165 | 35 |
end; |
36 |
||
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
37 |
structure Task_Queue: TASK_QUEUE = |
28165 | 38 |
struct |
39 |
||
29121 | 40 |
(* tasks *) |
41 |
||
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
42 |
abstype task = Task of int option * serial |
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
43 |
with |
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
44 |
|
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
45 |
val dummy_task = Task (NONE, ~1); |
29121 | 46 |
fun new_task pri = Task (pri, serial ()); |
28165 | 47 |
|
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
48 |
fun pri_of_task (Task (pri, _)) = the_default 0 pri; |
29121 | 49 |
fun str_of_task (Task (_, i)) = string_of_int i; |
28998 | 50 |
|
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
51 |
fun task_ord (Task t1, Task t2) = prod_ord (rev_order o option_ord int_ord) int_ord (t1, t2); |
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
52 |
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
|
53 |
|
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
54 |
end; |
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
55 |
|
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31632
diff
changeset
|
56 |
structure Task_Graph = Graph(type key = task val ord = task_ord); |
28165 | 57 |
|
28998 | 58 |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
59 |
(* nested groups *) |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
60 |
|
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
61 |
abstype group = Group of |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
62 |
{parent: group option, |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
63 |
id: serial, |
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
64 |
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
|
65 |
with |
29121 | 66 |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
67 |
fun make_group (parent, id, status) = Group {parent = parent, id = id, status = status}; |
32052 | 68 |
|
32251 | 69 |
fun new_group parent = make_group (parent, serial (), Synchronized.var "group" []); |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
70 |
|
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
71 |
fun group_id (Group {id, ...}) = id; |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
72 |
fun eq_group (group1, group2) = group_id group1 = group_id group2; |
28551 | 73 |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
74 |
fun group_ancestry (Group {parent, id, ...}) = |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
75 |
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
|
76 |
|
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
77 |
|
32221 | 78 |
(* group status *) |
79 |
||
32251 | 80 |
fun cancel_group (Group {status, ...}) exn = |
81 |
Synchronized.change status |
|
82 |
(fn exns => |
|
83 |
(case exn of |
|
84 |
Exn.Interrupt => if null exns then [exn] else exns |
|
85 |
| _ => exn :: exns)); |
|
29121 | 86 |
|
32251 | 87 |
fun is_canceled (Group {parent, status, ...}) = |
88 |
not (null (Synchronized.value status)) orelse |
|
89 |
(case parent of NONE => false | SOME group => is_canceled group); |
|
32221 | 90 |
|
34279
02936e77a07c
tasks of canceled groups are considered "ready" -- enables to purge the queue from tasks depending on unfinished promises (also improves general reactivity);
wenzelm
parents:
34277
diff
changeset
|
91 |
fun is_ready deps group = null deps orelse is_canceled group; |
02936e77a07c
tasks of canceled groups are considered "ready" -- enables to purge the queue from tasks depending on unfinished promises (also improves general reactivity);
wenzelm
parents:
34277
diff
changeset
|
92 |
|
32251 | 93 |
fun group_status (Group {parent, status, ...}) = |
94 |
Synchronized.value status @ |
|
95 |
(case parent of NONE => [] | SOME group => group_status group); |
|
28165 | 96 |
|
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
97 |
fun str_of_group group = |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
98 |
(is_canceled group ? enclose "(" ")") (string_of_int (group_id group)); |
28179 | 99 |
|
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
100 |
end; |
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
101 |
|
28165 | 102 |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
103 |
(* jobs *) |
28165 | 104 |
|
105 |
datatype job = |
|
29365 | 106 |
Job of (bool -> bool) list | |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
107 |
Running of Thread.thread | |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
108 |
Passive; |
28165 | 109 |
|
29121 | 110 |
type jobs = (group * job) Task_Graph.T; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
111 |
|
29121 | 112 |
fun get_group (jobs: jobs) task = #1 (Task_Graph.get_node jobs task); |
113 |
fun get_job (jobs: jobs) task = #2 (Task_Graph.get_node jobs task); |
|
29365 | 114 |
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
|
115 |
|
32250 | 116 |
fun add_job task dep (jobs: jobs) = |
117 |
Task_Graph.add_edge (dep, task) jobs handle Task_Graph.UNDEF _ => jobs; |
|
118 |
||
32814
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32784
diff
changeset
|
119 |
fun add_dep task dep (jobs: jobs) = |
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32784
diff
changeset
|
120 |
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
|
121 |
raise Fail "Cyclic dependency of future tasks" |
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32784
diff
changeset
|
122 |
else add_job task dep jobs; |
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32784
diff
changeset
|
123 |
|
32250 | 124 |
fun get_deps (jobs: jobs) task = |
125 |
Task_Graph.imm_preds jobs task handle Task_Graph.UNDEF _ => []; |
|
126 |
||
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
127 |
|
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
128 |
(* queue of grouped jobs *) |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
129 |
|
28165 | 130 |
datatype queue = Queue of |
28184
5ed5cb73a2e9
eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents:
28179
diff
changeset
|
131 |
{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
|
132 |
jobs: jobs}; (*job dependency graph*) |
28165 | 133 |
|
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
|
134 |
fun make_queue groups jobs = Queue {groups = groups, jobs = jobs}; |
28204 | 135 |
|
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
|
136 |
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
|
137 |
|
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
138 |
fun all_passive (Queue {jobs, ...}) = |
35012
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents:
34280
diff
changeset
|
139 |
Task_Graph.get_first |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
140 |
((fn Job _ => SOME () | Running _ => SOME () | Passive => NONE) o #2 o #1 o #2) jobs |> is_none; |
28165 | 141 |
|
142 |
||
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
143 |
(* queue status *) |
32052 | 144 |
|
145 |
fun status (Queue {jobs, ...}) = |
|
146 |
let |
|
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
147 |
val (x, y, z, w) = |
34279
02936e77a07c
tasks of canceled groups are considered "ready" -- enables to purge the queue from tasks depending on unfinished promises (also improves general reactivity);
wenzelm
parents:
34277
diff
changeset
|
148 |
Task_Graph.fold (fn (_, ((group, job), (deps, _))) => fn (x, y, z, w) => |
32052 | 149 |
(case job of |
34279
02936e77a07c
tasks of canceled groups are considered "ready" -- enables to purge the queue from tasks depending on unfinished promises (also improves general reactivity);
wenzelm
parents:
34277
diff
changeset
|
150 |
Job _ => if is_ready deps group 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
|
151 |
| Running _ => (x, y, z + 1, w) |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
152 |
| Passive => (x, y, z, w + 1))) |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
153 |
jobs (0, 0, 0, 0); |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
154 |
in {ready = x, pending = y, running = z, passive = w} end; |
32052 | 155 |
|
156 |
||
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
157 |
(* cancel -- peers and sub-groups *) |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
158 |
|
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
|
159 |
fun cancel (Queue {groups, jobs}) group = |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
160 |
let |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
161 |
val _ = cancel_group group Exn.Interrupt; |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
162 |
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
|
163 |
val running = |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
164 |
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
|
165 |
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
|
166 |
in null running end; |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
167 |
|
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
|
168 |
fun cancel_all (Queue {groups, jobs}) = |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
169 |
let |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
170 |
fun cancel_job (group, job) (groups, running) = |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
171 |
(cancel_group group Exn.Interrupt; |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
172 |
(case job of |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
173 |
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
|
174 |
| _ => (groups, running))); |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
175 |
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
|
176 |
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
|
177 |
in running_groups end; |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
178 |
|
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
179 |
|
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
180 |
(* enqueue *) |
28165 | 181 |
|
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
|
182 |
fun enqueue_passive group (Queue {groups, jobs}) = |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
183 |
let |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
184 |
val task = new_task NONE; |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
185 |
val groups' = groups |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
186 |
|> fold (fn gid => Inttab.cons_list (gid, task)) (group_ancestry group); |
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
187 |
val jobs' = jobs |> Task_Graph.new_node (task, (group, Passive)); |
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
|
188 |
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
|
189 |
|
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
|
190 |
fun enqueue group deps pri job (Queue {groups, jobs}) = |
28165 | 191 |
let |
34277
7325a5e3587f
added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents:
32814
diff
changeset
|
192 |
val task = new_task (SOME pri); |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
193 |
val groups' = groups |
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
194 |
|> fold (fn gid => Inttab.cons_list (gid, task)) (group_ancestry group); |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
195 |
val jobs' = jobs |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
196 |
|> Task_Graph.new_node (task, (group, Job [job])) |
32190
4fc7a882b41e
enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents:
32101
diff
changeset
|
197 |
|> fold (add_job task) deps |
4fc7a882b41e
enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents:
32101
diff
changeset
|
198 |
|> 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
|
199 |
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
|
200 |
in ((task, minimal), make_queue groups' jobs') end; |
28165 | 201 |
|
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
|
202 |
fun extend task job (Queue {groups, jobs}) = |
29365 | 203 |
(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
|
204 |
SOME (Job list) => SOME (make_queue groups (set_job task (Job (job :: list)) jobs)) |
29365 | 205 |
| _ => NONE); |
206 |
||
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
207 |
|
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
208 |
(* dequeue *) |
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
209 |
|
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
|
210 |
fun dequeue thread (queue as Queue {groups, jobs}) = |
29121 | 211 |
let |
34279
02936e77a07c
tasks of canceled groups are considered "ready" -- enables to purge the queue from tasks depending on unfinished promises (also improves general reactivity);
wenzelm
parents:
34277
diff
changeset
|
212 |
fun ready (task, ((group, Job list), (deps, _))) = |
02936e77a07c
tasks of canceled groups are considered "ready" -- enables to purge the queue from tasks depending on unfinished promises (also improves general reactivity);
wenzelm
parents:
34277
diff
changeset
|
213 |
if is_ready deps group then SOME (task, group, rev list) else NONE |
29121 | 214 |
| ready _ = NONE; |
215 |
in |
|
35012
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents:
34280
diff
changeset
|
216 |
(case Task_Graph.get_first ready jobs 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
|
217 |
NONE => (NONE, queue) |
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
|
218 |
| SOME (result as (task, _, _)) => |
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 |
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
|
220 |
in (SOME result, make_queue groups jobs') end) |
28384
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
221 |
end; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
222 |
|
28165 | 223 |
|
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
224 |
(* dequeue_towards -- adhoc dependencies *) |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
225 |
|
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
|
226 |
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
|
227 |
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
|
228 |
|
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
|
229 |
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
|
230 |
let |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
231 |
fun ready task = |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
232 |
(case Task_Graph.get_node jobs task of |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
233 |
(group, Job list) => |
34279
02936e77a07c
tasks of canceled groups are considered "ready" -- enables to purge the queue from tasks depending on unfinished promises (also improves general reactivity);
wenzelm
parents:
34277
diff
changeset
|
234 |
if is_ready (get_deps jobs task) group |
32101
e25107ff4f56
support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents:
32099
diff
changeset
|
235 |
then SOME (task, group, rev list) |
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
236 |
else NONE |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
237 |
| _ => NONE); |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
238 |
val tasks = filter (can (Task_Graph.get_node jobs)) deps; |
32192 | 239 |
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
|
240 |
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
|
241 |
in ((SOME res, tasks), make_queue groups jobs') end; |
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
242 |
in |
32093
30996b775a7f
tuned dequeu_towards: try immediate tasks before expensive all_preds;
wenzelm
parents:
32055
diff
changeset
|
243 |
(case get_first ready tasks of |
32192 | 244 |
SOME res => result res |
245 |
| NONE => |
|
32250 | 246 |
(case get_first (get_first ready o get_deps jobs) tasks of |
32192 | 247 |
SOME res => result res |
32224 | 248 |
| NONE => ((NONE, tasks), queue))) |
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
249 |
end; |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
250 |
|
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
251 |
|
32221 | 252 |
(* finish *) |
253 |
||
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
|
254 |
fun finish task (Queue {groups, jobs}) = |
32221 | 255 |
let |
256 |
val group = get_group jobs task; |
|
257 |
val groups' = groups |
|
35242
1c80c29086d7
eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents:
35012
diff
changeset
|
258 |
|> fold (fn gid => Inttab.remove_list eq_task (gid, task)) (group_ancestry group); |
32221 | 259 |
val jobs' = Task_Graph.del_node task jobs; |
260 |
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
|
261 |
in (maximal, make_queue groups' jobs') end; |
32221 | 262 |
|
28165 | 263 |
end; |