author | paulson |
Thu, 06 Sep 2007 17:03:53 +0200 | |
changeset 24547 | 64c20ee76bc1 |
parent 24297 | a50cdc42798d |
child 24668 | 4058b7b0925c |
permissions | -rw-r--r-- |
23961 | 1 |
(* Title: Pure/ML-Systems/multithreading_polyml.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
24214
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
5 |
Multithreading in Poly/ML 5.1 or later (cf. polyml/basis/Thread.sml). |
23961 | 6 |
*) |
7 |
||
8 |
open Thread; |
|
9 |
||
24208 | 10 |
signature MULTITHREADING = |
11 |
sig |
|
12 |
include MULTITHREADING |
|
24214
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
13 |
val uninterruptible: (Thread.threadAttribute list -> 'a -> 'b) -> 'a -> 'b |
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
14 |
val interruptible: (Thread.threadAttribute list -> 'a -> 'b) -> 'a -> 'b |
24208 | 15 |
end; |
16 |
||
23961 | 17 |
structure Multithreading: MULTITHREADING = |
18 |
struct |
|
19 |
||
24072
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
20 |
(* options *) |
24069 | 21 |
|
24119 | 22 |
val trace = ref 0; |
23 |
fun tracing level msg = |
|
24 |
if level <= ! trace |
|
23981 | 25 |
then (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr) |
26 |
else (); |
|
23961 | 27 |
|
23981 | 28 |
val available = true; |
23973 | 29 |
val max_threads = ref 1; |
30 |
||
31 |
||
24069 | 32 |
(* misc utils *) |
33 |
||
24208 | 34 |
fun cons x xs = x :: xs; |
24069 | 35 |
|
24208 | 36 |
fun change r f = r := f (! r); |
24069 | 37 |
|
38 |
fun inc i = (i := ! i + 1; ! i); |
|
39 |
fun dec i = (i := ! i - 1; ! i); |
|
40 |
||
24208 | 41 |
fun show "" = "" | show name = " " ^ name; |
42 |
fun show' "" = "" | show' name = " [" ^ name ^ "]"; |
|
43 |
||
44 |
||
45 |
(* thread attributes *) |
|
46 |
||
47 |
fun with_attributes new_atts f x = |
|
48 |
let |
|
49 |
val orig_atts = Thread.getAttributes (); |
|
50 |
fun restore () = Thread.setAttributes orig_atts; |
|
51 |
in |
|
52 |
Exn.release |
|
24214
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
53 |
(*RACE for fully asynchronous interrupts!*) |
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
54 |
(let |
24208 | 55 |
val _ = Thread.setAttributes new_atts; |
24214
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
56 |
val result = Exn.capture (f orig_atts) x; |
24208 | 57 |
val _ = restore (); |
58 |
in result end |
|
59 |
handle Interrupt => (restore (); Exn.Exn Interrupt)) |
|
60 |
end; |
|
61 |
||
24297
a50cdc42798d
improved treatment of global interrupts: Thread.EnableBroadcastInterrupt, redefine ignore/raise_interrupt;
wenzelm
parents:
24291
diff
changeset
|
62 |
fun uninterruptible f x = with_attributes |
a50cdc42798d
improved treatment of global interrupts: Thread.EnableBroadcastInterrupt, redefine ignore/raise_interrupt;
wenzelm
parents:
24291
diff
changeset
|
63 |
[Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer] f x; |
24208 | 64 |
|
24297
a50cdc42798d
improved treatment of global interrupts: Thread.EnableBroadcastInterrupt, redefine ignore/raise_interrupt;
wenzelm
parents:
24291
diff
changeset
|
65 |
fun interruptible f x = with_attributes |
a50cdc42798d
improved treatment of global interrupts: Thread.EnableBroadcastInterrupt, redefine ignore/raise_interrupt;
wenzelm
parents:
24291
diff
changeset
|
66 |
[Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce] f x; |
24208 | 67 |
|
24069 | 68 |
|
23961 | 69 |
(* critical section -- may be nested within the same thread *) |
70 |
||
71 |
local |
|
72 |
||
24063 | 73 |
val critical_lock = Mutex.mutex (); |
74 |
val critical_thread = ref (NONE: Thread.thread option); |
|
75 |
val critical_name = ref ""; |
|
76 |
||
23961 | 77 |
in |
78 |
||
79 |
fun self_critical () = |
|
80 |
(case ! critical_thread of |
|
81 |
NONE => false |
|
82 |
| SOME id => Thread.equal (id, Thread.self ())); |
|
83 |
||
23991 | 84 |
fun NAMED_CRITICAL name e = |
23961 | 85 |
if self_critical () then e () |
86 |
else |
|
24214
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
87 |
uninterruptible (fn atts => fn () => |
24208 | 88 |
let |
89 |
val name' = ! critical_name; |
|
90 |
val _ = |
|
91 |
if Mutex.trylock critical_lock then () |
|
92 |
else |
|
93 |
let |
|
94 |
val timer = Timer.startRealTimer (); |
|
95 |
val _ = tracing 4 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting"); |
|
96 |
val _ = Mutex.lock critical_lock; |
|
97 |
val time = Timer.checkRealTimer timer; |
|
98 |
val _ = tracing (if Time.> (time, Time.fromMilliseconds 10) then 3 else 4) (fn () => |
|
99 |
"CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time); |
|
100 |
in () end; |
|
101 |
val _ = critical_thread := SOME (Thread.self ()); |
|
102 |
val _ = critical_name := name; |
|
24214
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
103 |
val result = Exn.capture (with_attributes atts (fn _ => e)) (); |
24208 | 104 |
val _ = critical_name := ""; |
105 |
val _ = critical_thread := NONE; |
|
106 |
val _ = Mutex.unlock critical_lock; |
|
107 |
in Exn.release result end) (); |
|
23961 | 108 |
|
23991 | 109 |
fun CRITICAL e = NAMED_CRITICAL "" e; |
23981 | 110 |
|
23961 | 111 |
end; |
112 |
||
23973 | 113 |
|
24208 | 114 |
(* scheduling -- multiple threads working on a queue of tasks *) |
115 |
||
116 |
datatype 'a task = |
|
117 |
Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate; |
|
23973 | 118 |
|
24069 | 119 |
local |
120 |
||
121 |
val protected_name = ref ""; |
|
122 |
||
123 |
in |
|
24063 | 124 |
|
24214
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
125 |
fun schedule n next_task = uninterruptible (fn _ => fn tasks => |
23973 | 126 |
let |
127 |
(*protected execution*) |
|
128 |
val lock = Mutex.mutex (); |
|
24063 | 129 |
fun PROTECTED name e = |
23973 | 130 |
let |
24144 | 131 |
val name' = ! protected_name; |
23981 | 132 |
val _ = |
133 |
if Mutex.trylock lock then () |
|
134 |
else |
|
24144 | 135 |
let |
136 |
val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": waiting"); |
|
137 |
val _ = Mutex.lock lock; |
|
138 |
val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": passed"); |
|
139 |
in () end; |
|
24069 | 140 |
val _ = protected_name := name; |
23973 | 141 |
val res = Exn.capture e (); |
24069 | 142 |
val _ = protected_name := ""; |
23973 | 143 |
val _ = Mutex.unlock lock; |
144 |
in Exn.release res end; |
|
145 |
||
24072
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
146 |
(*wakeup condition*) |
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
147 |
val wakeup = ConditionVar.conditionVar (); |
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
148 |
fun wakeup_all () = ConditionVar.broadcast wakeup; |
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
149 |
fun wait () = ConditionVar.wait (wakeup, lock); |
24291 | 150 |
fun wait_timeout () = ConditionVar.waitUntil (wakeup, lock, Time.now () + Time.fromSeconds 1); |
24072
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
151 |
|
24214
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
152 |
(*queue of tasks*) |
23973 | 153 |
val queue = ref tasks; |
24072
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
154 |
val active = ref 0; |
24119 | 155 |
fun trace_active () = tracing 1 (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active"); |
24072
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
156 |
fun dequeue () = |
23973 | 157 |
let |
23981 | 158 |
val (next, tasks') = next_task (! queue); |
23973 | 159 |
val _ = queue := tasks'; |
24072
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
160 |
in |
24208 | 161 |
(case next of Wait => |
162 |
(dec active; trace_active (); |
|
163 |
wait (); |
|
164 |
inc active; trace_active (); |
|
165 |
dequeue ()) |
|
166 |
| _ => next) |
|
24072
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
167 |
end; |
23973 | 168 |
|
24208 | 169 |
(*pool of running threads*) |
23973 | 170 |
val status = ref ([]: exn list); |
24208 | 171 |
val running = ref ([]: Thread.thread list); |
172 |
fun start f = |
|
173 |
(inc active; |
|
174 |
change running (cons (Thread.fork (f, [Thread.InterruptState Thread.InterruptDefer])))); |
|
175 |
fun stop () = |
|
176 |
(dec active; |
|
177 |
change running (List.filter (fn t => not (Thread.equal (t, Thread.self ()))))); |
|
178 |
||
179 |
(*worker thread*) |
|
180 |
fun worker () = |
|
24072
8b9e5d776ef3
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents:
24069
diff
changeset
|
181 |
(case PROTECTED "dequeue" dequeue of |
24208 | 182 |
Task {body, cont, fail} => |
24214
0482ecc4ef11
(un)interruptible: pass-through original thread attributes;
wenzelm
parents:
24208
diff
changeset
|
183 |
(case Exn.capture (interruptible (fn _ => body)) () of |
24208 | 184 |
Exn.Result () => |
185 |
(PROTECTED "cont" (fn () => (change queue cont; wakeup_all ())); worker ()) |
|
23981 | 186 |
| Exn.Exn exn => |
24208 | 187 |
PROTECTED "fail" (fn () => |
188 |
(change status (cons exn); change queue fail; stop (); wakeup_all ()))) |
|
189 |
| Terminate => PROTECTED "terminate" (fn () => (stop (); wakeup_all ()))); |
|
23973 | 190 |
|
191 |
(*main control: fork and wait*) |
|
192 |
fun fork 0 = () |
|
24208 | 193 |
| fork k = (start worker; fork (k - 1)); |
24063 | 194 |
val _ = PROTECTED "main" (fn () => |
195 |
(fork (Int.max (n, 1)); |
|
24208 | 196 |
while not (List.null (! running)) do |
197 |
(trace_active (); |
|
198 |
if not (List.null (! status)) then (List.app Thread.interrupt (! running)) else (); |
|
24291 | 199 |
wait_timeout ()))); |
23973 | 200 |
|
24208 | 201 |
in ! status end); |
23973 | 202 |
|
23961 | 203 |
end; |
204 |
||
24069 | 205 |
end; |
206 |
||
23991 | 207 |
val NAMED_CRITICAL = Multithreading.NAMED_CRITICAL; |
23961 | 208 |
val CRITICAL = Multithreading.CRITICAL; |
24208 | 209 |
|
24297
a50cdc42798d
improved treatment of global interrupts: Thread.EnableBroadcastInterrupt, redefine ignore/raise_interrupt;
wenzelm
parents:
24291
diff
changeset
|
210 |
fun ignore_interrupt f = Multithreading.uninterruptible (fn _ => f); |
a50cdc42798d
improved treatment of global interrupts: Thread.EnableBroadcastInterrupt, redefine ignore/raise_interrupt;
wenzelm
parents:
24291
diff
changeset
|
211 |
fun raise_interrupt f = Multithreading.interruptible (fn _ => f); |
a50cdc42798d
improved treatment of global interrupts: Thread.EnableBroadcastInterrupt, redefine ignore/raise_interrupt;
wenzelm
parents:
24291
diff
changeset
|
212 |