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