src/Pure/Concurrent/task_queue.ML
author wenzelm
Wed, 02 Feb 2011 13:38:09 +0100
changeset 42561 b5d7b15166bf
parent 42560 a4c822915eaa
child 42562 44a2e0db281f
permissions -rw-r--r--
Future.join_results: discontinued post-hoc recording of dynamic dependencies;
abstract Task_Queue.deps;
tuned signature;
tuned;
     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
     9   type task
    10   val dummy_task: task
    11   val name_of_task: task -> string
    12   val pri_of_task: task -> int
    13   val str_of_task: task -> string
    14   val timing_of_task: task -> Time.time * Time.time * string list
    15   type group
    16   val new_group: group option -> group
    17   val group_id: group -> int
    18   val eq_group: group * group -> bool
    19   val cancel_group: group -> exn -> unit
    20   val is_canceled: group -> bool
    21   val group_status: group -> exn list
    22   val str_of_group: group -> string
    23   type queue
    24   val empty: queue
    25   val all_passive: queue -> bool
    26   val status: queue -> {ready: int, pending: int, running: int, passive: int}
    27   val cancel: queue -> group -> bool
    28   val cancel_all: queue -> group list
    29   val finish: task -> queue -> bool * queue
    30   val enqueue_passive: group -> (unit -> bool) -> queue -> task * queue
    31   val enqueue: string -> group -> task list -> int -> (bool -> bool) ->
    32     queue -> (task * bool) * queue
    33   val extend: task -> (bool -> bool) -> queue -> queue option
    34   val dequeue_passive: Thread.thread -> task -> queue -> bool * queue
    35   val dequeue: Thread.thread -> queue -> (task * group * (bool -> bool) list) option * queue
    36   type deps
    37   val init_deps: task list -> deps
    38   val finished_deps: deps -> bool
    39   val dequeue_deps: Thread.thread -> deps -> queue ->
    40     (((task * group * (bool -> bool) list) option * deps) * queue)
    41   val running: task -> (unit -> 'a) -> 'a
    42   val joining: task -> (unit -> 'a) -> 'a
    43   val waiting: task -> deps -> (unit -> 'a) -> 'a
    44 end;
    45 
    46 structure Task_Queue: TASK_QUEUE =
    47 struct
    48 
    49 val new_id = Synchronized.counter ();
    50 
    51 
    52 (** grouped tasks **)
    53 
    54 (* tasks *)
    55 
    56 type timing = Time.time * Time.time * string list;  (*run, wait, wait dependencies*)
    57 
    58 fun new_timing () =
    59   Synchronized.var "timing" ((Time.zeroTime, Time.zeroTime, []): timing);
    60 
    61 abstype task = Task of
    62  {name: string,
    63   id: int,
    64   pri: int option,
    65   timing: timing Synchronized.var}
    66 with
    67 
    68 val dummy_task = Task {name = "", id = ~1, pri = NONE, timing = new_timing ()};
    69 fun new_task name pri = Task {name = name, id = new_id (), pri = pri, timing = new_timing ()};
    70 
    71 fun name_of_task (Task {name, ...}) = name;
    72 fun pri_of_task (Task {pri, ...}) = the_default 0 pri;
    73 fun str_of_task (Task {name, id, ...}) =
    74   if name = "" then string_of_int id else string_of_int id ^ " (" ^ name ^ ")";
    75 
    76 fun timing_of_task (Task {timing, ...}) = Synchronized.value timing;
    77 
    78 fun update_timing update (Task {timing, ...}) e =
    79   let
    80     val start = Time.now ();
    81     val result = Exn.capture e ();
    82     val t = Time.- (Time.now (), start);
    83     val _ = Synchronized.change timing (update t);
    84   in Exn.release result end;
    85 
    86 fun task_ord (Task {id = id1, pri = pri1, ...}, Task {id = id2, pri = pri2, ...}) =
    87   prod_ord (rev_order o option_ord int_ord) int_ord ((pri1, id1), (pri2, id2));
    88 
    89 val eq_task = is_equal o task_ord;
    90 
    91 end;
    92 
    93 structure Task_Graph = Graph(type key = task val ord = task_ord);
    94 
    95 
    96 (* nested groups *)
    97 
    98 abstype group = Group of
    99  {parent: group option,
   100   id: int,
   101   status: exn list Synchronized.var}
   102 with
   103 
   104 fun make_group (parent, id, status) = Group {parent = parent, id = id, status = status};
   105 
   106 fun new_group parent = make_group (parent, new_id (), Synchronized.var "group" []);
   107 
   108 fun group_id (Group {id, ...}) = id;
   109 fun eq_group (group1, group2) = group_id group1 = group_id group2;
   110 
   111 fun group_ancestry (Group {parent, id, ...}) =
   112   id :: (case parent of NONE => [] | SOME group => group_ancestry group);
   113 
   114 
   115 (* group status *)
   116 
   117 fun cancel_group (Group {status, ...}) exn =
   118   Synchronized.change status
   119     (fn exns =>
   120       if not (Exn.is_interrupt exn) orelse null exns then exn :: exns
   121       else exns);
   122 
   123 fun is_canceled (Group {parent, status, ...}) =
   124   not (null (Synchronized.value status)) orelse
   125     (case parent of NONE => false | SOME group => is_canceled group);
   126 
   127 fun group_status (Group {parent, status, ...}) =
   128   Synchronized.value status @
   129     (case parent of NONE => [] | SOME group => group_status group);
   130 
   131 fun str_of_group group =
   132   (is_canceled group ? enclose "(" ")") (string_of_int (group_id group));
   133 
   134 end;
   135 
   136 
   137 (** queue of jobs and groups **)
   138 
   139 (* jobs *)
   140 
   141 datatype job =
   142   Job of (bool -> bool) list |
   143   Running of Thread.thread |
   144   Passive of unit -> bool;
   145 
   146 type jobs = (group * job) Task_Graph.T;
   147 
   148 fun get_group (jobs: jobs) task = #1 (Task_Graph.get_node jobs task);
   149 fun get_job (jobs: jobs) task = #2 (Task_Graph.get_node jobs task);
   150 fun set_job task job (jobs: jobs) = Task_Graph.map_node task (fn (group, _) => (group, job)) jobs;
   151 
   152 fun add_job task dep (jobs: jobs) =
   153   Task_Graph.add_edge (dep, task) jobs handle Task_Graph.UNDEF _ => jobs;
   154 
   155 fun get_deps (jobs: jobs) task =
   156   Task_Graph.imm_preds jobs task handle Task_Graph.UNDEF _ => [];
   157 
   158 
   159 (* queue *)
   160 
   161 datatype queue = Queue of
   162  {groups: task list Inttab.table,   (*groups with presently active members*)
   163   jobs: jobs};                      (*job dependency graph*)
   164 
   165 fun make_queue groups jobs = Queue {groups = groups, jobs = jobs};
   166 
   167 val empty = make_queue Inttab.empty Task_Graph.empty;
   168 
   169 
   170 (* job status *)
   171 
   172 fun ready_job task ((group, Job list), ([], _)) = SOME (task, group, rev list)
   173   | ready_job task ((group, Passive abort), ([], _)) =
   174       if is_canceled group then SOME (task, group, [fn _ => abort ()])
   175       else NONE
   176   | ready_job _ _ = NONE;
   177 
   178 fun active_job (_, Job _) = SOME ()
   179   | active_job (_, Running _) = SOME ()
   180   | active_job (group, Passive _) = if is_canceled group then SOME () else NONE;
   181 
   182 fun all_passive (Queue {jobs, ...}) =
   183   is_none (Task_Graph.get_first (active_job o #1 o #2) jobs);
   184 
   185 
   186 (* queue status *)
   187 
   188 fun status (Queue {jobs, ...}) =
   189   let
   190     val (x, y, z, w) =
   191       Task_Graph.fold (fn (_, ((_, job), (deps, _))) => fn (x, y, z, w) =>
   192           (case job of
   193             Job _ => if null deps then (x + 1, y, z, w) else (x, y + 1, z, w)
   194           | Running _ => (x, y, z + 1, w)
   195           | Passive _ => (x, y, z, w + 1)))
   196         jobs (0, 0, 0, 0);
   197   in {ready = x, pending = y, running = z, passive = w} end;
   198 
   199 
   200 
   201 (** task queue operations **)
   202 
   203 (* cancel -- peers and sub-groups *)
   204 
   205 fun cancel (Queue {groups, jobs}) group =
   206   let
   207     val _ = cancel_group group Exn.Interrupt;
   208     val tasks = Inttab.lookup_list groups (group_id group);
   209     val running =
   210       fold (get_job jobs #> (fn Running t => insert Thread.equal t | _ => I)) tasks [];
   211     val _ = List.app Simple_Thread.interrupt running;
   212   in null running end;
   213 
   214 fun cancel_all (Queue {jobs, ...}) =
   215   let
   216     fun cancel_job (group, job) (groups, running) =
   217       (cancel_group group Exn.Interrupt;
   218         (case job of
   219           Running t => (insert eq_group group groups, insert Thread.equal t running)
   220         | _ => (groups, running)));
   221     val (running_groups, running) = Task_Graph.fold (cancel_job o #1 o #2) jobs ([], []);
   222     val _ = List.app Simple_Thread.interrupt running;
   223   in running_groups end;
   224 
   225 
   226 (* finish *)
   227 
   228 fun finish task (Queue {groups, jobs}) =
   229   let
   230     val group = get_group jobs task;
   231     val groups' = groups
   232       |> fold (fn gid => Inttab.remove_list eq_task (gid, task)) (group_ancestry group);
   233     val jobs' = Task_Graph.del_node task jobs;
   234     val maximal = null (Task_Graph.imm_succs jobs task);
   235   in (maximal, make_queue groups' jobs') end;
   236 
   237 
   238 (* enqueue *)
   239 
   240 fun enqueue_passive group abort (Queue {groups, jobs}) =
   241   let
   242     val task = new_task "passive" NONE;
   243     val groups' = groups
   244       |> fold (fn gid => Inttab.cons_list (gid, task)) (group_ancestry group);
   245     val jobs' = jobs |> Task_Graph.new_node (task, (group, Passive abort));
   246   in (task, make_queue groups' jobs') end;
   247 
   248 fun enqueue name group deps pri job (Queue {groups, jobs}) =
   249   let
   250     val task = new_task name (SOME pri);
   251     val groups' = groups
   252       |> fold (fn gid => Inttab.cons_list (gid, task)) (group_ancestry group);
   253     val jobs' = jobs
   254       |> Task_Graph.new_node (task, (group, Job [job]))
   255       |> fold (add_job task) deps
   256       |> fold (fold (add_job task) o get_deps jobs) deps;
   257     val minimal = null (get_deps jobs' task);
   258   in ((task, minimal), make_queue groups' jobs') end;
   259 
   260 fun extend task job (Queue {groups, jobs}) =
   261   (case try (get_job jobs) task of
   262     SOME (Job list) => SOME (make_queue groups (set_job task (Job (job :: list)) jobs))
   263   | _ => NONE);
   264 
   265 
   266 (* dequeue *)
   267 
   268 fun dequeue_passive thread task (queue as Queue {groups, jobs}) =
   269   (case try (get_job jobs) task of
   270     SOME (Passive _) =>
   271       let val jobs' = set_job task (Running thread) jobs
   272       in (true, make_queue groups jobs') end
   273   | _ => (false, queue));
   274 
   275 fun dequeue thread (queue as Queue {groups, jobs}) =
   276   (case Task_Graph.get_first (uncurry ready_job) jobs of
   277     SOME (result as (task, _, _)) =>
   278       let val jobs' = set_job task (Running thread) jobs
   279       in (SOME result, make_queue groups jobs') end
   280   | NONE => (NONE, queue));
   281 
   282 
   283 (* dequeue wrt. dynamic dependencies *)
   284 
   285 abstype deps = Deps of task list
   286 with
   287 
   288 fun init_deps tasks = Deps tasks;
   289 fun finished_deps (Deps tasks) = null tasks;
   290 
   291 fun insert_deps (Deps tasks) = fold (insert (op =) o name_of_task) tasks;
   292 
   293 fun dequeue_deps thread (Deps deps) (queue as Queue {groups, jobs}) =
   294   let
   295     fun ready task = ready_job task (Task_Graph.get_entry jobs task);
   296     val tasks = filter (can (Task_Graph.get_node jobs)) deps;
   297     fun result (res as (task, _, _)) =
   298       let val jobs' = set_job task (Running thread) jobs
   299       in ((SOME res, Deps tasks), make_queue groups jobs') end;
   300   in
   301     (case get_first ready tasks of
   302       SOME res => result res
   303     | NONE =>
   304         (case get_first (get_first ready o get_deps jobs) tasks of
   305           SOME res => result res
   306         | NONE => ((NONE, Deps tasks), queue)))
   307   end;
   308 
   309 end;
   310 
   311 
   312 (* timing *)
   313 
   314 fun running task =
   315   update_timing (fn t => fn (a, b, ds) => (Time.+ (a, t), b, ds)) task;
   316 
   317 fun joining task =
   318   update_timing (fn t => fn (a, b, ds) => (Time.- (a, t), b, ds)) task;
   319 
   320 fun waiting task deps =
   321   update_timing (fn t => fn (a, b, ds) => (Time.- (a, t), Time.+ (b, t), insert_deps deps ds)) task;
   322 
   323 end;