src/Pure/Concurrent/future.ML
author wenzelm
Tue Sep 29 11:49:22 2009 +0200 (2009-09-29)
changeset 32738 15bb09ca0378
parent 32724 aaeeb0ba2035
child 32814 81897d30b97f
permissions -rw-r--r--
explicit indication of Unsynchronized.ref;
wenzelm@28156
     1
(*  Title:      Pure/Concurrent/future.ML
wenzelm@28156
     2
    Author:     Makarius
wenzelm@28156
     3
wenzelm@32246
     4
Future values, see also
wenzelm@32246
     5
http://www4.in.tum.de/~wenzelm/papers/parallel-isabelle.pdf
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@32220
    15
    group to be interrupted eventually.  Groups are block-structured.
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@29119
    30
  type task = Task_Queue.task
wenzelm@29119
    31
  type group = Task_Queue.group
wenzelm@32058
    32
  val is_worker: unit -> bool
wenzelm@32102
    33
  val worker_group: unit -> Task_Queue.group option
wenzelm@28972
    34
  type 'a future
wenzelm@28972
    35
  val task_of: 'a future -> task
wenzelm@28972
    36
  val group_of: 'a future -> group
wenzelm@28972
    37
  val peek: 'a future -> 'a Exn.result option
wenzelm@28972
    38
  val is_finished: 'a future -> bool
wenzelm@28997
    39
  val value: 'a -> 'a future
wenzelm@28979
    40
  val fork_group: group -> (unit -> 'a) -> 'a future
wenzelm@32724
    41
  val fork_deps_pri: 'b future list -> int -> (unit -> 'a) -> 'a future
wenzelm@28979
    42
  val fork_deps: 'b future list -> (unit -> 'a) -> 'a future
wenzelm@29119
    43
  val fork_pri: int -> (unit -> 'a) -> 'a future
wenzelm@32724
    44
  val fork: (unit -> 'a) -> 'a future
wenzelm@28972
    45
  val join_results: 'a future list -> 'a Exn.result list
wenzelm@28972
    46
  val join_result: 'a future -> 'a Exn.result
wenzelm@28972
    47
  val join: 'a future -> 'a
wenzelm@28972
    48
  val map: ('a -> 'b) -> 'a future -> 'b future
wenzelm@30618
    49
  val interruptible_task: ('a -> 'b) -> 'a -> 'b
wenzelm@29431
    50
  val cancel_group: group -> unit
wenzelm@28972
    51
  val cancel: 'a future -> unit
wenzelm@28203
    52
  val shutdown: unit -> unit
wenzelm@28156
    53
end;
wenzelm@28156
    54
wenzelm@28156
    55
structure Future: FUTURE =
wenzelm@28156
    56
struct
wenzelm@28156
    57
wenzelm@28177
    58
(** future values **)
wenzelm@28177
    59
wenzelm@28167
    60
(* identifiers *)
wenzelm@28167
    61
wenzelm@29119
    62
type task = Task_Queue.task;
wenzelm@29119
    63
type group = Task_Queue.group;
wenzelm@28167
    64
wenzelm@32058
    65
local
wenzelm@32058
    66
  val tag = Universal.tag () : (string * task * group) option Universal.tag;
wenzelm@32058
    67
in
wenzelm@28177
    68
  fun thread_data () = the_default NONE (Thread.getLocal tag);
wenzelm@32058
    69
  fun setmp_thread_data data f x =
wenzelm@32058
    70
    Library.setmp_thread_data tag (thread_data ()) (SOME data) f x;
wenzelm@28167
    71
end;
wenzelm@28167
    72
wenzelm@32058
    73
val is_worker = is_some o thread_data;
wenzelm@32102
    74
val worker_group = Option.map #3 o thread_data;
wenzelm@32058
    75
wenzelm@28167
    76
wenzelm@28167
    77
(* datatype future *)
wenzelm@28167
    78
wenzelm@28972
    79
datatype 'a future = Future of
wenzelm@28167
    80
 {task: task,
wenzelm@28177
    81
  group: group,
wenzelm@32253
    82
  result: 'a Exn.result option Synchronized.var};
wenzelm@28167
    83
wenzelm@28167
    84
fun task_of (Future {task, ...}) = task;
wenzelm@28167
    85
fun group_of (Future {group, ...}) = group;
wenzelm@32253
    86
fun result_of (Future {result, ...}) = result;
wenzelm@28167
    87
wenzelm@32592
    88
fun peek x = Synchronized.value (result_of x);
wenzelm@28558
    89
fun is_finished x = is_some (peek x);
wenzelm@28320
    90
wenzelm@28997
    91
fun value x = Future
wenzelm@29119
    92
 {task = Task_Queue.new_task 0,
wenzelm@32102
    93
  group = Task_Queue.new_group NONE,
wenzelm@32253
    94
  result = Synchronized.var "future" (SOME (Exn.Result x))};
wenzelm@28997
    95
wenzelm@28167
    96
wenzelm@28177
    97
wenzelm@28177
    98
(** scheduling **)
wenzelm@28177
    99
wenzelm@28177
   100
(* global state *)
wenzelm@28177
   101
wenzelm@32738
   102
val queue = Unsynchronized.ref Task_Queue.empty;
wenzelm@32738
   103
val next = Unsynchronized.ref 0;
wenzelm@32738
   104
val workers = Unsynchronized.ref ([]: (Thread.thread * bool) list);
wenzelm@32738
   105
val scheduler = Unsynchronized.ref (NONE: Thread.thread option);
wenzelm@32738
   106
val excessive = Unsynchronized.ref 0;
wenzelm@32738
   107
val canceled = Unsynchronized.ref ([]: Task_Queue.group list);
wenzelm@32738
   108
val do_shutdown = Unsynchronized.ref false;
wenzelm@28177
   109
wenzelm@28177
   110
wenzelm@28177
   111
(* synchronization *)
wenzelm@28156
   112
wenzelm@32219
   113
val scheduler_event = ConditionVar.conditionVar ();
wenzelm@32219
   114
val work_available = ConditionVar.conditionVar ();
wenzelm@32219
   115
val work_finished = ConditionVar.conditionVar ();
wenzelm@32219
   116
wenzelm@28156
   117
local
wenzelm@28156
   118
  val lock = Mutex.mutex ();
wenzelm@28156
   119
in
wenzelm@28156
   120
wenzelm@28575
   121
fun SYNCHRONIZED name = SimpleThread.synchronized name lock;
wenzelm@28156
   122
wenzelm@32219
   123
fun wait cond = (*requires SYNCHRONIZED*)
wenzelm@32295
   124
  Multithreading.sync_wait NONE NONE cond lock;
wenzelm@28206
   125
wenzelm@32295
   126
fun wait_timeout timeout cond = (*requires SYNCHRONIZED*)
wenzelm@32295
   127
  Multithreading.sync_wait NONE (SOME (Time.+ (Time.now (), timeout))) cond lock;
wenzelm@28166
   128
wenzelm@32219
   129
fun signal cond = (*requires SYNCHRONIZED*)
wenzelm@32219
   130
  ConditionVar.signal cond;
wenzelm@32219
   131
wenzelm@32219
   132
fun broadcast cond = (*requires SYNCHRONIZED*)
wenzelm@28166
   133
  ConditionVar.broadcast cond;
wenzelm@28156
   134
wenzelm@32248
   135
fun broadcast_work () = (*requires SYNCHRONIZED*)
wenzelm@32248
   136
 (ConditionVar.broadcast work_available;
wenzelm@32225
   137
  ConditionVar.broadcast work_finished);
wenzelm@32225
   138
wenzelm@28156
   139
end;
wenzelm@28156
   140
wenzelm@28156
   141
wenzelm@32099
   142
(* execute future jobs *)
wenzelm@32099
   143
wenzelm@32099
   144
fun future_job group (e: unit -> 'a) =
wenzelm@32099
   145
  let
wenzelm@32253
   146
    val result = Synchronized.var "future" (NONE: 'a Exn.result option);
wenzelm@32107
   147
    fun job ok =
wenzelm@32107
   148
      let
wenzelm@32107
   149
        val res =
wenzelm@32107
   150
          if ok then
wenzelm@32230
   151
            Exn.capture (fn () =>
wenzelm@32295
   152
              Multithreading.with_attributes Multithreading.private_interrupts (fn _ => e ())) ()
wenzelm@32107
   153
          else Exn.Exn Exn.Interrupt;
wenzelm@32295
   154
        val _ = Synchronized.change result
wenzelm@32295
   155
          (fn NONE => SOME res
wenzelm@32295
   156
            | SOME _ => raise Fail "Duplicate assignment of future value");
wenzelm@32107
   157
      in
wenzelm@32107
   158
        (case res of
wenzelm@32107
   159
          Exn.Exn exn => (Task_Queue.cancel_group group exn; false)
wenzelm@32107
   160
        | Exn.Result _ => true)
wenzelm@32107
   161
      end;
wenzelm@32099
   162
  in (result, job) end;
wenzelm@28156
   163
wenzelm@29341
   164
fun do_cancel group = (*requires SYNCHRONIZED*)
wenzelm@32738
   165
 (Unsynchronized.change canceled (insert Task_Queue.eq_group group);
wenzelm@32738
   166
  broadcast scheduler_event);
wenzelm@29341
   167
wenzelm@29366
   168
fun execute name (task, group, jobs) =
wenzelm@28167
   169
  let
wenzelm@32102
   170
    val valid = not (Task_Queue.is_canceled group);
wenzelm@32058
   171
    val ok = setmp_thread_data (name, task, group) (fn () =>
wenzelm@29384
   172
      fold (fn job => fn ok => job valid andalso ok) jobs true) ();
wenzelm@32246
   173
    val _ = SYNCHRONIZED "finish" (fn () =>
wenzelm@32219
   174
      let
wenzelm@32738
   175
        val maximal = Unsynchronized.change_result queue (Task_Queue.finish task);
wenzelm@32219
   176
        val _ =
wenzelm@32219
   177
          if ok then ()
wenzelm@32219
   178
          else if Task_Queue.cancel (! queue) group then ()
wenzelm@32219
   179
          else do_cancel group;
wenzelm@32219
   180
        val _ = broadcast work_finished;
wenzelm@32219
   181
        val _ = if maximal then () else broadcast work_available;
wenzelm@32219
   182
      in () end);
wenzelm@28167
   183
  in () end;
wenzelm@28167
   184
wenzelm@28167
   185
wenzelm@32246
   186
(* worker activity *)
wenzelm@32246
   187
wenzelm@32246
   188
fun count_active () = (*requires SYNCHRONIZED*)
wenzelm@32246
   189
  fold (fn (_, active) => fn i => if active then i + 1 else i) (! workers) 0;
wenzelm@32246
   190
wenzelm@32246
   191
fun change_active active = (*requires SYNCHRONIZED*)
wenzelm@32738
   192
  Unsynchronized.change workers
wenzelm@32738
   193
    (AList.update Thread.equal (Thread.self (), active));
wenzelm@32246
   194
wenzelm@32246
   195
wenzelm@28167
   196
(* worker threads *)
wenzelm@28167
   197
wenzelm@32219
   198
fun worker_wait cond = (*requires SYNCHRONIZED*)
wenzelm@32286
   199
  (change_active false; wait cond; change_active true);
wenzelm@28162
   200
wenzelm@29119
   201
fun worker_next () = (*requires SYNCHRONIZED*)
wenzelm@28167
   202
  if ! excessive > 0 then
wenzelm@32738
   203
    (Unsynchronized.dec excessive;
wenzelm@32738
   204
     Unsynchronized.change workers
wenzelm@32738
   205
      (filter_out (fn (thread, _) => Thread.equal (thread, Thread.self ())));
wenzelm@32219
   206
     broadcast scheduler_event;
wenzelm@28167
   207
     NONE)
wenzelm@32246
   208
  else if count_active () > Multithreading.max_threads_value () then
wenzelm@32246
   209
    (worker_wait scheduler_event; worker_next ())
wenzelm@28166
   210
  else
wenzelm@32738
   211
    (case Unsynchronized.change_result queue (Task_Queue.dequeue (Thread.self ())) of
wenzelm@32219
   212
      NONE => (worker_wait work_available; worker_next ())
wenzelm@28166
   213
    | some => some);
wenzelm@28156
   214
wenzelm@28167
   215
fun worker_loop name =
wenzelm@32107
   216
  (case SYNCHRONIZED name (fn () => worker_next ()) of
wenzelm@29119
   217
    NONE => ()
wenzelm@28167
   218
  | SOME work => (execute name work; worker_loop name));
wenzelm@28156
   219
wenzelm@28167
   220
fun worker_start name = (*requires SYNCHRONIZED*)
wenzelm@32738
   221
  Unsynchronized.change workers (cons (SimpleThread.fork false (fn () =>
wenzelm@32248
   222
     (broadcast scheduler_event; worker_loop name)), true));
wenzelm@28156
   223
wenzelm@28156
   224
wenzelm@28156
   225
(* scheduler *)
wenzelm@28156
   226
wenzelm@32738
   227
val last_status = Unsynchronized.ref Time.zeroTime;
wenzelm@32248
   228
val next_status = Time.fromMilliseconds 500;
wenzelm@32248
   229
val next_round = Time.fromMilliseconds 50;
wenzelm@32226
   230
wenzelm@28206
   231
fun scheduler_next () = (*requires SYNCHRONIZED*)
wenzelm@28156
   232
  let
wenzelm@32226
   233
    (*queue and worker status*)
wenzelm@32226
   234
    val _ =
wenzelm@32226
   235
      let val now = Time.now () in
wenzelm@32226
   236
        if Time.> (Time.+ (! last_status, next_status), now) then ()
wenzelm@32226
   237
        else
wenzelm@32226
   238
         (last_status := now; Multithreading.tracing 1 (fn () =>
wenzelm@32226
   239
            let
wenzelm@32226
   240
              val {ready, pending, running} = Task_Queue.status (! queue);
wenzelm@32226
   241
              val total = length (! workers);
wenzelm@32246
   242
              val active = count_active ();
wenzelm@32226
   243
            in
wenzelm@32617
   244
              "SCHEDULE " ^ Time.toString now ^ ": " ^
wenzelm@32226
   245
                string_of_int ready ^ " ready, " ^
wenzelm@32226
   246
                string_of_int pending ^ " pending, " ^
wenzelm@32226
   247
                string_of_int running ^ " running; " ^
wenzelm@32226
   248
                string_of_int total ^ " workers, " ^
wenzelm@32226
   249
                string_of_int active ^ " active"
wenzelm@32226
   250
            end))
wenzelm@32226
   251
      end;
wenzelm@32053
   252
wenzelm@28206
   253
    (*worker threads*)
wenzelm@28191
   254
    val _ =
wenzelm@32219
   255
      if forall (Thread.isActive o #1) (! workers) then ()
wenzelm@32095
   256
      else
wenzelm@32219
   257
        (case List.partition (Thread.isActive o #1) (! workers) of
wenzelm@32095
   258
          (_, []) => ()
wenzelm@32220
   259
        | (alive, dead) =>
wenzelm@32220
   260
            (workers := alive; Multithreading.tracing 0 (fn () =>
wenzelm@32220
   261
              "SCHEDULE: disposed " ^ string_of_int (length dead) ^ " dead worker threads")));
wenzelm@28191
   262
wenzelm@28206
   263
    val m = if ! do_shutdown then 0 else Multithreading.max_threads_value ();
wenzelm@32644
   264
    val mm = if m = 9999 then 1 else m * 2;
wenzelm@32219
   265
    val l = length (! workers);
wenzelm@32095
   266
    val _ = excessive := l - mm;
wenzelm@28203
   267
    val _ =
wenzelm@32095
   268
      if mm > l then
wenzelm@32738
   269
        funpow (mm - l) (fn () =>
wenzelm@32738
   270
          worker_start ("worker " ^ string_of_int (Unsynchronized.inc next))) ()
wenzelm@28203
   271
      else ();
wenzelm@28206
   272
wenzelm@28206
   273
    (*canceled groups*)
wenzelm@32225
   274
    val _ =
wenzelm@32225
   275
      if null (! canceled) then ()
wenzelm@32293
   276
      else
wenzelm@32293
   277
       (Multithreading.tracing 1 (fn () =>
wenzelm@32293
   278
          string_of_int (length (! canceled)) ^ " canceled groups");
wenzelm@32738
   279
        Unsynchronized.change canceled (filter_out (Task_Queue.cancel (! queue)));
wenzelm@32293
   280
        broadcast_work ());
wenzelm@28206
   281
wenzelm@32228
   282
    (*delay loop*)
wenzelm@32295
   283
    val _ = Exn.release (wait_timeout next_round scheduler_event);
wenzelm@28167
   284
wenzelm@32219
   285
    (*shutdown*)
wenzelm@32228
   286
    val _ = if Task_Queue.is_empty (! queue) then do_shutdown := true else ();
wenzelm@32219
   287
    val continue = not (! do_shutdown andalso null (! workers));
wenzelm@32219
   288
    val _ = if continue then () else scheduler := NONE;
wenzelm@32219
   289
    val _ = broadcast scheduler_event;
wenzelm@32295
   290
  in continue end
wenzelm@32295
   291
  handle Exn.Interrupt =>
wenzelm@32295
   292
   (Multithreading.tracing 1 (fn () => "Interrupt");
wenzelm@32296
   293
    uninterruptible (fn _ => fn () => List.app do_cancel (Task_Queue.cancel_all (! queue))) ();
wenzelm@32295
   294
    scheduler_next ());
wenzelm@32295
   295
wenzelm@28206
   296
fun scheduler_loop () =
wenzelm@32295
   297
  Multithreading.with_attributes
wenzelm@32295
   298
    (Multithreading.sync_interrupts Multithreading.public_interrupts)
wenzelm@32295
   299
    (fn _ => while SYNCHRONIZED "scheduler" (fn () => scheduler_next ()) do ());
wenzelm@28156
   300
wenzelm@28203
   301
fun scheduler_active () = (*requires SYNCHRONIZED*)
wenzelm@28203
   302
  (case ! scheduler of NONE => false | SOME thread => Thread.isActive thread);
wenzelm@28203
   303
wenzelm@32228
   304
fun scheduler_check () = (*requires SYNCHRONIZED*)
wenzelm@32228
   305
 (do_shutdown := false;
wenzelm@32248
   306
  if scheduler_active () then ()
wenzelm@32248
   307
  else scheduler := SOME (SimpleThread.fork false scheduler_loop));
wenzelm@28156
   308
wenzelm@28156
   309
wenzelm@29366
   310
wenzelm@29366
   311
(** futures **)
wenzelm@28156
   312
wenzelm@29366
   313
(* fork *)
wenzelm@29366
   314
wenzelm@29366
   315
fun fork_future opt_group deps pri e =
wenzelm@29366
   316
  let
wenzelm@32102
   317
    val group =
wenzelm@32102
   318
      (case opt_group of
wenzelm@32102
   319
        SOME group => group
wenzelm@32102
   320
      | NONE => Task_Queue.new_group (worker_group ()));
wenzelm@29366
   321
    val (result, job) = future_job group e;
wenzelm@32246
   322
    val task = SYNCHRONIZED "enqueue" (fn () =>
wenzelm@32219
   323
      let
wenzelm@32738
   324
        val (task, minimal) =
wenzelm@32738
   325
          Unsynchronized.change_result queue (Task_Queue.enqueue group deps pri job);
wenzelm@32219
   326
        val _ = if minimal then signal work_available else ();
wenzelm@32228
   327
        val _ = scheduler_check ();
wenzelm@32219
   328
      in task end);
wenzelm@28166
   329
  in Future {task = task, group = group, result = result} end;
wenzelm@28162
   330
wenzelm@29366
   331
fun fork_group group e = fork_future (SOME group) [] 0 e;
wenzelm@32724
   332
fun fork_deps_pri deps pri e = fork_future NONE (map task_of deps) pri e;
wenzelm@32724
   333
fun fork_deps deps e = fork_deps_pri deps 0 e;
wenzelm@32724
   334
fun fork_pri pri e = fork_deps_pri [] pri e;
wenzelm@32724
   335
fun fork e = fork_deps [] e;
wenzelm@28186
   336
wenzelm@28186
   337
wenzelm@29366
   338
(* join *)
wenzelm@29366
   339
wenzelm@29551
   340
local
wenzelm@29551
   341
wenzelm@32099
   342
fun get_result x =
wenzelm@32099
   343
  (case peek x of
wenzelm@32102
   344
    NONE => Exn.Exn (SYS_ERROR "unfinished future")
wenzelm@32102
   345
  | SOME (Exn.Exn Exn.Interrupt) =>
wenzelm@32102
   346
      Exn.Exn (Exn.EXCEPTIONS (Exn.flatten_list (Task_Queue.group_status (group_of x))))
wenzelm@32102
   347
  | SOME res => res);
wenzelm@28186
   348
wenzelm@32224
   349
fun join_wait x =
wenzelm@32253
   350
  Synchronized.guarded_access (result_of x) (fn NONE => NONE | some => SOME ((), some));
wenzelm@32224
   351
wenzelm@32095
   352
fun join_next deps = (*requires SYNCHRONIZED*)
wenzelm@32224
   353
  if null deps then NONE
wenzelm@32224
   354
  else
wenzelm@32738
   355
    (case Unsynchronized.change_result queue (Task_Queue.dequeue_towards (Thread.self ()) deps) of
wenzelm@32224
   356
      (NONE, []) => NONE
wenzelm@32224
   357
    | (NONE, deps') => (worker_wait work_finished; join_next deps')
wenzelm@32224
   358
    | (SOME work, deps') => SOME (work, deps'));
wenzelm@32095
   359
wenzelm@32224
   360
fun join_work deps =
wenzelm@32095
   361
  (case SYNCHRONIZED "join" (fn () => join_next deps) of
wenzelm@29551
   362
    NONE => ()
wenzelm@32224
   363
  | SOME (work, deps') => (execute "join" work; join_work deps'));
wenzelm@29551
   364
wenzelm@29551
   365
in
wenzelm@29551
   366
wenzelm@29366
   367
fun join_results xs =
wenzelm@29366
   368
  if forall is_finished xs then map get_result xs
wenzelm@32246
   369
  else if Multithreading.self_critical () then
wenzelm@32246
   370
    error "Cannot join future values within critical section"
wenzelm@29366
   371
  else uninterruptible (fn _ => fn () =>
wenzelm@32246
   372
     (if is_worker ()
wenzelm@32246
   373
      then join_work (map task_of xs)
wenzelm@32246
   374
      else List.app join_wait xs;
wenzelm@32246
   375
      map get_result xs)) ();
wenzelm@28186
   376
wenzelm@29551
   377
end;
wenzelm@29551
   378
wenzelm@28647
   379
fun join_result x = singleton join_results x;
wenzelm@28647
   380
fun join x = Exn.release (join_result x);
wenzelm@28156
   381
wenzelm@29366
   382
wenzelm@29366
   383
(* map *)
wenzelm@29366
   384
wenzelm@29384
   385
fun map_future f x =
wenzelm@29366
   386
  let
wenzelm@29384
   387
    val task = task_of x;
wenzelm@32102
   388
    val group = Task_Queue.new_group (SOME (group_of x));
wenzelm@29384
   389
    val (result, job) = future_job group (fn () => f (join x));
wenzelm@29384
   390
wenzelm@32246
   391
    val extended = SYNCHRONIZED "extend" (fn () =>
wenzelm@29366
   392
      (case Task_Queue.extend task job (! queue) of
wenzelm@29366
   393
        SOME queue' => (queue := queue'; true)
wenzelm@29366
   394
      | NONE => false));
wenzelm@29366
   395
  in
wenzelm@29384
   396
    if extended then Future {task = task, group = group, result = result}
wenzelm@32099
   397
    else fork_future (SOME group) [task] (Task_Queue.pri_of_task task) (fn () => f (join x))
wenzelm@29366
   398
  end;
wenzelm@28979
   399
wenzelm@28191
   400
wenzelm@29431
   401
(* cancellation *)
wenzelm@28202
   402
wenzelm@30618
   403
fun interruptible_task f x =
wenzelm@30618
   404
  if Multithreading.available then
wenzelm@30618
   405
    Multithreading.with_attributes
wenzelm@32058
   406
      (if is_worker ()
wenzelm@32295
   407
       then Multithreading.private_interrupts
wenzelm@32295
   408
       else Multithreading.public_interrupts)
wenzelm@32295
   409
      (fn _ => f x)
wenzelm@30618
   410
  else interruptible f x;
wenzelm@30618
   411
wenzelm@32228
   412
(*cancel: present and future group members will be interrupted eventually*)
wenzelm@32228
   413
fun cancel_group group = SYNCHRONIZED "cancel" (fn () => do_cancel group);
wenzelm@29431
   414
fun cancel x = cancel_group (group_of x);
wenzelm@28206
   415
wenzelm@29366
   416
wenzelm@32228
   417
(* shutdown *)
wenzelm@29366
   418
wenzelm@28203
   419
fun shutdown () =
wenzelm@28276
   420
  if Multithreading.available then
wenzelm@28276
   421
    SYNCHRONIZED "shutdown" (fn () =>
wenzelm@32228
   422
     while scheduler_active () do
wenzelm@32248
   423
      (wait scheduler_event; broadcast_work ()))
wenzelm@28276
   424
  else ();
wenzelm@28203
   425
wenzelm@29366
   426
wenzelm@29366
   427
(*final declarations of this structure!*)
wenzelm@29366
   428
val map = map_future;
wenzelm@29366
   429
wenzelm@28156
   430
end;
wenzelm@28972
   431
wenzelm@28972
   432
type 'a future = 'a Future.future;
wenzelm@28972
   433