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