author | wenzelm |
Thu, 04 Dec 2008 23:00:27 +0100 | |
changeset 28972 | cb8a2c3e188f |
parent 28647 | 8068cdc84e7e |
child 28979 | 3ce619d8d432 |
permissions | -rw-r--r-- |
28156 | 1 |
(* Title: Pure/Concurrent/future.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
28201 | 5 |
Future values. |
6 |
||
7 |
Notes: |
|
8 |
||
9 |
* Futures are similar to delayed evaluation, i.e. delay/force is |
|
10 |
generalized to fork/join (and variants). The idea is to model |
|
11 |
parallel value-oriented computations, but *not* communicating |
|
12 |
processes. |
|
13 |
||
14 |
* Futures are grouped; failure of one group member causes the whole |
|
15 |
group to be interrupted eventually. |
|
16 |
||
17 |
* Forked futures are evaluated spontaneously by a farm of worker |
|
18 |
threads in the background; join resynchronizes the computation and |
|
19 |
delivers results (values or exceptions). |
|
20 |
||
21 |
* The pool of worker threads is limited, usually in correlation with |
|
22 |
the number of physical cores on the machine. Note that allocation |
|
23 |
of runtime resources is distorted either if workers yield CPU time |
|
24 |
(e.g. via system sleep or wait operations), or if non-worker |
|
25 |
threads contend for significant runtime resources independently. |
|
28156 | 26 |
*) |
27 |
||
28 |
signature FUTURE = |
|
29 |
sig |
|
28645 | 30 |
val enabled: unit -> bool |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
31 |
type task = TaskQueue.task |
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
32 |
type group = TaskQueue.group |
28386 | 33 |
val thread_data: unit -> (string * task * group) option |
28972 | 34 |
type 'a future |
35 |
val task_of: 'a future -> task |
|
36 |
val group_of: 'a future -> group |
|
37 |
val peek: 'a future -> 'a Exn.result option |
|
38 |
val is_finished: 'a future -> bool |
|
39 |
val future: group option -> task list -> bool -> (unit -> 'a) -> 'a future |
|
40 |
val fork: (unit -> 'a) -> 'a future |
|
41 |
val fork_background: (unit -> 'a) -> 'a future |
|
42 |
val join_results: 'a future list -> 'a Exn.result list |
|
43 |
val join_result: 'a future -> 'a Exn.result |
|
44 |
val join: 'a future -> 'a |
|
45 |
val map: ('a -> 'b) -> 'a future -> 'b future |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
46 |
val focus: task list -> unit |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
47 |
val interrupt_task: string -> unit |
28972 | 48 |
val cancel: 'a future -> unit |
28203 | 49 |
val shutdown: unit -> unit |
28156 | 50 |
end; |
51 |
||
52 |
structure Future: FUTURE = |
|
53 |
struct |
|
54 |
||
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
|
55 |
(** 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
|
56 |
|
28645 | 57 |
fun enabled () = |
58 |
! future_scheduler andalso Multithreading.enabled () andalso |
|
59 |
not (Multithreading.self_critical ()); |
|
60 |
||
61 |
||
28167 | 62 |
(* identifiers *) |
63 |
||
64 |
type task = TaskQueue.task; |
|
65 |
type group = TaskQueue.group; |
|
66 |
||
28386 | 67 |
local val tag = Universal.tag () : (string * task * group) option Universal.tag in |
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
|
68 |
fun thread_data () = the_default NONE (Thread.getLocal tag); |
28390
0b9fb63b8e1d
proper setmp_thread_data for nested execute (cf. join_loop);
wenzelm
parents:
28386
diff
changeset
|
69 |
fun setmp_thread_data data f x = Library.setmp_thread_data tag (thread_data ()) (SOME data) f x; |
28167 | 70 |
end; |
71 |
||
72 |
||
73 |
(* datatype future *) |
|
74 |
||
28972 | 75 |
datatype 'a future = Future of |
28167 | 76 |
{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
|
77 |
group: group, |
28167 | 78 |
result: 'a Exn.result option ref}; |
79 |
||
80 |
fun task_of (Future {task, ...}) = task; |
|
81 |
fun group_of (Future {group, ...}) = group; |
|
82 |
||
28558 | 83 |
fun peek (Future {result, ...}) = ! result; |
84 |
fun is_finished x = is_some (peek x); |
|
28320 | 85 |
|
28167 | 86 |
|
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 |
(** 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
|
89 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
90 |
(* 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
|
91 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
92 |
val queue = ref TaskQueue.empty; |
28468 | 93 |
val next = ref 0; |
28192 | 94 |
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
|
95 |
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
|
96 |
val excessive = ref 0; |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
97 |
val canceled = ref ([]: TaskQueue.group list); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
98 |
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
|
99 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
100 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
101 |
(* synchronization *) |
28156 | 102 |
|
103 |
local |
|
104 |
val lock = Mutex.mutex (); |
|
105 |
val cond = ConditionVar.conditionVar (); |
|
106 |
in |
|
107 |
||
28575 | 108 |
fun SYNCHRONIZED name = SimpleThread.synchronized name lock; |
28156 | 109 |
|
28167 | 110 |
fun wait name = (*requires SYNCHRONIZED*) |
28468 | 111 |
(Multithreading.tracing 3 (fn () => name ^ ": wait ..."); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
112 |
ConditionVar.wait (cond, lock); |
28468 | 113 |
Multithreading.tracing 3 (fn () => name ^ ": ... continue")); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
114 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
115 |
fun wait_timeout name timeout = (*requires SYNCHRONIZED*) |
28468 | 116 |
(Multithreading.tracing 3 (fn () => name ^ ": wait ..."); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
117 |
ConditionVar.waitUntil (cond, lock, Time.+ (Time.now (), timeout)); |
28468 | 118 |
Multithreading.tracing 3 (fn () => name ^ ": ... continue")); |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
119 |
|
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
120 |
fun notify_all () = (*requires SYNCHRONIZED*) |
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
121 |
ConditionVar.broadcast cond; |
28156 | 122 |
|
123 |
end; |
|
124 |
||
125 |
||
28382 | 126 |
(* worker activity *) |
127 |
||
128 |
fun trace_active () = |
|
129 |
let |
|
130 |
val ws = ! workers; |
|
131 |
val m = string_of_int (length ws); |
|
132 |
val n = string_of_int (length (filter #2 ws)); |
|
133 |
in Multithreading.tracing 1 (fn () => "SCHEDULE: " ^ m ^ " workers, " ^ n ^ " active") end; |
|
134 |
||
135 |
fun change_active active = (*requires SYNCHRONIZED*) |
|
136 |
change workers (AList.update Thread.equal (Thread.self (), active)); |
|
137 |
||
138 |
||
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
|
139 |
(* execute *) |
28156 | 140 |
|
28167 | 141 |
fun execute name (task, group, run) = |
142 |
let |
|
28382 | 143 |
val _ = trace_active (); |
28390
0b9fb63b8e1d
proper setmp_thread_data for nested execute (cf. join_loop);
wenzelm
parents:
28386
diff
changeset
|
144 |
val ok = setmp_thread_data (name, task, group) run (); |
28192 | 145 |
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
|
146 |
(change queue (TaskQueue.finish task); |
28186 | 147 |
if ok then () |
28191 | 148 |
else if TaskQueue.cancel (! queue) group then () |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
149 |
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
|
150 |
notify_all ())); |
28167 | 151 |
in () end; |
152 |
||
153 |
||
154 |
(* worker threads *) |
|
155 |
||
28186 | 156 |
fun worker_wait name = (*requires SYNCHRONIZED*) |
157 |
(change_active false; wait name; change_active true); |
|
28162 | 158 |
|
28167 | 159 |
fun worker_next name = (*requires SYNCHRONIZED*) |
160 |
if ! excessive > 0 then |
|
161 |
(dec excessive; |
|
28192 | 162 |
change workers (filter_out (fn (thread, _) => Thread.equal (thread, Thread.self ()))); |
28203 | 163 |
notify_all (); |
28167 | 164 |
NONE) |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
165 |
else |
28186 | 166 |
(case change_result queue TaskQueue.dequeue of |
167 |
NONE => (worker_wait name; worker_next name) |
|
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
168 |
| some => some); |
28156 | 169 |
|
28167 | 170 |
fun worker_loop name = |
28192 | 171 |
(case SYNCHRONIZED name (fn () => worker_next name) of |
28468 | 172 |
NONE => Multithreading.tracing 3 (fn () => name ^ ": exit") |
28167 | 173 |
| SOME work => (execute name work; worker_loop name)); |
28156 | 174 |
|
28167 | 175 |
fun worker_start name = (*requires SYNCHRONIZED*) |
28242 | 176 |
change workers (cons (SimpleThread.fork false (fn () => worker_loop name), true)); |
28156 | 177 |
|
178 |
||
179 |
(* scheduler *) |
|
180 |
||
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
181 |
fun scheduler_next () = (*requires SYNCHRONIZED*) |
28156 | 182 |
let |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
183 |
(*worker threads*) |
28191 | 184 |
val _ = |
28192 | 185 |
(case List.partition (Thread.isActive o #1) (! workers) of |
28191 | 186 |
(_, []) => () |
187 |
| (active, inactive) => |
|
188 |
(workers := active; Multithreading.tracing 0 (fn () => |
|
28192 | 189 |
"SCHEDULE: disposed " ^ string_of_int (length inactive) ^ " dead worker threads"))); |
28382 | 190 |
val _ = trace_active (); |
28191 | 191 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
192 |
val m = if ! do_shutdown then 0 else Multithreading.max_threads_value (); |
28167 | 193 |
val l = length (! workers); |
194 |
val _ = excessive := l - m; |
|
28203 | 195 |
val _ = |
28468 | 196 |
if m > l then funpow (m - l) (fn () => worker_start ("worker " ^ string_of_int (inc next))) () |
28203 | 197 |
else (); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
198 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
199 |
(*canceled groups*) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
200 |
val _ = change canceled (filter_out (TaskQueue.cancel (! queue))); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
201 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
202 |
(*shutdown*) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
203 |
val continue = not (! do_shutdown andalso null (! workers)); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
204 |
val _ = if continue then () else scheduler := NONE; |
28167 | 205 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
206 |
val _ = notify_all (); |
28464 | 207 |
val _ = wait_timeout "scheduler" (Time.fromSeconds 3); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
208 |
in continue end; |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
209 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
210 |
fun scheduler_loop () = |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
211 |
(while SYNCHRONIZED "scheduler" scheduler_next do (); |
28534 | 212 |
Multithreading.tracing 3 (fn () => "scheduler: exit")); |
28156 | 213 |
|
28203 | 214 |
fun scheduler_active () = (*requires SYNCHRONIZED*) |
215 |
(case ! scheduler of NONE => false | SOME thread => Thread.isActive thread); |
|
216 |
||
28464 | 217 |
fun scheduler_check name = SYNCHRONIZED name (fn () => |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
218 |
if not (scheduler_active ()) then |
28534 | 219 |
(Multithreading.tracing 3 (fn () => "scheduler: fork"); |
220 |
do_shutdown := false; scheduler := SOME (SimpleThread.fork false scheduler_loop)) |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
221 |
else if ! do_shutdown then error "Scheduler shutdown in progress" |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
222 |
else ()); |
28156 | 223 |
|
224 |
||
28191 | 225 |
(* future values: fork independent computation *) |
28156 | 226 |
|
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28276
diff
changeset
|
227 |
fun future opt_group deps pri (e: unit -> 'a) = |
28156 | 228 |
let |
28464 | 229 |
val _ = scheduler_check "future 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
|
230 |
|
28191 | 231 |
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
|
232 |
|
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
233 |
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
|
234 |
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
|
235 |
(fn _ => fn ok => |
28532 | 236 |
let |
237 |
val res = if ok then Exn.capture e () else Exn.Exn Exn.Interrupt; |
|
28548
003f52c2bb8f
future result: Interrupt invalidates group, but pretends success otherwise;
wenzelm
parents:
28534
diff
changeset
|
238 |
val _ = result := SOME res; |
28532 | 239 |
val res_ok = |
240 |
(case res of |
|
241 |
Exn.Result _ => true |
|
28548
003f52c2bb8f
future result: Interrupt invalidates group, but pretends success otherwise;
wenzelm
parents:
28534
diff
changeset
|
242 |
| Exn.Exn Exn.Interrupt => (TaskQueue.invalidate_group group; true) |
28532 | 243 |
| _ => false); |
28548
003f52c2bb8f
future result: Interrupt invalidates group, but pretends success otherwise;
wenzelm
parents:
28534
diff
changeset
|
244 |
in res_ok end); |
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
|
245 |
|
28192 | 246 |
val task = SYNCHRONIZED "future" (fn () => |
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28276
diff
changeset
|
247 |
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
|
248 |
in Future {task = task, group = group, result = result} end; |
28162 | 249 |
|
28430
29b2886114fb
renamed Future.fork_irrelevant to Future.fork_background;
wenzelm
parents:
28415
diff
changeset
|
250 |
fun fork_common pri = future (Option.map #3 (thread_data ())) [] pri; |
29b2886114fb
renamed Future.fork_irrelevant to Future.fork_background;
wenzelm
parents:
28415
diff
changeset
|
251 |
|
29b2886114fb
renamed Future.fork_irrelevant to Future.fork_background;
wenzelm
parents:
28415
diff
changeset
|
252 |
fun fork e = fork_common true e; |
29b2886114fb
renamed Future.fork_irrelevant to Future.fork_background;
wenzelm
parents:
28415
diff
changeset
|
253 |
fun fork_background e = fork_common false e; |
28186 | 254 |
|
255 |
||
28191 | 256 |
(* join: retrieve results *) |
28186 | 257 |
|
28331
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
258 |
fun join_results [] = [] |
28532 | 259 |
| join_results xs = uninterruptible (fn _ => fn () => |
28331
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
260 |
let |
28464 | 261 |
val _ = scheduler_check "join check"; |
28331
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
262 |
val _ = Multithreading.self_critical () andalso |
28647
8068cdc84e7e
join_results: allow CRITICAL join of finished futures;
wenzelm
parents:
28645
diff
changeset
|
263 |
exists (not o is_finished) xs andalso |
28331
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
264 |
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
|
265 |
|
28386 | 266 |
fun join_loop _ [] = () |
267 |
| join_loop name tasks = |
|
268 |
(case SYNCHRONIZED name (fn () => |
|
28382 | 269 |
change_result queue (TaskQueue.dequeue_towards tasks)) of |
270 |
NONE => () |
|
28386 | 271 |
| SOME (work, tasks') => (execute name work; join_loop name tasks')); |
28331
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
272 |
val _ = |
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
273 |
(case thread_data () of |
28382 | 274 |
NONE => |
275 |
(*alien thread -- refrain from contending for resources*) |
|
276 |
while exists (not o is_finished) xs |
|
277 |
do SYNCHRONIZED "join_thread" (fn () => wait "join_thread") |
|
28386 | 278 |
| SOME (name, task, _) => |
28382 | 279 |
(*proper task -- actively work towards results*) |
280 |
let |
|
281 |
val unfinished = xs |> map_filter |
|
282 |
(fn Future {task, result = ref NONE, ...} => SOME task | _ => NONE); |
|
283 |
val _ = SYNCHRONIZED "join" (fn () => |
|
284 |
(change queue (TaskQueue.depend unfinished task); notify_all ())); |
|
28386 | 285 |
val _ = join_loop ("join_loop: " ^ name) unfinished; |
28382 | 286 |
val _ = |
287 |
while exists (not o is_finished) xs |
|
288 |
do SYNCHRONIZED "join_task" (fn () => worker_wait "join_task"); |
|
289 |
in () end); |
|
28186 | 290 |
|
28532 | 291 |
in xs |> map (fn Future {result = ref (SOME res), ...} => res) end) (); |
28186 | 292 |
|
28647
8068cdc84e7e
join_results: allow CRITICAL join of finished futures;
wenzelm
parents:
28645
diff
changeset
|
293 |
fun join_result x = singleton join_results x; |
8068cdc84e7e
join_results: allow CRITICAL join of finished futures;
wenzelm
parents:
28645
diff
changeset
|
294 |
fun join x = Exn.release (join_result x); |
28156 | 295 |
|
28191 | 296 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
297 |
(* misc operations *) |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
298 |
|
28972 | 299 |
(*map: dependent fork/join*) |
300 |
fun map f x = future (SOME (group_of x)) [task_of x] true (fn () => f (join x)); |
|
301 |
||
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
302 |
(*focus: collection of high-priority task*) |
28464 | 303 |
fun focus tasks = SYNCHRONIZED "focus" (fn () => |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
304 |
change queue (TaskQueue.focus tasks)); |
28191 | 305 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
306 |
(*interrupt: permissive signal, may get ignored*) |
28197 | 307 |
fun interrupt_task id = SYNCHRONIZED "interrupt" |
308 |
(fn () => TaskQueue.interrupt_external (! queue) id); |
|
28191 | 309 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
310 |
(*cancel: present and future group members will be interrupted eventually*) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
311 |
fun cancel x = |
28464 | 312 |
(scheduler_check "cancel check"; |
28208 | 313 |
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
|
314 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
315 |
|
28203 | 316 |
(*global join and shutdown*) |
317 |
fun shutdown () = |
|
28276 | 318 |
if Multithreading.available then |
28464 | 319 |
(scheduler_check "shutdown check"; |
28276 | 320 |
SYNCHRONIZED "shutdown" (fn () => |
321 |
(while not (scheduler_active ()) do wait "shutdown: scheduler inactive"; |
|
322 |
while not (TaskQueue.is_empty (! queue)) do wait "shutdown: join"; |
|
323 |
do_shutdown := true; |
|
324 |
notify_all (); |
|
325 |
while not (null (! workers)) do wait "shutdown: workers"; |
|
28470 | 326 |
while scheduler_active () do wait "shutdown: scheduler still active"; |
327 |
OS.Process.sleep (Time.fromMilliseconds 300)))) |
|
28276 | 328 |
else (); |
28203 | 329 |
|
28156 | 330 |
end; |
28972 | 331 |
|
332 |
type 'a future = 'a Future.future; |
|
333 |