src/Pure/Concurrent/task_queue.ML
author wenzelm
Mon, 27 Jul 2009 12:24:27 +0200
changeset 32221 fcbd6c9ee9bb
parent 32218 222f26693757
child 32222 572b92362496
permissions -rw-r--r--
tuned signature;
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
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    14
  val new_group: group option -> group
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    15
  val group_id: group -> int
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
    16
  val eq_group: group * group -> bool
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    17
  val cancel_group: group -> exn -> unit
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    18
  val is_canceled: group -> bool
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    19
  val group_status: group -> exn list
28179
8e8313aededc human-readable printing of TaskQueue.task/group;
wenzelm
parents: 28176
diff changeset
    20
  val str_of_group: group -> string
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    21
  type queue
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    22
  val empty: queue
28204
2d93b158ad99 added is_empty;
wenzelm
parents: 28202
diff changeset
    23
  val is_empty: queue -> bool
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    24
  val status: queue -> {ready: int, pending: int, running: int}
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    25
  val cancel: queue -> group -> bool
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    26
  val cancel_all: queue -> group list
32218
222f26693757 enqueue/finish: return minimal/maximal state of this task;
wenzelm
parents: 32192
diff changeset
    27
  val enqueue: group -> task list -> int -> (bool -> bool) -> queue -> (task * bool) * queue
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    28
  val extend: task -> (bool -> bool) -> queue -> queue option
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    29
  val dequeue: queue -> (task * group * (bool -> bool) list) option * queue
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
    30
  val dequeue_towards: task list -> queue ->
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
    31
    (((task * group * (bool -> bool) list) * task list) option * queue)
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    32
  val finish: task -> queue -> bool * queue
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    33
  val interrupt: queue -> task -> unit
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    34
  val interrupt_external: queue -> string -> unit
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    35
end;
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    36
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
    37
structure Task_Queue:> TASK_QUEUE =
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    38
struct
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
(* tasks *)
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    41
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    42
datatype task = Task of int * serial;
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    43
fun new_task pri = Task (pri, serial ());
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    44
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    45
fun pri_of_task (Task (pri, _)) = pri;
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    46
fun str_of_task (Task (_, i)) = string_of_int i;
28998
23cbaa9f9834 added new_task;
wenzelm
parents: 28551
diff changeset
    47
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    48
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
    49
structure Task_Graph = Graph(type key = task val ord = task_ord);
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    50
28998
23cbaa9f9834 added new_task;
wenzelm
parents: 28551
diff changeset
    51
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    52
(* nested groups *)
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    53
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    54
datatype group = Group of
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    55
 {parent: group option,
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    56
  id: serial,
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    57
  status: exn list ref};
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    58
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    59
fun make_group (parent, id, status) = Group {parent = parent, id = id, status = status};
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    60
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    61
fun new_group parent = make_group (parent, serial (), ref []);
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    62
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    63
fun group_id (Group {id, ...}) = id;
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    64
fun eq_group (group1, group2) = group_id group1 = group_id group2;
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
    65
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    66
fun group_ancestry (Group {parent, id, ...}) =
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    67
  id :: (case parent of NONE => [] | SOME group => group_ancestry group);
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    68
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    69
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    70
(* group status *)
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    71
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    72
fun cancel_group (Group {status, ...}) exn = CRITICAL (fn () =>
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    73
  (case exn of
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    74
    Exn.Interrupt => if null (! status) then status := [exn] else ()
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    75
  | _ => change status (cons exn)));
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    76
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    77
fun is_canceled (Group {parent, status, ...}) = (*non-critical*)
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    78
  not (null (! status)) orelse (case parent of NONE => false | SOME group => is_canceled group);
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    79
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    80
fun group_status (Group {parent, status, ...}) = (*non-critical*)
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    81
  ! status @ (case parent of NONE => [] | SOME group => group_status group);
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    82
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    83
fun str_of_group group =
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    84
  (is_canceled group ? enclose "(" ")") (string_of_int (group_id group));
28179
8e8313aededc human-readable printing of TaskQueue.task/group;
wenzelm
parents: 28176
diff changeset
    85
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    86
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    87
(* jobs *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    88
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    89
datatype job =
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    90
  Job of (bool -> bool) list |
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    91
  Running of Thread.thread;
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    92
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    93
type jobs = (group * job) Task_Graph.T;
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    94
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    95
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
    96
fun get_job (jobs: jobs) task = #2 (Task_Graph.get_node jobs task);
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    97
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
    98
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    99
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   100
(* queue of grouped jobs *)
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   101
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
   102
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
   103
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   104
datatype queue = Queue of
28184
5ed5cb73a2e9 eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents: 28179
diff changeset
   105
 {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
   106
  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
   107
  cache: result};                   (*last dequeue result*)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   108
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
   109
fun make_queue groups jobs cache = Queue {groups = groups, jobs = jobs, cache = cache};
28204
2d93b158ad99 added is_empty;
wenzelm
parents: 28202
diff changeset
   110
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
   111
val empty = make_queue Inttab.empty Task_Graph.empty No_Result;
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   112
fun is_empty (Queue {jobs, ...}) = Task_Graph.is_empty jobs;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   113
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   114
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   115
(* queue status *)
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   116
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   117
fun status (Queue {jobs, ...}) =
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   118
  let
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   119
    val (x, y, z) =
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   120
      Task_Graph.fold (fn (task, ((_, job), (deps, _))) => fn (x, y, z) =>
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   121
          (case job of
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   122
            Job _ => if null deps then (x + 1, y, z) else (x, y + 1, z)
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   123
          | Running _ => (x, y, z + 1)))
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   124
        jobs (0, 0, 0);
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   125
  in {ready = x, pending = y, running = z} end;
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   126
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   127
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   128
(* cancel -- peers and sub-groups *)
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   129
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   130
fun cancel (Queue {groups, jobs, ...}) group =
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   131
  let
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   132
    val _ = cancel_group group Exn.Interrupt;
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   133
    val tasks = Inttab.lookup_list groups (group_id group);
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   134
    val running =
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   135
      fold (get_job jobs #> (fn Running t => insert Thread.equal t | _ => I)) tasks [];
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   136
    val _ = List.app SimpleThread.interrupt running;
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   137
  in null running end;
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   138
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   139
fun cancel_all (Queue {jobs, ...}) =
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   140
  let
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   141
    fun cancel_job (group, job) (groups, running) =
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   142
      (cancel_group group Exn.Interrupt;
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   143
        (case job of Running t => (insert eq_group group groups, insert Thread.equal t running)
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   144
        | _ => (groups, running)));
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   145
    val (groups, running) = Task_Graph.fold (cancel_job o #1 o #2) jobs ([], []);
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   146
    val _ = List.app SimpleThread.interrupt running;
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   147
  in groups end;
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   148
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   149
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   150
(* enqueue *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   151
32190
4fc7a882b41e enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents: 32101
diff changeset
   152
fun add_job task dep (jobs: jobs) =
4fc7a882b41e enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents: 32101
diff changeset
   153
  Task_Graph.add_edge (dep, task) jobs handle Task_Graph.UNDEF _ => jobs;
4fc7a882b41e enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents: 32101
diff changeset
   154
4fc7a882b41e enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents: 32101
diff changeset
   155
fun get_deps (jobs: jobs) task =
4fc7a882b41e enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents: 32101
diff changeset
   156
  Task_Graph.imm_preds jobs task handle Task_Graph.UNDEF _ => [];
4fc7a882b41e enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents: 32101
diff changeset
   157
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   158
fun enqueue group deps pri job (Queue {groups, jobs, cache}) =
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   159
  let
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   160
    val task = new_task pri;
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   161
    val groups' = groups
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   162
      |> fold (fn gid => Inttab.cons_list (gid, task)) (group_ancestry group);
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   163
    val jobs' = jobs
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   164
      |> Task_Graph.new_node (task, (group, Job [job]))
32190
4fc7a882b41e enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents: 32101
diff changeset
   165
      |> fold (add_job task) deps
4fc7a882b41e enqueue: maintain transitive closure, which simplifies dequeue_towards;
wenzelm
parents: 32101
diff changeset
   166
      |> fold (fold (add_job task) o get_deps jobs) deps;
32218
222f26693757 enqueue/finish: return minimal/maximal state of this task;
wenzelm
parents: 32192
diff changeset
   167
    val minimal = null (get_deps jobs' task);
31632
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   168
    val cache' =
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   169
      (case cache of
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   170
        Result last =>
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   171
          if task_ord (last, task) = LESS
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   172
          then cache else Unknown
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   173
      | _ => Unknown);
32218
222f26693757 enqueue/finish: return minimal/maximal state of this task;
wenzelm
parents: 32192
diff changeset
   174
  in ((task, minimal), make_queue groups' jobs' cache') end;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   175
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
   176
fun extend task job (Queue {groups, jobs, cache}) =
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   177
  (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
   178
    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
   179
  | _ => NONE);
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   180
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   181
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   182
(* dequeue *)
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   183
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
   184
fun dequeue (queue as Queue {groups, jobs, cache}) =
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   185
  let
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   186
    fun ready (task, ((group, Job list), ([], _))) = SOME (task, group, rev list)
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   187
      | 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
   188
    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
   189
      (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
   190
        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
   191
      | 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
   192
          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
   193
            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
   194
            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
   195
          in (SOME result, make_queue groups jobs' cache') end);
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   196
  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
   197
    (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
   198
      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
   199
    | 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
   200
    | No_Result => (NONE, queue))
28384
70abca69247b dequeue_towards: return bound for unfinished tasks;
wenzelm
parents: 28332
diff changeset
   201
  end;
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   202
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   203
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   204
(* dequeue_towards -- adhoc dependencies *)
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   205
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   206
fun dequeue_towards deps (queue as Queue {groups, jobs, ...}) =
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   207
  let
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   208
    fun ready task =
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   209
      (case Task_Graph.get_node jobs task of
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   210
        (group, Job list) =>
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   211
          if null (Task_Graph.imm_preds jobs task)
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   212
          then SOME (task, group, rev list)
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   213
          else NONE
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   214
      | _ => NONE);
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   215
    val tasks = filter (can (Task_Graph.get_node jobs)) deps;
32192
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   216
    fun result (res as (task, _, _)) =
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   217
      let
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   218
        val jobs' = set_job task (Running (Thread.self ())) jobs;
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   219
        val cache' = Unknown;
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   220
      in (SOME (res, tasks), make_queue groups jobs' cache') end;
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   221
  in
32093
30996b775a7f tuned dequeu_towards: try immediate tasks before expensive all_preds;
wenzelm
parents: 32055
diff changeset
   222
    (case get_first ready tasks of
32192
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   223
      SOME res => result res
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   224
    | NONE =>
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   225
        (case get_first (get_first ready o Task_Graph.imm_preds jobs) tasks of
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   226
          SOME res => result res
eb09607094b3 dequeue_towards: need to try imm_preds as well;
wenzelm
parents: 32190
diff changeset
   227
        | NONE => (NONE, queue)))
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   228
  end;
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   229
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   230
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   231
(* finish *)
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   232
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   233
fun finish task (Queue {groups, jobs, cache}) =
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   234
  let
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   235
    val group = get_group jobs task;
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   236
    val groups' = groups
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   237
      |> fold (fn gid => Inttab.remove_list (op =) (gid, task)) (group_ancestry group);
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   238
    val jobs' = Task_Graph.del_node task jobs;
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   239
    val maximal = null (Task_Graph.imm_succs jobs task);
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   240
    val cache' = if maximal then cache else Unknown;
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   241
  in (maximal, make_queue groups' jobs' cache') end;
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   242
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   243
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   244
(* sporadic interrupts *)
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   245
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   246
fun interrupt (Queue {jobs, ...}) task =
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   247
  (case try (get_job jobs) task of
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   248
    SOME (Running thread) => SimpleThread.interrupt thread
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   249
  | _ => ());
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   250
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   251
fun interrupt_external (queue as Queue {jobs, ...}) str =
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   252
  (case Int.fromString str of
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   253
    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
   254
      (case Task_Graph.get_first NONE
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   255
          (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
   256
        of SOME task => interrupt queue task | NONE => ())
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   257
  | NONE => ());
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   258
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   259
end;