/usr/share/doc/mlton/guide/MLtonThread is in mlton-doc 20100608-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">
<title>MLtonThread - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">
<link rel="Start" href="Home">
</head>
<body lang="en" dir="ltr">
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
<tr>
<td style = "
border: 0px;
color: darkblue;
font-size: 150%;
text-align: left;">
<a class = mltona href="Home">MLton MLTONWIKIVERSION</a>
<td style = "
border: 0px;
font-size: 150%;
text-align: center;
width: 50%;">
MLtonThread
<td style = "
border: 0px;
text-align: right;">
<table cellspacing = 0 style = "border: 0px">
<tr style = "vertical-align: middle;">
</table>
<tr style = "background-color: white;">
<td colspan = 3
style = "
border: 0px;
font-size:70%;
text-align: right;">
<a href = "Home">Home</a>
<a href = "TitleIndex">Index</a>
</table>
<div id="content" lang="en" dir="ltr">
<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> MLTON_THREAD =
<B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#0000FF">structure</FONT></B> AtomicState:
<B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">NonAtomic</FONT> </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Atomic</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> int
</FONT></B><B><FONT COLOR="#0000FF">end</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> atomically: (unit -> 'a) -> 'a
<B><FONT COLOR="#A020F0">val</FONT></B> atomicBegin: unit -> unit
<B><FONT COLOR="#A020F0">val</FONT></B> atomicEnd: unit -> unit
<B><FONT COLOR="#A020F0">val</FONT></B> atomicState: unit -> AtomicState.t
<B><FONT COLOR="#0000FF">structure</FONT></B> Runnable:
<B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t
</FONT></B><B><FONT COLOR="#0000FF">end</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> atomicSwitch: ('a t -> Runnable.t) -> 'a
<B><FONT COLOR="#A020F0">val</FONT></B> new: ('a -> unit) -> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> prepend: 'a t * ('b -> 'a) -> 'b t
<B><FONT COLOR="#A020F0">val</FONT></B> prepare: 'a t * 'a -> Runnable.t
<B><FONT COLOR="#A020F0">val</FONT></B> switch: ('a t -> Runnable.t) -> 'a
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
<tt>MLton.Thread</tt> provides access to MLton's user-level thread implementation (i.e. not OS-level threads). Threads are lightweight data structures that represent a paused computation. Runnable threads are threads that will begin or continue computing when <tt>switch</tt>ed to. <tt>MLton.Thread</tt> does not include a default scheduling mechanism, but it can be used to implement both preemptive and non-preemptive threads.
</p>
<ul>
<li>
<p>
<tt>type AtomicState.t</tt> <br>
the type of atomic states.
</p>
</li>
<li class="gap">
<p>
<tt>atomically f</tt> <br>
runs <tt>f</tt> in a critical section.
</p>
</li>
<li class="gap">
<p>
<tt>atomicBegin ()</tt> <br>
begins a critical section.
</p>
</li>
<li class="gap">
<p>
<tt>atomicEnd ()</tt> <br>
ends a critical section.
</p>
</li>
<li class="gap">
<p>
<tt>atomicState ()</tt> <br>
returns the current atomic state.
</p>
</li>
<li class="gap">
<p>
<tt>type Runnable.t</tt> <br>
the type of threads that can be resumed.
</p>
</li>
<li class="gap">
<p>
<tt>type 'a t</tt> <br>
the type of threads that expect a value of type <tt>'a</tt>.
</p>
</li>
<li class="gap">
<p>
<tt>atomicSwitch f</tt> <br>
like <tt>switch</tt>, but assumes an atomic calling context. Upon <tt>switch</tt>ing back to the current thread, an implicit <tt>atomicEnd</tt> is performed.
</p>
</li>
<li class="gap">
<p>
<tt>new f</tt> <br>
creates a new thread that, when run, applies <tt>f</tt> to the value given to the thread. <tt>f</tt> must terminate by <tt>switch</tt>ing to another thread or exiting the process.
</p>
</li>
<li class="gap">
<p>
<tt>prepend (t, f)</tt> <br>
creates a new thread (destroying <tt>t</tt> in the process) that first applies <tt>f</tt> to the value given to the thread and then continues with <tt>t</tt>. This is a constant time operation.
</p>
</li>
<li class="gap">
<p>
<tt>prepare (t, v)</tt> <br>
prepares a new runnable thread (destroying <tt>t</tt> in the process) that will evaluate <tt>t</tt> on <tt>v</tt>.
</p>
</li>
<li class="gap">
<p>
<tt>switch f</tt> <br>
applies <tt>f</tt> to the current thread to get <tt>rt</tt>, and then start running thread <tt>rt</tt>. It is an error for <tt>f</tt> to perform another <tt>switch</tt>. <tt>f</tt> is guaranteed to run atomically.
</p>
</li>
</ul>
<h2 id="head-b59f5a84d311ffa9c081e150b9d22e77bb539300">Example of non-preemptive threads</h2>
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Queue:
<B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> new: unit -> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> enque: 'a t * 'a -> unit
<B><FONT COLOR="#A020F0">val</FONT></B> deque: 'a t -> 'a option
<B><FONT COLOR="#0000FF">end</FONT></B> =
<B><FONT COLOR="#0000FF">struct</FONT></B>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">T</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {front: 'a list ref, back: 'a list ref}
</FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> new() = T{front = ref [], back = ref []}
<B><FONT COLOR="#A020F0">fun</FONT></B> enque(T{back, ...}, x) = back := x :: !back
<B><FONT COLOR="#A020F0">fun</FONT></B> deque(T{front, back}) =
<B><FONT COLOR="#A020F0">case</FONT></B> !front <B><FONT COLOR="#A020F0">of</FONT></B>
[] => (<B><FONT COLOR="#A020F0">case</FONT></B> !back <B><FONT COLOR="#A020F0">of</FONT></B>
[] => NONE
| l => <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#A020F0">val</FONT></B> l = rev l
<B><FONT COLOR="#A020F0">in</FONT></B> <B><FONT COLOR="#A020F0">case</FONT></B> l <B><FONT COLOR="#A020F0">of</FONT></B>
[] => <B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"deque"</FONT></B>
| x :: l => (back := []; front := l; SOME x)
<B><FONT COLOR="#A020F0">end</FONT></B>)
| x :: l => (front := l; SOME x)
<B><FONT COLOR="#0000FF">end</FONT></B>
<B><FONT COLOR="#0000FF">structure</FONT></B> Thread:
<B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> exit: unit -> 'a
<B><FONT COLOR="#A020F0">val</FONT></B> run: unit -> unit
<B><FONT COLOR="#A020F0">val</FONT></B> spawn: (unit -> unit) -> unit
<B><FONT COLOR="#A020F0">val</FONT></B> yield: unit -> unit
<B><FONT COLOR="#0000FF">end</FONT></B> =
<B><FONT COLOR="#0000FF">struct</FONT></B>
<B><FONT COLOR="#0000FF">open</FONT></B> MLton
<B><FONT COLOR="#0000FF">open</FONT></B> Thread
<B><FONT COLOR="#A020F0">val</FONT></B> topLevel: Thread.Runnable.t option ref = ref NONE
<B><FONT COLOR="#A020F0">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> threads: Thread.Runnable.t Queue.t = Queue.new()
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> ready (t: Thread.Runnable.t) : unit =
Queue.enque(threads, t)
<B><FONT COLOR="#A020F0">fun</FONT></B> next () : Thread.Runnable.t =
<B><FONT COLOR="#A020F0">case</FONT></B> Queue.deque threads <B><FONT COLOR="#A020F0">of</FONT></B>
NONE => valOf(!topLevel)
| SOME t => t
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> <B><FONT COLOR="#228B22">'a</FONT></B> exit(): 'a = switch(<B><FONT COLOR="#A020F0">fn</FONT></B> _ => next())
<B><FONT COLOR="#A020F0">fun</FONT></B> new(f: unit -> unit): Thread.Runnable.t =
Thread.prepare
(Thread.new (<B><FONT COLOR="#A020F0">fn</FONT></B> () => ((f() <B><FONT COLOR="#A020F0">handle</FONT></B> _ => exit())
; exit())),
())
<B><FONT COLOR="#A020F0">fun</FONT></B> schedule t = (ready t; next())
<B><FONT COLOR="#A020F0">fun</FONT></B> yield(): unit = switch(<B><FONT COLOR="#A020F0">fn</FONT></B> t => schedule (Thread.prepare (t, ())))
<B><FONT COLOR="#A020F0">val</FONT></B> spawn = ready o new
<B><FONT COLOR="#A020F0">fun</FONT></B> run(): unit =
(switch(<B><FONT COLOR="#A020F0">fn</FONT></B> t =>
(topLevel := SOME (Thread.prepare (t, ()))
; next()))
; topLevel := NONE)
<B><FONT COLOR="#0000FF">end</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> loop =
<B><FONT COLOR="#A020F0">fn</FONT></B> <B><FONT COLOR="#5F9EA0">0</FONT></B> => ()
| n => (print(concat[Int.toString n, <B><FONT COLOR="#BC8F8F">"\n"</FONT></B>])
; Thread.yield()
; loop(n - <B><FONT COLOR="#5F9EA0">1</FONT></B>))
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> loop' =
<B><FONT COLOR="#A020F0">fn</FONT></B> <B><FONT COLOR="#5F9EA0">0</FONT></B> => ()
| n => (Thread.spawn(<B><FONT COLOR="#A020F0">fn</FONT></B> () => loop n); loop'(n - <B><FONT COLOR="#5F9EA0">2</FONT></B>))
<B><FONT COLOR="#A020F0">val</FONT></B> _ = Thread.spawn(<B><FONT COLOR="#A020F0">fn</FONT></B> () => loop' <B><FONT COLOR="#5F9EA0">10</FONT></B>)
<B><FONT COLOR="#A020F0">val</FONT></B> _ = Thread.run()
<B><FONT COLOR="#A020F0">val</FONT></B> _ = print <B><FONT COLOR="#BC8F8F">"success\n"</FONT></B>
</PRE>
<p>
</p>
<h2 id="head-5312b6df875ff55a1aac1703500af9a957b9a09a">Example of preemptive threads</h2>
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Queue:
<B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> new: unit -> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> enque: 'a t * 'a -> unit
<B><FONT COLOR="#A020F0">val</FONT></B> deque: 'a t -> 'a option
<B><FONT COLOR="#0000FF">end</FONT></B> =
<B><FONT COLOR="#0000FF">struct</FONT></B>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">T</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {front: 'a list ref, back: 'a list ref}
</FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> new () = T {front = ref [], back = ref []}
<B><FONT COLOR="#A020F0">fun</FONT></B> enque (T {back, ...}, x) = back := x :: !back
<B><FONT COLOR="#A020F0">fun</FONT></B> deque (T {front, back}) =
<B><FONT COLOR="#A020F0">case</FONT></B> !front <B><FONT COLOR="#A020F0">of</FONT></B>
[] => (<B><FONT COLOR="#A020F0">case</FONT></B> !back <B><FONT COLOR="#A020F0">of</FONT></B>
[] => NONE
| l => <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#A020F0">val</FONT></B> l = rev l
<B><FONT COLOR="#A020F0">in</FONT></B> <B><FONT COLOR="#A020F0">case</FONT></B> l <B><FONT COLOR="#A020F0">of</FONT></B>
[] => <B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"deque"</FONT></B>
| x :: l => (back := []; front := l; SOME x)
<B><FONT COLOR="#A020F0">end</FONT></B>)
| x :: l => (front := l; SOME x)
<B><FONT COLOR="#0000FF">end</FONT></B>
<B><FONT COLOR="#0000FF">structure</FONT></B> Thread:
<B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> exit: unit -> 'a
<B><FONT COLOR="#A020F0">val</FONT></B> run: unit -> unit
<B><FONT COLOR="#A020F0">val</FONT></B> spawn: (unit -> unit) -> unit
<B><FONT COLOR="#A020F0">val</FONT></B> yield: unit -> unit
<B><FONT COLOR="#0000FF">end</FONT></B> =
<B><FONT COLOR="#0000FF">struct</FONT></B>
<B><FONT COLOR="#0000FF">open</FONT></B> Posix.Signal
<B><FONT COLOR="#0000FF">open</FONT></B> MLton
<B><FONT COLOR="#0000FF">open</FONT></B> Itimer Signal Thread
<B><FONT COLOR="#A020F0">val</FONT></B> topLevel: Thread.Runnable.t option ref = ref NONE
<B><FONT COLOR="#A020F0">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> threads: Thread.Runnable.t Queue.t = Queue.new ()
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> ready t = Queue.enque (threads, t)
<B><FONT COLOR="#A020F0">fun</FONT></B> next () =
<B><FONT COLOR="#A020F0">case</FONT></B> Queue.deque threads <B><FONT COLOR="#A020F0">of</FONT></B>
NONE => valOf (!topLevel)
| SOME t => t
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> <B><FONT COLOR="#228B22">'a</FONT></B> exit (): 'a = switch (<B><FONT COLOR="#A020F0">fn</FONT></B> _ => next ())
<B><FONT COLOR="#A020F0">fun</FONT></B> new (f: unit -> unit): Thread.Runnable.t =
Thread.prepare
(Thread.new (<B><FONT COLOR="#A020F0">fn</FONT></B> () => ((f () <B><FONT COLOR="#A020F0">handle</FONT></B> _ => exit ())
; exit ())),
())
<B><FONT COLOR="#A020F0">fun</FONT></B> schedule t = (ready t; next ())
<B><FONT COLOR="#A020F0">fun</FONT></B> yield (): unit = switch (<B><FONT COLOR="#A020F0">fn</FONT></B> t => schedule (Thread.prepare (t, ())))
<B><FONT COLOR="#A020F0">val</FONT></B> spawn = ready o new
<B><FONT COLOR="#A020F0">fun</FONT></B> setItimer t =
Itimer.set (Itimer.Real,
{value = t,
interval = t})
<B><FONT COLOR="#A020F0">fun</FONT></B> run (): unit =
(switch (<B><FONT COLOR="#A020F0">fn</FONT></B> t =>
(topLevel := SOME (Thread.prepare (t, ()))
; new (<B><FONT COLOR="#A020F0">fn</FONT></B> () => (setHandler (alrm, Handler.handler schedule)
; setItimer (Time.fromMilliseconds <B><FONT COLOR="#5F9EA0">20</FONT></B>)))))
; setItimer Time.zeroTime
; ignore alrm
; topLevel := NONE)
<B><FONT COLOR="#0000FF">end</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> delay =
<B><FONT COLOR="#A020F0">fn</FONT></B> <B><FONT COLOR="#5F9EA0">0</FONT></B> => ()
| n => delay (n - <B><FONT COLOR="#5F9EA0">1</FONT></B>)
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> loop =
<B><FONT COLOR="#A020F0">fn</FONT></B> <B><FONT COLOR="#5F9EA0">0</FONT></B> => ()
| n => (delay <B><FONT COLOR="#5F9EA0">500000</FONT></B>; loop (n - <B><FONT COLOR="#5F9EA0">1</FONT></B>))
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> loop' =
<B><FONT COLOR="#A020F0">fn</FONT></B> <B><FONT COLOR="#5F9EA0">0</FONT></B> => ()
| n => (Thread.spawn (<B><FONT COLOR="#A020F0">fn</FONT></B> () => loop n); loop' (n - <B><FONT COLOR="#5F9EA0">1</FONT></B>))
<B><FONT COLOR="#A020F0">val</FONT></B> _ = Thread.spawn (<B><FONT COLOR="#A020F0">fn</FONT></B> () => loop' <B><FONT COLOR="#5F9EA0">10</FONT></B>)
<B><FONT COLOR="#A020F0">val</FONT></B> _ = Thread.run ()
<B><FONT COLOR="#A020F0">val</FONT></B> _ = print <B><FONT COLOR="#BC8F8F">"success\n"</FONT></B>
</PRE>
<p>
</p>
</div>
<p>
<hr>
Last edited on 2007-08-23 03:45:45 by <span title="c-71-57-91-146.hsd1.il.comcast.net"><a href="MatthewFluet">MatthewFluet</a></span>.
</body></html>
|