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