| 1 |
(* run-cml.sml |
| 2 |
* 2004 Matthew Fluet ([email protected]) |
| 3 |
* Ported to MLton threads. |
| 4 |
*) |
| 5 |
|
| 6 |
(* run-cml-fn.sml |
| 7 |
* |
| 8 |
* COPYRIGHT (c) 1996 AT&T Research. |
| 9 |
* COPYRIGHT (c) 1989-1991 John H. Reppy |
| 10 |
*) |
| 11 |
|
| 12 |
structure RunCML : RUN_CML = |
| 13 |
struct |
| 14 |
structure Assert = LocalAssert(val assert = false) |
| 15 |
structure Debug = LocalDebug(val debug = false) |
| 16 |
|
| 17 |
structure R = Running |
| 18 |
structure S = Scheduler |
| 19 |
structure SH = SchedulerHooks |
| 20 |
structure TID = ThreadID |
| 21 |
structure TO = TimeOut |
| 22 |
fun debug msg = Debug.sayDebug ([S.atomicMsg, S.tidMsg], msg) |
| 23 |
fun debug' msg = debug (fn () => msg) |
| 24 |
|
| 25 |
|
| 26 |
local |
| 27 |
structure Signal = MLton.Signal |
| 28 |
structure Itimer = MLton.Itimer |
| 29 |
|
| 30 |
fun getAlrmHandler () = |
| 31 |
Signal.getHandler Posix.Signal.alrm |
| 32 |
fun setAlrmHandler h = |
| 33 |
Signal.setHandler (Posix.Signal.alrm, h) |
| 34 |
|
| 35 |
fun setItimer t = |
| 36 |
Itimer.set (Itimer.Real, {value = t, interval = t}) |
| 37 |
in |
| 38 |
fun prepareAlrmHandler tq = |
| 39 |
let |
| 40 |
val origAlrmHandler = getAlrmHandler () |
| 41 |
val tq = |
| 42 |
case tq of |
| 43 |
SOME tq => tq |
| 44 |
| NONE => Time.fromMilliseconds 20 |
| 45 |
in |
| 46 |
(fn alrmHandler => |
| 47 |
(setAlrmHandler (Signal.Handler.handler (S.unwrap alrmHandler)) |
| 48 |
; setItimer tq), |
| 49 |
fn () => |
| 50 |
(setItimer Time.zeroTime |
| 51 |
; setAlrmHandler origAlrmHandler)) |
| 52 |
end |
| 53 |
end |
| 54 |
|
| 55 |
fun isRunning () = !R.isRunning |
| 56 |
|
| 57 |
fun reset running = |
| 58 |
(S.reset running |
| 59 |
; SH.reset () |
| 60 |
; TID.reset () |
| 61 |
; TO.reset ()) |
| 62 |
|
| 63 |
fun alrmHandler thrd = |
| 64 |
let |
| 65 |
val () = Assert.assertAtomic' ("RunCML.alrmHandler", NONE) |
| 66 |
val () = debug' "alrmHandler" (* Atomic 1 *) |
| 67 |
val () = Assert.assertAtomic' ("RunCML.alrmHandler", SOME 1) |
| 68 |
val () = S.preempt thrd |
| 69 |
val () = ignore (IOManager.preempt ()) |
| 70 |
val () = ignore (TO.preempt ()) |
| 71 |
in |
| 72 |
S.next () |
| 73 |
end |
| 74 |
|
| 75 |
(* Note that SH.pauseHook is only invoked by S.next |
| 76 |
* when there are no threads on the ready queue; |
| 77 |
* Furthermore, note that alrmHandler always |
| 78 |
* enqueues the preempted thread (via S.preempt). |
| 79 |
* Hence, the ready queue is never empty |
| 80 |
* at the S.next in alrmHandler. Therefore, |
| 81 |
* pauseHook is never run within alrmHandler. |
| 82 |
*) |
| 83 |
fun pauseHook () = |
| 84 |
let |
| 85 |
val () = Assert.assertAtomic' ("RunCML.pauseHook", NONE) |
| 86 |
val () = debug' "pauseHook" (* Atomic 1 *) |
| 87 |
val () = Assert.assertAtomic' ("RunCML.pauseHook", SOME 1) |
| 88 |
val io = IOManager.preempt () |
| 89 |
val to = TO.preempt () |
| 90 |
in |
| 91 |
case (io, to) of |
| 92 |
(IOManager.EMPTYQUEUE, NONE) => |
| 93 |
(* no waiting threads *) |
| 94 |
S.prepFn (!SH.shutdownHook, fn () => (true, OS.Process.failure)) |
| 95 |
| (_, SOME NONE) => |
| 96 |
(* enqueued a waiting timeout thread *) |
| 97 |
S.next () |
| 98 |
| (IOManager.READIED, _) => |
| 99 |
(* enqueued a waiting IO manager thread *) |
| 100 |
S.next () |
| 101 |
| (IOManager.EMPTYQUEUE, SOME (SOME t)) => |
| 102 |
(* a waiting timeout thread will be ready in t time *) |
| 103 |
(if Time.toSeconds t <= 0 |
| 104 |
then () |
| 105 |
else S.doMasked (fn () => OS.Process.sleep t) |
| 106 |
; pauseHook ()) |
| 107 |
| (IOManager.INQUEUE pds, SOME (SOME t)) => |
| 108 |
(* poll descriptors for t time *) |
| 109 |
(if Time.toSeconds t <= 0 |
| 110 |
then () |
| 111 |
else S.doMasked (fn () => ignore (OS.IO.poll (pds, SOME t))) |
| 112 |
; pauseHook ()) |
| 113 |
| (IOManager.INQUEUE pds, NONE) => |
| 114 |
(* poll descriptors indefinitely *) |
| 115 |
(S.doMasked (fn () => ignore (OS.IO.poll (pds, NONE))); pauseHook ()) |
| 116 |
end |
| 117 |
|
| 118 |
fun doit (initialProc: unit -> unit, |
| 119 |
tq: Time.time option) = |
| 120 |
let |
| 121 |
val () = |
| 122 |
if isRunning () |
| 123 |
then raise Fail "CML is running" |
| 124 |
else () |
| 125 |
val (installAlrmHandler, restoreAlrmHandler) = prepareAlrmHandler tq |
| 126 |
val ((*cleanUp*)_, status) = |
| 127 |
S.switchToNext |
| 128 |
(fn thrd => |
| 129 |
let |
| 130 |
val () = R.isRunning := true |
| 131 |
val () = reset true |
| 132 |
val () = SH.shutdownHook := S.prepend (thrd, fn arg => (S.atomicBegin (); debug' "shutdownHook"; arg)) |
| 133 |
val () = SH.pauseHook := pauseHook |
| 134 |
val () = installAlrmHandler alrmHandler |
| 135 |
val () = ignore (Thread.spawn initialProc) |
| 136 |
in |
| 137 |
() |
| 138 |
end) |
| 139 |
val () = restoreAlrmHandler () |
| 140 |
val () = reset false |
| 141 |
val () = R.isRunning := false |
| 142 |
val () = S.atomicEnd () |
| 143 |
in |
| 144 |
status |
| 145 |
end |
| 146 |
|
| 147 |
fun shutdown status = |
| 148 |
if isRunning () |
| 149 |
then S.switch (fn _ => S.prepVal (!SH.shutdownHook, (true, status))) |
| 150 |
else raise Fail "CML is not running" |
| 151 |
end |