author | wenzelm |
Thu, 05 Nov 2009 13:01:11 +0100 | |
changeset 33415 | 352fe8e9162d |
parent 33413 | cb409680dda8 |
child 33416 | 13d00799fe49 |
permissions | -rw-r--r-- |
28156 | 1 |
(* Title: Pure/Concurrent/future.ML |
2 |
Author: Makarius |
|
3 |
||
32246 | 4 |
Future values, see also |
5 |
http://www4.in.tum.de/~wenzelm/papers/parallel-isabelle.pdf |
|
28201 | 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 |
|
32220 | 15 |
group to be interrupted eventually. Groups are block-structured. |
28201 | 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 |
|
29119 | 30 |
type task = Task_Queue.task |
31 |
type group = Task_Queue.group |
|
32058 | 32 |
val is_worker: unit -> bool |
32814
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
33 |
val worker_task: unit -> Task_Queue.task option |
32102 | 34 |
val worker_group: unit -> Task_Queue.group option |
28972 | 35 |
type 'a future |
36 |
val task_of: 'a future -> task |
|
37 |
val group_of: 'a future -> group |
|
38 |
val peek: 'a future -> 'a Exn.result option |
|
39 |
val is_finished: 'a future -> bool |
|
28997 | 40 |
val value: 'a -> 'a future |
28979
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
41 |
val fork_group: group -> (unit -> 'a) -> 'a future |
32724 | 42 |
val fork_deps_pri: 'b future list -> int -> (unit -> 'a) -> 'a future |
28979
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
43 |
val fork_deps: 'b future list -> (unit -> 'a) -> 'a future |
29119 | 44 |
val fork_pri: int -> (unit -> 'a) -> 'a future |
32724 | 45 |
val fork: (unit -> 'a) -> 'a future |
28972 | 46 |
val join_results: 'a future list -> 'a Exn.result list |
47 |
val join_result: 'a future -> 'a Exn.result |
|
48 |
val join: 'a future -> 'a |
|
49 |
val map: ('a -> 'b) -> 'a future -> 'b future |
|
30618
046f4f986fb5
restricted interrupts for tasks running as future worker thread -- attempt to prevent interrupt race conditions;
wenzelm
parents:
30612
diff
changeset
|
50 |
val interruptible_task: ('a -> 'b) -> 'a -> 'b |
29431 | 51 |
val cancel_group: group -> unit |
28972 | 52 |
val cancel: 'a future -> unit |
28203 | 53 |
val shutdown: unit -> unit |
28156 | 54 |
end; |
55 |
||
56 |
structure Future: FUTURE = |
|
57 |
struct |
|
58 |
||
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
|
59 |
(** 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
|
60 |
|
28167 | 61 |
(* identifiers *) |
62 |
||
29119 | 63 |
type task = Task_Queue.task; |
64 |
type group = Task_Queue.group; |
|
28167 | 65 |
|
32058 | 66 |
local |
33408 | 67 |
val tag = Universal.tag () : (task * group) option Universal.tag; |
32058 | 68 |
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
|
69 |
fun thread_data () = the_default NONE (Thread.getLocal tag); |
32058 | 70 |
fun setmp_thread_data data f x = |
71 |
Library.setmp_thread_data tag (thread_data ()) (SOME data) f x; |
|
28167 | 72 |
end; |
73 |
||
32058 | 74 |
val is_worker = is_some o thread_data; |
33408 | 75 |
val worker_task = Option.map #1 o thread_data; |
76 |
val worker_group = Option.map #2 o thread_data; |
|
32058 | 77 |
|
28167 | 78 |
|
79 |
(* datatype future *) |
|
80 |
||
28972 | 81 |
datatype 'a future = Future of |
28167 | 82 |
{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
|
83 |
group: group, |
32253 | 84 |
result: 'a Exn.result option Synchronized.var}; |
28167 | 85 |
|
86 |
fun task_of (Future {task, ...}) = task; |
|
87 |
fun group_of (Future {group, ...}) = group; |
|
32253 | 88 |
fun result_of (Future {result, ...}) = result; |
28167 | 89 |
|
32592
e29c0b7dcf66
Synchronized.value does not require locking, since assigments are atomic;
wenzelm
parents:
32420
diff
changeset
|
90 |
fun peek x = Synchronized.value (result_of x); |
28558 | 91 |
fun is_finished x = is_some (peek x); |
28320 | 92 |
|
28997 | 93 |
fun value x = Future |
29119 | 94 |
{task = Task_Queue.new_task 0, |
32102 | 95 |
group = Task_Queue.new_group NONE, |
32253 | 96 |
result = Synchronized.var "future" (SOME (Exn.Result x))}; |
28997 | 97 |
|
28167 | 98 |
|
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
|
99 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
100 |
(** 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
|
101 |
|
8c0335bc9336
inherit group from running thread, or create a new one -- make it harder to re-use canceled groups;
wenzelm
parents:
28170
diff
changeset
|
102 |
(* synchronization *) |
28156 | 103 |
|
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
104 |
val scheduler_event = ConditionVar.conditionVar (); |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
105 |
val work_available = ConditionVar.conditionVar (); |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
106 |
val work_finished = ConditionVar.conditionVar (); |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
107 |
|
28156 | 108 |
local |
109 |
val lock = Mutex.mutex (); |
|
110 |
in |
|
111 |
||
28575 | 112 |
fun SYNCHRONIZED name = SimpleThread.synchronized name lock; |
28156 | 113 |
|
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
114 |
fun wait cond = (*requires SYNCHRONIZED*) |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
115 |
Multithreading.sync_wait NONE NONE cond lock; |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
116 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
117 |
fun wait_timeout timeout cond = (*requires SYNCHRONIZED*) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
118 |
Multithreading.sync_wait NONE (SOME (Time.+ (Time.now (), timeout))) cond lock; |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
119 |
|
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
120 |
fun signal cond = (*requires SYNCHRONIZED*) |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
121 |
ConditionVar.signal cond; |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
122 |
|
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
123 |
fun broadcast cond = (*requires SYNCHRONIZED*) |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
124 |
ConditionVar.broadcast cond; |
28156 | 125 |
|
32248
0241916a5f06
more precise treatment of scheduler_event: continous pulse (50ms) instead of flooding, which was burning many CPU cycles in spare threads;
wenzelm
parents:
32247
diff
changeset
|
126 |
fun broadcast_work () = (*requires SYNCHRONIZED*) |
0241916a5f06
more precise treatment of scheduler_event: continous pulse (50ms) instead of flooding, which was burning many CPU cycles in spare threads;
wenzelm
parents:
32247
diff
changeset
|
127 |
(ConditionVar.broadcast work_available; |
32225
d5d6f47fb018
cancel: improved reactivity due to more careful broadcasting;
wenzelm
parents:
32224
diff
changeset
|
128 |
ConditionVar.broadcast work_finished); |
d5d6f47fb018
cancel: improved reactivity due to more careful broadcasting;
wenzelm
parents:
32224
diff
changeset
|
129 |
|
28156 | 130 |
end; |
131 |
||
132 |
||
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
133 |
(* global state *) |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
134 |
|
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
135 |
val queue = Unsynchronized.ref Task_Queue.empty; |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
136 |
val next = Unsynchronized.ref 0; |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
137 |
val scheduler = Unsynchronized.ref (NONE: Thread.thread option); |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
138 |
val canceled = Unsynchronized.ref ([]: Task_Queue.group list); |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
139 |
val do_shutdown = Unsynchronized.ref false; |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
140 |
val max_workers = Unsynchronized.ref 0; |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
141 |
val max_active = Unsynchronized.ref 0; |
33411
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
142 |
val worker_trend = Unsynchronized.ref 0; |
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
143 |
|
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
144 |
datatype worker_state = Working | Waiting | Sleeping; |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
145 |
val workers = Unsynchronized.ref ([]: (Thread.thread * worker_state Unsynchronized.ref) list); |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
146 |
|
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
147 |
fun count_workers state = (*requires SYNCHRONIZED*) |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
148 |
fold (fn (_, state_ref) => fn i => if ! state_ref = state then i + 1 else i) (! workers) 0; |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
149 |
|
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
150 |
|
32099 | 151 |
(* execute future jobs *) |
152 |
||
153 |
fun future_job group (e: unit -> 'a) = |
|
154 |
let |
|
32253 | 155 |
val result = Synchronized.var "future" (NONE: 'a Exn.result option); |
32107
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
156 |
fun job ok = |
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
157 |
let |
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
158 |
val res = |
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
159 |
if ok then |
32230
9f6461b1c9cc
interruptible: Thread.testInterrupt before changing thread attributes;
wenzelm
parents:
32229
diff
changeset
|
160 |
Exn.capture (fn () => |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
161 |
Multithreading.with_attributes Multithreading.private_interrupts (fn _ => e ())) () |
32107
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
162 |
else Exn.Exn Exn.Interrupt; |
33061
e3e61133e0fc
use Synchronized.assign to achieve actual immutable results;
wenzelm
parents:
32814
diff
changeset
|
163 |
val _ = Synchronized.assign result (K (SOME res)); |
32107
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
164 |
in |
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
165 |
(case res of |
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
166 |
Exn.Exn exn => (Task_Queue.cancel_group group exn; false) |
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
167 |
| Exn.Result _ => true) |
47d0da617fcc
future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents:
32102
diff
changeset
|
168 |
end; |
32099 | 169 |
in (result, job) end; |
28156 | 170 |
|
29341
6bb007a0f9f2
more reactive scheduler: reduced loop timeout, propagate broadcast interrupt via TaskQueue.cancel_all;
wenzelm
parents:
29119
diff
changeset
|
171 |
fun do_cancel group = (*requires SYNCHRONIZED*) |
32738 | 172 |
(Unsynchronized.change canceled (insert Task_Queue.eq_group group); |
173 |
broadcast scheduler_event); |
|
29341
6bb007a0f9f2
more reactive scheduler: reduced loop timeout, propagate broadcast interrupt via TaskQueue.cancel_all;
wenzelm
parents:
29119
diff
changeset
|
174 |
|
33408 | 175 |
fun execute (task, group, jobs) = |
28167 | 176 |
let |
32102 | 177 |
val valid = not (Task_Queue.is_canceled group); |
33408 | 178 |
val ok = setmp_thread_data (task, group) (fn () => |
29384
a3c7e9ae9b71
more robust propagation of errors through bulk jobs;
wenzelm
parents:
29366
diff
changeset
|
179 |
fold (fn job => fn ok => job valid andalso ok) jobs true) (); |
32246 | 180 |
val _ = SYNCHRONIZED "finish" (fn () => |
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
181 |
let |
32738 | 182 |
val maximal = Unsynchronized.change_result queue (Task_Queue.finish task); |
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
183 |
val _ = |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
184 |
if ok then () |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
185 |
else if Task_Queue.cancel (! queue) group then () |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
186 |
else do_cancel group; |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
187 |
val _ = broadcast work_finished; |
33413
cb409680dda8
avoid broadcast work_available, use daisy-chained signal instead;
wenzelm
parents:
33411
diff
changeset
|
188 |
val _ = if maximal then () else signal work_available; |
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
189 |
in () end); |
28167 | 190 |
in () end; |
191 |
||
192 |
||
193 |
(* worker threads *) |
|
194 |
||
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
195 |
fun worker_wait active cond = (*requires SYNCHRONIZED*) |
33406
1ddcb8472bd2
slightly leaner and more direct control of worker activity etc.;
wenzelm
parents:
33061
diff
changeset
|
196 |
let |
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
197 |
val state = |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
198 |
(case AList.lookup Thread.equal (! workers) (Thread.self ()) of |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
199 |
SOME state => state |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
200 |
| NONE => raise Fail "Unregistered worker thread"); |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
201 |
val _ = state := (if active then Waiting else Sleeping); |
33406
1ddcb8472bd2
slightly leaner and more direct control of worker activity etc.;
wenzelm
parents:
33061
diff
changeset
|
202 |
val _ = wait cond; |
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
203 |
val _ = state := Working; |
33406
1ddcb8472bd2
slightly leaner and more direct control of worker activity etc.;
wenzelm
parents:
33061
diff
changeset
|
204 |
in () end; |
28162 | 205 |
|
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
206 |
fun worker_next () = (*requires SYNCHRONIZED*) |
33406
1ddcb8472bd2
slightly leaner and more direct control of worker activity etc.;
wenzelm
parents:
33061
diff
changeset
|
207 |
if length (! workers) > ! max_workers then |
1ddcb8472bd2
slightly leaner and more direct control of worker activity etc.;
wenzelm
parents:
33061
diff
changeset
|
208 |
(Unsynchronized.change workers (AList.delete Thread.equal (Thread.self ())); |
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
209 |
signal work_available; |
28167 | 210 |
NONE) |
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
211 |
else if count_workers Working > ! max_active then |
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
212 |
(worker_wait false work_available; worker_next ()) |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
213 |
else |
32738 | 214 |
(case Unsynchronized.change_result queue (Task_Queue.dequeue (Thread.self ())) of |
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
215 |
NONE => (worker_wait false work_available; worker_next ()) |
33413
cb409680dda8
avoid broadcast work_available, use daisy-chained signal instead;
wenzelm
parents:
33411
diff
changeset
|
216 |
| some => (signal work_available; some)); |
28156 | 217 |
|
28167 | 218 |
fun worker_loop name = |
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
219 |
(case SYNCHRONIZED name (fn () => worker_next ()) of |
29119 | 220 |
NONE => () |
33408 | 221 |
| SOME work => (execute work; worker_loop name)); |
28156 | 222 |
|
33407
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
223 |
fun worker_start name = (*requires SYNCHRONIZED*) |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
224 |
Unsynchronized.change workers (cons (SimpleThread.fork false (fn () => worker_loop name), |
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
225 |
Unsynchronized.ref Working)); |
28156 | 226 |
|
227 |
||
228 |
(* scheduler *) |
|
229 |
||
33407
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
230 |
val status_ticks = Unsynchronized.ref 0; |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
231 |
|
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
232 |
val last_round = Unsynchronized.ref Time.zeroTime; |
32248
0241916a5f06
more precise treatment of scheduler_event: continous pulse (50ms) instead of flooding, which was burning many CPU cycles in spare threads;
wenzelm
parents:
32247
diff
changeset
|
233 |
val next_round = Time.fromMilliseconds 50; |
32226 | 234 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
235 |
fun scheduler_next () = (*requires SYNCHRONIZED*) |
28156 | 236 |
let |
33407
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
237 |
val now = Time.now (); |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
238 |
val tick = Time.<= (Time.+ (! last_round, next_round), now); |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
239 |
val _ = if tick then last_round := now else (); |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
240 |
|
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
241 |
|
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
242 |
(* queue and worker status *) |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
243 |
|
32226 | 244 |
val _ = |
33407
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
245 |
if tick then Unsynchronized.change status_ticks (fn i => (i + 1) mod 10) else (); |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
246 |
val _ = |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
247 |
if tick andalso ! status_ticks = 0 then |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
248 |
Multithreading.tracing 1 (fn () => |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
249 |
let |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
250 |
val {ready, pending, running} = Task_Queue.status (! queue); |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
251 |
val total = length (! workers); |
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
252 |
val active = count_workers Working; |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
253 |
val waiting = count_workers Waiting; |
33407
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
254 |
in |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
255 |
"SCHEDULE " ^ Time.toString now ^ ": " ^ |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
256 |
string_of_int ready ^ " ready, " ^ |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
257 |
string_of_int pending ^ " pending, " ^ |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
258 |
string_of_int running ^ " running; " ^ |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
259 |
string_of_int total ^ " workers, " ^ |
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
260 |
string_of_int active ^ " active, " ^ |
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
261 |
string_of_int waiting ^ " waiting " |
33407
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
262 |
end) |
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
263 |
else (); |
32053 | 264 |
|
28191 | 265 |
val _ = |
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
266 |
if forall (Thread.isActive o #1) (! workers) then () |
32095 | 267 |
else |
33409 | 268 |
let |
269 |
val (alive, dead) = List.partition (Thread.isActive o #1) (! workers); |
|
270 |
val _ = workers := alive; |
|
271 |
in |
|
272 |
Multithreading.tracing 0 (fn () => |
|
273 |
"SCHEDULE: disposed " ^ string_of_int (length dead) ^ " dead worker threads") |
|
274 |
end; |
|
28191 | 275 |
|
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
276 |
|
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
277 |
(* worker pool adjustments *) |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
278 |
|
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
279 |
val max_active0 = ! max_active; |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
280 |
val max_workers0 = ! max_workers; |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
281 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
282 |
val m = if ! do_shutdown then 0 else Multithreading.max_threads_value (); |
33406
1ddcb8472bd2
slightly leaner and more direct control of worker activity etc.;
wenzelm
parents:
33061
diff
changeset
|
283 |
val _ = max_active := m; |
1ddcb8472bd2
slightly leaner and more direct control of worker activity etc.;
wenzelm
parents:
33061
diff
changeset
|
284 |
|
33411
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
285 |
val mm = |
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
286 |
if ! do_shutdown then 0 |
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
287 |
else if m = 9999 then 1 |
33413
cb409680dda8
avoid broadcast work_available, use daisy-chained signal instead;
wenzelm
parents:
33411
diff
changeset
|
288 |
else Int.min (Int.max (count_workers Working + 2 * count_workers Waiting, m), 4 * m); |
33411
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
289 |
val _ = |
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
290 |
if tick andalso mm > ! max_workers then |
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
291 |
Unsynchronized.change worker_trend (fn w => if w < 0 then 0 else w + 1) |
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
292 |
else if tick andalso mm < ! max_workers then |
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
293 |
Unsynchronized.change worker_trend (fn w => if w > 0 then 0 else w - 1) |
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
294 |
else (); |
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
295 |
val _ = |
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
296 |
if mm = 0 orelse ! worker_trend > 50 orelse ! worker_trend < ~50 then |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
297 |
max_workers := mm |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
298 |
else if ! worker_trend > 5 andalso ! max_workers < 2 * m then |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
299 |
max_workers := Int.min (mm, 2 * m) |
33411
a07558eb5029
worker_next: treat wait for work_available as Sleeping, not Waiting;
wenzelm
parents:
33410
diff
changeset
|
300 |
else (); |
33406
1ddcb8472bd2
slightly leaner and more direct control of worker activity etc.;
wenzelm
parents:
33061
diff
changeset
|
301 |
|
33407
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
302 |
val missing = ! max_workers - length (! workers); |
28203 | 303 |
val _ = |
33407
1427333220bc
worker_next: ensure that work_available is passed on before sleeping (was occasionally lost when worker configuration changed, causing scheduler deadlock);
wenzelm
parents:
33406
diff
changeset
|
304 |
if missing > 0 then |
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
305 |
funpow missing (fn () => |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
306 |
ignore (worker_start ("worker " ^ string_of_int (Unsynchronized.inc next)))) () |
28203 | 307 |
else (); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
308 |
|
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
309 |
val _ = |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
310 |
if ! max_active = max_active0 andalso ! max_workers = max_workers0 then () |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
311 |
else signal work_available; |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
312 |
|
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
313 |
|
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
314 |
(* canceled groups *) |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
315 |
|
32225
d5d6f47fb018
cancel: improved reactivity due to more careful broadcasting;
wenzelm
parents:
32224
diff
changeset
|
316 |
val _ = |
d5d6f47fb018
cancel: improved reactivity due to more careful broadcasting;
wenzelm
parents:
32224
diff
changeset
|
317 |
if null (! canceled) then () |
32293 | 318 |
else |
319 |
(Multithreading.tracing 1 (fn () => |
|
320 |
string_of_int (length (! canceled)) ^ " canceled groups"); |
|
32738 | 321 |
Unsynchronized.change canceled (filter_out (Task_Queue.cancel (! queue))); |
32293 | 322 |
broadcast_work ()); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
323 |
|
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
324 |
|
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
325 |
(* delay loop *) |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
326 |
|
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
327 |
val _ = Exn.release (wait_timeout next_round scheduler_event); |
28167 | 328 |
|
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
329 |
|
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
330 |
(* shutdown *) |
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
331 |
|
32228
7622c03141b0
scheduler: shutdown spontaneously (after some delay) if queue is empty;
wenzelm
parents:
32227
diff
changeset
|
332 |
val _ = if Task_Queue.is_empty (! queue) then do_shutdown := true else (); |
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
333 |
val continue = not (! do_shutdown andalso null (! workers)); |
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
334 |
val _ = if continue then () else scheduler := NONE; |
33415
352fe8e9162d
worker_next: plain signalling via work_available only, not scheduler_event;
wenzelm
parents:
33413
diff
changeset
|
335 |
|
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
336 |
val _ = broadcast scheduler_event; |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
337 |
in continue end |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
338 |
handle Exn.Interrupt => |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
339 |
(Multithreading.tracing 1 (fn () => "Interrupt"); |
32296 | 340 |
uninterruptible (fn _ => fn () => List.app do_cancel (Task_Queue.cancel_all (! queue))) (); |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
341 |
scheduler_next ()); |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
342 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
343 |
fun scheduler_loop () = |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
344 |
Multithreading.with_attributes |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
345 |
(Multithreading.sync_interrupts Multithreading.public_interrupts) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
346 |
(fn _ => while SYNCHRONIZED "scheduler" (fn () => scheduler_next ()) do ()); |
28156 | 347 |
|
28203 | 348 |
fun scheduler_active () = (*requires SYNCHRONIZED*) |
349 |
(case ! scheduler of NONE => false | SOME thread => Thread.isActive thread); |
|
350 |
||
32228
7622c03141b0
scheduler: shutdown spontaneously (after some delay) if queue is empty;
wenzelm
parents:
32227
diff
changeset
|
351 |
fun scheduler_check () = (*requires SYNCHRONIZED*) |
7622c03141b0
scheduler: shutdown spontaneously (after some delay) if queue is empty;
wenzelm
parents:
32227
diff
changeset
|
352 |
(do_shutdown := false; |
32248
0241916a5f06
more precise treatment of scheduler_event: continous pulse (50ms) instead of flooding, which was burning many CPU cycles in spare threads;
wenzelm
parents:
32247
diff
changeset
|
353 |
if scheduler_active () then () |
0241916a5f06
more precise treatment of scheduler_event: continous pulse (50ms) instead of flooding, which was burning many CPU cycles in spare threads;
wenzelm
parents:
32247
diff
changeset
|
354 |
else scheduler := SOME (SimpleThread.fork false scheduler_loop)); |
28156 | 355 |
|
356 |
||
29366 | 357 |
|
358 |
(** futures **) |
|
28156 | 359 |
|
29366 | 360 |
(* fork *) |
361 |
||
362 |
fun fork_future opt_group deps pri e = |
|
363 |
let |
|
32102 | 364 |
val group = |
365 |
(case opt_group of |
|
366 |
SOME group => group |
|
367 |
| NONE => Task_Queue.new_group (worker_group ())); |
|
29366 | 368 |
val (result, job) = future_job group e; |
32246 | 369 |
val task = SYNCHRONIZED "enqueue" (fn () => |
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
370 |
let |
32738 | 371 |
val (task, minimal) = |
372 |
Unsynchronized.change_result queue (Task_Queue.enqueue group deps pri job); |
|
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
373 |
val _ = if minimal then signal work_available else (); |
32228
7622c03141b0
scheduler: shutdown spontaneously (after some delay) if queue is empty;
wenzelm
parents:
32227
diff
changeset
|
374 |
val _ = scheduler_check (); |
32219
9a2566d1fdbd
more specific conditions: scheduler_event, work_available, work_finished -- considereably reduces overhead with many threads;
wenzelm
parents:
32186
diff
changeset
|
375 |
in task end); |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
376 |
in Future {task = task, group = group, result = result} end; |
28162 | 377 |
|
29366 | 378 |
fun fork_group group e = fork_future (SOME group) [] 0 e; |
32724 | 379 |
fun fork_deps_pri deps pri e = fork_future NONE (map task_of deps) pri e; |
380 |
fun fork_deps deps e = fork_deps_pri deps 0 e; |
|
381 |
fun fork_pri pri e = fork_deps_pri [] pri e; |
|
382 |
fun fork e = fork_deps [] e; |
|
28186 | 383 |
|
384 |
||
29366 | 385 |
(* join *) |
386 |
||
29551
95e469919c3e
join_results: when dependencies are resulved (but not finished yet),
wenzelm
parents:
29431
diff
changeset
|
387 |
local |
95e469919c3e
join_results: when dependencies are resulved (but not finished yet),
wenzelm
parents:
29431
diff
changeset
|
388 |
|
32099 | 389 |
fun get_result x = |
390 |
(case peek x of |
|
32102 | 391 |
NONE => Exn.Exn (SYS_ERROR "unfinished future") |
392 |
| SOME (Exn.Exn Exn.Interrupt) => |
|
393 |
Exn.Exn (Exn.EXCEPTIONS (Exn.flatten_list (Task_Queue.group_status (group_of x)))) |
|
394 |
| SOME res => res); |
|
28186 | 395 |
|
33409 | 396 |
fun passive_wait x = |
33061
e3e61133e0fc
use Synchronized.assign to achieve actual immutable results;
wenzelm
parents:
32814
diff
changeset
|
397 |
Synchronized.readonly_access (result_of x) (fn NONE => NONE | SOME _ => SOME ()); |
32224 | 398 |
|
32095 | 399 |
fun join_next deps = (*requires SYNCHRONIZED*) |
32224 | 400 |
if null deps then NONE |
401 |
else |
|
32738 | 402 |
(case Unsynchronized.change_result queue (Task_Queue.dequeue_towards (Thread.self ()) deps) of |
32224 | 403 |
(NONE, []) => NONE |
33410
e351f4c1f18c
worker activity: distinguish between waiting (formerly active) and sleeping;
wenzelm
parents:
33409
diff
changeset
|
404 |
| (NONE, deps') => (worker_wait true work_finished; join_next deps') |
32224 | 405 |
| (SOME work, deps') => SOME (work, deps')); |
32095 | 406 |
|
32814
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
407 |
fun execute_work NONE = () |
33408 | 408 |
| execute_work (SOME (work, deps')) = (execute work; join_work deps') |
32814
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
409 |
and join_work deps = |
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
410 |
execute_work (SYNCHRONIZED "join" (fn () => join_next deps)); |
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
411 |
|
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
412 |
fun join_depend task deps = |
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
413 |
execute_work (SYNCHRONIZED "join" (fn () => |
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
414 |
(Unsynchronized.change queue (Task_Queue.depend task deps); join_next deps))); |
29551
95e469919c3e
join_results: when dependencies are resulved (but not finished yet),
wenzelm
parents:
29431
diff
changeset
|
415 |
|
95e469919c3e
join_results: when dependencies are resulved (but not finished yet),
wenzelm
parents:
29431
diff
changeset
|
416 |
in |
95e469919c3e
join_results: when dependencies are resulved (but not finished yet),
wenzelm
parents:
29431
diff
changeset
|
417 |
|
29366 | 418 |
fun join_results xs = |
419 |
if forall is_finished xs then map get_result xs |
|
32246 | 420 |
else if Multithreading.self_critical () then |
421 |
error "Cannot join future values within critical section" |
|
32814
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
422 |
else |
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
423 |
(case worker_task () of |
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
424 |
SOME task => join_depend task (map task_of xs) |
33409 | 425 |
| NONE => List.app passive_wait xs; |
32814
81897d30b97f
added Task_Queue.depend (again) -- light-weight version for transitive graph;
wenzelm
parents:
32738
diff
changeset
|
426 |
map get_result xs); |
28186 | 427 |
|
29551
95e469919c3e
join_results: when dependencies are resulved (but not finished yet),
wenzelm
parents:
29431
diff
changeset
|
428 |
end; |
95e469919c3e
join_results: when dependencies are resulved (but not finished yet),
wenzelm
parents:
29431
diff
changeset
|
429 |
|
28647
8068cdc84e7e
join_results: allow CRITICAL join of finished futures;
wenzelm
parents:
28645
diff
changeset
|
430 |
fun join_result x = singleton join_results x; |
8068cdc84e7e
join_results: allow CRITICAL join of finished futures;
wenzelm
parents:
28645
diff
changeset
|
431 |
fun join x = Exn.release (join_result x); |
28156 | 432 |
|
29366 | 433 |
|
434 |
(* map *) |
|
435 |
||
29384
a3c7e9ae9b71
more robust propagation of errors through bulk jobs;
wenzelm
parents:
29366
diff
changeset
|
436 |
fun map_future f x = |
29366 | 437 |
let |
29384
a3c7e9ae9b71
more robust propagation of errors through bulk jobs;
wenzelm
parents:
29366
diff
changeset
|
438 |
val task = task_of x; |
32102 | 439 |
val group = Task_Queue.new_group (SOME (group_of x)); |
29384
a3c7e9ae9b71
more robust propagation of errors through bulk jobs;
wenzelm
parents:
29366
diff
changeset
|
440 |
val (result, job) = future_job group (fn () => f (join x)); |
a3c7e9ae9b71
more robust propagation of errors through bulk jobs;
wenzelm
parents:
29366
diff
changeset
|
441 |
|
32246 | 442 |
val extended = SYNCHRONIZED "extend" (fn () => |
29366 | 443 |
(case Task_Queue.extend task job (! queue) of |
444 |
SOME queue' => (queue := queue'; true) |
|
445 |
| NONE => false)); |
|
446 |
in |
|
29384
a3c7e9ae9b71
more robust propagation of errors through bulk jobs;
wenzelm
parents:
29366
diff
changeset
|
447 |
if extended then Future {task = task, group = group, result = result} |
32099 | 448 |
else fork_future (SOME group) [task] (Task_Queue.pri_of_task task) (fn () => f (join x)) |
29366 | 449 |
end; |
28979
3ce619d8d432
fork/map: no inheritance of group (structure is nested, not parallel);
wenzelm
parents:
28972
diff
changeset
|
450 |
|
28191 | 451 |
|
29431 | 452 |
(* cancellation *) |
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
453 |
|
30618
046f4f986fb5
restricted interrupts for tasks running as future worker thread -- attempt to prevent interrupt race conditions;
wenzelm
parents:
30612
diff
changeset
|
454 |
fun interruptible_task f x = |
046f4f986fb5
restricted interrupts for tasks running as future worker thread -- attempt to prevent interrupt race conditions;
wenzelm
parents:
30612
diff
changeset
|
455 |
if Multithreading.available then |
046f4f986fb5
restricted interrupts for tasks running as future worker thread -- attempt to prevent interrupt race conditions;
wenzelm
parents:
30612
diff
changeset
|
456 |
Multithreading.with_attributes |
32058 | 457 |
(if is_worker () |
32295
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
458 |
then Multithreading.private_interrupts |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
459 |
else Multithreading.public_interrupts) |
400cc493d466
renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents:
32293
diff
changeset
|
460 |
(fn _ => f x) |
30618
046f4f986fb5
restricted interrupts for tasks running as future worker thread -- attempt to prevent interrupt race conditions;
wenzelm
parents:
30612
diff
changeset
|
461 |
else interruptible f x; |
046f4f986fb5
restricted interrupts for tasks running as future worker thread -- attempt to prevent interrupt race conditions;
wenzelm
parents:
30612
diff
changeset
|
462 |
|
32228
7622c03141b0
scheduler: shutdown spontaneously (after some delay) if queue is empty;
wenzelm
parents:
32227
diff
changeset
|
463 |
(*cancel: present and future group members will be interrupted eventually*) |
7622c03141b0
scheduler: shutdown spontaneously (after some delay) if queue is empty;
wenzelm
parents:
32227
diff
changeset
|
464 |
fun cancel_group group = SYNCHRONIZED "cancel" (fn () => do_cancel group); |
29431 | 465 |
fun cancel x = cancel_group (group_of x); |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
466 |
|
29366 | 467 |
|
32228
7622c03141b0
scheduler: shutdown spontaneously (after some delay) if queue is empty;
wenzelm
parents:
32227
diff
changeset
|
468 |
(* shutdown *) |
29366 | 469 |
|
28203 | 470 |
fun shutdown () = |
28276 | 471 |
if Multithreading.available then |
472 |
SYNCHRONIZED "shutdown" (fn () => |
|
32228
7622c03141b0
scheduler: shutdown spontaneously (after some delay) if queue is empty;
wenzelm
parents:
32227
diff
changeset
|
473 |
while scheduler_active () do |
32248
0241916a5f06
more precise treatment of scheduler_event: continous pulse (50ms) instead of flooding, which was burning many CPU cycles in spare threads;
wenzelm
parents:
32247
diff
changeset
|
474 |
(wait scheduler_event; broadcast_work ())) |
28276 | 475 |
else (); |
28203 | 476 |
|
29366 | 477 |
|
478 |
(*final declarations of this structure!*) |
|
479 |
val map = map_future; |
|
480 |
||
28156 | 481 |
end; |
28972 | 482 |
|
483 |
type 'a future = 'a Future.future; |
|
484 |