author | wenzelm |
Sat, 03 Jan 2009 21:44:24 +0100 | |
changeset 29340 | 057a30ee8570 |
parent 29121 | ad73b63ae2c5 |
child 29342 | 23504001c4fb |
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 |
14 |
val eq_group: group * group -> bool |
|
28165 | 15 |
val new_group: unit -> group |
28551 | 16 |
val invalidate_group: group -> unit |
28179 | 17 |
val str_of_group: group -> string |
28165 | 18 |
type queue |
19 |
val empty: queue |
|
28204 | 20 |
val is_empty: queue -> bool |
29121 | 21 |
val enqueue: group -> task list -> int -> (bool -> bool) -> queue -> task * queue |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
22 |
val depend: task list -> task -> queue -> queue |
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
23 |
val dequeue: queue -> (task * group * (unit -> bool)) option * queue |
28384
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
24 |
val dequeue_towards: task list -> queue -> |
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
25 |
(((task * group * (unit -> bool)) * task list) option * queue) |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
26 |
val interrupt: queue -> task -> unit |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
27 |
val interrupt_external: queue -> string -> unit |
29340 | 28 |
val cancel: queue -> group -> bool |
29 |
val cancel_all: queue -> group list |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
30 |
val finish: task -> queue -> queue |
28165 | 31 |
end; |
32 |
||
29340 | 33 |
structure Task_Queue:> TASK_QUEUE = |
28165 | 34 |
struct |
35 |
||
29121 | 36 |
(* tasks *) |
37 |
||
38 |
datatype task = Task of int * serial; |
|
39 |
fun new_task pri = Task (pri, serial ()); |
|
28165 | 40 |
|
29121 | 41 |
fun pri_of_task (Task (pri, _)) = pri; |
42 |
fun str_of_task (Task (_, i)) = string_of_int i; |
|
28998 | 43 |
|
29121 | 44 |
fun task_ord (Task t1, Task t2) = prod_ord (rev_order o int_ord) int_ord (t1, t2); |
45 |
structure Task_Graph = GraphFun(type key = task val ord = task_ord); |
|
28165 | 46 |
|
28998 | 47 |
|
29121 | 48 |
(* groups *) |
49 |
||
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
50 |
datatype group = Group of serial * bool ref; |
29340 | 51 |
fun eq_group (Group (gid1, _), Group (gid2, _)) = gid1 = gid2; |
28551 | 52 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
53 |
fun new_group () = Group (serial (), ref true); |
29121 | 54 |
|
28551 | 55 |
fun invalidate_group (Group (_, ok)) = ok := false; |
28165 | 56 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
57 |
fun str_of_group (Group (i, ref ok)) = |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
58 |
if ok then string_of_int i else enclose "(" ")" (string_of_int i); |
28179 | 59 |
|
28165 | 60 |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
61 |
(* jobs *) |
28165 | 62 |
|
63 |
datatype job = |
|
29121 | 64 |
Job of bool -> bool | |
28165 | 65 |
Running of Thread.thread; |
66 |
||
29121 | 67 |
type jobs = (group * job) Task_Graph.T; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
68 |
|
29121 | 69 |
fun get_group (jobs: jobs) task = #1 (Task_Graph.get_node jobs task); |
70 |
fun get_job (jobs: jobs) task = #2 (Task_Graph.get_node jobs task); |
|
71 |
fun map_job task f (jobs: jobs) = Task_Graph.map_node task (apsnd f) jobs; |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
72 |
|
29121 | 73 |
fun add_job task dep (jobs: jobs) = |
74 |
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
|
75 |
|
29121 | 76 |
fun add_job_acyclic task dep (jobs: jobs) = |
77 |
Task_Graph.add_edge_acyclic (dep, task) jobs handle Task_Graph.UNDEF _ => jobs; |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
78 |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
79 |
|
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
80 |
(* queue of grouped jobs *) |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
81 |
|
28165 | 82 |
datatype queue = Queue of |
28184
5ed5cb73a2e9
eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents:
28179
diff
changeset
|
83 |
{groups: task list Inttab.table, (*groups with presently active members*) |
29121 | 84 |
jobs: jobs}; (*job dependency graph*) |
28165 | 85 |
|
29121 | 86 |
fun make_queue groups jobs = Queue {groups = groups, jobs = jobs}; |
28204 | 87 |
|
29121 | 88 |
val empty = make_queue Inttab.empty Task_Graph.empty; |
89 |
fun is_empty (Queue {jobs, ...}) = Task_Graph.is_empty jobs; |
|
28165 | 90 |
|
91 |
||
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
92 |
(* enqueue *) |
28165 | 93 |
|
29121 | 94 |
fun enqueue (group as Group (gid, _)) deps pri job (Queue {groups, jobs}) = |
28165 | 95 |
let |
29121 | 96 |
val task = new_task pri; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
97 |
val groups' = Inttab.cons_list (gid, task) groups; |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
98 |
val jobs' = jobs |
29121 | 99 |
|> Task_Graph.new_node (task, (group, Job job)) |> fold (add_job task) deps; |
100 |
in (task, make_queue groups' jobs') end; |
|
28165 | 101 |
|
29121 | 102 |
fun depend deps task (Queue {groups, jobs}) = |
103 |
make_queue groups (fold (add_job_acyclic task) deps jobs); |
|
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
104 |
|
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
105 |
|
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
106 |
(* dequeue *) |
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
107 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
108 |
local |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
109 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
110 |
fun dequeue_result NONE queue = (NONE, queue) |
29121 | 111 |
| dequeue_result (SOME (result as (task, _, _))) (Queue {groups, jobs}) = |
112 |
(SOME result, make_queue groups (map_job task (K (Running (Thread.self ()))) jobs)); |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
113 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
114 |
in |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
115 |
|
29121 | 116 |
fun dequeue (queue as Queue {jobs, ...}) = |
117 |
let |
|
118 |
fun ready (task, ((group as Group (_, ref ok), Job job), ([], _))) = |
|
119 |
SOME (task, group, (fn () => job ok)) |
|
120 |
| ready _ = NONE; |
|
121 |
in dequeue_result (Task_Graph.get_first ready jobs) queue end; |
|
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
122 |
|
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
123 |
fun dequeue_towards tasks (queue as Queue {jobs, ...}) = |
29121 | 124 |
let |
125 |
val tasks' = filter (can (Task_Graph.get_node jobs)) tasks; |
|
126 |
||
127 |
fun ready task = |
|
128 |
(case Task_Graph.get_node jobs task of |
|
129 |
(group as Group (_, ref ok), Job job) => |
|
130 |
if null (Task_Graph.imm_preds jobs task) then SOME (task, group, (fn () => job ok)) |
|
131 |
else NONE |
|
132 |
| _ => NONE); |
|
133 |
in |
|
134 |
(case dequeue_result (get_first ready (Task_Graph.all_preds jobs tasks')) queue of |
|
28384
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
135 |
(NONE, queue') => (NONE, queue') |
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
136 |
| (SOME work, queue') => (SOME (work, tasks'), queue')) |
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
137 |
end; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
138 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
139 |
end; |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
140 |
|
28165 | 141 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
142 |
(* sporadic interrupts *) |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
143 |
|
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
144 |
fun interrupt (Queue {jobs, ...}) task = |
28551 | 145 |
(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
|
146 |
|
29121 | 147 |
fun interrupt_external (queue as Queue {jobs, ...}) str = |
148 |
(case Int.fromString str of |
|
149 |
SOME i => |
|
150 |
(case Task_Graph.get_first |
|
151 |
(fn (task as Task (_, j), _) => if i = j then SOME task else NONE) jobs |
|
152 |
of SOME task => interrupt queue task | NONE => ()) |
|
153 |
| NONE => ()); |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
154 |
|
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
155 |
|
29340 | 156 |
(* termination *) |
28165 | 157 |
|
28551 | 158 |
fun cancel (Queue {groups, jobs, ...}) (group as Group (gid, _)) = |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
159 |
let |
28551 | 160 |
val _ = invalidate_group group; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
161 |
val tasks = Inttab.lookup_list groups gid; |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
162 |
val running = fold (get_job jobs #> (fn Running thread => cons thread | _ => I)) tasks []; |
28551 | 163 |
val _ = List.app SimpleThread.interrupt running; |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
164 |
in null running end; |
28165 | 165 |
|
29340 | 166 |
fun cancel_all (Queue {jobs, ...}) = |
167 |
let |
|
168 |
fun cancel_job (group, job) (groups, running) = |
|
169 |
(invalidate_group group; |
|
170 |
(case job of Running thread => (insert eq_group group groups, thread :: running) |
|
171 |
| _ => (groups, running))); |
|
172 |
val (groups, running) = Task_Graph.fold (cancel_job o #1 o #2) jobs ([], []); |
|
173 |
val _ = List.app SimpleThread.interrupt running; |
|
174 |
in groups end; |
|
175 |
||
29121 | 176 |
fun finish task (Queue {groups, jobs}) = |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
177 |
let |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
178 |
val Group (gid, _) = get_group jobs task; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
179 |
val groups' = Inttab.remove_list (op =) (gid, task) groups; |
29121 | 180 |
val jobs' = Task_Graph.del_node task jobs; |
181 |
in make_queue groups' jobs' end; |
|
28165 | 182 |
|
183 |
end; |