src/Pure/Concurrent/task_queue.ML
author wenzelm
Sun, 19 Jul 2009 14:14:25 +0200
changeset 32055 6a46898aa805
parent 32052 8c391a12df1d
child 32093 30996b775a7f
permissions -rw-r--r--
recovered a version of dequeue_towards (cf. bb7b5a5942c7); join_results: work only towards explicit dependencies -- otherwise could produce dynamic cycle (not recorded in queue);
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
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    14
  val group_id: group -> int
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
    15
  val eq_group: group * group -> bool
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    16
  val new_group: unit -> group
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    17
  val is_valid: group -> bool
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
    18
  val invalidate_group: group -> unit
28179
8e8313aededc human-readable printing of TaskQueue.task/group;
wenzelm
parents: 28176
diff changeset
    19
  val str_of_group: group -> string
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    20
  type queue
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    21
  val empty: queue
28204
2d93b158ad99 added is_empty;
wenzelm
parents: 28202
diff changeset
    22
  val is_empty: queue -> bool
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    23
  val status: queue -> {ready: int, pending: int, running: int}
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    24
  val enqueue: group -> task list -> int -> (bool -> bool) -> queue -> task * queue
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    25
  val extend: task -> (bool -> bool) -> queue -> queue option
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    26
  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
    27
  val dequeue_towards: task list -> queue ->
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
    28
    (((task * group * (bool -> bool) list) * task list) option * queue)
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    29
  val interrupt: queue -> task -> unit
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    30
  val interrupt_external: queue -> string -> unit
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
    31
  val cancel: queue -> group -> bool
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
    32
  val cancel_all: queue -> group list
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    33
  val finish: task -> queue -> queue
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    34
end;
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    35
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
    36
structure Task_Queue:> TASK_QUEUE =
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    37
struct
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    38
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    39
(* tasks *)
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    40
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    41
datatype task = Task of int * serial;
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    42
fun new_task pri = Task (pri, serial ());
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    43
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    44
fun pri_of_task (Task (pri, _)) = pri;
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    45
fun str_of_task (Task (_, i)) = string_of_int i;
28998
23cbaa9f9834 added new_task;
wenzelm
parents: 28551
diff changeset
    46
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    47
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
    48
structure Task_Graph = Graph(type key = task val ord = task_ord);
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    49
28998
23cbaa9f9834 added new_task;
wenzelm
parents: 28551
diff changeset
    50
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    51
(* groups *)
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    52
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    53
datatype group = Group of serial * bool ref;
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    54
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    55
fun group_id (Group (gid, _)) = gid;
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
    56
fun eq_group (Group (gid1, _), Group (gid2, _)) = gid1 = gid2;
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
    57
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    58
fun new_group () = Group (serial (), ref true);
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    59
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    60
fun is_valid (Group (_, ref ok)) = ok;
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
    61
fun invalidate_group (Group (_, ok)) = ok := false;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    62
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    63
fun str_of_group (Group (i, ref ok)) =
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
    64
  if ok then string_of_int i else enclose "(" ")" (string_of_int i);
28179
8e8313aededc human-readable printing of TaskQueue.task/group;
wenzelm
parents: 28176
diff changeset
    65
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    66
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    67
(* jobs *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    68
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    69
datatype job =
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    70
  Job of (bool -> bool) list |
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    71
  Running of Thread.thread;
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    72
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    73
type jobs = (group * job) Task_Graph.T;
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    74
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    75
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
    76
fun get_job (jobs: jobs) task = #2 (Task_Graph.get_node jobs task);
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
    77
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
    78
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    79
fun add_job task dep (jobs: jobs) =
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    80
  Task_Graph.add_edge (dep, task) jobs handle Task_Graph.UNDEF _ => jobs;
29117
5a79ec2fedfb tuned enqueue: plain add_edge, acyclic not required here;
wenzelm
parents: 28998
diff changeset
    81
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    82
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    83
(* queue of grouped jobs *)
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
    84
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
    85
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
    86
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    87
datatype queue = Queue of
28184
5ed5cb73a2e9 eliminated cache, access queue efficiently via IntGraph.get_first;
wenzelm
parents: 28179
diff changeset
    88
 {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
    89
  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
    90
  cache: result};                   (*last dequeue result*)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    91
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
    92
fun make_queue groups jobs cache = Queue {groups = groups, jobs = jobs, cache = cache};
28204
2d93b158ad99 added is_empty;
wenzelm
parents: 28202
diff changeset
    93
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
    94
val empty = make_queue Inttab.empty Task_Graph.empty No_Result;
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
    95
fun is_empty (Queue {jobs, ...}) = Task_Graph.is_empty jobs;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    96
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
    97
32052
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    98
(* status *)
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
    99
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   100
fun status (Queue {jobs, ...}) =
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   101
  let
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   102
    val (x, y, z) =
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   103
      Task_Graph.fold (fn (task, ((_, job), (deps, _))) => fn (x, y, z) =>
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   104
          (case job of
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   105
            Job _ => if null deps then (x + 1, y, z) else (x, y + 1, z)
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   106
          | Running _ => (x, y, z + 1)))
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   107
        jobs (0, 0, 0);
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   108
  in {ready = x, pending = y, running = z} end;
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   109
8c391a12df1d added group_id;
wenzelm
parents: 31971
diff changeset
   110
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   111
(* enqueue *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   112
31632
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   113
fun enqueue (group as Group (gid, _)) deps pri job (Queue {groups, jobs, cache}) =
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   114
  let
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   115
    val task = new_task pri;
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   116
    val groups' = Inttab.cons_list (gid, task) groups;
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   117
    val jobs' = jobs
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   118
      |> Task_Graph.new_node (task, (group, Job [job])) |> fold (add_job task) deps;
31632
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   119
    val cache' =
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   120
      (case cache of
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   121
        Result last =>
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   122
          if task_ord (last, task) = LESS
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   123
          then cache else Unknown
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   124
      | _ => Unknown);
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   125
  in (task, make_queue groups' jobs' cache') end;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   126
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
   127
fun extend task job (Queue {groups, jobs, cache}) =
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   128
  (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
   129
    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
   130
  | _ => NONE);
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   131
28185
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   132
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   133
(* dequeue *)
0f20cbce4935 simplified dequeue: provide Thread.self internally;
wenzelm
parents: 28184
diff changeset
   134
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
   135
fun dequeue (queue as Queue {groups, jobs, cache}) =
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   136
  let
29365
5c5bc17d9135 added is_valid;
wenzelm
parents: 29342
diff changeset
   137
    fun ready (task, ((group, Job list), ([], _))) = SOME (task, group, rev list)
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   138
      | 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
   139
    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
   140
      (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
   141
        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
   142
      | 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
   143
          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
   144
            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
   145
            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
   146
          in (SOME result, make_queue groups jobs' cache') end);
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   147
  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
   148
    (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
   149
      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
   150
    | 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
   151
    | No_Result => (NONE, queue))
28384
70abca69247b dequeue_towards: return bound for unfinished tasks;
wenzelm
parents: 28332
diff changeset
   152
  end;
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28196
diff changeset
   153
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   154
32055
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   155
(* dequeue_towards -- adhoc dependencies *)
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   156
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   157
fun dequeue_towards deps (queue as Queue {groups, jobs, ...}) =
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   158
  let
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   159
    fun ready task =
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   160
      (case Task_Graph.get_node jobs task of
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   161
        (group, Job list) =>
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   162
          if null (Task_Graph.imm_preds jobs task) then SOME (task, group, rev list)
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   163
          else NONE
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   164
      | _ => NONE);
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   165
    val tasks = filter (can (Task_Graph.get_node jobs)) deps;
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   166
  in
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   167
    (case get_first ready (Task_Graph.all_preds jobs tasks) of
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   168
      NONE => (NONE, queue)
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   169
    | SOME (result as (task, _, _)) =>
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   170
        let
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   171
          val jobs' = set_job task (Running (Thread.self ())) jobs;
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   172
          val cache' = Unknown;
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   173
        in (SOME (result, tasks), make_queue groups jobs' cache') end)
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   174
  end;
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   175
6a46898aa805 recovered a version of dequeue_towards (cf. bb7b5a5942c7);
wenzelm
parents: 32052
diff changeset
   176
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   177
(* sporadic interrupts *)
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   178
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   179
fun interrupt (Queue {jobs, ...}) task =
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
   180
  (case try (get_job jobs) task of SOME (Running thread) => SimpleThread.interrupt thread | _ => ());
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   181
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   182
fun interrupt_external (queue as Queue {jobs, ...}) str =
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   183
  (case Int.fromString str of
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   184
    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
   185
      (case Task_Graph.get_first NONE
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   186
          (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
   187
        of SOME task => interrupt queue task | NONE => ())
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   188
  | NONE => ());
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   189
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   190
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   191
(* termination *)
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   192
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
   193
fun cancel (Queue {groups, jobs, ...}) (group as Group (gid, _)) =
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   194
  let
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
   195
    val _ = invalidate_group group;
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   196
    val tasks = Inttab.lookup_list groups gid;
29342
23504001c4fb cancel: unique threads;
wenzelm
parents: 29340
diff changeset
   197
    val running = fold (get_job jobs #> (fn Running t => insert Thread.equal t | _ => I)) tasks [];
28551
91eec4012bc5 added invalidate_group;
wenzelm
parents: 28384
diff changeset
   198
    val _ = List.app SimpleThread.interrupt running;
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   199
  in null running end;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   200
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   201
fun cancel_all (Queue {jobs, ...}) =
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   202
  let
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   203
    fun cancel_job (group, job) (groups, running) =
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   204
      (invalidate_group group;
29342
23504001c4fb cancel: unique threads;
wenzelm
parents: 29340
diff changeset
   205
        (case job of Running t => (insert eq_group group groups, insert Thread.equal t running)
29340
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   206
        | _ => (groups, running)));
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   207
    val (groups, running) = Task_Graph.fold (cancel_job o #1 o #2) jobs ([], []);
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   208
    val _ = List.app SimpleThread.interrupt running;
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   209
  in groups end;
057a30ee8570 added eq_group;
wenzelm
parents: 29121
diff changeset
   210
31632
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   211
fun finish task (Queue {groups, jobs, cache}) =
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   212
  let
28190
0a2434cf38c9 cancel: invalidate group implicitly, via bool ref;
wenzelm
parents: 28185
diff changeset
   213
    val Group (gid, _) = get_group jobs task;
28176
01b21886e7f0 job: explicit 'ok' status -- false for canceled jobs;
wenzelm
parents: 28171
diff changeset
   214
    val groups' = Inttab.remove_list (op =) (gid, task) groups;
29121
ad73b63ae2c5 renamed structure TaskQueue to Task_Queue;
wenzelm
parents: 29117
diff changeset
   215
    val jobs' = Task_Graph.del_node task jobs;
31632
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   216
    val cache' =
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   217
      if null (Task_Graph.imm_succs jobs task) then cache
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   218
      else Unknown;
fae680e35958 removed obsolete depend, add_job_acyclic;
wenzelm
parents: 31617
diff changeset
   219
  in make_queue groups' jobs' cache' end;
28165
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   220
26bb048f463c Ordered queue of grouped tasks.
wenzelm
parents:
diff changeset
   221
end;