src/Pure/Concurrent/task_queue.ML
author wenzelm
Thu Jul 09 22:01:41 2009 +0200 (2009-07-09)
changeset 31971 8c1b845ed105
parent 31632 fae680e35958
child 32052 8c391a12df1d
permissions -rw-r--r--
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm@28165
     1
(*  Title:      Pure/Concurrent/task_queue.ML
wenzelm@28165
     2
    Author:     Makarius
wenzelm@28165
     3
wenzelm@28165
     4
Ordered queue of grouped tasks.
wenzelm@28165
     5
*)
wenzelm@28165
     6
wenzelm@28165
     7
signature TASK_QUEUE =
wenzelm@28165
     8
sig
wenzelm@29340
     9
  type task
wenzelm@29121
    10
  val new_task: int -> task
wenzelm@29121
    11
  val pri_of_task: task -> int
wenzelm@28196
    12
  val str_of_task: task -> string
wenzelm@29340
    13
  type group
wenzelm@29340
    14
  val eq_group: group * group -> bool
wenzelm@28165
    15
  val new_group: unit -> group
wenzelm@29365
    16
  val is_valid: group -> bool
wenzelm@28551
    17
  val invalidate_group: group -> unit
wenzelm@28179
    18
  val str_of_group: group -> string
wenzelm@28165
    19
  type queue
wenzelm@28165
    20
  val empty: queue
wenzelm@28204
    21
  val is_empty: queue -> bool
wenzelm@29121
    22
  val enqueue: group -> task list -> int -> (bool -> bool) -> queue -> task * queue
wenzelm@29365
    23
  val extend: task -> (bool -> bool) -> queue -> queue option
wenzelm@29365
    24
  val dequeue: queue -> (task * group * (bool -> bool) list) option * queue
wenzelm@28190
    25
  val interrupt: queue -> task -> unit
wenzelm@28190
    26
  val interrupt_external: queue -> string -> unit
wenzelm@29340
    27
  val cancel: queue -> group -> bool
wenzelm@29340
    28
  val cancel_all: queue -> group list
wenzelm@28176
    29
  val finish: task -> queue -> queue
wenzelm@28165
    30
end;
wenzelm@28165
    31
wenzelm@29340
    32
structure Task_Queue:> TASK_QUEUE =
wenzelm@28165
    33
struct
wenzelm@28165
    34
wenzelm@29121
    35
(* tasks *)
wenzelm@29121
    36
wenzelm@29121
    37
datatype task = Task of int * serial;
wenzelm@29121
    38
fun new_task pri = Task (pri, serial ());
wenzelm@28165
    39
wenzelm@29121
    40
fun pri_of_task (Task (pri, _)) = pri;
wenzelm@29121
    41
fun str_of_task (Task (_, i)) = string_of_int i;
wenzelm@28998
    42
wenzelm@29121
    43
fun task_ord (Task t1, Task t2) = prod_ord (rev_order o int_ord) int_ord (t1, t2);
wenzelm@31971
    44
structure Task_Graph = Graph(type key = task val ord = task_ord);
wenzelm@28165
    45
wenzelm@28998
    46
wenzelm@29121
    47
(* groups *)
wenzelm@29121
    48
wenzelm@28190
    49
datatype group = Group of serial * bool ref;
wenzelm@29340
    50
fun eq_group (Group (gid1, _), Group (gid2, _)) = gid1 = gid2;
wenzelm@28551
    51
wenzelm@28190
    52
fun new_group () = Group (serial (), ref true);
wenzelm@29121
    53
wenzelm@29365
    54
fun is_valid (Group (_, ref ok)) = ok;
wenzelm@28551
    55
fun invalidate_group (Group (_, ok)) = ok := false;
wenzelm@28165
    56
wenzelm@28190
    57
fun str_of_group (Group (i, ref ok)) =
wenzelm@28190
    58
  if ok then string_of_int i else enclose "(" ")" (string_of_int i);
wenzelm@28179
    59
wenzelm@28165
    60
wenzelm@28176
    61
(* jobs *)
wenzelm@28165
    62
wenzelm@28165
    63
datatype job =
wenzelm@29365
    64
  Job of (bool -> bool) list |
wenzelm@28165
    65
  Running of Thread.thread;
wenzelm@28165
    66
wenzelm@29121
    67
type jobs = (group * job) Task_Graph.T;
wenzelm@28176
    68
wenzelm@29121
    69
fun get_group (jobs: jobs) task = #1 (Task_Graph.get_node jobs task);
wenzelm@29121
    70
fun get_job (jobs: jobs) task = #2 (Task_Graph.get_node jobs task);
wenzelm@29365
    71
fun set_job task job (jobs: jobs) = Task_Graph.map_node task (fn (group, _) => (group, job)) jobs;
wenzelm@28202
    72
wenzelm@29121
    73
fun add_job task dep (jobs: jobs) =
wenzelm@29121
    74
  Task_Graph.add_edge (dep, task) jobs handle Task_Graph.UNDEF _ => jobs;
wenzelm@29117
    75
wenzelm@28176
    76
wenzelm@28176
    77
(* queue of grouped jobs *)
wenzelm@28176
    78
wenzelm@31617
    79
datatype result = Unknown | Result of task | No_Result;
wenzelm@31617
    80
wenzelm@28165
    81
datatype queue = Queue of
wenzelm@28184
    82
 {groups: task list Inttab.table,   (*groups with presently active members*)
wenzelm@31617
    83
  jobs: jobs,                       (*job dependency graph*)
wenzelm@31617
    84
  cache: result};                   (*last dequeue result*)
wenzelm@28165
    85
wenzelm@31617
    86
fun make_queue groups jobs cache = Queue {groups = groups, jobs = jobs, cache = cache};
wenzelm@28204
    87
wenzelm@31617
    88
val empty = make_queue Inttab.empty Task_Graph.empty No_Result;
wenzelm@29121
    89
fun is_empty (Queue {jobs, ...}) = Task_Graph.is_empty jobs;
wenzelm@28165
    90
wenzelm@28165
    91
wenzelm@28185
    92
(* enqueue *)
wenzelm@28165
    93
wenzelm@31632
    94
fun enqueue (group as Group (gid, _)) deps pri job (Queue {groups, jobs, cache}) =
wenzelm@28165
    95
  let
wenzelm@29121
    96
    val task = new_task pri;
wenzelm@28176
    97
    val groups' = Inttab.cons_list (gid, task) groups;
wenzelm@28185
    98
    val jobs' = jobs
wenzelm@29365
    99
      |> Task_Graph.new_node (task, (group, Job [job])) |> fold (add_job task) deps;
wenzelm@31632
   100
    val cache' =
wenzelm@31632
   101
      (case cache of
wenzelm@31632
   102
        Result last =>
wenzelm@31632
   103
          if task_ord (last, task) = LESS
wenzelm@31632
   104
          then cache else Unknown
wenzelm@31632
   105
      | _ => Unknown);
wenzelm@31632
   106
  in (task, make_queue groups' jobs' cache') end;
wenzelm@28165
   107
wenzelm@31617
   108
fun extend task job (Queue {groups, jobs, cache}) =
wenzelm@29365
   109
  (case try (get_job jobs) task of
wenzelm@31617
   110
    SOME (Job list) => SOME (make_queue groups (set_job task (Job (job :: list)) jobs) cache)
wenzelm@29365
   111
  | _ => NONE);
wenzelm@29365
   112
wenzelm@28185
   113
wenzelm@28185
   114
(* dequeue *)
wenzelm@28185
   115
wenzelm@31617
   116
fun dequeue (queue as Queue {groups, jobs, cache}) =
wenzelm@29121
   117
  let
wenzelm@29365
   118
    fun ready (task, ((group, Job list), ([], _))) = SOME (task, group, rev list)
wenzelm@29121
   119
      | ready _ = NONE;
wenzelm@31617
   120
    fun deq boundary =
wenzelm@31617
   121
      (case Task_Graph.get_first boundary ready jobs of
wenzelm@31617
   122
        NONE => (NONE, make_queue groups jobs No_Result)
wenzelm@31617
   123
      | SOME (result as (task, _, _)) =>
wenzelm@31617
   124
          let
wenzelm@31617
   125
            val jobs' = set_job task (Running (Thread.self ())) jobs;
wenzelm@31617
   126
            val cache' = Result task;
wenzelm@31617
   127
          in (SOME result, make_queue groups jobs' cache') end);
wenzelm@29121
   128
  in
wenzelm@31617
   129
    (case cache of
wenzelm@31617
   130
      Unknown => deq NONE
wenzelm@31617
   131
    | Result last => deq (SOME last)
wenzelm@31617
   132
    | No_Result => (NONE, queue))
wenzelm@28384
   133
  end;
wenzelm@28202
   134
wenzelm@28165
   135
wenzelm@28190
   136
(* sporadic interrupts *)
wenzelm@28190
   137
wenzelm@28190
   138
fun interrupt (Queue {jobs, ...}) task =
wenzelm@28551
   139
  (case try (get_job jobs) task of SOME (Running thread) => SimpleThread.interrupt thread | _ => ());
wenzelm@28190
   140
wenzelm@29121
   141
fun interrupt_external (queue as Queue {jobs, ...}) str =
wenzelm@29121
   142
  (case Int.fromString str of
wenzelm@29121
   143
    SOME i =>
wenzelm@31617
   144
      (case Task_Graph.get_first NONE
wenzelm@29121
   145
          (fn (task as Task (_, j), _) => if i = j then SOME task else NONE) jobs
wenzelm@29121
   146
        of SOME task => interrupt queue task | NONE => ())
wenzelm@29121
   147
  | NONE => ());
wenzelm@28190
   148
wenzelm@28190
   149
wenzelm@29340
   150
(* termination *)
wenzelm@28165
   151
wenzelm@28551
   152
fun cancel (Queue {groups, jobs, ...}) (group as Group (gid, _)) =
wenzelm@28176
   153
  let
wenzelm@28551
   154
    val _ = invalidate_group group;
wenzelm@28176
   155
    val tasks = Inttab.lookup_list groups gid;
wenzelm@29342
   156
    val running = fold (get_job jobs #> (fn Running t => insert Thread.equal t | _ => I)) tasks [];
wenzelm@28551
   157
    val _ = List.app SimpleThread.interrupt running;
wenzelm@28190
   158
  in null running end;
wenzelm@28165
   159
wenzelm@29340
   160
fun cancel_all (Queue {jobs, ...}) =
wenzelm@29340
   161
  let
wenzelm@29340
   162
    fun cancel_job (group, job) (groups, running) =
wenzelm@29340
   163
      (invalidate_group group;
wenzelm@29342
   164
        (case job of Running t => (insert eq_group group groups, insert Thread.equal t running)
wenzelm@29340
   165
        | _ => (groups, running)));
wenzelm@29340
   166
    val (groups, running) = Task_Graph.fold (cancel_job o #1 o #2) jobs ([], []);
wenzelm@29340
   167
    val _ = List.app SimpleThread.interrupt running;
wenzelm@29340
   168
  in groups end;
wenzelm@29340
   169
wenzelm@31632
   170
fun finish task (Queue {groups, jobs, cache}) =
wenzelm@28176
   171
  let
wenzelm@28190
   172
    val Group (gid, _) = get_group jobs task;
wenzelm@28176
   173
    val groups' = Inttab.remove_list (op =) (gid, task) groups;
wenzelm@29121
   174
    val jobs' = Task_Graph.del_node task jobs;
wenzelm@31632
   175
    val cache' =
wenzelm@31632
   176
      if null (Task_Graph.imm_succs jobs task) then cache
wenzelm@31632
   177
      else Unknown;
wenzelm@31632
   178
  in make_queue groups' jobs' cache' end;
wenzelm@28165
   179
wenzelm@28165
   180
end;