89 - a simple round-robin scheduler

This commit is contained in:
Kartik K. Agaram 2014-08-28 20:44:16 -07:00
parent aa66c8327d
commit f3b0f4dc05
2 changed files with 37 additions and 5 deletions

18
mu.arc
View File

@ -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))

View File

@ -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")
))