1 (* Title: Pure/Concurrent/task_queue.ML
5 Ordered queue of grouped tasks.
11 val str_of_task: task -> string
13 val new_group: unit -> group
14 val str_of_group: group -> string
17 val is_empty: queue -> bool
18 val enqueue: group -> task list -> bool -> (bool -> bool) -> queue -> task * queue
19 val depend: task list -> task -> queue -> queue
20 val focus: task list -> queue -> queue
21 val dequeue: queue -> (task * group * (unit -> bool)) option * queue
22 val dequeue_towards: task list -> queue -> (task * group * (unit -> bool)) option * queue
23 val interrupt: queue -> task -> unit
24 val interrupt_external: queue -> string -> unit
25 val finish: task -> queue -> queue
26 val cancel: queue -> group -> bool
29 structure TaskQueue: TASK_QUEUE =
34 datatype task = Task of serial;
35 fun str_of_task (Task i) = string_of_int i;
37 datatype group = Group of serial * bool ref;
38 fun new_group () = Group (serial (), ref true);
40 fun str_of_group (Group (i, ref ok)) =
41 if ok then string_of_int i else enclose "(" ")" (string_of_int i);
47 Job of bool * (bool -> bool) | (*priority, job: status -> status*)
48 Running of Thread.thread;
50 type jobs = (group * job) IntGraph.T;
52 fun get_group (jobs: jobs) (Task id) = #1 (IntGraph.get_node jobs id);
53 fun get_job (jobs: jobs) (Task id) = #2 (IntGraph.get_node jobs id);
54 fun map_job (Task id) f (jobs: jobs) = IntGraph.map_node id (apsnd f) jobs;
56 fun add_job (Task id) (Task dep) (jobs: jobs) =
57 IntGraph.add_edge_acyclic (dep, id) jobs handle IntGraph.UNDEF _ => jobs;
59 fun check_job (jobs: jobs) (task as Task id) =
60 if can (IntGraph.get_node jobs) id then SOME task else NONE;
63 (* queue of grouped jobs *)
65 datatype queue = Queue of
66 {groups: task list Inttab.table, (*groups with presently active members*)
67 jobs: jobs, (*job dependency graph*)
68 focus: task list}; (*particular collection of high-priority tasks*)
70 fun make_queue groups jobs focus = Queue {groups = groups, jobs = jobs, focus = focus};
72 val empty = make_queue Inttab.empty IntGraph.empty [];
73 fun is_empty (Queue {jobs, ...}) = IntGraph.is_empty jobs;
78 fun enqueue (group as Group (gid, _)) deps pri job (Queue {groups, jobs, focus}) =
82 val groups' = Inttab.cons_list (gid, task) groups;
84 |> IntGraph.new_node (id, (group, Job (pri, job))) |> fold (add_job task) deps;
85 in (task, make_queue groups' jobs' focus) end;
87 fun depend deps task (Queue {groups, jobs, focus}) =
88 make_queue groups (fold (add_job task) deps jobs) focus;
90 fun focus tasks (Queue {groups, jobs, ...}) =
91 make_queue groups jobs (map_filter (check_job jobs) tasks);
98 fun dequeue_result NONE queue = (NONE, queue)
99 | dequeue_result (SOME (result as (task, _, _))) (Queue {groups, jobs, focus}) =
100 (SOME result, make_queue groups (map_job task (K (Running (Thread.self ()))) jobs) focus);
102 fun dequeue_global req_pri (queue as Queue {jobs, ...}) =
104 fun ready (id, ((group as Group (_, ref ok), Job (pri, job)), ([], _))) =
105 if pri = req_pri then SOME (Task id, group, (fn () => job ok)) else NONE
107 in dequeue_result (IntGraph.get_first ready jobs) queue end;
109 fun dequeue_local focus (queue as Queue {jobs, ...}) =
112 (case IntGraph.get_node jobs id of
113 (group as Group (_, ref ok), Job (_, job)) =>
114 if null (IntGraph.imm_preds jobs id) then SOME (Task id, group, (fn () => job ok))
117 val ids = map (fn Task id => id) focus;
118 in dequeue_result (get_first ready (IntGraph.all_preds jobs ids)) queue end;
122 fun dequeue (queue as Queue {focus, ...}) =
123 (case dequeue_local focus queue of
125 (case dequeue_global true queue of (NONE, _) => dequeue_global false queue | res => res)
128 fun dequeue_towards tasks (queue as Queue {jobs, ...}) =
129 dequeue_local (map_filter (check_job jobs) tasks) queue;
134 (* sporadic interrupts *)
136 fun interrupt_thread thread = Thread.interrupt thread handle Thread _ => ();
138 fun interrupt (Queue {jobs, ...}) task =
139 (case try (get_job jobs) task of SOME (Running thread) => interrupt_thread thread | _ => ());
141 fun interrupt_external queue str =
142 (case Int.fromString str of SOME id => interrupt queue (Task id) | NONE => ());
145 (* misc operations *)
147 fun cancel (Queue {groups, jobs, ...}) (Group (gid, ok)) =
149 val _ = ok := false; (*invalidate any future group members*)
150 val tasks = Inttab.lookup_list groups gid;
151 val running = fold (get_job jobs #> (fn Running thread => cons thread | _ => I)) tasks [];
152 val _ = List.app interrupt_thread running;
155 fun finish (task as Task id) (Queue {groups, jobs, focus}) =
157 val Group (gid, _) = get_group jobs task;
158 val groups' = Inttab.remove_list (op =) (gid, task) groups;
159 val jobs' = IntGraph.del_node id jobs;
160 val focus' = remove (op =) task focus;
161 in make_queue groups' jobs' focus' end;