author | wenzelm |
Thu, 11 Sep 2008 21:53:53 +0200 | |
changeset 28206 | bcd48c6897d4 |
parent 28203 | 88f18387f1c9 |
child 28208 | 3a8b3453129a |
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*) |
28192 | 163 |
change workers |
164 |
(cons (Thread.fork (fn () => worker_loop name, Multithreading.no_interrupts), true)); |
|
28156 | 165 |
|
166 |
||
167 |
(* scheduler *) |
|
168 |
||
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
169 |
fun scheduler_next () = (*requires SYNCHRONIZED*) |
28156 | 170 |
let |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
171 |
(*worker threads*) |
28191 | 172 |
val _ = |
28192 | 173 |
(case List.partition (Thread.isActive o #1) (! workers) of |
28191 | 174 |
(_, []) => () |
175 |
| (active, inactive) => |
|
176 |
(workers := active; Multithreading.tracing 0 (fn () => |
|
28192 | 177 |
"SCHEDULE: disposed " ^ string_of_int (length inactive) ^ " dead worker threads"))); |
28191 | 178 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
179 |
val m = if ! do_shutdown then 0 else Multithreading.max_threads_value (); |
28167 | 180 |
val l = length (! workers); |
181 |
val _ = excessive := l - m; |
|
28203 | 182 |
val _ = |
183 |
if m > l then funpow (m - l) (fn () => worker_start ("worker " ^ serial_string ())) () |
|
184 |
else (); |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
185 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
186 |
(*canceled groups*) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
187 |
val _ = change canceled (filter_out (TaskQueue.cancel (! queue))); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
188 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
189 |
(*shutdown*) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
190 |
val continue = not (! do_shutdown andalso null (! workers)); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
191 |
val _ = if continue then () else scheduler := NONE; |
28167 | 192 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
193 |
val _ = notify_all (); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
194 |
val _ = wait_timeout "scheduler" (Time.fromSeconds 1); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
195 |
in continue end; |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
196 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
197 |
fun scheduler_loop () = |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
198 |
(while SYNCHRONIZED "scheduler" scheduler_next do (); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
199 |
Multithreading.tracing 4 (fn () => "scheduler: exit")); |
28156 | 200 |
|
28203 | 201 |
fun scheduler_active () = (*requires SYNCHRONIZED*) |
202 |
(case ! scheduler of NONE => false | SOME thread => Thread.isActive thread); |
|
203 |
||
28192 | 204 |
fun scheduler_check () = SYNCHRONIZED "scheduler_check" (fn () => |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
205 |
if not (scheduler_active ()) then |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
206 |
(do_shutdown := false; |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
207 |
scheduler := SOME (Thread.fork (scheduler_loop, Multithreading.no_interrupts))) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
208 |
else if ! do_shutdown then error "Scheduler shutdown in progress" |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
209 |
else ()); |
28156 | 210 |
|
211 |
||
28191 | 212 |
(* future values: fork independent computation *) |
28156 | 213 |
|
28191 | 214 |
fun future opt_group deps (e: unit -> 'a) = |
28156 | 215 |
let |
28191 | 216 |
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
|
217 |
|
28191 | 218 |
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
|
219 |
|
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
220 |
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
|
221 |
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
|
222 |
(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
|
223 |
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
|
224 |
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
|
225 |
|
28192 | 226 |
val task = SYNCHRONIZED "future" (fn () => |
28166
43087721a66e
moved task, thread_data, group, queue to task_queue.ML;
wenzelm
parents:
28163
diff
changeset
|
227 |
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
|
228 |
in Future {task = task, group = group, result = result} end; |
28162 | 229 |
|
28191 | 230 |
fun fork e = future (Option.map #2 (thread_data ())) [] e; |
28186 | 231 |
|
232 |
||
28191 | 233 |
(* join: retrieve results *) |
28186 | 234 |
|
28193
7ed74d0ba607
replaced join_all by join_results, which returns Exn.results;
wenzelm
parents:
28192
diff
changeset
|
235 |
fun join_results xs = |
28156 | 236 |
let |
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
237 |
val _ = scheduler_check (); |
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"; |
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
|
240 |
|
28186 | 241 |
fun unfinished () = |
242 |
xs |> map_filter (fn Future {task, result = ref NONE, ...} => SOME task | _ => NONE); |
|
243 |
||
244 |
(*alien thread -- refrain from contending for resources*) |
|
245 |
fun passive_join () = (*requires SYNCHRONIZED*) |
|
246 |
(case unfinished () of [] => () |
|
28203 | 247 |
| _ => (wait "passive_join"; passive_join ())); |
28186 | 248 |
|
249 |
(*proper worker thread -- actively work towards results*) |
|
250 |
fun active_join () = (*requires SYNCHRONIZED*) |
|
251 |
(case unfinished () of [] => () |
|
252 |
| tasks => |
|
253 |
(case change_result queue (TaskQueue.dequeue_towards tasks) of |
|
28203 | 254 |
NONE => (worker_wait "active_join"; active_join ()) |
255 |
| SOME work => (execute "active_join" work; active_join ()))); |
|
28186 | 256 |
|
257 |
val _ = |
|
258 |
(case thread_data () of |
|
28203 | 259 |
NONE => SYNCHRONIZED "passive_join" passive_join |
260 |
| SOME (task, _) => SYNCHRONIZED "active_join" (fn () => |
|
28186 | 261 |
(change queue (TaskQueue.depend (unfinished ()) task); active_join ()))); |
262 |
||
28193
7ed74d0ba607
replaced join_all by join_results, which returns Exn.results;
wenzelm
parents:
28192
diff
changeset
|
263 |
in xs |> map (fn Future {result = ref (SOME res), ...} => res) end; |
28186 | 264 |
|
28193
7ed74d0ba607
replaced join_all by join_results, which returns Exn.results;
wenzelm
parents:
28192
diff
changeset
|
265 |
fun join x = Exn.release (singleton join_results x); |
28156 | 266 |
|
28191 | 267 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
268 |
(* misc operations *) |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
269 |
|
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
270 |
(*focus: collection of high-priority task*) |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
271 |
fun focus tasks = SYNCHRONIZED "interrupt" (fn () => |
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
272 |
change queue (TaskQueue.focus tasks)); |
28191 | 273 |
|
28202
23cb9a974630
added focus, which indicates a particular collection of high-priority tasks;
wenzelm
parents:
28201
diff
changeset
|
274 |
(*interrupt: permissive signal, may get ignored*) |
28197 | 275 |
fun interrupt_task id = SYNCHRONIZED "interrupt" |
276 |
(fn () => TaskQueue.interrupt_external (! queue) id); |
|
28191 | 277 |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
278 |
(*cancel: present and future group members will be interrupted eventually*) |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
279 |
fun cancel x = |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
280 |
(scheduler_check (); SYNCHRONIZED "cancel" (fn () => change canceled (cons (group_of x)))); |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
281 |
|
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
282 |
|
28203 | 283 |
(*global join and shutdown*) |
284 |
fun shutdown () = |
|
285 |
(scheduler_check (); |
|
286 |
SYNCHRONIZED "shutdown" (fn () => |
|
28206
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
287 |
(while not (scheduler_active ()) do wait "shutdown: scheduler inactive"; |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
288 |
while not (TaskQueue.is_empty (! queue)) do wait "shutdown: join"; |
bcd48c6897d4
eliminated requests, use global state variables uniformly;
wenzelm
parents:
28203
diff
changeset
|
289 |
do_shutdown := true; |
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; |