src/Pure/Concurrent/task_queue.ML
author wenzelm
Tue, 16 Dec 2008 00:19:47 +0100
changeset 29117 5a79ec2fedfb
parent 28998 23cbaa9f9834
child 29121 ad73b63ae2c5
permissions -rw-r--r--
tuned enqueue: plain add_edge, acyclic not required here;
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
    ID:         $Id$
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
     4
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
     5
Ordered queue of grouped tasks.
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
     6
*)
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
     7
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
     8
signature TASK_QUEUE =
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
     9
sig
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    10
  eqtype task
28998
23cbaa9f9834 added new_task;
wenzelm
parents: 28551
diff changeset
    11
  val new_task: unit -> task
28196
wenzelm
parents: 28190
diff changeset
    12
  val str_of_task: task -> string
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    13
  eqtype group
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    14
  val new_group: unit -> group
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
    15
  val invalidate_group: group -> unit
28179
8e8313aededc human-readable printing of TaskQueue.task/group;
wenzelm
parents: 28176
diff changeset
    16
  val str_of_group: group -> string
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    17
  type queue
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    18
  val empty: queue
28204
2d93b158ad99 added is_empty;
wenzelm
parents: 28202
diff changeset
    19
  val is_empty: queue -> bool
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
    20
  val enqueue: group -> task list -> bool -> (bool -> bool) -> queue -> task * queue
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
    21
  val depend: task list -> task -> queue -> queue
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    22
  val focus: task list -> queue -> queue
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
    23
  val dequeue: queue -> (task * group * (unit -> bool)) option * queue
28384
70abca69247b dequeue_towards: return bound for unfinished tasks;
wenzelm
parents: 28332
diff changeset
    24
  val dequeue_towards: task list -> queue ->
70abca69247b dequeue_towards: return bound for unfinished tasks;
wenzelm
parents: 28332
diff changeset
    25
    (((task * group * (unit -> bool)) * task list) option * queue)
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    26
  val interrupt: queue -> task -> unit
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    27
  val interrupt_external: queue -> string -> unit
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    28
  val finish: task -> queue -> queue
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    29
  val cancel: queue -> group -> bool
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    30
end;
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    31
28171
9b2f9cc9ff4b proper signature constraint;
wenzelm
parents: 28168
diff changeset
    32
structure TaskQueue: 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
28168
ba410235ff04 moved thread data to future.ML (again);
wenzelm
parents: 28165
diff changeset
    35
(* identifiers *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    36
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    37
datatype task = Task of serial;
28998
23cbaa9f9834 added new_task;
wenzelm
parents: 28551
diff changeset
    38
fun new_task () = Task (serial ());
23cbaa9f9834 added new_task;
wenzelm
parents: 28551
diff changeset
    39
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    40
fun str_of_task (Task i) = string_of_int i;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    41
28998
23cbaa9f9834 added new_task;
wenzelm
parents: 28551
diff changeset
    42
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    43
datatype group = Group of serial * bool ref;
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
    44
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    45
fun new_group () = Group (serial (), ref true);
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
    46
fun invalidate_group (Group (_, ok)) = ok := false;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    47
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    48
fun str_of_group (Group (i, ref ok)) =
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    49
  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
    50
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    51
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    52
(* jobs *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    53
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    54
datatype job =
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
    55
  Job of bool * (bool -> bool) |   (*priority, job: status -> status*)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    56
  Running of Thread.thread;
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    57
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    58
type jobs = (group * job) IntGraph.T;
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    59
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    60
fun get_group (jobs: jobs) (Task id) = #1 (IntGraph.get_node jobs id);
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    61
fun get_job (jobs: jobs) (Task id) = #2 (IntGraph.get_node jobs id);
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    62
fun map_job (Task id) f (jobs: jobs) = IntGraph.map_node id (apsnd f) jobs;
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    63
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
    64
fun add_job (Task id) (Task dep) (jobs: jobs) =
29117
5a79ec2fedfb tuned enqueue: plain add_edge, acyclic not required here;
wenzelm
parents: 28998
diff changeset
    65
  IntGraph.add_edge (dep, id) jobs handle IntGraph.UNDEF _ => jobs;
5a79ec2fedfb tuned enqueue: plain add_edge, acyclic not required here;
wenzelm
parents: 28998
diff changeset
    66
5a79ec2fedfb tuned enqueue: plain add_edge, acyclic not required here;
wenzelm
parents: 28998
diff changeset
    67
fun add_job_acyclic (Task id) (Task dep) (jobs: jobs) =
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
    68
  IntGraph.add_edge_acyclic (dep, id) jobs handle IntGraph.UNDEF _ => jobs;
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    69
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    70
fun check_job (jobs: jobs) (task as Task id) =
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    71
  if can (IntGraph.get_node jobs) id then SOME task else NONE;
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    72
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    73
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    74
(* queue of grouped jobs *)
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    75
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    76
datatype queue = Queue of
28184
5ed5cb73a2e9 eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents: 28179
diff changeset
    77
 {groups: task list Inttab.table,   (*groups with presently active members*)
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    78
  jobs: jobs,                       (*job dependency graph*)
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    79
  focus: task list};                (*particular collection of high-priority tasks*)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    80
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    81
fun make_queue groups jobs focus = Queue {groups = groups, jobs = jobs, focus = focus};
28204
2d93b158ad99 added is_empty;
wenzelm
parents: 28202
diff changeset
    82
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    83
val empty = make_queue Inttab.empty IntGraph.empty [];
28204
2d93b158ad99 added is_empty;
wenzelm
parents: 28202
diff changeset
    84
fun is_empty (Queue {jobs, ...}) = IntGraph.is_empty jobs;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    85
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    86
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
    87
(* enqueue *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    88
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
    89
fun enqueue (group as Group (gid, _)) deps pri job (Queue {groups, jobs, focus}) =
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    90
  let
28998
23cbaa9f9834 added new_task;
wenzelm
parents: 28551
diff changeset
    91
    val task as Task id = new_task ();
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    92
    val groups' = Inttab.cons_list (gid, task) groups;
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
    93
    val jobs' = jobs
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
    94
      |> IntGraph.new_node (id, (group, Job (pri, job))) |> fold (add_job task) deps;
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    95
  in (task, make_queue groups' jobs' focus) end;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    96
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    97
fun depend deps task (Queue {groups, jobs, focus}) =
29117
5a79ec2fedfb tuned enqueue: plain add_edge, acyclic not required here;
wenzelm
parents: 28998
diff changeset
    98
  make_queue groups (fold (add_job_acyclic task) deps jobs) focus;
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
    99
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   100
fun focus tasks (Queue {groups, jobs, ...}) =
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   101
  make_queue groups jobs (map_filter (check_job jobs) tasks);
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   102
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   103
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   104
(* dequeue *)
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   105
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   106
local
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   107
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   108
fun dequeue_result NONE queue = (NONE, queue)
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   109
  | dequeue_result (SOME (result as (task, _, _))) (Queue {groups, jobs, focus}) =
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   110
      (SOME result, make_queue groups (map_job task (K (Running (Thread.self ()))) jobs) focus);
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   111
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
   112
fun dequeue_global req_pri (queue as Queue {jobs, ...}) =
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   113
  let
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
   114
    fun ready (id, ((group as Group (_, ref ok), Job (pri, job)), ([], _))) =
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
   115
          if pri = req_pri then SOME (Task id, group, (fn () => job ok)) else NONE
28184
5ed5cb73a2e9 eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents: 28179
diff changeset
   116
      | ready _ = NONE;
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   117
  in dequeue_result (IntGraph.get_first ready jobs) queue end;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   118
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   119
fun dequeue_local focus (queue as Queue {jobs, ...}) =
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   120
  let
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   121
    fun ready id =
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   122
      (case IntGraph.get_node jobs id of
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
   123
        (group as Group (_, ref ok), Job (_, job)) =>
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   124
          if null (IntGraph.imm_preds jobs id) then SOME (Task id, group, (fn () => job ok))
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   125
          else NONE
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   126
      | _ => NONE);
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   127
    val ids = map (fn Task id => id) focus;
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   128
  in dequeue_result (get_first ready (IntGraph.all_preds jobs ids)) queue end;
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   129
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   130
in
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   131
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   132
fun dequeue (queue as Queue {focus, ...}) =
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
   133
  (case dequeue_local focus queue of
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
   134
    (NONE, _) =>
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
   135
      (case dequeue_global true queue of (NONE, _) => dequeue_global false queue | res => res)
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28204
diff changeset
   136
  | res => res);
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   137
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   138
fun dequeue_towards tasks (queue as Queue {jobs, ...}) =
28384
70abca69247b dequeue_towards: return bound for unfinished tasks;
wenzelm
parents: 28332
diff changeset
   139
  let val tasks' = map_filter (check_job jobs) tasks in
70abca69247b dequeue_towards: return bound for unfinished tasks;
wenzelm
parents: 28332
diff changeset
   140
    (case dequeue_local tasks' queue of
70abca69247b dequeue_towards: return bound for unfinished tasks;
wenzelm
parents: 28332
diff changeset
   141
      (NONE, queue') => (NONE, queue')
70abca69247b dequeue_towards: return bound for unfinished tasks;
wenzelm
parents: 28332
diff changeset
   142
    | (SOME work, queue') => (SOME (work, tasks'), queue'))
70abca69247b dequeue_towards: return bound for unfinished tasks;
wenzelm
parents: 28332
diff changeset
   143
  end;
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   144
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   145
end;
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   146
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   147
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   148
(* sporadic interrupts *)
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   149
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   150
fun interrupt (Queue {jobs, ...}) task =
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
   151
  (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
   152
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   153
fun interrupt_external queue str =
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   154
  (case Int.fromString str of SOME id => interrupt queue (Task id) | NONE => ());
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   155
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   156
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   157
(* misc operations *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   158
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
   159
fun cancel (Queue {groups, jobs, ...}) (group as Group (gid, _)) =
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   160
  let
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
   161
    val _ = invalidate_group group;
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   162
    val tasks = Inttab.lookup_list groups gid;
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   163
    val running = fold (get_job jobs #> (fn Running thread => cons thread | _ => I)) tasks [];
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
   164
    val _ = List.app SimpleThread.interrupt running;
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   165
  in null running end;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   166
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   167
fun finish (task as Task id) (Queue {groups, jobs, focus}) =
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   168
  let
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   169
    val Group (gid, _) = get_group jobs task;
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   170
    val groups' = Inttab.remove_list (op =) (gid, task) groups;
28332
c33c8ad8de70 IntGraph.del_node;
wenzelm
parents: 28304
diff changeset
   171
    val jobs' = IntGraph.del_node id jobs;
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   172
    val focus' = remove (op =) task focus;
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   173
  in make_queue groups' jobs' focus' end;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   174
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   175
end;