src/Pure/Concurrent/future.ML
author wenzelm
Tue Jan 06 13:46:48 2009 +0100 (2009-01-06 ago)
changeset 29366 1ffc8cbf39ec
parent 29341 6bb007a0f9f2
child 29384 a3c7e9ae9b71
permissions -rw-r--r--
tuned map: reduced overhead due to bulk jobs;
tuned join_results: reduced overhead for finished futures;
tuned;
wenzelm@28156
     1
(*  Title:      Pure/Concurrent/future.ML
wenzelm@28156
     2
    Author:     Makarius
wenzelm@28156
     3
wenzelm@28201
     4
Future values.
wenzelm@28201
     5
wenzelm@28201
     6
Notes:
wenzelm@28201
     7
wenzelm@28201
     8
  * Futures are similar to delayed evaluation, i.e. delay/force is
wenzelm@28201
     9
    generalized to fork/join (and variants).  The idea is to model
wenzelm@28201
    10
    parallel value-oriented computations, but *not* communicating
wenzelm@28201
    11
    processes.
wenzelm@28201
    12
wenzelm@28201
    13
  * Futures are grouped; failure of one group member causes the whole
wenzelm@28201
    14
    group to be interrupted eventually.
wenzelm@28201
    15
wenzelm@28201
    16
  * Forked futures are evaluated spontaneously by a farm of worker
wenzelm@28201
    17
    threads in the background; join resynchronizes the computation and
wenzelm@28201
    18
    delivers results (values or exceptions).
wenzelm@28201
    19
wenzelm@28201
    20
  * The pool of worker threads is limited, usually in correlation with
wenzelm@28201
    21
    the number of physical cores on the machine.  Note that allocation
wenzelm@28201
    22
    of runtime resources is distorted either if workers yield CPU time
wenzelm@28201
    23
    (e.g. via system sleep or wait operations), or if non-worker
wenzelm@28201
    24
    threads contend for significant runtime resources independently.
wenzelm@28156
    25
*)
wenzelm@28156
    26
wenzelm@28156
    27
signature FUTURE =
wenzelm@28156
    28
sig
wenzelm@28645
    29
  val enabled: unit -> bool
wenzelm@29119
    30
  type task = Task_Queue.task
wenzelm@29119
    31
  type group = Task_Queue.group
wenzelm@28979
    32
  val thread_data: unit -> (string * task) option
wenzelm@28972
    33
  type 'a future
wenzelm@28972
    34
  val task_of: 'a future -> task
wenzelm@28972
    35
  val group_of: 'a future -> group
wenzelm@28972
    36
  val peek: 'a future -> 'a Exn.result option
wenzelm@28972
    37
  val is_finished: 'a future -> bool
wenzelm@28997
    38
  val value: 'a -> 'a future
wenzelm@28972
    39
  val fork: (unit -> 'a) -> 'a future
wenzelm@28979
    40
  val fork_group: group -> (unit -> 'a) -> 'a future
wenzelm@28979
    41
  val fork_deps: 'b future list -> (unit -> 'a) -> 'a future
wenzelm@29119
    42
  val fork_pri: int -> (unit -> 'a) -> 'a future
wenzelm@28972
    43
  val join_results: 'a future list -> 'a Exn.result list
wenzelm@28972
    44
  val join_result: 'a future -> 'a Exn.result
wenzelm@28972
    45
  val join: 'a future -> 'a
wenzelm@28972
    46
  val map: ('a -> 'b) -> 'a future -> 'b future
wenzelm@28206
    47
  val interrupt_task: string -> unit
wenzelm@28972
    48
  val cancel: 'a future -> unit
wenzelm@28203
    49
  val shutdown: unit -> unit
wenzelm@28156
    50
end;
wenzelm@28156
    51
wenzelm@28156
    52
structure Future: FUTURE =
wenzelm@28156
    53
struct
wenzelm@28156
    54
wenzelm@28177
    55
(** future values **)
wenzelm@28177
    56
wenzelm@28645
    57
fun enabled () =
wenzelm@29118
    58
  Multithreading.enabled () andalso
wenzelm@28645
    59
    not (Multithreading.self_critical ());
wenzelm@28645
    60
wenzelm@28645
    61
wenzelm@28167
    62
(* identifiers *)
wenzelm@28167
    63
wenzelm@29119
    64
type task = Task_Queue.task;
wenzelm@29119
    65
type group = Task_Queue.group;
wenzelm@28167
    66
wenzelm@28979
    67
local val tag = Universal.tag () : (string * task) option Universal.tag in
wenzelm@28177
    68
  fun thread_data () = the_default NONE (Thread.getLocal tag);
wenzelm@28390
    69
  fun setmp_thread_data data f x = Library.setmp_thread_data tag (thread_data ()) (SOME data) f x;
wenzelm@28167
    70
end;
wenzelm@28167
    71
wenzelm@28167
    72
wenzelm@28167
    73
(* datatype future *)
wenzelm@28167
    74
wenzelm@28972
    75
datatype 'a future = Future of
wenzelm@28167
    76
 {task: task,
wenzelm@28177
    77
  group: group,
wenzelm@28167
    78
  result: 'a Exn.result option ref};
wenzelm@28167
    79
wenzelm@28167
    80
fun task_of (Future {task, ...}) = task;
wenzelm@28167
    81
fun group_of (Future {group, ...}) = group;
wenzelm@28167
    82
wenzelm@28558
    83
fun peek (Future {result, ...}) = ! result;
wenzelm@28558
    84
fun is_finished x = is_some (peek x);
wenzelm@28320
    85
wenzelm@28997
    86
fun value x = Future
wenzelm@29119
    87
 {task = Task_Queue.new_task 0,
wenzelm@29119
    88
  group = Task_Queue.new_group (),
wenzelm@28997
    89
  result = ref (SOME (Exn.Result x))};
wenzelm@28997
    90
wenzelm@28167
    91
wenzelm@28177
    92
wenzelm@28177
    93
(** scheduling **)
wenzelm@28177
    94
wenzelm@28177
    95
(* global state *)
wenzelm@28177
    96
wenzelm@29119
    97
val queue = ref Task_Queue.empty;
wenzelm@28468
    98
val next = ref 0;
wenzelm@28192
    99
val workers = ref ([]: (Thread.thread * bool) list);
wenzelm@28177
   100
val scheduler = ref (NONE: Thread.thread option);
wenzelm@28177
   101
val excessive = ref 0;
wenzelm@29119
   102
val canceled = ref ([]: Task_Queue.group list);
wenzelm@28206
   103
val do_shutdown = ref false;
wenzelm@28177
   104
wenzelm@28177
   105
wenzelm@28177
   106
(* synchronization *)
wenzelm@28156
   107
wenzelm@28156
   108
local
wenzelm@28156
   109
  val lock = Mutex.mutex ();
wenzelm@28156
   110
  val cond = ConditionVar.conditionVar ();
wenzelm@28156
   111
in
wenzelm@28156
   112
wenzelm@28575
   113
fun SYNCHRONIZED name = SimpleThread.synchronized name lock;
wenzelm@28156
   114
wenzelm@29119
   115
fun wait () = (*requires SYNCHRONIZED*)
wenzelm@28206
   116
  ConditionVar.wait (cond, lock);
wenzelm@28206
   117
wenzelm@29119
   118
fun wait_timeout timeout = (*requires SYNCHRONIZED*)
wenzelm@29341
   119
  ignore (ConditionVar.waitUntil (cond, lock, Time.+ (Time.now (), timeout)));
wenzelm@28166
   120
wenzelm@28166
   121
fun notify_all () = (*requires SYNCHRONIZED*)
wenzelm@28166
   122
  ConditionVar.broadcast cond;
wenzelm@28156
   123
wenzelm@28156
   124
end;
wenzelm@28156
   125
wenzelm@28156
   126
wenzelm@28382
   127
(* worker activity *)
wenzelm@28382
   128
wenzelm@28382
   129
fun trace_active () =
wenzelm@28382
   130
  let
wenzelm@28382
   131
    val ws = ! workers;
wenzelm@28382
   132
    val m = string_of_int (length ws);
wenzelm@28382
   133
    val n = string_of_int (length (filter #2 ws));
wenzelm@28382
   134
  in Multithreading.tracing 1 (fn () => "SCHEDULE: " ^ m ^ " workers, " ^ n ^ " active") end;
wenzelm@28382
   135
wenzelm@28382
   136
fun change_active active = (*requires SYNCHRONIZED*)
wenzelm@28382
   137
  change workers (AList.update Thread.equal (Thread.self (), active));
wenzelm@28382
   138
wenzelm@28382
   139
wenzelm@29366
   140
(* execute jobs *)
wenzelm@28156
   141
wenzelm@29341
   142
fun do_cancel group = (*requires SYNCHRONIZED*)
wenzelm@29341
   143
  change canceled (insert Task_Queue.eq_group group);
wenzelm@29341
   144
wenzelm@29366
   145
fun execute name (task, group, jobs) =
wenzelm@28167
   146
  let
wenzelm@28382
   147
    val _ = trace_active ();
wenzelm@29366
   148
    val ok = setmp_thread_data (name, task) (fn () =>
wenzelm@29366
   149
      fold (fn job => fn ok => job ok) jobs (Task_Queue.is_valid group)) ();
wenzelm@28192
   150
    val _ = SYNCHRONIZED "execute" (fn () =>
wenzelm@29119
   151
     (change queue (Task_Queue.finish task);
wenzelm@28186
   152
      if ok then ()
wenzelm@29119
   153
      else if Task_Queue.cancel (! queue) group then ()
wenzelm@29341
   154
      else do_cancel group;
wenzelm@28177
   155
      notify_all ()));
wenzelm@28167
   156
  in () end;
wenzelm@28167
   157
wenzelm@28167
   158
wenzelm@28167
   159
(* worker threads *)
wenzelm@28167
   160
wenzelm@29119
   161
fun worker_wait () = (*requires SYNCHRONIZED*)
wenzelm@29119
   162
  (change_active false; wait (); change_active true);
wenzelm@28162
   163
wenzelm@29119
   164
fun worker_next () = (*requires SYNCHRONIZED*)
wenzelm@28167
   165
  if ! excessive > 0 then
wenzelm@28167
   166
    (dec excessive;
wenzelm@28192
   167
     change workers (filter_out (fn (thread, _) => Thread.equal (thread, Thread.self ())));
wenzelm@28203
   168
     notify_all ();
wenzelm@28167
   169
     NONE)
wenzelm@28166
   170
  else
wenzelm@29119
   171
    (case change_result queue Task_Queue.dequeue of
wenzelm@29119
   172
      NONE => (worker_wait (); worker_next ())
wenzelm@28166
   173
    | some => some);
wenzelm@28156
   174
wenzelm@28167
   175
fun worker_loop name =
wenzelm@29119
   176
  (case SYNCHRONIZED name worker_next of
wenzelm@29119
   177
    NONE => ()
wenzelm@28167
   178
  | SOME work => (execute name work; worker_loop name));
wenzelm@28156
   179
wenzelm@28167
   180
fun worker_start name = (*requires SYNCHRONIZED*)
wenzelm@28242
   181
  change workers (cons (SimpleThread.fork false (fn () => worker_loop name), true));
wenzelm@28156
   182
wenzelm@28156
   183
wenzelm@28156
   184
(* scheduler *)
wenzelm@28156
   185
wenzelm@28206
   186
fun scheduler_next () = (*requires SYNCHRONIZED*)
wenzelm@28156
   187
  let
wenzelm@28206
   188
    (*worker threads*)
wenzelm@28191
   189
    val _ =
wenzelm@28192
   190
      (case List.partition (Thread.isActive o #1) (! workers) of
wenzelm@28191
   191
        (_, []) => ()
wenzelm@28191
   192
      | (active, inactive) =>
wenzelm@28191
   193
          (workers := active; Multithreading.tracing 0 (fn () =>
wenzelm@28192
   194
            "SCHEDULE: disposed " ^ string_of_int (length inactive) ^ " dead worker threads")));
wenzelm@28382
   195
    val _ = trace_active ();
wenzelm@28191
   196
wenzelm@28206
   197
    val m = if ! do_shutdown then 0 else Multithreading.max_threads_value ();
wenzelm@28167
   198
    val l = length (! workers);
wenzelm@28167
   199
    val _ = excessive := l - m;
wenzelm@28203
   200
    val _ =
wenzelm@28468
   201
      if m > l then funpow (m - l) (fn () => worker_start ("worker " ^ string_of_int (inc next))) ()
wenzelm@28203
   202
      else ();
wenzelm@28206
   203
wenzelm@28206
   204
    (*canceled groups*)
wenzelm@29119
   205
    val _ =  change canceled (filter_out (Task_Queue.cancel (! queue)));
wenzelm@28206
   206
wenzelm@28206
   207
    (*shutdown*)
wenzelm@28206
   208
    val continue = not (! do_shutdown andalso null (! workers));
wenzelm@28206
   209
    val _ = if continue then () else scheduler := NONE;
wenzelm@28167
   210
wenzelm@28206
   211
    val _ = notify_all ();
wenzelm@29341
   212
    val _ = interruptible (fn () => wait_timeout (Time.fromSeconds 1)) ()
wenzelm@29341
   213
      handle Exn.Interrupt => List.app do_cancel (Task_Queue.cancel_all (! queue));
wenzelm@28206
   214
  in continue end;
wenzelm@28206
   215
wenzelm@28206
   216
fun scheduler_loop () =
wenzelm@29119
   217
  while SYNCHRONIZED "scheduler" scheduler_next do ();
wenzelm@28156
   218
wenzelm@28203
   219
fun scheduler_active () = (*requires SYNCHRONIZED*)
wenzelm@28203
   220
  (case ! scheduler of NONE => false | SOME thread => Thread.isActive thread);
wenzelm@28203
   221
wenzelm@28464
   222
fun scheduler_check name = SYNCHRONIZED name (fn () =>
wenzelm@28206
   223
  if not (scheduler_active ()) then
wenzelm@29119
   224
    (do_shutdown := false; scheduler := SOME (SimpleThread.fork false scheduler_loop))
wenzelm@28206
   225
  else if ! do_shutdown then error "Scheduler shutdown in progress"
wenzelm@28206
   226
  else ());
wenzelm@28156
   227
wenzelm@28156
   228
wenzelm@29366
   229
wenzelm@29366
   230
(** futures **)
wenzelm@28156
   231
wenzelm@29366
   232
(* future job: fill result *)
wenzelm@29366
   233
wenzelm@29366
   234
fun future_job group (e: unit -> 'a) =
wenzelm@28156
   235
  let
wenzelm@28166
   236
    val result = ref (NONE: 'a Exn.result option);
wenzelm@29366
   237
    val job = Multithreading.with_attributes (Thread.getAttributes ())
wenzelm@28177
   238
      (fn _ => fn ok =>
wenzelm@28532
   239
        let
wenzelm@28532
   240
          val res = if ok then Exn.capture e () else Exn.Exn Exn.Interrupt;
wenzelm@28548
   241
          val _ = result := SOME res;
wenzelm@28532
   242
          val res_ok =
wenzelm@28532
   243
            (case res of
wenzelm@28532
   244
              Exn.Result _ => true
wenzelm@29119
   245
            | Exn.Exn Exn.Interrupt => (Task_Queue.invalidate_group group; true)
wenzelm@28532
   246
            | _ => false);
wenzelm@28548
   247
        in res_ok end);
wenzelm@29366
   248
  in (result, job) end;
wenzelm@28177
   249
wenzelm@29366
   250
wenzelm@29366
   251
(* fork *)
wenzelm@29366
   252
wenzelm@29366
   253
fun fork_future opt_group deps pri e =
wenzelm@29366
   254
  let
wenzelm@29366
   255
    val _ = scheduler_check "future check";
wenzelm@29366
   256
wenzelm@29366
   257
    val group = (case opt_group of SOME group => group | NONE => Task_Queue.new_group ());
wenzelm@29366
   258
    val (result, job) = future_job group e;
wenzelm@28192
   259
    val task = SYNCHRONIZED "future" (fn () =>
wenzelm@29366
   260
      change_result queue (Task_Queue.enqueue group deps pri job) before notify_all ());
wenzelm@28166
   261
  in Future {task = task, group = group, result = result} end;
wenzelm@28162
   262
wenzelm@29366
   263
fun fork e = fork_future NONE [] 0 e;
wenzelm@29366
   264
fun fork_group group e = fork_future (SOME group) [] 0 e;
wenzelm@29366
   265
fun fork_deps deps e = fork_future NONE (map task_of deps) 0 e;
wenzelm@29366
   266
fun fork_pri pri e = fork_future NONE [] pri e;
wenzelm@28186
   267
wenzelm@28186
   268
wenzelm@29366
   269
(* join *)
wenzelm@29366
   270
wenzelm@29366
   271
fun get_result x = the_default (Exn.Exn (SYS_ERROR "unfinished future")) (peek x);
wenzelm@28186
   272
wenzelm@29366
   273
fun join_results xs =
wenzelm@29366
   274
  if forall is_finished xs then map get_result xs
wenzelm@29366
   275
  else uninterruptible (fn _ => fn () =>
wenzelm@29366
   276
    let
wenzelm@29366
   277
      val _ = scheduler_check "join check";
wenzelm@29366
   278
      val _ = Multithreading.self_critical () andalso
wenzelm@29366
   279
        error "Cannot join future values within critical section";
wenzelm@28177
   280
wenzelm@29366
   281
      fun join_loop _ [] = ()
wenzelm@29366
   282
        | join_loop name deps =
wenzelm@29366
   283
            (case SYNCHRONIZED name (fn () =>
wenzelm@29366
   284
                change_result queue (Task_Queue.dequeue_towards deps)) of
wenzelm@29366
   285
              NONE => ()
wenzelm@29366
   286
            | SOME (work, deps') => (execute name work; join_loop name deps'));
wenzelm@29366
   287
      val _ =
wenzelm@29366
   288
        (case thread_data () of
wenzelm@29366
   289
          NONE =>
wenzelm@29366
   290
            (*alien thread -- refrain from contending for resources*)
wenzelm@29366
   291
            while not (forall is_finished xs)
wenzelm@29366
   292
            do SYNCHRONIZED "join_thread" (fn () => wait ())
wenzelm@29366
   293
        | SOME (name, task) =>
wenzelm@29366
   294
            (*proper task -- actively work towards results*)
wenzelm@29366
   295
            let
wenzelm@29366
   296
              val pending = filter_out is_finished xs;
wenzelm@29366
   297
              val deps = map task_of pending;
wenzelm@29366
   298
              val _ = SYNCHRONIZED "join" (fn () =>
wenzelm@29366
   299
                (change queue (Task_Queue.depend deps task); notify_all ()));
wenzelm@29366
   300
              val _ = join_loop ("join_loop: " ^ name) deps;
wenzelm@29366
   301
              val _ =
wenzelm@29366
   302
                while not (forall is_finished pending)
wenzelm@29366
   303
                do SYNCHRONIZED "join_task" (fn () => worker_wait ());
wenzelm@29366
   304
            in () end);
wenzelm@28186
   305
wenzelm@29366
   306
    in map get_result xs end) ();
wenzelm@28186
   307
wenzelm@28647
   308
fun join_result x = singleton join_results x;
wenzelm@28647
   309
fun join x = Exn.release (join_result x);
wenzelm@28156
   310
wenzelm@29366
   311
wenzelm@29366
   312
(* map *)
wenzelm@29366
   313
wenzelm@29366
   314
fun map_future f (x as Future {task, group, ...}) =
wenzelm@29366
   315
  let
wenzelm@29366
   316
    val _ = scheduler_check "map_future check";
wenzelm@29366
   317
wenzelm@29366
   318
    val (result', job) = future_job group (fn () => f (join x));
wenzelm@29366
   319
    val extended = SYNCHRONIZED "map_future" (fn () =>
wenzelm@29366
   320
      (case Task_Queue.extend task job (! queue) of
wenzelm@29366
   321
        SOME queue' => (queue := queue'; true)
wenzelm@29366
   322
      | NONE => false));
wenzelm@29366
   323
  in
wenzelm@29366
   324
    if extended then Future {task = task, group = group, result = result'}
wenzelm@29366
   325
    else fork_future (SOME group) [task] (Task_Queue.pri_of_task task) (fn () => f (join x))
wenzelm@29366
   326
  end;
wenzelm@28979
   327
wenzelm@28191
   328
wenzelm@29366
   329
(* cancel *)
wenzelm@28202
   330
wenzelm@28202
   331
(*interrupt: permissive signal, may get ignored*)
wenzelm@28197
   332
fun interrupt_task id = SYNCHRONIZED "interrupt"
wenzelm@29119
   333
  (fn () => Task_Queue.interrupt_external (! queue) id);
wenzelm@28191
   334
wenzelm@28206
   335
(*cancel: present and future group members will be interrupted eventually*)
wenzelm@28206
   336
fun cancel x =
wenzelm@28464
   337
 (scheduler_check "cancel check";
wenzelm@29341
   338
  SYNCHRONIZED "cancel" (fn () => (do_cancel (group_of x); notify_all ())));
wenzelm@28206
   339
wenzelm@28206
   340
wenzelm@29366
   341
wenzelm@29366
   342
(** global join and shutdown **)
wenzelm@29366
   343
wenzelm@28203
   344
fun shutdown () =
wenzelm@28276
   345
  if Multithreading.available then
wenzelm@28464
   346
   (scheduler_check "shutdown check";
wenzelm@28276
   347
    SYNCHRONIZED "shutdown" (fn () =>
wenzelm@29119
   348
     (while not (scheduler_active ()) do wait ();
wenzelm@29119
   349
      while not (Task_Queue.is_empty (! queue)) do wait ();
wenzelm@28276
   350
      do_shutdown := true;
wenzelm@28276
   351
      notify_all ();
wenzelm@29119
   352
      while not (null (! workers)) do wait ();
wenzelm@29119
   353
      while scheduler_active () do wait ();
wenzelm@28470
   354
      OS.Process.sleep (Time.fromMilliseconds 300))))
wenzelm@28276
   355
  else ();
wenzelm@28203
   356
wenzelm@29366
   357
wenzelm@29366
   358
(*final declarations of this structure!*)
wenzelm@29366
   359
val map = map_future;
wenzelm@29366
   360
wenzelm@28156
   361
end;
wenzelm@28972
   362
wenzelm@28972
   363
type 'a future = 'a Future.future;
wenzelm@28972
   364