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