Debian Codegraph @

lib/cml/core-cml/scheduler.sml

raw
1 (* scheduler.sml
2 * 2004 Matthew Fluet ([email protected])
3 * Ported to MLton threads.
4 *)
5
6 (* scheduler.sml
7 *
8 * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
9 * COPYRIGHT (c) 1989-1991 John H. Reppy
10 *
11 * This module implements the scheduling queues and preemption
12 * mechanisms.
13 *)
14
15 structure Scheduler : SCHEDULER =
16 struct
17 structure Assert = LocalAssert(val assert = false)
18 structure GlobalDebug = Debug
19 structure Debug = LocalDebug(val debug = false)
20
21 open Critical
22
23 structure Q = ImpQueue
24 structure T = MLton.Thread
25 structure TID = ThreadID
26 structure SH = SchedulerHooks
27
28 type thread_id = ThreadID.thread_id
29 datatype thread = datatype RepTypes.thread
30 datatype rdy_thread = datatype RepTypes.rdy_thread
31
32 fun prep (THRD (tid, t)) = RTHRD (tid, T.prepare (t, ()))
33 fun prepVal (THRD (tid, t), v) = RTHRD (tid, T.prepare (t, v))
34 fun prepFn (THRD (tid, t), f) = RTHRD (tid, T.prepare (T.prepend (t, f), ()))
35
36 (* the dummy thread Id; this is used when an ID is needed to get
37 * the types right
38 *)
39 val dummyTid = TID.bogus "dummy"
40 (* the error thread. This thread is used to trap attempts to run CML
41 * without proper initialization (i.e., via RunCML). This thread is
42 * enqueued by reset.
43 *)
44 val errorTid = TID.bogus "error"
45 fun errorThrd () : unit thread =
46 THRD (errorTid, T.new (fn () =>
47 (GlobalDebug.sayDebug
48 ([fn () => "CML"], fn () => "**** Use RunCML.doit to run CML ****")
49 ; raise Fail "CML not initialized")))
50
51 local
52 val curTid : thread_id ref = ref dummyTid
53 in
54 fun getThreadId (THRD (tid, _)) = tid
55 fun getCurThreadId () =
56 let
57 val tid = !curTid
58 in
59 tid
60 end
61 fun setCurThreadId tid =
62 let
63 val () = Assert.assertAtomic' ("Scheduler.setCurThreadId", NONE)
64 in
65 curTid := tid
66 end
67 end
68 fun tidMsg () = TID.tidToString (getCurThreadId ())
69 fun debug msg = Debug.sayDebug ([atomicMsg, tidMsg], msg)
70 fun debug' msg = debug (fn () => msg)
71
72 (* The thread ready queues:
73 * rdyQ1 is the primary queue and rdyQ2 is the secondary queue.
74 *)
75 val rdyQ1 : rdy_thread Q.t = Q.new ()
76 and rdyQ2 : rdy_thread Q.t = Q.new ()
77
78 (* enqueue a thread in the primary queue *)
79 fun enque1 thrd =
80 (Assert.assertAtomic' ("Scheduler.enque1", NONE)
81 ; Q.enque (rdyQ1, thrd))
82 (* enqueue a thread in the secondary queue *)
83 fun enque2 thrd =
84 (Assert.assertAtomic' ("Scheduler.enque2", NONE)
85 ; Q.enque (rdyQ2, thrd))
86 (* dequeue a thread from the primary queue *)
87 fun deque1 () =
88 (Assert.assertAtomic' ("Scheduler.deque1", NONE)
89 ; case Q.deque rdyQ1 of
90 NONE => deque2 ()
91 | SOME thrd => SOME thrd)
92 (* dequeue a thread from the secondary queue *)
93 and deque2 () =
94 (Assert.assertAtomic' ("Scheduler.deque2", NONE)
95 ; case Q.deque rdyQ2 of
96 NONE => NONE
97 | SOME thrd => SOME thrd)
98 (* promote a thread from the secondary queue to the primary queue *)
99 fun promote () =
100 (Assert.assertAtomic' ("Scheduler.promote", NONE)
101 ; case deque2 () of
102 NONE => ()
103 | SOME thrd => enque1 thrd)
104
105 fun next () =
106 let
107 val () = Assert.assertAtomic' ("Scheduler.next", NONE)
108 val () = debug' "Scheduler.next" (* Atomic 1 *)
109 val thrd =
110 case deque1 () of
111 NONE => !SH.pauseHook ()
112 | SOME thrd => thrd
113 in
114 thrd
115 end
116 fun ready thrd =
117 let
118 val () = Assert.assertAtomic' ("Scheduler.ready", NONE)
119 val () = enque1 thrd
120 in
121 ()
122 end
123 local
124 fun atomicSwitchAux msg f =
125 (Assert.assertAtomic (fn () => "Scheduler." ^ msg, NONE)
126 ; debug (fn () => "Scheduler." ^ msg)
127 ; T.atomicSwitch (fn t =>
128 let
129 val tid = getCurThreadId ()
130 val () = TID.mark tid
131 val RTHRD (tid',t') = f (THRD (tid, t))
132 val () = setCurThreadId tid'
133 in
134 t'
135 end))
136 in
137 fun atomicSwitch (f: 'a thread -> rdy_thread) =
138 atomicSwitchAux "atomicSwitch" f
139 fun switch (f: 'a thread -> rdy_thread) =
140 (atomicBegin (); atomicSwitch f)
141 fun atomicSwitchToNext (f: 'a thread -> unit) =
142 atomicSwitchAux "atomicSwitchToNext" (fn thrd => (f thrd; next ()))
143 fun switchToNext (f: 'a thread -> unit) =
144 (atomicBegin (); atomicSwitchToNext f)
145 fun atomicReadyAndSwitch (f: unit -> rdy_thread) =
146 atomicSwitchAux "atomicReadyAndSwitch" (fn thrd => (ready (prep thrd); f ()))
147 fun readyAndSwitch (f: unit -> rdy_thread) =
148 (atomicBegin (); atomicReadyAndSwitch f)
149 fun atomicReadyAndSwitchToNext (f: unit -> unit) =
150 atomicSwitchAux "atomicReadyAndSwitchToNext" (fn thrd => (ready (prep thrd); f (); next ()))
151 fun readyAndSwitchToNext (f: unit -> unit) =
152 (atomicBegin (); atomicReadyAndSwitchToNext f)
153 end
154
155 fun new (f : thread_id -> ('a -> unit)) : 'a thread =
156 let
157 val () = Assert.assertAtomic' ("Scheduler.new", NONE)
158 val tid = TID.new ()
159 val t = T.new (f tid)
160 in
161 THRD (tid, t)
162 end
163
164 fun prepend (thrd : 'a thread, f : 'b -> 'a) : 'b thread =
165 let
166 val () = Assert.assertAtomic' ("Scheduler.prepend", NONE)
167 val THRD (tid, t) = thrd
168 val t = T.prepend (t, f)
169 in
170 THRD (tid, t)
171 end
172
173 fun unwrap (f : rdy_thread -> rdy_thread) (t: T.Runnable.t) : T.Runnable.t =
174 let
175 val () = Assert.assertAtomic' ("Scheduler.unwrap", NONE)
176 val tid = getCurThreadId ()
177 val RTHRD (tid', t') = f (RTHRD (tid, t))
178 val () = setCurThreadId tid'
179 in
180 t'
181 end
182
183
184 (* reset various pieces of state *)
185 fun reset running =
186 (atomicBegin ()
187 ; setCurThreadId dummyTid
188 ; Q.reset rdyQ1; Q.reset rdyQ2
189 ; if not running then ready (prep (errorThrd ())) else ()
190 ; atomicEnd ())
191 (* what to do at a preemption (with the current thread) *)
192 fun preempt (thrd as RTHRD (tid, _)) =
193 let
194 val () = Assert.assertAtomic' ("Scheduler.preempt", NONE)
195 val () = debug' "Scheduler.preempt" (* Atomic 1 *)
196 val () = Assert.assertAtomic' ("Scheduler.preempt", SOME 1)
197 val () =
198 if TID.isMarked tid
199 then (TID.unmark tid
200 ; promote ()
201 ; enque1 thrd)
202 else enque2 thrd
203 in
204 ()
205 end
206
207 val _ = reset false
208 end