author | wenzelm |
Sun, 14 Jun 2009 23:25:49 +0200 | |
changeset 31632 | fae680e35958 |
parent 31617 | bb7b5a5942c7 |
child 31971 | 8c1b845ed105 |
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 |
24 |
val dequeue: queue -> (task * group * (bool -> bool) list) option * queue |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
25 |
val interrupt: queue -> task -> unit |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
26 |
val interrupt_external: queue -> string -> unit |
29340 | 27 |
val cancel: queue -> group -> bool |
28 |
val cancel_all: queue -> group list |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
29 |
val finish: task -> queue -> queue |
28165 | 30 |
end; |
31 |
||
29340 | 32 |
structure Task_Queue:> TASK_QUEUE = |
28165 | 33 |
struct |
34 |
||
29121 | 35 |
(* tasks *) |
36 |
||
37 |
datatype task = Task of int * serial; |
|
38 |
fun new_task pri = Task (pri, serial ()); |
|
28165 | 39 |
|
29121 | 40 |
fun pri_of_task (Task (pri, _)) = pri; |
41 |
fun str_of_task (Task (_, i)) = string_of_int i; |
|
28998 | 42 |
|
29121 | 43 |
fun task_ord (Task t1, Task t2) = prod_ord (rev_order o int_ord) int_ord (t1, t2); |
44 |
structure Task_Graph = GraphFun(type key = task val ord = task_ord); |
|
28165 | 45 |
|
28998 | 46 |
|
29121 | 47 |
(* groups *) |
48 |
||
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
49 |
datatype group = Group of serial * bool ref; |
29340 | 50 |
fun eq_group (Group (gid1, _), Group (gid2, _)) = gid1 = gid2; |
28551 | 51 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
52 |
fun new_group () = Group (serial (), ref true); |
29121 | 53 |
|
29365 | 54 |
fun is_valid (Group (_, ref ok)) = ok; |
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 = |
|
29365 | 64 |
Job of (bool -> bool) list | |
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); |
|
29365 | 71 |
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
|
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 |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
76 |
|
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
77 |
(* queue of grouped jobs *) |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
78 |
|
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
|
79 |
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
|
80 |
|
28165 | 81 |
datatype queue = Queue of |
28184
5ed5cb73a2e9
eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents:
28179
diff
changeset
|
82 |
{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
|
83 |
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
|
84 |
cache: result}; (*last dequeue result*) |
28165 | 85 |
|
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
|
86 |
fun make_queue groups jobs cache = Queue {groups = groups, jobs = jobs, cache = cache}; |
28204 | 87 |
|
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
|
88 |
val empty = make_queue Inttab.empty Task_Graph.empty No_Result; |
29121 | 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 |
|
31632 | 94 |
fun enqueue (group as Group (gid, _)) deps pri job (Queue {groups, jobs, cache}) = |
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 |
29365 | 99 |
|> Task_Graph.new_node (task, (group, Job [job])) |> fold (add_job task) deps; |
31632 | 100 |
val cache' = |
101 |
(case cache of |
|
102 |
Result last => |
|
103 |
if task_ord (last, task) = LESS |
|
104 |
then cache else Unknown |
|
105 |
| _ => Unknown); |
|
106 |
in (task, make_queue groups' jobs' cache') end; |
|
28165 | 107 |
|
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
|
108 |
fun extend task job (Queue {groups, jobs, cache}) = |
29365 | 109 |
(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
|
110 |
SOME (Job list) => SOME (make_queue groups (set_job task (Job (job :: list)) jobs) cache) |
29365 | 111 |
| _ => NONE); |
112 |
||
28185
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 |
|
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
|
116 |
fun dequeue (queue as Queue {groups, jobs, cache}) = |
29121 | 117 |
let |
29365 | 118 |
fun ready (task, ((group, Job list), ([], _))) = SOME (task, group, rev list) |
29121 | 119 |
| 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
|
120 |
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
|
121 |
(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
|
122 |
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
|
123 |
| 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
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
in (SOME result, make_queue groups jobs' cache') end); |
29121 | 128 |
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
|
129 |
(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
|
130 |
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
|
131 |
| 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
|
132 |
| No_Result => (NONE, queue)) |
28384
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
133 |
end; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
134 |
|
28165 | 135 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
136 |
(* sporadic interrupts *) |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
137 |
|
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
138 |
fun interrupt (Queue {jobs, ...}) task = |
28551 | 139 |
(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
|
140 |
|
29121 | 141 |
fun interrupt_external (queue as Queue {jobs, ...}) str = |
142 |
(case Int.fromString str of |
|
143 |
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
|
144 |
(case Task_Graph.get_first NONE |
29121 | 145 |
(fn (task as Task (_, j), _) => if i = j then SOME task else NONE) jobs |
146 |
of SOME task => interrupt queue task | NONE => ()) |
|
147 |
| NONE => ()); |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
148 |
|
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
149 |
|
29340 | 150 |
(* termination *) |
28165 | 151 |
|
28551 | 152 |
fun cancel (Queue {groups, jobs, ...}) (group as Group (gid, _)) = |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
153 |
let |
28551 | 154 |
val _ = invalidate_group group; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
155 |
val tasks = Inttab.lookup_list groups gid; |
29342 | 156 |
val running = fold (get_job jobs #> (fn Running t => insert Thread.equal t | _ => I)) tasks []; |
28551 | 157 |
val _ = List.app SimpleThread.interrupt running; |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
158 |
in null running end; |
28165 | 159 |
|
29340 | 160 |
fun cancel_all (Queue {jobs, ...}) = |
161 |
let |
|
162 |
fun cancel_job (group, job) (groups, running) = |
|
163 |
(invalidate_group group; |
|
29342 | 164 |
(case job of Running t => (insert eq_group group groups, insert Thread.equal t running) |
29340 | 165 |
| _ => (groups, running))); |
166 |
val (groups, running) = Task_Graph.fold (cancel_job o #1 o #2) jobs ([], []); |
|
167 |
val _ = List.app SimpleThread.interrupt running; |
|
168 |
in groups end; |
|
169 |
||
31632 | 170 |
fun finish task (Queue {groups, jobs, cache}) = |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
171 |
let |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
172 |
val Group (gid, _) = get_group jobs task; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
173 |
val groups' = Inttab.remove_list (op =) (gid, task) groups; |
29121 | 174 |
val jobs' = Task_Graph.del_node task jobs; |
31632 | 175 |
val cache' = |
176 |
if null (Task_Graph.imm_succs jobs task) then cache |
|
177 |
else Unknown; |
|
178 |
in make_queue groups' jobs' cache' end; |
|
28165 | 179 |
|
180 |
end; |