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