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
|
|
11 |
eqtype group
|
|
12 |
val new_group: unit -> group
|
|
13 |
type queue
|
|
14 |
val empty: queue
|
28168
|
15 |
val enqueue: group option -> task list -> (unit -> bool) -> queue -> task * queue
|
|
16 |
val dequeue: Thread.thread -> queue -> (task * group option * (unit -> bool)) option * queue
|
28165
|
17 |
val finished: task -> queue -> queue
|
28168
|
18 |
val interrupt_task: queue -> task -> unit
|
28165
|
19 |
val interrupt_group: queue -> group -> unit
|
28168
|
20 |
val interrupt_task_group: queue -> task -> unit
|
28165
|
21 |
end;
|
|
22 |
|
28171
|
23 |
structure TaskQueue: TASK_QUEUE =
|
28165
|
24 |
struct
|
|
25 |
|
28168
|
26 |
(* identifiers *)
|
28165
|
27 |
|
|
28 |
datatype task = Task of serial;
|
|
29 |
|
|
30 |
datatype group = Group of serial;
|
|
31 |
fun new_group () = Group (serial ());
|
|
32 |
|
|
33 |
|
|
34 |
(* queue of dependent jobs *)
|
|
35 |
|
|
36 |
datatype job =
|
28168
|
37 |
Job of unit -> bool |
|
28165
|
38 |
Running of Thread.thread;
|
|
39 |
|
|
40 |
datatype queue = Queue of
|
28168
|
41 |
{jobs: (group option * job) IntGraph.T, (*job dependency graph*)
|
|
42 |
cache: (task * group option * (unit -> bool)) Queue.T option, (*cache of ready tasks*)
|
|
43 |
groups: task list Inttab.table}; (*active group members*)
|
28165
|
44 |
|
|
45 |
fun make_queue jobs cache groups =
|
|
46 |
Queue {jobs = jobs, cache = cache, groups = groups};
|
|
47 |
|
|
48 |
|
|
49 |
(* queue operations *)
|
|
50 |
|
|
51 |
val empty = make_queue IntGraph.empty NONE Inttab.empty;
|
|
52 |
|
|
53 |
fun enqueue group deps job (Queue {jobs, groups, ...}) =
|
|
54 |
let
|
|
55 |
val id = serial ();
|
|
56 |
val task = Task id;
|
|
57 |
|
|
58 |
fun add_dep (Task dep) G = IntGraph.add_edge_acyclic (dep, id) G
|
|
59 |
handle IntGraph.UNDEF _ => G;
|
28168
|
60 |
val jobs' = jobs |> IntGraph.new_node (id, (group, Job job)) |> fold add_dep deps;
|
28165
|
61 |
|
|
62 |
val groups' =
|
|
63 |
(case group of
|
|
64 |
NONE => groups
|
|
65 |
| SOME (Group gid) => Inttab.cons_list (gid, task) groups);
|
|
66 |
in (task, make_queue jobs' NONE groups') end;
|
|
67 |
|
|
68 |
fun dequeue thread (queue as Queue {jobs, cache, groups}) =
|
|
69 |
let
|
|
70 |
val ready =
|
|
71 |
(case cache of
|
|
72 |
SOME ready => ready
|
|
73 |
| NONE =>
|
28168
|
74 |
let
|
|
75 |
fun add (id, ((group, Job job), ([], _))) = Queue.enqueue (Task id, group, job)
|
|
76 |
| add _ = I;
|
28165
|
77 |
in IntGraph.fold add jobs Queue.empty end);
|
|
78 |
in
|
|
79 |
if Queue.is_empty ready then (NONE, make_queue jobs (SOME ready) groups)
|
|
80 |
else
|
|
81 |
let
|
28168
|
82 |
val (result as (Task id, _, _), ready') = Queue.dequeue ready;
|
|
83 |
val jobs' = IntGraph.map_node id (fn (group, _) => (group, Running thread)) jobs;
|
|
84 |
in (SOME result, make_queue jobs' (SOME ready') groups) end
|
28165
|
85 |
end;
|
|
86 |
|
|
87 |
fun finished (task as Task id) (Queue {jobs, groups, ...}) =
|
|
88 |
let
|
|
89 |
val groups' =
|
28168
|
90 |
(case #1 (IntGraph.get_node jobs id) of
|
28165
|
91 |
NONE => groups
|
|
92 |
| SOME (Group gid) => Inttab.remove_list (op =) (gid, task) groups);
|
|
93 |
val jobs' = IntGraph.del_nodes [id] jobs;
|
|
94 |
in make_queue jobs' NONE groups' end;
|
|
95 |
|
|
96 |
|
28168
|
97 |
(* interrupt *)
|
28165
|
98 |
|
28168
|
99 |
fun interrupt_task (Queue {jobs, ...}) (Task id) =
|
28165
|
100 |
(case IntGraph.get_node jobs id of
|
28168
|
101 |
(_, Running thread) => (Thread.interrupt thread handle Thread _ => ())
|
28165
|
102 |
| _ => ())
|
|
103 |
handle IntGraph.UNDEF _ => ();
|
|
104 |
|
|
105 |
fun interrupt_group (queue as Queue {groups, ...}) (Group gid) =
|
28168
|
106 |
List.app (interrupt_task queue) (Inttab.lookup_list groups gid);
|
|
107 |
|
|
108 |
fun interrupt_task_group (queue as Queue {jobs, ...}) (task as Task id) =
|
|
109 |
(case IntGraph.get_node jobs id of
|
|
110 |
(NONE, _) => interrupt_task queue task
|
|
111 |
| (SOME group, _) => interrupt_group queue group);
|
28165
|
112 |
|
|
113 |
end;
|