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