src/Pure/Concurrent/future.ML
author haftmann
Tue, 23 Sep 2008 18:11:44 +0200
changeset 28337 93964076e7b8
parent 28331 33d58fdc177d
child 28380 0130201cc0e3
permissions -rw-r--r--
case default fallback for NBE
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Concurrent/future.ML
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
     4
28201
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
     5
Future values.
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
     6
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
     7
Notes:
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
     8
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
     9
  * Futures are similar to delayed evaluation, i.e. delay/force is
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    10
    generalized to fork/join (and variants).  The idea is to model
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    11
    parallel value-oriented computations, but *not* communicating
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    12
    processes.
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    13
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    14
  * Futures are grouped; failure of one group member causes the whole
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    15
    group to be interrupted eventually.
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    16
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    17
  * Forked futures are evaluated spontaneously by a farm of worker
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    18
    threads in the background; join resynchronizes the computation and
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    19
    delivers results (values or exceptions).
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    20
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    21
  * The pool of worker threads is limited, usually in correlation with
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    22
    the number of physical cores on the machine.  Note that allocation
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    23
    of runtime resources is distorted either if workers yield CPU time
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    24
    (e.g. via system sleep or wait operations), or if non-worker
7ae5cdb7b122 some general notes on future values;
wenzelm
parents: 28197
diff changeset
    25
    threads contend for significant runtime resources independently.
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    26
*)
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    27
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    28
signature FUTURE =
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    29
sig
28166
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
    30
  type task = TaskQueue.task
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
    31
  type group = TaskQueue.group
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    32
  type 'a T
28166
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
    33
  val task_of: 'a T -> task
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    34
  val group_of: 'a T -> group
28320
c6aef67f964d added is_finished;
wenzelm
parents: 28304
diff changeset
    35
  val is_finished: 'a T -> bool
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28276
diff changeset
    36
  val future: group option -> task list -> bool -> (unit -> 'a) -> 'a T
28166
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
    37
  val fork: (unit -> 'a) -> 'a T
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28276
diff changeset
    38
  val fork_irrelevant: (unit -> 'a) -> 'a T
28193
7ed74d0ba607 replaced join_all by join_results, which returns Exn.results;
wenzelm
parents: 28192
diff changeset
    39
  val join_results: 'a T list -> 'a Exn.result list
28166
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
    40
  val join: 'a T -> 'a
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28201
diff changeset
    41
  val focus: task list -> unit
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
    42
  val interrupt_task: string -> unit
28197
7053c539ecd8 added interrupt_task (external id);
wenzelm
parents: 28193
diff changeset
    43
  val cancel: 'a T -> unit
28203
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
    44
  val shutdown: unit -> unit
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    45
end;
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    46
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    47
structure Future: FUTURE =
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    48
struct
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    49
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    50
(** future values **)
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    51
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    52
(* identifiers *)
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    53
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    54
type task = TaskQueue.task;
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    55
type group = TaskQueue.group;
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    56
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    57
local val tag = Universal.tag () : (task * group) option Universal.tag in
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    58
  fun thread_data () = the_default NONE (Thread.getLocal tag);
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    59
  fun set_thread_data x = Thread.setLocal (tag, x);
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    60
end;
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    61
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    62
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    63
(* datatype future *)
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    64
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    65
datatype 'a T = Future of
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    66
 {task: task,
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    67
  group: group,
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    68
  result: 'a Exn.result option ref};
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    69
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    70
fun task_of (Future {task, ...}) = task;
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    71
fun group_of (Future {group, ...}) = group;
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    72
28320
c6aef67f964d added is_finished;
wenzelm
parents: 28304
diff changeset
    73
fun is_finished (Future {result, ...}) = is_some (! result);
c6aef67f964d added is_finished;
wenzelm
parents: 28304
diff changeset
    74
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
    75
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    76
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    77
(** scheduling **)
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    78
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    79
(* global state *)
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    80
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    81
val queue = ref TaskQueue.empty;
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
    82
val workers = ref ([]: (Thread.thread * bool) list);
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    83
val scheduler = ref (NONE: Thread.thread option);
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    84
val excessive = ref 0;
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
    85
val canceled = ref ([]: TaskQueue.group list);
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
    86
val do_shutdown = ref false;
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    87
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    88
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
    89
(* synchronization *)
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    90
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    91
local
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    92
  val lock = Mutex.mutex ();
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    93
  val cond = ConditionVar.conditionVar ();
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    94
in
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
    95
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
    96
fun SYNCHRONIZED name e = uninterruptible (fn restore_attributes => fn () =>
28162
55772e4e95e0 tuned check_cache;
wenzelm
parents: 28156
diff changeset
    97
  let
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
    98
    val _ = Multithreading.tracing 4 (fn () => name ^ ": locking");
28162
55772e4e95e0 tuned check_cache;
wenzelm
parents: 28156
diff changeset
    99
    val _ = Mutex.lock lock;
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   100
    val _ = Multithreading.tracing 4 (fn () => name ^ ": locked");
28162
55772e4e95e0 tuned check_cache;
wenzelm
parents: 28156
diff changeset
   101
    val result = Exn.capture (restore_attributes e) ();
55772e4e95e0 tuned check_cache;
wenzelm
parents: 28156
diff changeset
   102
    val _ = Mutex.unlock lock;
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   103
    val _ = Multithreading.tracing 4 (fn () => name ^ ": unlocked");
28162
55772e4e95e0 tuned check_cache;
wenzelm
parents: 28156
diff changeset
   104
  in Exn.release result end) ();
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   105
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   106
fun wait name = (*requires SYNCHRONIZED*)
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   107
  ConditionVar.wait (cond, lock);
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   108
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   109
fun wait_timeout name timeout = (*requires SYNCHRONIZED*)
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   110
  ConditionVar.waitUntil (cond, lock, Time.+ (Time.now (), timeout));
28166
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
   111
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
   112
fun notify_all () = (*requires SYNCHRONIZED*)
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
   113
  ConditionVar.broadcast cond;
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   114
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   115
end;
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   116
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   117
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   118
(* execute *)
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   119
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   120
fun execute name (task, group, run) =
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   121
  let
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   122
    val _ = set_thread_data (SOME (task, group));
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   123
    val _ = Multithreading.tracing 4 (fn () => name ^ ": running");
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   124
    val ok = run ();
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   125
    val _ = Multithreading.tracing 4 (fn () => name ^ ": finished");
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   126
    val _ = set_thread_data NONE;
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   127
    val _ = SYNCHRONIZED "execute" (fn () =>
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   128
     (change queue (TaskQueue.finish task);
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   129
      if ok then ()
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   130
      else if TaskQueue.cancel (! queue) group then ()
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   131
      else change canceled (cons group);
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   132
      notify_all ()));
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   133
  in () end;
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   134
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   135
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   136
(* worker threads *)
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   137
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   138
fun change_active active = (*requires SYNCHRONIZED*)
28203
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   139
  let
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   140
    val _ = change workers (AList.update Thread.equal (Thread.self (), active));
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   141
    val ws = ! workers;
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   142
    val m = string_of_int (length ws);
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   143
    val n = string_of_int (length (filter #2 ws));
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   144
  in Multithreading.tracing 1 (fn () => "SCHEDULE: " ^ m ^ " workers, " ^ n ^ " active") end;
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   145
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   146
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   147
fun worker_wait name = (*requires SYNCHRONIZED*)
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   148
  (change_active false; wait name; change_active true);
28162
55772e4e95e0 tuned check_cache;
wenzelm
parents: 28156
diff changeset
   149
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   150
fun worker_next name = (*requires SYNCHRONIZED*)
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   151
  if ! excessive > 0 then
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   152
    (dec excessive;
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   153
     change workers (filter_out (fn (thread, _) => Thread.equal (thread, Thread.self ())));
28203
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   154
     notify_all ();
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   155
     NONE)
28166
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
   156
  else
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   157
    (case change_result queue TaskQueue.dequeue of
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   158
      NONE => (worker_wait name; worker_next name)
28166
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
   159
    | some => some);
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   160
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   161
fun worker_loop name =
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   162
  (case SYNCHRONIZED name (fn () => worker_next name) of
28203
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   163
    NONE => Multithreading.tracing 4 (fn () => name ^ ": exit")
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   164
  | SOME work => (execute name work; worker_loop name));
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   165
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   166
fun worker_start name = (*requires SYNCHRONIZED*)
28242
f978c8e75118 SimpleThread.fork;
wenzelm
parents: 28208
diff changeset
   167
  change workers (cons (SimpleThread.fork false (fn () => worker_loop name), true));
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   168
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   169
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   170
(* scheduler *)
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   171
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   172
fun scheduler_next () = (*requires SYNCHRONIZED*)
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   173
  let
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   174
    (*worker threads*)
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   175
    val _ =
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   176
      (case List.partition (Thread.isActive o #1) (! workers) of
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   177
        (_, []) => ()
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   178
      | (active, inactive) =>
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   179
          (workers := active; Multithreading.tracing 0 (fn () =>
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   180
            "SCHEDULE: disposed " ^ string_of_int (length inactive) ^ " dead worker threads")));
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   181
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   182
    val m = if ! do_shutdown then 0 else Multithreading.max_threads_value ();
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   183
    val l = length (! workers);
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   184
    val _ = excessive := l - m;
28203
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   185
    val _ =
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   186
      if m > l then funpow (m - l) (fn () => worker_start ("worker " ^ serial_string ())) ()
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   187
      else ();
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   188
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   189
    (*canceled groups*)
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   190
    val _ =  change canceled (filter_out (TaskQueue.cancel (! queue)));
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   191
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   192
    (*shutdown*)
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   193
    val continue = not (! do_shutdown andalso null (! workers));
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   194
    val _ = if continue then () else scheduler := NONE;
28167
27e2ca41b58c more interrupt operations;
wenzelm
parents: 28166
diff changeset
   195
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   196
    val _ = notify_all ();
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   197
    val _ = wait_timeout "scheduler" (Time.fromSeconds 1);
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   198
  in continue end;
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   199
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   200
fun scheduler_loop () =
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   201
 (while SYNCHRONIZED "scheduler" scheduler_next do ();
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   202
  Multithreading.tracing 4 (fn () => "scheduler: exit"));
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   203
28203
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   204
fun scheduler_active () = (*requires SYNCHRONIZED*)
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   205
  (case ! scheduler of NONE => false | SOME thread => Thread.isActive thread);
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   206
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   207
fun scheduler_check () = SYNCHRONIZED "scheduler_check" (fn () =>
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   208
  if not (scheduler_active ()) then
28242
f978c8e75118 SimpleThread.fork;
wenzelm
parents: 28208
diff changeset
   209
    (do_shutdown := false; scheduler := SOME (SimpleThread.fork false scheduler_loop))
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   210
  else if ! do_shutdown then error "Scheduler shutdown in progress"
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   211
  else ());
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   212
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   213
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   214
(* future values: fork independent computation *)
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   215
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28276
diff changeset
   216
fun future opt_group deps pri (e: unit -> 'a) =
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   217
  let
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   218
    val _ = scheduler_check ();
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   219
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   220
    val group = (case opt_group of SOME group => group | NONE => TaskQueue.new_group ());
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   221
28166
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
   222
    val result = ref (NONE: 'a Exn.result option);
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   223
    val run = Multithreading.with_attributes (Thread.getAttributes ())
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   224
      (fn _ => fn ok =>
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   225
        let val res = if ok then Exn.capture e () else Exn.Exn Interrupt
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   226
        in result := SOME res; is_some (Exn.get_result res) end);
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   227
28192
6d977729c8fa workers: explicit activity flag;
wenzelm
parents: 28191
diff changeset
   228
    val task = SYNCHRONIZED "future" (fn () =>
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28276
diff changeset
   229
      change_result queue (TaskQueue.enqueue group deps pri run) before notify_all ());
28166
43087721a66e moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents: 28163
diff changeset
   230
  in Future {task = task, group = group, result = result} end;
28162
55772e4e95e0 tuned check_cache;
wenzelm
parents: 28156
diff changeset
   231
28304
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28276
diff changeset
   232
fun fork e = future (Option.map #2 (thread_data ())) [] true e;
4b0477452943 future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents: 28276
diff changeset
   233
fun fork_irrelevant e = future (Option.map #2 (thread_data ())) [] false e;
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   234
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   235
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   236
(* join: retrieve results *)
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   237
28331
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   238
fun join_results [] = []
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   239
  | join_results xs =
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   240
      let
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   241
        val _ = scheduler_check ();
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   242
        val _ = Multithreading.self_critical () andalso
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   243
          error "Cannot join future values within critical section";
28177
8c0335bc9336 inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents: 28170
diff changeset
   244
28331
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   245
        fun unfinished () =
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   246
          xs |> map_filter (fn Future {task, result = ref NONE, ...} => SOME task | _ => NONE);
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   247
28331
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   248
        (*alien thread -- refrain from contending for resources*)
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   249
        fun passive_join () = (*requires SYNCHRONIZED*)
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   250
          (case unfinished () of [] => ()
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   251
          | _ => (wait "passive_join"; passive_join ()));
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   252
28331
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   253
        (*proper worker thread -- actively work towards results*)
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   254
        fun active_join () = (*requires SYNCHRONIZED*)
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   255
          (case unfinished () of [] => ()
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   256
          | tasks =>
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   257
              (case change_result queue (TaskQueue.dequeue_towards tasks) of
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   258
                NONE => (worker_wait "active_join"; active_join ())
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   259
              | SOME work => (execute "active_join" work; active_join ())));
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   260
28331
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   261
        val _ =
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   262
          (case thread_data () of
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   263
            NONE => SYNCHRONIZED "passive_join" passive_join
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   264
          | SOME (task, _) => SYNCHRONIZED "active_join" (fn () =>
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   265
             (change queue (TaskQueue.depend (unfinished ()) task); active_join ())));
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   266
28331
33d58fdc177d join_results: special case for empty list, works without multithreading;
wenzelm
parents: 28320
diff changeset
   267
      in xs |> map (fn Future {result = ref (SOME res), ...} => res) end;
28186
6a8417f36837 cancel: check_scheduler;
wenzelm
parents: 28177
diff changeset
   268
28193
7ed74d0ba607 replaced join_all by join_results, which returns Exn.results;
wenzelm
parents: 28192
diff changeset
   269
fun join x = Exn.release (singleton join_results x);
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   270
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   271
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28201
diff changeset
   272
(* misc operations *)
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28201
diff changeset
   273
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28201
diff changeset
   274
(*focus: collection of high-priority task*)
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28201
diff changeset
   275
fun focus tasks = SYNCHRONIZED "interrupt" (fn () =>
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28201
diff changeset
   276
  change queue (TaskQueue.focus tasks));
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   277
28202
23cb9a974630 added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents: 28201
diff changeset
   278
(*interrupt: permissive signal, may get ignored*)
28197
7053c539ecd8 added interrupt_task (external id);
wenzelm
parents: 28193
diff changeset
   279
fun interrupt_task id = SYNCHRONIZED "interrupt"
7053c539ecd8 added interrupt_task (external id);
wenzelm
parents: 28193
diff changeset
   280
  (fn () => TaskQueue.interrupt_external (! queue) id);
28191
9e5f556409c6 future: allow explicit group;
wenzelm
parents: 28186
diff changeset
   281
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   282
(*cancel: present and future group members will be interrupted eventually*)
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   283
fun cancel x =
28208
3a8b3453129a cancel, shutdown: notify_all;
wenzelm
parents: 28206
diff changeset
   284
 (scheduler_check ();
3a8b3453129a cancel, shutdown: notify_all;
wenzelm
parents: 28206
diff changeset
   285
  SYNCHRONIZED "cancel" (fn () => (change canceled (cons (group_of x)); notify_all ())));
28206
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   286
bcd48c6897d4 eliminated requests, use global state variables uniformly;
wenzelm
parents: 28203
diff changeset
   287
28203
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   288
(*global join and shutdown*)
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   289
fun shutdown () =
28276
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   290
  if Multithreading.available then
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   291
   (scheduler_check ();
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   292
    SYNCHRONIZED "shutdown" (fn () =>
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   293
     (while not (scheduler_active ()) do wait "shutdown: scheduler inactive";
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   294
      while not (TaskQueue.is_empty (! queue)) do wait "shutdown: join";
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   295
      do_shutdown := true;
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   296
      notify_all ();
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   297
      while not (null (! workers)) do wait "shutdown: workers";
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   298
      while scheduler_active () do wait "shutdown: scheduler still active")))
fbc707811203 shutdown only if Multithreading.available;
wenzelm
parents: 28242
diff changeset
   299
  else ();
28203
88f18387f1c9 shutdown: global join-and-shutdown operation;
wenzelm
parents: 28202
diff changeset
   300
28156
5205f7979b4f Functional threads as future values.
wenzelm
parents:
diff changeset
   301
end;