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