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