author | wenzelm |
Thu, 11 Sep 2008 13:43:42 +0200 | |
changeset 28201 | 7ae5cdb7b122 |
parent 28197 | 7053c539ecd8 |
child 28202 | 23cb9a974630 |
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 |
28156 | 32 |
type 'a T |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
33 |
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
|
34 |
val group_of: 'a T -> group |
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 shutdown_request: unit -> unit |
28191 | 36 |
val future: group option -> task list -> (unit -> 'a) -> 'a T |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
37 |
val fork: (unit -> 'a) -> 'a T |
28193
7ed74d0ba607
replaced join_all by join_results, which returns Exn.results;
wenzelm
parents:
28192
diff
changeset
|
38 |
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
|
39 |
val join: 'a T -> 'a |
28197 | 40 |
val cancel: 'a T -> unit |
41 |
val interrupt_task: string -> unit |
|
28156 | 42 |
end; |
43 |
||
44 |
structure Future: FUTURE = |
|
45 |
struct |
|
46 |
||
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
|
47 |
(** 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
|
48 |
|
28167 | 49 |
(* identifiers *) |
50 |
||
51 |
type task = TaskQueue.task; |
|
52 |
type group = TaskQueue.group; |
|
53 |
||
28177
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
54 |
local val tag = Universal.tag () : (task * group) option Universal.tag in |
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
55 |
fun thread_data () = the_default NONE (Thread.getLocal tag); |
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
56 |
fun set_thread_data x = Thread.setLocal (tag, x); |
28167 | 57 |
end; |
58 |
||
59 |
||
60 |
(* datatype future *) |
|
61 |
||
62 |
datatype 'a T = Future of |
|
63 |
{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
|
64 |
group: group, |
28167 | 65 |
result: 'a Exn.result option ref}; |
66 |
||
67 |
fun task_of (Future {task, ...}) = task; |
|
68 |
fun group_of (Future {group, ...}) = group; |
|
69 |
||
70 |
||
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
|
71 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
72 |
(** 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
|
73 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
74 |
(* 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
|
75 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
76 |
val queue = ref TaskQueue.empty; |
28192 | 77 |
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
|
78 |
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
|
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 |
val excessive = ref 0; |
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 |
fun trace_active () = |
28192 | 83 |
let |
84 |
val ws = ! workers; |
|
85 |
val m = string_of_int (length ws); |
|
86 |
val n = string_of_int (length (filter #2 ws)); |
|
87 |
in Multithreading.tracing 1 (fn () => "SCHEDULE: " ^ m ^ " workers, " ^ n ^ " active") 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
|
88 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
89 |
|
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 |
(* requests *) |
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 |
datatype request = Shutdown | Cancel of group; |
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 requests = Mailbox.create () : request Mailbox.T; |
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 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
95 |
fun shutdown_request () = Mailbox.send requests Shutdown; |
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 |
fun cancel_request group = Mailbox.send requests (Cancel group); |
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 |
||
28192 | 106 |
fun SYNCHRONIZED name e = uninterruptible (fn restore_attributes => fn () => |
28162 | 107 |
let |
28192 | 108 |
val _ = Multithreading.tracing 4 (fn () => name ^ ": locking"); |
28162 | 109 |
val _ = Mutex.lock lock; |
28192 | 110 |
val _ = Multithreading.tracing 4 (fn () => name ^ ": locked"); |
28162 | 111 |
val result = Exn.capture (restore_attributes e) (); |
112 |
val _ = Mutex.unlock lock; |
|
28192 | 113 |
val _ = Multithreading.tracing 4 (fn () => name ^ ": unlocked"); |
28162 | 114 |
in Exn.release result end) (); |
28156 | 115 |
|
28167 | 116 |
fun wait name = (*requires SYNCHRONIZED*) |
117 |
let |
|
28192 | 118 |
val _ = Multithreading.tracing 4 (fn () => name ^ ": waiting"); |
28167 | 119 |
val _ = ConditionVar.wait (cond, lock); |
28192 | 120 |
val _ = Multithreading.tracing 4 (fn () => name ^ ": notified"); |
28167 | 121 |
in () end; |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
122 |
|
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
123 |
fun notify_all () = (*requires SYNCHRONIZED*) |
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
124 |
ConditionVar.broadcast cond; |
28156 | 125 |
|
126 |
end; |
|
127 |
||
128 |
||
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
|
129 |
(* execute *) |
28156 | 130 |
|
28167 | 131 |
fun execute name (task, group, run) = |
132 |
let |
|
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 |
val _ = set_thread_data (SOME (task, group)); |
28167 | 134 |
val _ = Multithreading.tracing 4 (fn () => name ^ ": running"); |
135 |
val ok = run (); |
|
136 |
val _ = Multithreading.tracing 4 (fn () => name ^ ": finished"); |
|
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 |
val _ = set_thread_data NONE; |
28192 | 138 |
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
|
139 |
(change queue (TaskQueue.finish task); |
28186 | 140 |
if ok then () |
28191 | 141 |
else if TaskQueue.cancel (! queue) group then () |
28186 | 142 |
else cancel_request 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
|
143 |
notify_all ())); |
28167 | 144 |
in () end; |
145 |
||
146 |
||
147 |
(* worker threads *) |
|
148 |
||
28192 | 149 |
fun change_active active = (*requires SYNCHRONIZED*) |
150 |
(change workers (AList.update Thread.equal (Thread.self (), active)); trace_active ()); |
|
28186 | 151 |
|
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 ()))); |
28167 | 159 |
NONE) |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
160 |
else |
28186 | 161 |
(case change_result queue TaskQueue.dequeue of |
162 |
NONE => (worker_wait name; worker_next name) |
|
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
163 |
| some => some); |
28156 | 164 |
|
28167 | 165 |
fun worker_loop name = |
28192 | 166 |
(case SYNCHRONIZED name (fn () => worker_next name) of |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
167 |
NONE => () |
28167 | 168 |
| SOME work => (execute name work; worker_loop name)); |
28156 | 169 |
|
28167 | 170 |
fun worker_start name = (*requires SYNCHRONIZED*) |
28192 | 171 |
change workers |
172 |
(cons (Thread.fork (fn () => worker_loop name, Multithreading.no_interrupts), true)); |
|
28156 | 173 |
|
174 |
||
175 |
(* scheduler *) |
|
176 |
||
28192 | 177 |
fun scheduler_fork shutdown = SYNCHRONIZED "scheduler_fork" (fn () => |
28156 | 178 |
let |
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
|
179 |
val _ = trace_active (); |
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"))); |
28191 | 186 |
|
187 |
val m = if shutdown then 0 else Multithreading.max_threads_value (); |
|
28167 | 188 |
val l = length (! workers); |
189 |
val _ = excessive := l - m; |
|
28191 | 190 |
val _ = List.app (fn i => worker_start ("worker " ^ string_of_int i)) (l upto m - 1); |
28192 | 191 |
val _ = if shutdown then notify_all () else (); |
192 |
in shutdown andalso null (! workers) end); |
|
28167 | 193 |
|
28191 | 194 |
fun scheduler_loop (shutdown, canceled) = |
195 |
if scheduler_fork shutdown then () |
|
196 |
else |
|
28192 | 197 |
let |
198 |
val canceled' = SYNCHRONIZED "scheduler" |
|
199 |
(fn () => filter_out (TaskQueue.cancel (! queue)) canceled); |
|
200 |
in |
|
28191 | 201 |
(case Mailbox.receive_timeout (Time.fromSeconds 1) requests of |
202 |
SOME Shutdown => scheduler_loop (true, canceled') |
|
203 |
| SOME (Cancel group) => scheduler_loop (shutdown, group :: canceled') |
|
204 |
| NONE => scheduler_loop (shutdown, canceled')) |
|
205 |
end; |
|
28156 | 206 |
|
28192 | 207 |
fun scheduler_check () = SYNCHRONIZED "scheduler_check" (fn () => |
28167 | 208 |
if (case ! scheduler of NONE => false | SOME thread => Thread.isActive thread) then () |
28191 | 209 |
else scheduler := |
210 |
SOME (Thread.fork (fn () => scheduler_loop (false, []), Multithreading.no_interrupts))); |
|
28156 | 211 |
|
212 |
||
28191 | 213 |
(* future values: fork independent computation *) |
28156 | 214 |
|
28191 | 215 |
fun future opt_group deps (e: unit -> 'a) = |
28156 | 216 |
let |
28191 | 217 |
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
|
218 |
|
28191 | 219 |
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
|
220 |
|
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
221 |
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
|
222 |
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
|
223 |
(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
|
224 |
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
|
225 |
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
|
226 |
|
28192 | 227 |
val task = SYNCHRONIZED "future" (fn () => |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
228 |
change_result queue (TaskQueue.enqueue group deps run) before notify_all ()); |
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
229 |
in Future {task = task, group = group, result = result} end; |
28162 | 230 |
|
28191 | 231 |
fun fork e = future (Option.map #2 (thread_data ())) [] e; |
28186 | 232 |
|
233 |
||
28191 | 234 |
(* join: retrieve results *) |
28186 | 235 |
|
28193
7ed74d0ba607
replaced join_all by join_results, which returns Exn.results;
wenzelm
parents:
28192
diff
changeset
|
236 |
fun join_results xs = |
28156 | 237 |
let |
28193
7ed74d0ba607
replaced join_all by join_results, which returns Exn.results;
wenzelm
parents:
28192
diff
changeset
|
238 |
val _ = Multithreading.self_critical () andalso |
7ed74d0ba607
replaced join_all by join_results, which returns Exn.results;
wenzelm
parents:
28192
diff
changeset
|
239 |
error "Cannot join future values within critical section"; |
28191 | 240 |
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
|
241 |
|
28186 | 242 |
fun unfinished () = |
243 |
xs |> map_filter (fn Future {task, result = ref NONE, ...} => SOME task | _ => NONE); |
|
244 |
||
245 |
(*alien thread -- refrain from contending for resources*) |
|
246 |
fun passive_join () = (*requires SYNCHRONIZED*) |
|
247 |
(case unfinished () of [] => () |
|
248 |
| _ => (wait "join"; passive_join ())); |
|
249 |
||
250 |
(*proper worker thread -- actively work towards results*) |
|
251 |
fun active_join () = (*requires SYNCHRONIZED*) |
|
252 |
(case unfinished () of [] => () |
|
253 |
| tasks => |
|
254 |
(case change_result queue (TaskQueue.dequeue_towards tasks) of |
|
255 |
NONE => (worker_wait "join"; active_join ()) |
|
256 |
| SOME work => (execute "join" work; active_join ()))); |
|
257 |
||
258 |
val _ = |
|
259 |
(case thread_data () of |
|
28192 | 260 |
NONE => SYNCHRONIZED "join" passive_join |
261 |
| SOME (task, _) => SYNCHRONIZED "join" (fn () => |
|
28186 | 262 |
(change queue (TaskQueue.depend (unfinished ()) task); active_join ()))); |
263 |
||
28193
7ed74d0ba607
replaced join_all by join_results, which returns Exn.results;
wenzelm
parents:
28192
diff
changeset
|
264 |
in xs |> map (fn Future {result = ref (SOME res), ...} => res) end; |
28186 | 265 |
|
28193
7ed74d0ba607
replaced join_all by join_results, which returns Exn.results;
wenzelm
parents:
28192
diff
changeset
|
266 |
fun join x = Exn.release (singleton join_results x); |
28156 | 267 |
|
28191 | 268 |
|
269 |
(* termination *) |
|
270 |
||
271 |
(*cancel: present and future group members will be interrupted eventually*) |
|
272 |
fun cancel x = (scheduler_check (); cancel_request (group_of x)); |
|
273 |
||
274 |
(*interrupt: adhoc signal, permissive, may get ignored*) |
|
28197 | 275 |
fun interrupt_task id = SYNCHRONIZED "interrupt" |
276 |
(fn () => TaskQueue.interrupt_external (! queue) id); |
|
28191 | 277 |
|
28156 | 278 |
end; |