about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2014-08-28 20:44:16 -0700
committerKartik K. Agaram <vc@akkartik.com>2014-08-28 20:44:16 -0700
commitf3b0f4dc0513388135a57961b5a7a00fd2b9e4e6 (patch)
tree0be201c936adb0c0b29123cd425dad9b6528ecc0
parentaa66c8327de1b6ccae47cd107d8f99241da97487 (diff)
downloadmu-f3b0f4dc0513388135a57961b5a7a00fd2b9e4e6.tar.gz
89 - a simple round-robin scheduler
-rw-r--r--mu.arc18
-rw-r--r--mu.arc.t24
2 files changed, 37 insertions, 5 deletions
diff --git a/mu.arc b/mu.arc
index b8f6137b..75660d15 100644
--- a/mu.arc
+++ b/mu.arc
@@ -181,13 +181,20 @@
   (let (oargs _ _)  (parse-instr ((body context 1) (pc context 1)))
     oargs))
 
-(def run (fn-name)
+(= contexts* (queue))
+
+(def run ((o fn-name))
   (ret result 0
-    (let context (make-context fn-name)
-      (while (~empty context)
-;?         (prn "== " context)
+    (aif fn-name
+      (enq make-context.it contexts*))
+    ; simple round-robin scheduler
+    (while (~empty contexts*)
+      (let context deq.contexts*
+        (trace "schedule" top.context!fn-name)
         (let insts-run (run-for-time-slice context scheduling-interval*)
-          (= result (+ result insts-run)))))))
+          (= result (+ result insts-run)))
+        (if (~empty context)
+          (enq context contexts*))))))
 
 (def run-for-time-slice (context time-slice)
 ;?   (prn "AAA")
@@ -199,6 +206,7 @@
         (pop-stack context)
         (if empty.context (return ninstrs))
         (++ pc.context))
+      (trace "run" top.context!fn-name " " pc.context ": " (body.context pc.context))
 ;?       (prn "--- " top.context!fn-name " " pc.context ": " (body.context pc.context))
 ;?       (prn "  " memory*)
       (let (oarg op arg)  (parse-instr (body.context pc.context))
diff --git a/mu.arc.t b/mu.arc.t
index f02f9610..2ab4d9c8 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -662,3 +662,27 @@
     (prn "F - 'new' returns current high-water mark"))
   (if (~iso Memory-in-use-until (+ before 5))
     (prn "F - 'new' on primitive arrays increments high-water mark by their size")))
+
+(reset)
+(add-fns
+  '((f1
+      ((1 integer) <- literal 3))
+    (f2
+      ((2 integer) <- literal 4))))
+(enq make-context!f1 contexts*)
+(enq make-context!f2 contexts*)
+(let ninsts (run)
+  (when (~iso 2 ninsts)
+    (prn "F - scheduler didn't run the right number of instructions: " ninsts)))
+(if (~iso memory* (obj 1 3  2 4))
+  (prn "F - scheduler runs multiple functions: " memory*))
+(check-trace-contents "scheduler orders functions correctly"
+  '(("schedule" "f1")
+    ("schedule" "f2")
+  ))
+(check-trace-contents "scheduler orders schedule and run events correctly"
+  '(("schedule" "f1")
+    ("run" "f1 0")
+    ("schedule" "f2")
+    ("run" "f2 0")
+  ))