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