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