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