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