author | wenzelm |
Sat, 06 Dec 2008 00:02:11 +0100 | |
changeset 28998 | 23cbaa9f9834 |
parent 28551 | 91eec4012bc5 |
child 29117 | 5a79ec2fedfb |
permissions | -rw-r--r-- |
28165 | 1 |
(* Title: Pure/Concurrent/task_queue.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Ordered queue of grouped tasks. |
|
6 |
*) |
|
7 |
||
8 |
signature TASK_QUEUE = |
|
9 |
sig |
|
10 |
eqtype task |
|
28998 | 11 |
val new_task: unit -> task |
28196 | 12 |
val str_of_task: task -> string |
28165 | 13 |
eqtype group |
14 |
val new_group: unit -> group |
|
28551 | 15 |
val invalidate_group: group -> unit |
28179 | 16 |
val str_of_group: group -> string |
28165 | 17 |
type queue |
18 |
val empty: queue |
|
28204 | 19 |
val is_empty: queue -> bool |
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
20 |
val enqueue: group -> task list -> bool -> (bool -> bool) -> queue -> task * queue |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
21 |
val depend: task list -> task -> queue -> queue |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
22 |
val focus: task list -> queue -> queue |
28185
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 |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
28 |
val finish: task -> queue -> queue |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
29 |
val cancel: queue -> group -> bool |
28165 | 30 |
end; |
31 |
||
28171 | 32 |
structure TaskQueue: TASK_QUEUE = |
28165 | 33 |
struct |
34 |
||
28168 | 35 |
(* identifiers *) |
28165 | 36 |
|
37 |
datatype task = Task of serial; |
|
28998 | 38 |
fun new_task () = Task (serial ()); |
39 |
||
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
40 |
fun str_of_task (Task i) = string_of_int i; |
28165 | 41 |
|
28998 | 42 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
43 |
datatype group = Group of serial * bool ref; |
28551 | 44 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
45 |
fun new_group () = Group (serial (), ref true); |
28551 | 46 |
fun invalidate_group (Group (_, ok)) = ok := false; |
28165 | 47 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
48 |
fun str_of_group (Group (i, ref ok)) = |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
49 |
if ok then string_of_int i else enclose "(" ")" (string_of_int i); |
28179 | 50 |
|
28165 | 51 |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
52 |
(* jobs *) |
28165 | 53 |
|
54 |
datatype job = |
|
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
55 |
Job of bool * (bool -> bool) | (*priority, job: status -> status*) |
28165 | 56 |
Running of Thread.thread; |
57 |
||
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
58 |
type jobs = (group * job) IntGraph.T; |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
59 |
|
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
60 |
fun get_group (jobs: jobs) (Task id) = #1 (IntGraph.get_node jobs id); |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
61 |
fun get_job (jobs: jobs) (Task id) = #2 (IntGraph.get_node jobs id); |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
62 |
fun map_job (Task id) f (jobs: jobs) = IntGraph.map_node id (apsnd f) jobs; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
63 |
|
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
64 |
fun add_job (Task id) (Task dep) (jobs: jobs) = |
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
65 |
IntGraph.add_edge_acyclic (dep, id) jobs handle IntGraph.UNDEF _ => jobs; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
66 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
67 |
fun check_job (jobs: jobs) (task as Task id) = |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
68 |
if can (IntGraph.get_node jobs) id then SOME task else NONE; |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
69 |
|
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
70 |
|
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
71 |
(* queue of grouped jobs *) |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
72 |
|
28165 | 73 |
datatype queue = Queue of |
28184
5ed5cb73a2e9
eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents:
28179
diff
changeset
|
74 |
{groups: task list Inttab.table, (*groups with presently active members*) |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
75 |
jobs: jobs, (*job dependency graph*) |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
76 |
focus: task list}; (*particular collection of high-priority tasks*) |
28165 | 77 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
78 |
fun make_queue groups jobs focus = Queue {groups = groups, jobs = jobs, focus = focus}; |
28204 | 79 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
80 |
val empty = make_queue Inttab.empty IntGraph.empty []; |
28204 | 81 |
fun is_empty (Queue {jobs, ...}) = IntGraph.is_empty jobs; |
28165 | 82 |
|
83 |
||
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
84 |
(* enqueue *) |
28165 | 85 |
|
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
86 |
fun enqueue (group as Group (gid, _)) deps pri job (Queue {groups, jobs, focus}) = |
28165 | 87 |
let |
28998 | 88 |
val task as Task id = new_task (); |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
89 |
val groups' = Inttab.cons_list (gid, task) groups; |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
90 |
val jobs' = jobs |
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
91 |
|> IntGraph.new_node (id, (group, Job (pri, job))) |> fold (add_job task) deps; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
92 |
in (task, make_queue groups' jobs' focus) end; |
28165 | 93 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
94 |
fun depend deps task (Queue {groups, jobs, focus}) = |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
95 |
make_queue groups (fold (add_job task) deps jobs) focus; |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
96 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
97 |
fun focus tasks (Queue {groups, jobs, ...}) = |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
98 |
make_queue groups jobs (map_filter (check_job jobs) tasks); |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
99 |
|
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
100 |
|
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
101 |
(* dequeue *) |
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
102 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
103 |
local |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
104 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
105 |
fun dequeue_result NONE queue = (NONE, queue) |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
106 |
| dequeue_result (SOME (result as (task, _, _))) (Queue {groups, jobs, focus}) = |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
107 |
(SOME result, make_queue groups (map_job task (K (Running (Thread.self ()))) jobs) focus); |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
108 |
|
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
109 |
fun dequeue_global req_pri (queue as Queue {jobs, ...}) = |
28165 | 110 |
let |
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
111 |
fun ready (id, ((group as Group (_, ref ok), Job (pri, job)), ([], _))) = |
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
112 |
if pri = req_pri then SOME (Task id, group, (fn () => job ok)) else NONE |
28184
5ed5cb73a2e9
eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents:
28179
diff
changeset
|
113 |
| ready _ = NONE; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
114 |
in dequeue_result (IntGraph.get_first ready jobs) queue end; |
28165 | 115 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
116 |
fun dequeue_local focus (queue as Queue {jobs, ...}) = |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
117 |
let |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
118 |
fun ready id = |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
119 |
(case IntGraph.get_node jobs id of |
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
120 |
(group as Group (_, ref ok), Job (_, job)) => |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
121 |
if null (IntGraph.imm_preds jobs id) then SOME (Task id, group, (fn () => job ok)) |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
122 |
else NONE |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
123 |
| _ => NONE); |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
124 |
val ids = map (fn Task id => id) focus; |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
125 |
in dequeue_result (get_first ready (IntGraph.all_preds jobs ids)) queue end; |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
126 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
127 |
in |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
128 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
129 |
fun dequeue (queue as Queue {focus, ...}) = |
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
130 |
(case dequeue_local focus queue of |
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
131 |
(NONE, _) => |
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
132 |
(case dequeue_global true queue of (NONE, _) => dequeue_global false queue | res => res) |
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28204
diff
changeset
|
133 |
| res => res); |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
134 |
|
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
135 |
fun dequeue_towards tasks (queue as Queue {jobs, ...}) = |
28384
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
136 |
let val tasks' = map_filter (check_job jobs) tasks in |
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
137 |
(case dequeue_local tasks' queue of |
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
138 |
(NONE, queue') => (NONE, queue') |
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
139 |
| (SOME work, queue') => (SOME (work, tasks'), queue')) |
70abca69247b
dequeue_towards: return bound for unfinished tasks;
wenzelm
parents:
28332
diff
changeset
|
140 |
end; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
141 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
142 |
end; |
28185
0f20cbce4935
simplified dequeue: provide Thread.self internally;
wenzelm
parents:
28184
diff
changeset
|
143 |
|
28165 | 144 |
|
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
145 |
(* sporadic interrupts *) |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
146 |
|
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
147 |
fun interrupt (Queue {jobs, ...}) task = |
28551 | 148 |
(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
|
149 |
|
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
150 |
fun interrupt_external queue str = |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
151 |
(case Int.fromString str of SOME id => interrupt queue (Task id) | NONE => ()); |
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
152 |
|
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
153 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
154 |
(* misc operations *) |
28165 | 155 |
|
28551 | 156 |
fun cancel (Queue {groups, jobs, ...}) (group as Group (gid, _)) = |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
157 |
let |
28551 | 158 |
val _ = invalidate_group group; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
159 |
val tasks = Inttab.lookup_list groups gid; |
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
160 |
val running = fold (get_job jobs #> (fn Running thread => cons thread | _ => I)) tasks []; |
28551 | 161 |
val _ = List.app SimpleThread.interrupt running; |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
162 |
in null running end; |
28165 | 163 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
164 |
fun finish (task as Task id) (Queue {groups, jobs, focus}) = |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
165 |
let |
28190
0a2434cf38c9
cancel: invalidate group implicitly, via bool ref;
wenzelm
parents:
28185
diff
changeset
|
166 |
val Group (gid, _) = get_group jobs task; |
28176
01b21886e7f0
job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents:
28171
diff
changeset
|
167 |
val groups' = Inttab.remove_list (op =) (gid, task) groups; |
28332 | 168 |
val jobs' = IntGraph.del_node id jobs; |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
169 |
val focus' = remove (op =) task focus; |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28196
diff
changeset
|
170 |
in make_queue groups' jobs' focus' end; |
28165 | 171 |
|
172 |
end; |