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
|
open Posix.Signal MLton.Signal
(* Translated from prodcons.ocaml. *)
fun for (start, stop, f) =
let
fun loop i =
if i > stop
then ()
else (f i; loop (i + 1))
in
loop start
end
fun print s = ()
structure Queue:
sig
type 'a t
val new: unit -> 'a t
val enque: 'a t * 'a -> unit
val deque: 'a t -> 'a option
end =
struct
datatype 'a t = T of {front: 'a list ref, back: 'a list ref}
fun new () = T {front = ref [], back = ref []}
fun enque (T {back, ...}, x) = back := x :: !back
fun deque (T {front, back}) =
case !front of
[] => (case !back of
[] => NONE
| l => let val l = rev l
in case l of
[] => raise Fail "deque"
| x :: l => (back := []; front := l; SOME x)
end)
| x :: l => (front := l; SOME x)
end
structure Thread:
sig
val exit: unit -> 'a
val run: unit -> unit
val spawn: (unit -> unit) -> unit
val yield: unit -> unit
structure Mutex:
sig
type t
val new: unit -> t
val lock: t * string -> unit
val unlock: t -> unit
end
structure Condition:
sig
type t
val new: unit -> t
val signal: t -> unit
val wait: t * Mutex.t -> unit
end
end =
struct
open MLton
open Itimer Signal Thread
val topLevel: Thread.Runnable.t option ref = ref NONE
local
val threads: Thread.Runnable.t Queue.t = Queue.new ()
in
fun ready t: unit = Queue.enque (threads, t)
fun next () : Thread.Runnable.t =
case Queue.deque threads of
NONE => (print "switching to toplevel\n"
; valOf (!topLevel))
| SOME t => t
end
fun 'a exit (): 'a = switch (fn _ => next ())
fun new (f: unit -> unit): Thread.Runnable.t =
Thread.prepare
(Thread.new (fn () => ((f () handle _ => exit ())
; exit ())),
())
fun schedule t =
(print "scheduling\n"
; ready t
; next ())
fun yield (): unit = switch (fn t => schedule (Thread.prepare (t, ())))
val spawn = ready o new
fun setItimer t =
Itimer.set (Itimer.Real,
{value = t,
interval = t})
fun run (): unit =
(switch (fn t =>
(topLevel := SOME (Thread.prepare (t, ()))
; new (fn () => (setHandler (alrm, Handler.handler schedule)
; setItimer (Time.fromMilliseconds 20)))))
; setItimer Time.zeroTime
; ignore alrm
; topLevel := NONE)
structure Mutex =
struct
datatype t = T of {locked: bool ref,
waiting: unit Thread.t Queue.t}
fun new () =
T {locked = ref false,
waiting = Queue.new ()}
fun lock (T {locked, waiting, ...}, name) =
let
fun loop () =
(print (concat [name, " lock looping\n"])
; Thread.atomicBegin ()
; if !locked
then (print "mutex is locked\n"
; switch (fn t =>
(Thread.atomicEnd ()
; Queue.enque (waiting, t)
; next ()))
; loop ())
else (print "mutex is not locked\n"
; locked := true
; Thread.atomicEnd ()))
in loop ()
end
fun safeUnlock (T {locked, waiting, ...}) =
(locked := false
; (case Queue.deque waiting of
NONE => ()
| SOME t => (print "unlock found waiting thread\n"
; ready (Thread.prepare (t, ())))))
fun unlock (m: t) =
(print "unlock atomicBegin\n"
; Thread.atomicBegin ()
; safeUnlock m
; Thread.atomicEnd ())
end
structure Condition =
struct
datatype t = T of {waiting: unit Thread.t Queue.t}
fun new () = T {waiting = Queue.new ()}
fun wait (T {waiting, ...}, m) =
(switch (fn t =>
(Mutex.safeUnlock m
; print "wait unlocked mutex\n"
; Queue.enque (waiting, t)
; next ()))
; Mutex.lock (m, "wait"))
fun signal (T {waiting, ...}) =
case Queue.deque waiting of
NONE => ()
| SOME t => ready (Thread.prepare (t, ()))
end
end
structure Mutex = Thread.Mutex
structure Condition = Thread.Condition
val count = ref 0
val data = ref 0
val produced = ref 0
val consumed = ref 0
val m = Mutex.new ()
val c = Condition.new ()
fun producer n =
for (1, n, fn i =>
(print (concat ["producer acquiring lock ", Int.toString i, "\n"])
; Mutex.lock (m, "producer")
; print "producer acquired lock\n"
; while !count = 1 do Condition.wait (c, m)
; print "producer passed condition\n"
; data := i
; count := 1
; Condition.signal c
; print "producer releasing lock\n"
; Mutex.unlock m
; print "producer released lock\n"
; produced := !produced + 1))
fun consumer n =
let val i = ref 0
in
while !i <> n do
(print (concat ["consumer acquiring lock ", Int.toString (!i), "\n"])
; Mutex.lock (m, "consumer")
; print "consumer acquired lock\n"
; while !count = 0 do Condition.wait (c, m)
; i := !data
; count := 0
; Condition.signal c
; print "consumer releasing lock\n"
; Mutex.unlock m
; print "consumer released lock\n"
; consumed := !consumed + 1)
end
fun atoi s = case Int.fromString s of SOME num => num | NONE => 0
fun printl [] = TextIO.print "\n" | printl (h::t) = ( TextIO.print h ; printl t )
fun main (name, args) =
let
val n = atoi (hd (args @ ["1"]))
val p = Thread.spawn (fn () => producer n)
val c = Thread.spawn (fn () => consumer n)
val _ = Thread.run ()
val _ = Posix.Process.sleep (Time.fromSeconds 1)
val _ = printl [Int.toString (!produced),
" ",
Int.toString (!consumed)]
in
()
end
val _ = main ( "prodcons", ["100000"] )
|