author | wenzelm |
Sun, 19 Jul 2009 14:14:25 +0200 | |
changeset 32055 | 6a46898aa805 |
parent 32052 | 8c391a12df1d |
child 32093 | 30996b775a7f |
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 |
29121 | 10 |
val new_task: int -> task |
11 |
val pri_of_task: task -> int |
|
28196 | 12 |
val str_of_task: task -> string |
29340 | 13 |
type group |
32052 | 14 |
val group_id: group -> int |
29340 | 15 |
val eq_group: group * group -> bool |
28165 | 16 |
val new_group: unit -> group |
29365 | 17 |
val is_valid: group -> bool |
28551 | 18 |
val invalidate_group: group -> unit |
28179 | 19 |
val str_of_group: group -> string |
28165 | 20 |
type queue |
21 |
val empty: queue |
|
28204 | 22 |
val is_empty: queue -> bool |
32052 | 23 |
val status: queue -> {ready: int, pending: int, running: int} |
29121 | 24 |
val enqueue: group -> task list -> int -> (bool -> bool) -> queue -> task * queue |
29365 | 25 |
val extend: task -> (bool -> bool) -> queue -> queue option |
26 |
val dequeue: queue -> (task * group * (bool -> bool) list) option * queue |
|
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
27 |
val dequeue_towards: task list -> queue -> |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
28 |
(((task * group * (bool -> bool) list) * task list) option * queue) |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
29 |
val interrupt: queue -> task -> unit |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
30 |
val interrupt_external: queue -> string -> unit |
29340 | 31 |
val cancel: queue -> group -> bool |
32 |
val cancel_all: queue -> group list |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
33 |
val finish: task -> queue -> queue |
28165 | 34 |
end; |
35 |
||
29340 | 36 |
structure Task_Queue:> TASK_QUEUE = |
28165 | 37 |
struct |
38 |
||
29121 | 39 |
(* tasks *) |
40 |
||
41 |
datatype task = Task of int * serial; |
|
42 |
fun new_task pri = Task (pri, serial ()); |
|
28165 | 43 |
|
29121 | 44 |
fun pri_of_task (Task (pri, _)) = pri; |
45 |
fun str_of_task (Task (_, i)) = string_of_int i; |
|
28998 | 46 |
|
29121 | 47 |
fun task_ord (Task t1, Task t2) = prod_ord (rev_order o int_ord) int_ord (t1, t2); |
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31632
diff
changeset
|
48 |
structure Task_Graph = Graph(type key = task val ord = task_ord); |
28165 | 49 |
|
28998 | 50 |
|
29121 | 51 |
(* groups *) |
52 |
||
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
53 |
datatype group = Group of serial * bool ref; |
32052 | 54 |
|
55 |
fun group_id (Group (gid, _)) = gid; |
|
29340 | 56 |
fun eq_group (Group (gid1, _), Group (gid2, _)) = gid1 = gid2; |
28551 | 57 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
58 |
fun new_group () = Group (serial (), ref true); |
29121 | 59 |
|
29365 | 60 |
fun is_valid (Group (_, ref ok)) = ok; |
28551 | 61 |
fun invalidate_group (Group (_, ok)) = ok := false; |
28165 | 62 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
63 |
fun str_of_group (Group (i, ref ok)) = |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
64 |
if ok then string_of_int i else enclose "(" ")" (string_of_int i); |
28179 | 65 |
|
28165 | 66 |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
67 |
(* jobs *) |
28165 | 68 |
|
69 |
datatype job = |
|
29365 | 70 |
Job of (bool -> bool) list | |
28165 | 71 |
Running of Thread.thread; |
72 |
||
29121 | 73 |
type jobs = (group * job) Task_Graph.T; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
74 |
|
29121 | 75 |
fun get_group (jobs: jobs) task = #1 (Task_Graph.get_node jobs task); |
76 |
fun get_job (jobs: jobs) task = #2 (Task_Graph.get_node jobs task); |
|
29365 | 77 |
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
|
78 |
|
29121 | 79 |
fun add_job task dep (jobs: jobs) = |
80 |
Task_Graph.add_edge (dep, task) jobs handle Task_Graph.UNDEF _ => jobs; |
|
29117
5a79ec2fedfb
tuned enqueue: plain add_edge, acyclic not required here;
wenzelm
parents:
28998
diff
changeset
|
81 |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
82 |
|
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
83 |
(* queue of grouped jobs *) |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
84 |
|
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
85 |
datatype result = Unknown | Result of task | No_Result; |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
86 |
|
28165 | 87 |
datatype queue = Queue of |
28184
5ed5cb73a2e9
eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents:
28179
diff
changeset
|
88 |
{groups: task list Inttab.table, (*groups with presently active members*) |
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
89 |
jobs: jobs, (*job dependency graph*) |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
90 |
cache: result}; (*last dequeue result*) |
28165 | 91 |
|
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
92 |
fun make_queue groups jobs cache = Queue {groups = groups, jobs = jobs, cache = cache}; |
28204 | 93 |
|
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
94 |
val empty = make_queue Inttab.empty Task_Graph.empty No_Result; |
29121 | 95 |
fun is_empty (Queue {jobs, ...}) = Task_Graph.is_empty jobs; |
28165 | 96 |
|
97 |
||
32052 | 98 |
(* status *) |
99 |
||
100 |
fun status (Queue {jobs, ...}) = |
|
101 |
let |
|
102 |
val (x, y, z) = |
|
103 |
Task_Graph.fold (fn (task, ((_, job), (deps, _))) => fn (x, y, z) => |
|
104 |
(case job of |
|
105 |
Job _ => if null deps then (x + 1, y, z) else (x, y + 1, z) |
|
106 |
| Running _ => (x, y, z + 1))) |
|
107 |
jobs (0, 0, 0); |
|
108 |
in {ready = x, pending = y, running = z} end; |
|
109 |
||
110 |
||
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
111 |
(* enqueue *) |
28165 | 112 |
|
31632 | 113 |
fun enqueue (group as Group (gid, _)) deps pri job (Queue {groups, jobs, cache}) = |
28165 | 114 |
let |
29121 | 115 |
val task = new_task pri; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
116 |
val groups' = Inttab.cons_list (gid, task) groups; |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
117 |
val jobs' = jobs |
29365 | 118 |
|> Task_Graph.new_node (task, (group, Job [job])) |> fold (add_job task) deps; |
31632 | 119 |
val cache' = |
120 |
(case cache of |
|
121 |
Result last => |
|
122 |
if task_ord (last, task) = LESS |
|
123 |
then cache else Unknown |
|
124 |
| _ => Unknown); |
|
125 |
in (task, make_queue groups' jobs' cache') end; |
|
28165 | 126 |
|
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
127 |
fun extend task job (Queue {groups, jobs, cache}) = |
29365 | 128 |
(case try (get_job jobs) task of |
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
129 |
SOME (Job list) => SOME (make_queue groups (set_job task (Job (job :: list)) jobs) cache) |
29365 | 130 |
| _ => NONE); |
131 |
||
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
132 |
|
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
133 |
(* dequeue *) |
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
134 |
|
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
135 |
fun dequeue (queue as Queue {groups, jobs, cache}) = |
29121 | 136 |
let |
29365 | 137 |
fun ready (task, ((group, Job list), ([], _))) = SOME (task, group, rev list) |
29121 | 138 |
| ready _ = NONE; |
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
139 |
fun deq boundary = |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
140 |
(case Task_Graph.get_first boundary ready jobs of |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
141 |
NONE => (NONE, make_queue groups jobs No_Result) |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
142 |
| SOME (result as (task, _, _)) => |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
143 |
let |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
144 |
val jobs' = set_job task (Running (Thread.self ())) jobs; |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
145 |
val cache' = Result task; |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
146 |
in (SOME result, make_queue groups jobs' cache') end); |
29121 | 147 |
in |
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
148 |
(case cache of |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
149 |
Unknown => deq NONE |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
150 |
| Result last => deq (SOME last) |
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
151 |
| No_Result => (NONE, queue)) |
28384
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
152 |
end; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
153 |
|
28165 | 154 |
|
32055
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
155 |
(* dequeue_towards -- adhoc dependencies *) |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
156 |
|
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
157 |
fun dequeue_towards deps (queue as Queue {groups, jobs, ...}) = |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
158 |
let |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
159 |
fun ready task = |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
160 |
(case Task_Graph.get_node jobs task of |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
161 |
(group, Job list) => |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
162 |
if null (Task_Graph.imm_preds jobs task) then SOME (task, group, rev list) |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
163 |
else NONE |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
164 |
| _ => NONE); |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
165 |
val tasks = filter (can (Task_Graph.get_node jobs)) deps; |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
166 |
in |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
167 |
(case get_first ready (Task_Graph.all_preds jobs tasks) of |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
168 |
NONE => (NONE, queue) |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
169 |
| SOME (result as (task, _, _)) => |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
170 |
let |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
171 |
val jobs' = set_job task (Running (Thread.self ())) jobs; |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
172 |
val cache' = Unknown; |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
173 |
in (SOME (result, tasks), make_queue groups jobs' cache') end) |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
174 |
end; |
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
175 |
|
6a46898aa805
recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents:
32052
diff
changeset
|
176 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
177 |
(* sporadic interrupts *) |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
178 |
|
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
179 |
fun interrupt (Queue {jobs, ...}) task = |
28551 | 180 |
(case try (get_job jobs) task of SOME (Running thread) => SimpleThread.interrupt thread | _ => ()); |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
181 |
|
29121 | 182 |
fun interrupt_external (queue as Queue {jobs, ...}) str = |
183 |
(case Int.fromString str of |
|
184 |
SOME i => |
|
31617
bb7b5a5942c7
simplified join_results: no longer work "towards" deps, which simplifies task queue management and maintains strict bottom up discipline (without "transfer of priority" to required futures);
wenzelm
parents:
29365
diff
changeset
|
185 |
(case Task_Graph.get_first NONE |
29121 | 186 |
(fn (task as Task (_, j), _) => if i = j then SOME task else NONE) jobs |
187 |
of SOME task => interrupt queue task | NONE => ()) |
|
188 |
| NONE => ()); |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
189 |
|
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
190 |
|
29340 | 191 |
(* termination *) |
28165 | 192 |
|
28551 | 193 |
fun cancel (Queue {groups, jobs, ...}) (group as Group (gid, _)) = |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
194 |
let |
28551 | 195 |
val _ = invalidate_group group; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
196 |
val tasks = Inttab.lookup_list groups gid; |
29342 | 197 |
val running = fold (get_job jobs #> (fn Running t => insert Thread.equal t | _ => I)) tasks []; |
28551 | 198 |
val _ = List.app SimpleThread.interrupt running; |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
199 |
in null running end; |
28165 | 200 |
|
29340 | 201 |
fun cancel_all (Queue {jobs, ...}) = |
202 |
let |
|
203 |
fun cancel_job (group, job) (groups, running) = |
|
204 |
(invalidate_group group; |
|
29342 | 205 |
(case job of Running t => (insert eq_group group groups, insert Thread.equal t running) |
29340 | 206 |
| _ => (groups, running))); |
207 |
val (groups, running) = Task_Graph.fold (cancel_job o #1 o #2) jobs ([], []); |
|
208 |
val _ = List.app SimpleThread.interrupt running; |
|
209 |
in groups end; |
|
210 |
||
31632 | 211 |
fun finish task (Queue {groups, jobs, cache}) = |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
212 |
let |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
213 |
val Group (gid, _) = get_group jobs task; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
214 |
val groups' = Inttab.remove_list (op =) (gid, task) groups; |
29121 | 215 |
val jobs' = Task_Graph.del_node task jobs; |
31632 | 216 |
val cache' = |
217 |
if null (Task_Graph.imm_succs jobs task) then cache |
|
218 |
else Unknown; |
|
219 |
in make_queue groups' jobs' cache' end; |
|
28165 | 220 |
|
221 |
end; |