src/Pure/Concurrent/task_queue.ML
author wenzelm
Wed, 02 Feb 2011 17:26:07 +0100
changeset 41684 b828d4b53386
parent 41683 73dde8006820
child 41695 afdbec23b92b
permissions -rw-r--r--
refined Task_Queue.dequeue_deps (more incremental);
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 group
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    10
  val new_group: group option -> group
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    11
  val group_id: group -> int
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
    12
  val eq_group: group * group -> bool
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    13
  val cancel_group: group -> exn -> unit
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    14
  val is_canceled: group -> bool
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    15
  val group_status: group -> exn list
28179
8e8313aededc human-readable printing of TaskQueue.task/group;
wenzelm
parents: 28176
diff changeset
    16
  val str_of_group: group -> string
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    17
  type task
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    18
  val dummy_task: unit -> task
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    19
  val group_of_task: task -> group
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    20
  val name_of_task: task -> string
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    21
  val pri_of_task: task -> int
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    22
  val str_of_task: task -> string
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    23
  val timing_of_task: task -> Time.time * Time.time * string list
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    24
  type queue
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    25
  val empty: queue
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
    26
  val all_passive: queue -> bool
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
    27
  val status: queue -> {ready: int, pending: int, running: int, passive: int}
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
    28
  val cancel: queue -> group -> bool
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
    29
  val cancel_all: queue -> group list
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
    30
  val finish: task -> queue -> bool * queue
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
    31
  val enqueue_passive: group -> (unit -> bool) -> queue -> task * queue
41673
1c191a39549f support named tasks, for improved tracing;
wenzelm
parents: 41670
diff changeset
    32
  val enqueue: string -> group -> task list -> int -> (bool -> bool) ->
1c191a39549f support named tasks, for improved tracing;
wenzelm
parents: 41670
diff changeset
    33
    queue -> (task * bool) * queue
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    34
  val extend: task -> (bool -> bool) -> queue -> queue option
39243
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
    35
  val dequeue_passive: Thread.thread -> task -> queue -> bool * queue
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    36
  val dequeue: Thread.thread -> queue -> (task * (bool -> bool) list) option * queue
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
    37
  type deps
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
    38
  val init_deps: task list -> deps
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
    39
  val finished_deps: deps -> bool
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
    40
  val dequeue_deps: Thread.thread -> deps -> queue ->
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    41
    (((task * (bool -> bool) list) option * deps) * queue)
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
    42
  val running: task -> (unit -> 'a) -> 'a
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
    43
  val joining: task -> (unit -> 'a) -> 'a
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
    44
  val waiting: task -> deps -> (unit -> 'a) -> 'a
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    45
end;
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    46
35242
1c80c29086d7 eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents: 35012
diff changeset
    47
structure Task_Queue: TASK_QUEUE =
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    48
struct
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    49
40450
8eab60e1baeb private counter, to keep externalized ids a bit smaller;
wenzelm
parents: 39243
diff changeset
    50
val new_id = Synchronized.counter ();
8eab60e1baeb private counter, to keep externalized ids a bit smaller;
wenzelm
parents: 39243
diff changeset
    51
8eab60e1baeb private counter, to keep externalized ids a bit smaller;
wenzelm
parents: 39243
diff changeset
    52
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    53
(** nested groups of tasks **)
41670
74010c6af0a4 added basic task timing;
wenzelm
parents: 40450
diff changeset
    54
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    55
(* groups *)
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    56
35242
1c80c29086d7 eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents: 35012
diff changeset
    57
abstype group = Group of
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    58
 {parent: group option,
40450
8eab60e1baeb private counter, to keep externalized ids a bit smaller;
wenzelm
parents: 39243
diff changeset
    59
  id: int,
35242
1c80c29086d7 eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents: 35012
diff changeset
    60
  status: exn list Synchronized.var}
1c80c29086d7 eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents: 35012
diff changeset
    61
with
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    62
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    63
fun make_group (parent, id, status) = Group {parent = parent, id = id, status = status};
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    64
40450
8eab60e1baeb private counter, to keep externalized ids a bit smaller;
wenzelm
parents: 39243
diff changeset
    65
fun new_group parent = make_group (parent, new_id (), Synchronized.var "group" []);
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    66
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    67
fun group_id (Group {id, ...}) = id;
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    68
fun eq_group (group1, group2) = group_id group1 = group_id group2;
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
    69
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    70
fun group_ancestry (Group {parent, id, ...}) =
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    71
  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
    72
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    73
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    74
(* group status *)
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    75
32251
e586c82fdf69 group status: Synchronized.var;
wenzelm
parents: 32250
diff changeset
    76
fun cancel_group (Group {status, ...}) exn =
e586c82fdf69 group status: Synchronized.var;
wenzelm
parents: 32250
diff changeset
    77
  Synchronized.change status
e586c82fdf69 group status: Synchronized.var;
wenzelm
parents: 32250
diff changeset
    78
    (fn exns =>
39232
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 37854
diff changeset
    79
      if not (Exn.is_interrupt exn) orelse null exns then exn :: exns
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 37854
diff changeset
    80
      else exns);
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    81
32251
e586c82fdf69 group status: Synchronized.var;
wenzelm
parents: 32250
diff changeset
    82
fun is_canceled (Group {parent, status, ...}) =
e586c82fdf69 group status: Synchronized.var;
wenzelm
parents: 32250
diff changeset
    83
  not (null (Synchronized.value status)) orelse
e586c82fdf69 group status: Synchronized.var;
wenzelm
parents: 32250
diff changeset
    84
    (case parent of NONE => false | SOME group => is_canceled group);
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
    85
32251
e586c82fdf69 group status: Synchronized.var;
wenzelm
parents: 32250
diff changeset
    86
fun group_status (Group {parent, status, ...}) =
e586c82fdf69 group status: Synchronized.var;
wenzelm
parents: 32250
diff changeset
    87
  Synchronized.value status @
e586c82fdf69 group status: Synchronized.var;
wenzelm
parents: 32250
diff changeset
    88
    (case parent of NONE => [] | SOME group => group_status group);
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    89
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    90
fun str_of_group group =
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
    91
  (is_canceled group ? enclose "(" ")") (string_of_int (group_id group));
28179
8e8313aededc human-readable printing of TaskQueue.task/group;
wenzelm
parents: 28176
diff changeset
    92
35242
1c80c29086d7 eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents: 35012
diff changeset
    93
end;
1c80c29086d7 eliminated opaque signature matching -- tends to cause problems with toplevel pp for abstract types;
wenzelm
parents: 35012
diff changeset
    94
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    95
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    96
(* tasks *)
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    97
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    98
type timing = Time.time * Time.time * string list;  (*run, wait, wait dependencies*)
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
    99
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   100
fun new_timing () =
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   101
  Synchronized.var "timing" ((Time.zeroTime, Time.zeroTime, []): timing);
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   102
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   103
abstype task = Task of
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   104
 {group: group,
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   105
  name: string,
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   106
  id: int,
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   107
  pri: int option,
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   108
  timing: timing Synchronized.var}
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   109
with
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   110
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   111
fun dummy_task () =
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   112
  Task {group = new_group NONE, name = "", id = 0, pri = NONE, timing = new_timing ()};
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   113
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   114
fun new_task group name pri =
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   115
  Task {group = group, name = name, id = new_id (), pri = pri, timing = new_timing ()};
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   116
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   117
fun group_of_task (Task {group, ...}) = group;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   118
fun name_of_task (Task {name, ...}) = name;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   119
fun pri_of_task (Task {pri, ...}) = the_default 0 pri;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   120
fun str_of_task (Task {name, id, ...}) =
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   121
  if name = "" then string_of_int id else string_of_int id ^ " (" ^ name ^ ")";
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   122
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   123
fun timing_of_task (Task {timing, ...}) = Synchronized.value timing;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   124
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   125
fun update_timing update (Task {timing, ...}) e =
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   126
  let
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   127
    val start = Time.now ();
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   128
    val result = Exn.capture e ();
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   129
    val t = Time.- (Time.now (), start);
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   130
    val _ = Synchronized.change timing (update t);
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   131
  in Exn.release result end;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   132
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   133
fun task_ord (Task {id = id1, pri = pri1, ...}, Task {id = id2, pri = pri2, ...}) =
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   134
  prod_ord (rev_order o option_ord int_ord) int_ord ((pri1, id1), (pri2, id2));
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   135
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   136
val eq_task = is_equal o task_ord;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   137
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   138
end;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   139
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   140
structure Task_Graph = Graph(type key = task val ord = task_ord);
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   141
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   142
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   143
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   144
(** queue of jobs and groups **)
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   145
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   146
(* jobs *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   147
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   148
datatype job =
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   149
  Job of (bool -> bool) list |
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   150
  Running of Thread.thread |
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   151
  Passive of unit -> bool;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   152
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   153
type jobs = job Task_Graph.T;
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   154
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   155
fun get_job (jobs: jobs) task = Task_Graph.get_node jobs task;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   156
fun set_job task job (jobs: jobs) = Task_Graph.map_node task (K job) jobs;
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   157
32250
wenzelm
parents: 32249
diff changeset
   158
fun add_job task dep (jobs: jobs) =
wenzelm
parents: 32249
diff changeset
   159
  Task_Graph.add_edge (dep, task) jobs handle Task_Graph.UNDEF _ => jobs;
wenzelm
parents: 32249
diff changeset
   160
wenzelm
parents: 32249
diff changeset
   161
fun get_deps (jobs: jobs) task =
wenzelm
parents: 32249
diff changeset
   162
  Task_Graph.imm_preds jobs task handle Task_Graph.UNDEF _ => [];
wenzelm
parents: 32249
diff changeset
   163
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   164
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   165
(* queue *)
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   166
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   167
datatype queue = Queue of
41682
44a2e0db281f tuned comment;
wenzelm
parents: 41681
diff changeset
   168
 {groups: task list Inttab.table,   (*groups with presently known members*)
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   169
  jobs: jobs};                      (*job dependency graph*)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   170
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   171
fun make_queue groups jobs = Queue {groups = groups, jobs = jobs};
28204
2d93b158ad99 added is_empty;
wenzelm
parents: 28202
diff changeset
   172
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   173
val empty = make_queue Inttab.empty Task_Graph.empty;
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   174
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   175
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   176
(* job status *)
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   177
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   178
fun ready_job task (Job list, ([], _)) = SOME (task, rev list)
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   179
  | ready_job task (Passive abort, ([], _)) =
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   180
      if is_canceled (group_of_task task) then SOME (task, [fn _ => abort ()])
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   181
      else NONE
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   182
  | ready_job _ _ = NONE;
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   183
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   184
fun active_job (_, (Job _, _)) = SOME ()
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   185
  | active_job (_, (Running _, _)) = SOME ()
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   186
  | active_job (task, (Passive _, _)) =
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   187
      if is_canceled (group_of_task task) then SOME () else NONE;
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   188
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   189
fun all_passive (Queue {jobs, ...}) = is_none (Task_Graph.get_first active_job jobs);
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   190
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   191
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   192
(* queue status *)
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   193
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   194
fun status (Queue {jobs, ...}) =
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   195
  let
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   196
    val (x, y, z, w) =
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   197
      Task_Graph.fold (fn (_, (job, (deps, _))) => fn (x, y, z, w) =>
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   198
          (case job of
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   199
            Job _ => if null deps then (x + 1, y, z, w) else (x, y + 1, z, w)
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   200
          | Running _ => (x, y, z + 1, w)
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   201
          | Passive _ => (x, y, z, w + 1)))
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   202
        jobs (0, 0, 0, 0);
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   203
  in {ready = x, pending = y, running = z, passive = w} end;
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   204
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   205
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   206
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   207
(** task queue operations **)
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   208
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   209
(* cancel -- peers and sub-groups *)
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   210
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   211
fun cancel (Queue {groups, jobs}) group =
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   212
  let
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   213
    val _ = cancel_group group Exn.Interrupt;
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   214
    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
   215
    val running =
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   216
      fold (get_job jobs #> (fn Running t => insert Thread.equal t | _ => I)) tasks [];
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35242
diff changeset
   217
    val _ = List.app Simple_Thread.interrupt running;
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   218
  in null running end;
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   219
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   220
fun cancel_all (Queue {jobs, ...}) =
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   221
  let
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   222
    fun cancel_job (task, (job, _)) (groups, running) =
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   223
      let
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   224
        val group = group_of_task task;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   225
        val _ = cancel_group group Exn.Interrupt;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   226
      in
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   227
        (case job of
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   228
          Running t => (insert eq_group group groups, insert Thread.equal t running)
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   229
        | _ => (groups, running))
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   230
      end;
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   231
    val (running_groups, running) = Task_Graph.fold cancel_job jobs ([], []);
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 35242
diff changeset
   232
    val _ = List.app Simple_Thread.interrupt running;
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   233
  in running_groups end;
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   234
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   235
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   236
(* finish *)
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   237
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   238
fun finish task (Queue {groups, jobs}) =
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   239
  let
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   240
    val group = group_of_task task;
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   241
    val groups' = groups
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   242
      |> fold (fn gid => Inttab.remove_list eq_task (gid, task)) (group_ancestry group);
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   243
    val jobs' = Task_Graph.del_node task jobs;
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   244
    val maximal = null (Task_Graph.imm_succs jobs task);
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   245
  in (maximal, make_queue groups' jobs') end;
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   246
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   247
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   248
(* enqueue *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   249
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   250
fun enqueue_passive group abort (Queue {groups, jobs}) =
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   251
  let
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   252
    val task = new_task group "passive" NONE;
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   253
    val groups' = groups
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   254
      |> fold (fn gid => Inttab.cons_list (gid, task)) (group_ancestry group);
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   255
    val jobs' = jobs |> Task_Graph.new_node (task, Passive abort);
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   256
  in (task, make_queue groups' jobs') end;
34277
7325a5e3587f added Future.promise/fulfill -- promised futures that are fulfilled by external means;
wenzelm
parents: 32814
diff changeset
   257
41673
1c191a39549f support named tasks, for improved tracing;
wenzelm
parents: 41670
diff changeset
   258
fun enqueue name group deps pri job (Queue {groups, jobs}) =
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   259
  let
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   260
    val task = new_task group name (SOME pri);
32101
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   261
    val groups' = groups
e25107ff4f56 support for nested groups -- cancellation is propagated to peers and subgroups;
wenzelm
parents: 32099
diff changeset
   262
      |> fold (fn gid => Inttab.cons_list (gid, task)) (group_ancestry group);
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   263
    val jobs' = jobs
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   264
      |> Task_Graph.new_node (task, Job [job])
41684
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   265
      |> fold (add_job task) deps;
32218
222f26693757 enqueue/finish: return minimal/maximal state of this task;
wenzelm
parents: 32192
diff changeset
   266
    val minimal = null (get_deps jobs' task);
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   267
  in ((task, minimal), make_queue groups' jobs') end;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   268
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   269
fun extend task job (Queue {groups, jobs}) =
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   270
  (case try (get_job jobs) task of
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   271
    SOME (Job list) => SOME (make_queue groups (set_job task (Job (job :: list)) jobs))
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   272
  | _ => NONE);
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   273
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   274
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   275
(* dequeue *)
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   276
39243
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
   277
fun dequeue_passive thread task (queue as Queue {groups, jobs}) =
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
   278
  (case try (get_job jobs) task of
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
   279
    SOME (Passive _) =>
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
   280
      let val jobs' = set_job task (Running thread) jobs
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
   281
      in (true, make_queue groups jobs') end
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
   282
  | _ => (false, queue));
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
   283
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   284
fun dequeue thread (queue as Queue {groups, jobs}) =
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   285
  (case Task_Graph.get_first (uncurry ready_job) jobs of
41683
73dde8006820 maintain Task_Queue.group within Task_Queue.task;
wenzelm
parents: 41682
diff changeset
   286
    SOME (result as (task, _)) =>
37854
047c96f41455 back to more strict dependencies, even for canceled groups (reverting parts of 02936e77a07c);
wenzelm
parents: 37216
diff changeset
   287
      let val jobs' = set_job task (Running thread) jobs
39243
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
   288
      in (SOME result, make_queue groups jobs') end
307e3d07d19f Future.promise: more robust treatment of concurrent abort vs. fulfill (amending 047c96f41455);
wenzelm
parents: 39232
diff changeset
   289
  | NONE => (NONE, queue));
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   290
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   291
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   292
(* dequeue wrt. dynamic dependencies *)
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   293
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   294
abstype deps = Deps of task list
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   295
with
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   296
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   297
fun init_deps tasks = Deps tasks;
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   298
fun finished_deps (Deps tasks) = null tasks;
32814
81897d30b97f added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents: 32784
diff changeset
   299
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   300
fun insert_deps (Deps tasks) = fold (insert (op =) o name_of_task) tasks;
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   301
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   302
fun dequeue_deps thread (Deps deps) (queue as Queue {groups, jobs}) =
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   303
  let
41684
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   304
    fun ready [] rest = (NONE, rev rest)
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   305
      | ready (task :: tasks) rest =
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   306
          (case try (Task_Graph.get_entry jobs) task of
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   307
            NONE => ready tasks rest
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   308
          | SOME entry =>
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   309
              (case ready_job task entry of
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   310
                NONE => ready tasks (task :: rest)
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   311
              | some => (some, List.revAppend (rest, tasks))));
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   312
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   313
    fun ready_dep _ [] = NONE
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   314
      | ready_dep seen (task :: tasks) =
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   315
          if member eq_task seen task then ready_dep seen tasks
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   316
          else
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   317
            let val entry as (_, (ds, _)) = Task_Graph.get_entry jobs task in
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   318
              (case ready_job task entry of
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   319
                NONE => ready_dep (task :: seen) (ds @ tasks)
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   320
              | some => some)
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   321
            end;
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   322
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   323
    fun result (res as (task, _)) deps' =
34280
16bf3e9786a3 eliminated cache, which complicates the code without making a real difference (NB: deque_towards often disrupts caching, and daisy-chaining of workers already reduces queue overhead);
wenzelm
parents: 34279
diff changeset
   324
      let val jobs' = set_job task (Running thread) jobs
41684
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   325
      in ((SOME res, Deps deps'), make_queue groups jobs') end;
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   326
  in
41684
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   327
    (case ready deps [] of
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   328
      (SOME res, deps') => result res deps'
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   329
    | (NONE, deps') =>
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   330
        (case ready_dep [] deps' of
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   331
          SOME res => result res deps'
b828d4b53386 refined Task_Queue.dequeue_deps (more incremental);
wenzelm
parents: 41683
diff changeset
   332
        | NONE => ((NONE, Deps deps'), queue)))
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   333
  end;
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   334
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   335
end;
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   336
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   337
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   338
(* timing *)
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   339
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   340
fun running task =
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   341
  update_timing (fn t => fn (a, b, ds) => (Time.+ (a, t), b, ds)) task;
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   342
41681
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   343
fun joining task =
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   344
  update_timing (fn t => fn (a, b, ds) => (Time.- (a, t), b, ds)) task;
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   345
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   346
fun waiting task deps =
b5d7b15166bf Future.join_results: discontinued post-hoc recording of dynamic dependencies;
wenzelm
parents: 41680
diff changeset
   347
  update_timing (fn t => fn (a, b, ds) => (Time.- (a, t), Time.+ (b, t), insert_deps deps ds)) task;
32221
fcbd6c9ee9bb tuned signature;
wenzelm
parents: 32218
diff changeset
   348
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   349
end;