author | wenzelm |
Sat, 13 Dec 2008 15:00:39 +0100 | |
changeset 29091 | b81fe045e799 |
parent 28997 | 1b99dcae2156 |
child 29118 | 8f2481aa363d |
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 |
28979
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
33 |
val thread_data: unit -> (string * task) 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 |
|
28997 | 39 |
val value: 'a -> 'a future |
28972 | 40 |
val fork: (unit -> 'a) -> 'a future |
28979
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
41 |
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
|
42 |
val fork_deps: 'b future list -> (unit -> 'a) -> 'a future |
28972 | 43 |
val fork_background: (unit -> 'a) -> 'a future |
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 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
48 |
val focus: task list -> unit |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
49 |
val interrupt_task: string -> 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 () = |
60 |
! future_scheduler andalso Multithreading.enabled () andalso |
|
61 |
not (Multithreading.self_critical ()); |
|
62 |
||
63 |
||
28167 | 64 |
(* identifiers *) |
65 |
||
66 |
type task = TaskQueue.task; |
|
67 |
type group = TaskQueue.group; |
|
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 |
89 |
{task = TaskQueue.new_task (), |
|
90 |
group = TaskQueue.new_group (), |
|
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 |
|
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 |
val queue = ref TaskQueue.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; |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
104 |
val canceled = ref ([]: TaskQueue.group list); |
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 |
|
28167 | 117 |
fun wait name = (*requires SYNCHRONIZED*) |
28468 | 118 |
(Multithreading.tracing 3 (fn () => name ^ ": wait ..."); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
119 |
ConditionVar.wait (cond, lock); |
28468 | 120 |
Multithreading.tracing 3 (fn () => name ^ ": ... continue")); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
121 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
122 |
fun wait_timeout name timeout = (*requires SYNCHRONIZED*) |
28468 | 123 |
(Multithreading.tracing 3 (fn () => name ^ ": wait ..."); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
124 |
ConditionVar.waitUntil (cond, lock, Time.+ (Time.now (), timeout)); |
28468 | 125 |
Multithreading.tracing 3 (fn () => name ^ ": ... continue")); |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
126 |
|
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
127 |
fun notify_all () = (*requires SYNCHRONIZED*) |
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
128 |
ConditionVar.broadcast cond; |
28156 | 129 |
|
130 |
end; |
|
131 |
||
132 |
||
28382 | 133 |
(* worker activity *) |
134 |
||
135 |
fun trace_active () = |
|
136 |
let |
|
137 |
val ws = ! workers; |
|
138 |
val m = string_of_int (length ws); |
|
139 |
val n = string_of_int (length (filter #2 ws)); |
|
140 |
in Multithreading.tracing 1 (fn () => "SCHEDULE: " ^ m ^ " workers, " ^ n ^ " active") end; |
|
141 |
||
142 |
fun change_active active = (*requires SYNCHRONIZED*) |
|
143 |
change workers (AList.update Thread.equal (Thread.self (), active)); |
|
144 |
||
145 |
||
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 |
(* execute *) |
28156 | 147 |
|
28167 | 148 |
fun execute name (task, group, run) = |
149 |
let |
|
28382 | 150 |
val _ = trace_active (); |
28979
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
151 |
val ok = setmp_thread_data (name, task) run (); |
28192 | 152 |
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
|
153 |
(change queue (TaskQueue.finish task); |
28186 | 154 |
if ok then () |
28191 | 155 |
else if TaskQueue.cancel (! queue) group then () |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
156 |
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
|
157 |
notify_all ())); |
28167 | 158 |
in () end; |
159 |
||
160 |
||
161 |
(* worker threads *) |
|
162 |
||
28186 | 163 |
fun worker_wait name = (*requires SYNCHRONIZED*) |
164 |
(change_active false; wait name; change_active true); |
|
28162 | 165 |
|
28167 | 166 |
fun worker_next name = (*requires SYNCHRONIZED*) |
167 |
if ! excessive > 0 then |
|
168 |
(dec excessive; |
|
28192 | 169 |
change workers (filter_out (fn (thread, _) => Thread.equal (thread, Thread.self ()))); |
28203 | 170 |
notify_all (); |
28167 | 171 |
NONE) |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
172 |
else |
28186 | 173 |
(case change_result queue TaskQueue.dequeue of |
174 |
NONE => (worker_wait name; worker_next name) |
|
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
175 |
| some => some); |
28156 | 176 |
|
28167 | 177 |
fun worker_loop name = |
28192 | 178 |
(case SYNCHRONIZED name (fn () => worker_next name) of |
28468 | 179 |
NONE => Multithreading.tracing 3 (fn () => name ^ ": exit") |
28167 | 180 |
| SOME work => (execute name work; worker_loop name)); |
28156 | 181 |
|
28167 | 182 |
fun worker_start name = (*requires SYNCHRONIZED*) |
28242 | 183 |
change workers (cons (SimpleThread.fork false (fn () => worker_loop name), true)); |
28156 | 184 |
|
185 |
||
186 |
(* scheduler *) |
|
187 |
||
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
188 |
fun scheduler_next () = (*requires SYNCHRONIZED*) |
28156 | 189 |
let |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
190 |
(*worker threads*) |
28191 | 191 |
val _ = |
28192 | 192 |
(case List.partition (Thread.isActive o #1) (! workers) of |
28191 | 193 |
(_, []) => () |
194 |
| (active, inactive) => |
|
195 |
(workers := active; Multithreading.tracing 0 (fn () => |
|
28192 | 196 |
"SCHEDULE: disposed " ^ string_of_int (length inactive) ^ " dead worker threads"))); |
28382 | 197 |
val _ = trace_active (); |
28191 | 198 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
199 |
val m = if ! do_shutdown then 0 else Multithreading.max_threads_value (); |
28167 | 200 |
val l = length (! workers); |
201 |
val _ = excessive := l - m; |
|
28203 | 202 |
val _ = |
28468 | 203 |
if m > l then funpow (m - l) (fn () => worker_start ("worker " ^ string_of_int (inc next))) () |
28203 | 204 |
else (); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
205 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
206 |
(*canceled groups*) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
207 |
val _ = change canceled (filter_out (TaskQueue.cancel (! queue))); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
208 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
209 |
(*shutdown*) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
210 |
val continue = not (! do_shutdown andalso null (! workers)); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
211 |
val _ = if continue then () else scheduler := NONE; |
28167 | 212 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
213 |
val _ = notify_all (); |
28464 | 214 |
val _ = wait_timeout "scheduler" (Time.fromSeconds 3); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
215 |
in continue end; |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
216 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
217 |
fun scheduler_loop () = |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
218 |
(while SYNCHRONIZED "scheduler" scheduler_next do (); |
28534 | 219 |
Multithreading.tracing 3 (fn () => "scheduler: exit")); |
28156 | 220 |
|
28203 | 221 |
fun scheduler_active () = (*requires SYNCHRONIZED*) |
222 |
(case ! scheduler of NONE => false | SOME thread => Thread.isActive thread); |
|
223 |
||
28464 | 224 |
fun scheduler_check name = SYNCHRONIZED name (fn () => |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
225 |
if not (scheduler_active ()) then |
28534 | 226 |
(Multithreading.tracing 3 (fn () => "scheduler: fork"); |
227 |
do_shutdown := false; scheduler := SOME (SimpleThread.fork false scheduler_loop)) |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
228 |
else if ! do_shutdown then error "Scheduler shutdown in progress" |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
229 |
else ()); |
28156 | 230 |
|
231 |
||
28191 | 232 |
(* future values: fork independent computation *) |
28156 | 233 |
|
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28276
diff
changeset
|
234 |
fun future opt_group deps pri (e: unit -> 'a) = |
28156 | 235 |
let |
28464 | 236 |
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
|
237 |
|
28191 | 238 |
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
|
239 |
|
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); |
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
|
241 |
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
|
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 |
|
28548
003f52c2bb8f
future result: Interrupt invalidates group, but pretends success otherwise;
wenzelm
parents:
28534
diff
changeset
|
249 |
| Exn.Exn Exn.Interrupt => (TaskQueue.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); |
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
|
252 |
|
28192 | 253 |
val task = SYNCHRONIZED "future" (fn () => |
28304
4b0477452943
future tasks: support boolean priorities (true = high, false = low/irrelevant);
wenzelm
parents:
28276
diff
changeset
|
254 |
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
|
255 |
in Future {task = task, group = group, result = result} end; |
28162 | 256 |
|
28979
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
257 |
fun fork e = future NONE [] true e; |
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
258 |
fun fork_group group e = future (SOME group) [] true e; |
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
259 |
fun fork_deps deps e = future NONE (map task_of deps) true e; |
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
260 |
fun fork_background e = future NONE [] false e; |
28186 | 261 |
|
262 |
||
28191 | 263 |
(* join: retrieve results *) |
28186 | 264 |
|
28331
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
265 |
fun join_results [] = [] |
28532 | 266 |
| join_results xs = uninterruptible (fn _ => fn () => |
28331
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
267 |
let |
28464 | 268 |
val _ = scheduler_check "join check"; |
28331
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
269 |
val _ = Multithreading.self_critical () andalso |
28647
8068cdc84e7e
join_results: allow CRITICAL join of finished futures;
wenzelm
parents:
28645
diff
changeset
|
270 |
exists (not o is_finished) xs andalso |
28331
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
271 |
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
|
272 |
|
28386 | 273 |
fun join_loop _ [] = () |
274 |
| join_loop name tasks = |
|
275 |
(case SYNCHRONIZED name (fn () => |
|
28382 | 276 |
change_result queue (TaskQueue.dequeue_towards tasks)) of |
277 |
NONE => () |
|
28386 | 278 |
| 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
|
279 |
val _ = |
33d58fdc177d
join_results: special case for empty list, works without multithreading;
wenzelm
parents:
28320
diff
changeset
|
280 |
(case thread_data () of |
28382 | 281 |
NONE => |
282 |
(*alien thread -- refrain from contending for resources*) |
|
283 |
while exists (not o is_finished) xs |
|
284 |
do SYNCHRONIZED "join_thread" (fn () => wait "join_thread") |
|
28979
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
285 |
| SOME (name, task) => |
28382 | 286 |
(*proper task -- actively work towards results*) |
287 |
let |
|
288 |
val unfinished = xs |> map_filter |
|
289 |
(fn Future {task, result = ref NONE, ...} => SOME task | _ => NONE); |
|
290 |
val _ = SYNCHRONIZED "join" (fn () => |
|
291 |
(change queue (TaskQueue.depend unfinished task); notify_all ())); |
|
28386 | 292 |
val _ = join_loop ("join_loop: " ^ name) unfinished; |
28382 | 293 |
val _ = |
294 |
while exists (not o is_finished) xs |
|
295 |
do SYNCHRONIZED "join_task" (fn () => worker_wait "join_task"); |
|
296 |
in () end); |
|
28186 | 297 |
|
28532 | 298 |
in xs |> map (fn Future {result = ref (SOME res), ...} => res) end) (); |
28186 | 299 |
|
28647
8068cdc84e7e
join_results: allow CRITICAL join of finished futures;
wenzelm
parents:
28645
diff
changeset
|
300 |
fun join_result x = singleton join_results x; |
8068cdc84e7e
join_results: allow CRITICAL join of finished futures;
wenzelm
parents:
28645
diff
changeset
|
301 |
fun join x = Exn.release (join_result x); |
28156 | 302 |
|
28979
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
303 |
fun map f x = fork_deps [x] (fn () => f (join x)); |
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
304 |
|
28191 | 305 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
306 |
(* misc operations *) |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
307 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
308 |
(*focus: collection of high-priority task*) |
28464 | 309 |
fun focus tasks = SYNCHRONIZED "focus" (fn () => |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
310 |
change queue (TaskQueue.focus tasks)); |
28191 | 311 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
312 |
(*interrupt: permissive signal, may get ignored*) |
28197 | 313 |
fun interrupt_task id = SYNCHRONIZED "interrupt" |
314 |
(fn () => TaskQueue.interrupt_external (! queue) id); |
|
28191 | 315 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
316 |
(*cancel: present and future group members will be interrupted eventually*) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
317 |
fun cancel x = |
28464 | 318 |
(scheduler_check "cancel check"; |
28208 | 319 |
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
|
320 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
321 |
|
28203 | 322 |
(*global join and shutdown*) |
323 |
fun shutdown () = |
|
28276 | 324 |
if Multithreading.available then |
28464 | 325 |
(scheduler_check "shutdown check"; |
28276 | 326 |
SYNCHRONIZED "shutdown" (fn () => |
327 |
(while not (scheduler_active ()) do wait "shutdown: scheduler inactive"; |
|
328 |
while not (TaskQueue.is_empty (! queue)) do wait "shutdown: join"; |
|
329 |
do_shutdown := true; |
|
330 |
notify_all (); |
|
331 |
while not (null (! workers)) do wait "shutdown: workers"; |
|
28470 | 332 |
while scheduler_active () do wait "shutdown: scheduler still active"; |
333 |
OS.Process.sleep (Time.fromMilliseconds 300)))) |
|
28276 | 334 |
else (); |
28203 | 335 |
|
28156 | 336 |
end; |
28972 | 337 |
|
338 |
type 'a future = 'a Future.future; |
|
339 |