<!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>