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