about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--mu.arc26
-rw-r--r--mu.arc.t24
2 files changed, 47 insertions, 3 deletions
diff --git a/mu.arc b/mu.arc
index 4395bc27..5779ac47 100644
--- a/mu.arc
+++ b/mu.arc
@@ -171,6 +171,8 @@
 (on-init
   (= running-routines* (queue))
   (= completed-routines* (queue))
+  ; set of sleeping routines; don't modify routines while they're in this table
+  (= sleeping-routines* (table))
   (= routine* nil)
   (= abort-routine* (parameter nil))
   (= curr-cycle* 0)
@@ -189,12 +191,23 @@
     (enq make-routine.it running-routines*))
   ; simple round-robin scheduler
   (while (~empty running-routines*)
+    (each (routine _) sleeping-routines*
+      (awhen (> curr-cycle* rep.routine!sleep.1)
+        (trace "schedule" "waking up " top.routine!fn-name)
+        (wipe sleeping-routines*.routine)  ; before modifying routine below
+        (wipe rep.routine!sleep)
+        (++ pc.routine)  ; complete the sleep instruction
+        (enq routine running-routines*)))
     (= routine* deq.running-routines*)
     (trace "schedule" top.routine*!fn-name)
     (routine-mark:run-for-time-slice scheduling-interval*)
-    (if (~empty routine*)
-      (enq routine* running-routines*)
-      (enq-limit routine* completed-routines*))))
+    (if rep.routine*!sleep
+          (do (trace "schedule" "pushing " top.routine*!fn-name " to sleep queue")
+              (set sleeping-routines*.routine*))
+        (~empty routine*)
+          (enq routine* running-routines*)
+        :else
+          (enq-limit routine* completed-routines*))))
 
 (def die (msg)
   (= rep.routine*!error msg)
@@ -202,6 +215,11 @@
   (wipe rep.routine*!call-stack)
   ((abort-routine*)))
 
+(def sleep-for (delay)
+  (trace "run" "sleeping until " (+ curr-cycle* delay))
+  (= rep.routine*!sleep `(literal ,(+ curr-cycle* delay)))
+  ((abort-routine*)))
+
 ;; running a single routine
 (mac v (operand)  ; for value
   `(,operand 0))
@@ -465,6 +483,8 @@
                 ; inspect it
                 assert
                   (assert (m arg.0))
+                sleep
+                  (sleep-for (m arg.0))
 
                 ; text interaction
                 cls
diff --git a/mu.arc.t b/mu.arc.t
index e8257606..41561520 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -1674,6 +1674,30 @@
     ("run" "f2 1")
   ))
 
+(reset)
+(new-trace "sleep")
+(add-fns
+  '((f1
+      (sleep (1 literal))
+      ((1 integer) <- copy (3 literal))
+      ((1 integer) <- copy (3 literal)))
+    (f2
+      ((2 integer) <- copy (4 literal))
+      ((2 integer) <- copy (4 literal)))))
+;? (= dump-trace* (obj whitelist '("run" "schedule")))
+(= scheduling-interval* 1)
+(run 'f1 'f2)
+(check-trace-contents "scheduler handles sleeping routines"
+  '(("run" "f1 0")
+    ("run" "sleeping until 2")
+    ("schedule" "pushing f1 to sleep queue")
+    ("run" "f2 0")
+    ("run" "f2 1")
+    ("schedule" "waking up f1")
+    ("run" "f1 1")
+    ("run" "f1 2")
+  ))
+
 ; The scheduler needs to keep track of the call stack for each routine.
 ; Eventually we'll want to save this information in mu's address space itself,
 ; along with the types array, the magic buffers for args and oargs, and so on.
9ab857d305a0e58b'>^
4c37b3e9 ^
d1c9392a ^


1f08b541 ^
5a2cb154 ^


4c37b3e9 ^
5a2cb154 ^
4c37b3e9 ^
5a2cb154 ^


4c37b3e9 ^
5a2cb154 ^
4c37b3e9 ^

5a2cb154 ^


4c37b3e9 ^
5a2cb154 ^

d1c9392a ^

5a2cb154 ^
d1c9392a ^
5a2cb154 ^
d1c9392a ^
5a2cb154 ^
4c37b3e9 ^
5a2cb154 ^
d1c9392a ^
5a2cb154 ^
4c37b3e9 ^

5a2cb154 ^

4c37b3e9 ^
d1c9392a ^
4c37b3e9 ^


5a2cb154 ^





4c37b3e9 ^
5a2cb154 ^

4c37b3e9 ^
d1c9392a ^
4c37b3e9 ^
d1c9392a ^

4c37b3e9 ^
5a2cb154 ^



d1c9392a ^
4c37b3e9 ^
5a2cb154 ^
4c37b3e9 ^
5a2cb154 ^

d1c9392a ^
5a2cb154 ^
d1c9392a ^
5a2cb154 ^

4c37b3e9 ^
5a2cb154 ^

4c37b3e9 ^
d1c9392a ^



5a2cb154 ^

4c37b3e9 ^


5a2cb154 ^
4c37b3e9 ^
5a2cb154 ^

4c37b3e9 ^

d1c9392a ^
5a2cb154 ^
4c37b3e9 ^

5a2cb154 ^
d1c9392a ^
5a2cb154 ^

4c37b3e9 ^
d1c9392a ^

5a2cb154 ^

4c37b3e9 ^
5a2cb154 ^
d1c9392a ^
5a2cb154 ^

d1c9392a ^
5a2cb154 ^

d1c9392a ^

5a2cb154 ^



104e521c ^


4c37b3e9 ^


d1c9392a ^
4c37b3e9 ^
104e521c ^
4c37b3e9 ^
d1c9392a ^





1f08b541 ^

4c37b3e9 ^
1f08b541 ^


4c37b3e9 ^
1f08b541 ^
104e521c ^


4c37b3e9 ^
104e521c ^
4c37b3e9 ^
1f08b541 ^





4c37b3e9 ^
1f08b541 ^
4c37b3e9 ^
d1c9392a ^
1f08b541 ^









805d58c6 ^



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