src/Pure/Concurrent/future.ML
author wenzelm
Thu Sep 11 13:43:42 2008 +0200 (2008-09-11 ago)
changeset 28201 7ae5cdb7b122
parent 28197 7053c539ecd8
child 28202 23cb9a974630
permissions -rw-r--r--
some general notes on future values;
wenzelm@28156
     1
(*  Title:      Pure/Concurrent/future.ML
wenzelm@28156
     2
    ID:         $Id$
wenzelm@28156
     3
    Author:     Makarius
wenzelm@28156
     4
wenzelm@28201
     5
Future values.
wenzelm@28201
     6
wenzelm@28201
     7
Notes:
wenzelm@28201
     8
wenzelm@28201
     9
  * Futures are similar to delayed evaluation, i.e. delay/force is
wenzelm@28201
    10
    generalized to fork/join (and variants).  The idea is to model
wenzelm@28201
    11
    parallel value-oriented computations, but *not* communicating
wenzelm@28201
    12
    processes.
wenzelm@28201
    13
wenzelm@28201
    14
  * Futures are grouped; failure of one group member causes the whole
wenzelm@28201
    15
    group to be interrupted eventually.
wenzelm@28201
    16
wenzelm@28201
    17
  * Forked futures are evaluated spontaneously by a farm of worker
wenzelm@28201
    18
    threads in the background; join resynchronizes the computation and
wenzelm@28201
    19
    delivers results (values or exceptions).
wenzelm@28201
    20
wenzelm@28201
    21
  * The pool of worker threads is limited, usually in correlation with
wenzelm@28201
    22
    the number of physical cores on the machine.  Note that allocation
wenzelm@28201
    23
    of runtime resources is distorted either if workers yield CPU time
wenzelm@28201
    24
    (e.g. via system sleep or wait operations), or if non-worker
wenzelm@28201
    25
    threads contend for significant runtime resources independently.
wenzelm@28156
    26
*)
wenzelm@28156
    27
wenzelm@28156
    28
signature FUTURE =
wenzelm@28156
    29
sig
wenzelm@28166
    30
  type task = TaskQueue.task
wenzelm@28166
    31
  type group = TaskQueue.group
wenzelm@28156
    32
  type 'a T
wenzelm@28166
    33
  val task_of: 'a T -> task
wenzelm@28177
    34
  val group_of: 'a T -> group
wenzelm@28177
    35
  val shutdown_request: unit -> unit
wenzelm@28191
    36
  val future: group option -> task list -> (unit -> 'a) -> 'a T
wenzelm@28166
    37
  val fork: (unit -> 'a) -> 'a T
wenzelm@28193
    38
  val join_results: 'a T list -> 'a Exn.result list
wenzelm@28166
    39
  val join: 'a T -> 'a
wenzelm@28197
    40
  val cancel: 'a T -> unit
wenzelm@28197
    41
  val interrupt_task: string -> unit
wenzelm@28156
    42
end;
wenzelm@28156
    43
wenzelm@28156
    44
structure Future: FUTURE =
wenzelm@28156
    45
struct
wenzelm@28156
    46
wenzelm@28177
    47
(** future values **)
wenzelm@28177
    48
wenzelm@28167
    49
(* identifiers *)
wenzelm@28167
    50
wenzelm@28167
    51
type task = TaskQueue.task;
wenzelm@28167
    52
type group = TaskQueue.group;
wenzelm@28167
    53
wenzelm@28177
    54
local val tag = Universal.tag () : (task * group) option Universal.tag in
wenzelm@28177
    55
  fun thread_data () = the_default NONE (Thread.getLocal tag);
wenzelm@28177
    56
  fun set_thread_data x = Thread.setLocal (tag, x);
wenzelm@28167
    57
end;
wenzelm@28167
    58
wenzelm@28167
    59
wenzelm@28167
    60
(* datatype future *)
wenzelm@28167
    61
wenzelm@28167
    62
datatype 'a T = Future of
wenzelm@28167
    63
 {task: task,
wenzelm@28177
    64
  group: group,
wenzelm@28167
    65
  result: 'a Exn.result option ref};
wenzelm@28167
    66
wenzelm@28167
    67
fun task_of (Future {task, ...}) = task;
wenzelm@28167
    68
fun group_of (Future {group, ...}) = group;
wenzelm@28167
    69
wenzelm@28167
    70
wenzelm@28177
    71
wenzelm@28177
    72
(** scheduling **)
wenzelm@28177
    73
wenzelm@28177
    74
(* global state *)
wenzelm@28177
    75
wenzelm@28177
    76
val queue = ref TaskQueue.empty;
wenzelm@28192
    77
val workers = ref ([]: (Thread.thread * bool) list);
wenzelm@28177
    78
val scheduler = ref (NONE: Thread.thread option);
wenzelm@28177
    79
wenzelm@28177
    80
val excessive = ref 0;
wenzelm@28177
    81
wenzelm@28177
    82
fun trace_active () =
wenzelm@28192
    83
  let
wenzelm@28192
    84
    val ws = ! workers;
wenzelm@28192
    85
    val m = string_of_int (length ws);
wenzelm@28192
    86
    val n = string_of_int (length (filter #2 ws));
wenzelm@28192
    87
  in Multithreading.tracing 1 (fn () => "SCHEDULE: " ^ m ^ " workers, " ^ n ^ " active") end;
wenzelm@28177
    88
wenzelm@28177
    89
wenzelm@28177
    90
(* requests *)
wenzelm@28177
    91
wenzelm@28177
    92
datatype request = Shutdown | Cancel of group;
wenzelm@28177
    93
val requests = Mailbox.create () : request Mailbox.T;
wenzelm@28177
    94
wenzelm@28177
    95
fun shutdown_request () = Mailbox.send requests Shutdown;
wenzelm@28177
    96
fun cancel_request group = Mailbox.send requests (Cancel group);
wenzelm@28177
    97
wenzelm@28177
    98
wenzelm@28177
    99
(* synchronization *)
wenzelm@28156
   100
wenzelm@28156
   101
local
wenzelm@28156
   102
  val lock = Mutex.mutex ();
wenzelm@28156
   103
  val cond = ConditionVar.conditionVar ();
wenzelm@28156
   104
in
wenzelm@28156
   105
wenzelm@28192
   106
fun SYNCHRONIZED name e = uninterruptible (fn restore_attributes => fn () =>
wenzelm@28162
   107
  let
wenzelm@28192
   108
    val _ = Multithreading.tracing 4 (fn () => name ^ ": locking");
wenzelm@28162
   109
    val _ = Mutex.lock lock;
wenzelm@28192
   110
    val _ = Multithreading.tracing 4 (fn () => name ^ ": locked");
wenzelm@28162
   111
    val result = Exn.capture (restore_attributes e) ();
wenzelm@28162
   112
    val _ = Mutex.unlock lock;
wenzelm@28192
   113
    val _ = Multithreading.tracing 4 (fn () => name ^ ": unlocked");
wenzelm@28162
   114
  in Exn.release result end) ();
wenzelm@28156
   115
wenzelm@28167
   116
fun wait name = (*requires SYNCHRONIZED*)
wenzelm@28167
   117
  let
wenzelm@28192
   118
    val _ = Multithreading.tracing 4 (fn () => name ^ ": waiting");
wenzelm@28167
   119
    val _ = ConditionVar.wait (cond, lock);
wenzelm@28192
   120
    val _ = Multithreading.tracing 4 (fn () => name ^ ": notified");
wenzelm@28167
   121
  in () end;
wenzelm@28166
   122
wenzelm@28166
   123
fun notify_all () = (*requires SYNCHRONIZED*)
wenzelm@28166
   124
  ConditionVar.broadcast cond;
wenzelm@28156
   125
wenzelm@28156
   126
end;
wenzelm@28156
   127
wenzelm@28156
   128
wenzelm@28177
   129
(* execute *)
wenzelm@28156
   130
wenzelm@28167
   131
fun execute name (task, group, run) =
wenzelm@28167
   132
  let
wenzelm@28177
   133
    val _ = set_thread_data (SOME (task, group));
wenzelm@28167
   134
    val _ = Multithreading.tracing 4 (fn () => name ^ ": running");
wenzelm@28167
   135
    val ok = run ();
wenzelm@28167
   136
    val _ = Multithreading.tracing 4 (fn () => name ^ ": finished");
wenzelm@28177
   137
    val _ = set_thread_data NONE;
wenzelm@28192
   138
    val _ = SYNCHRONIZED "execute" (fn () =>
wenzelm@28177
   139
     (change queue (TaskQueue.finish task);
wenzelm@28186
   140
      if ok then ()
wenzelm@28191
   141
      else if TaskQueue.cancel (! queue) group then ()
wenzelm@28186
   142
      else cancel_request group;
wenzelm@28177
   143
      notify_all ()));
wenzelm@28167
   144
  in () end;
wenzelm@28167
   145
wenzelm@28167
   146
wenzelm@28167
   147
(* worker threads *)
wenzelm@28167
   148
wenzelm@28192
   149
fun change_active active = (*requires SYNCHRONIZED*)
wenzelm@28192
   150
  (change workers (AList.update Thread.equal (Thread.self (), active)); trace_active ());
wenzelm@28186
   151
wenzelm@28186
   152
fun worker_wait name = (*requires SYNCHRONIZED*)
wenzelm@28186
   153
  (change_active false; wait name; change_active true);
wenzelm@28162
   154
wenzelm@28167
   155
fun worker_next name = (*requires SYNCHRONIZED*)
wenzelm@28167
   156
  if ! excessive > 0 then
wenzelm@28167
   157
    (dec excessive;
wenzelm@28192
   158
     change workers (filter_out (fn (thread, _) => Thread.equal (thread, Thread.self ())));
wenzelm@28167
   159
     NONE)
wenzelm@28166
   160
  else
wenzelm@28186
   161
    (case change_result queue TaskQueue.dequeue of
wenzelm@28186
   162
      NONE => (worker_wait name; worker_next name)
wenzelm@28166
   163
    | some => some);
wenzelm@28156
   164
wenzelm@28167
   165
fun worker_loop name =
wenzelm@28192
   166
  (case SYNCHRONIZED name (fn () => worker_next name) of
wenzelm@28166
   167
    NONE => ()
wenzelm@28167
   168
  | SOME work => (execute name work; worker_loop name));
wenzelm@28156
   169
wenzelm@28167
   170
fun worker_start name = (*requires SYNCHRONIZED*)
wenzelm@28192
   171
  change workers
wenzelm@28192
   172
    (cons (Thread.fork (fn () => worker_loop name, Multithreading.no_interrupts), true));
wenzelm@28156
   173
wenzelm@28156
   174
wenzelm@28156
   175
(* scheduler *)
wenzelm@28156
   176
wenzelm@28192
   177
fun scheduler_fork shutdown = SYNCHRONIZED "scheduler_fork" (fn () =>
wenzelm@28156
   178
  let
wenzelm@28177
   179
    val _ = trace_active ();
wenzelm@28191
   180
    val _ =
wenzelm@28192
   181
      (case List.partition (Thread.isActive o #1) (! workers) of
wenzelm@28191
   182
        (_, []) => ()
wenzelm@28191
   183
      | (active, inactive) =>
wenzelm@28191
   184
          (workers := active; Multithreading.tracing 0 (fn () =>
wenzelm@28192
   185
            "SCHEDULE: disposed " ^ string_of_int (length inactive) ^ " dead worker threads")));
wenzelm@28191
   186
wenzelm@28191
   187
    val m = if shutdown then 0 else Multithreading.max_threads_value ();
wenzelm@28167
   188
    val l = length (! workers);
wenzelm@28167
   189
    val _ = excessive := l - m;
wenzelm@28191
   190
    val _ = List.app (fn i => worker_start ("worker " ^ string_of_int i)) (l upto m - 1);
wenzelm@28192
   191
    val _ = if shutdown then notify_all () else ();
wenzelm@28192
   192
  in shutdown andalso null (! workers) end);
wenzelm@28167
   193
wenzelm@28191
   194
fun scheduler_loop (shutdown, canceled) =
wenzelm@28191
   195
  if scheduler_fork shutdown then ()
wenzelm@28191
   196
  else
wenzelm@28192
   197
    let
wenzelm@28192
   198
      val canceled' = SYNCHRONIZED "scheduler"
wenzelm@28192
   199
        (fn () => filter_out (TaskQueue.cancel (! queue)) canceled);
wenzelm@28192
   200
    in
wenzelm@28191
   201
      (case Mailbox.receive_timeout (Time.fromSeconds 1) requests of
wenzelm@28191
   202
        SOME Shutdown => scheduler_loop (true, canceled')
wenzelm@28191
   203
      | SOME (Cancel group) => scheduler_loop (shutdown, group :: canceled')
wenzelm@28191
   204
      | NONE => scheduler_loop (shutdown, canceled'))
wenzelm@28191
   205
    end;
wenzelm@28156
   206
wenzelm@28192
   207
fun scheduler_check () = SYNCHRONIZED "scheduler_check" (fn () =>
wenzelm@28167
   208
  if (case ! scheduler of NONE => false | SOME thread => Thread.isActive thread) then ()
wenzelm@28191
   209
  else scheduler :=
wenzelm@28191
   210
    SOME (Thread.fork (fn () => scheduler_loop (false, []), Multithreading.no_interrupts)));
wenzelm@28156
   211
wenzelm@28156
   212
wenzelm@28191
   213
(* future values: fork independent computation *)
wenzelm@28156
   214
wenzelm@28191
   215
fun future opt_group deps (e: unit -> 'a) =
wenzelm@28156
   216
  let
wenzelm@28191
   217
    val _ = scheduler_check ();
wenzelm@28177
   218
wenzelm@28191
   219
    val group = (case opt_group of SOME group => group | NONE => TaskQueue.new_group ());
wenzelm@28177
   220
wenzelm@28166
   221
    val result = ref (NONE: 'a Exn.result option);
wenzelm@28177
   222
    val run = Multithreading.with_attributes (Thread.getAttributes ())
wenzelm@28177
   223
      (fn _ => fn ok =>
wenzelm@28177
   224
        let val res = if ok then Exn.capture e () else Exn.Exn Interrupt
wenzelm@28177
   225
        in result := SOME res; is_some (Exn.get_result res) end);
wenzelm@28177
   226
wenzelm@28192
   227
    val task = SYNCHRONIZED "future" (fn () =>
wenzelm@28166
   228
      change_result queue (TaskQueue.enqueue group deps run) before notify_all ());
wenzelm@28166
   229
  in Future {task = task, group = group, result = result} end;
wenzelm@28162
   230
wenzelm@28191
   231
fun fork e = future (Option.map #2 (thread_data ())) [] e;
wenzelm@28186
   232
wenzelm@28186
   233
wenzelm@28191
   234
(* join: retrieve results *)
wenzelm@28186
   235
wenzelm@28193
   236
fun join_results xs =
wenzelm@28156
   237
  let
wenzelm@28193
   238
    val _ = Multithreading.self_critical () andalso
wenzelm@28193
   239
      error "Cannot join future values within critical section";
wenzelm@28191
   240
    val _ = scheduler_check ();
wenzelm@28177
   241
wenzelm@28186
   242
    fun unfinished () =
wenzelm@28186
   243
      xs |> map_filter (fn Future {task, result = ref NONE, ...} => SOME task | _ => NONE);
wenzelm@28186
   244
wenzelm@28186
   245
    (*alien thread -- refrain from contending for resources*)
wenzelm@28186
   246
    fun passive_join () = (*requires SYNCHRONIZED*)
wenzelm@28186
   247
      (case unfinished () of [] => ()
wenzelm@28186
   248
      | _ => (wait "join"; passive_join ()));
wenzelm@28186
   249
wenzelm@28186
   250
    (*proper worker thread -- actively work towards results*)
wenzelm@28186
   251
    fun active_join () = (*requires SYNCHRONIZED*)
wenzelm@28186
   252
      (case unfinished () of [] => ()
wenzelm@28186
   253
      | tasks =>
wenzelm@28186
   254
          (case change_result queue (TaskQueue.dequeue_towards tasks) of
wenzelm@28186
   255
            NONE => (worker_wait "join"; active_join ())
wenzelm@28186
   256
          | SOME work => (execute "join" work; active_join ())));
wenzelm@28186
   257
wenzelm@28186
   258
    val _ =
wenzelm@28186
   259
      (case thread_data () of
wenzelm@28192
   260
        NONE => SYNCHRONIZED "join" passive_join
wenzelm@28192
   261
      | SOME (task, _) => SYNCHRONIZED "join" (fn () =>
wenzelm@28186
   262
         (change queue (TaskQueue.depend (unfinished ()) task); active_join ())));
wenzelm@28186
   263
wenzelm@28193
   264
  in xs |> map (fn Future {result = ref (SOME res), ...} => res) end;
wenzelm@28186
   265
wenzelm@28193
   266
fun join x = Exn.release (singleton join_results x);
wenzelm@28156
   267
wenzelm@28191
   268
wenzelm@28191
   269
(* termination *)
wenzelm@28191
   270
wenzelm@28191
   271
(*cancel: present and future group members will be interrupted eventually*)
wenzelm@28191
   272
fun cancel x = (scheduler_check (); cancel_request (group_of x));
wenzelm@28191
   273
wenzelm@28191
   274
(*interrupt: adhoc signal, permissive, may get ignored*)
wenzelm@28197
   275
fun interrupt_task id = SYNCHRONIZED "interrupt"
wenzelm@28197
   276
  (fn () => TaskQueue.interrupt_external (! queue) id);
wenzelm@28191
   277
wenzelm@28156
   278
end;