From f3b0f4dc0513388135a57961b5a7a00fd2b9e4e6 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 28 Aug 2014 20:44:16 -0700 Subject: 89 - a simple round-robin scheduler --- mu.arc | 18 +++++++++++++----- mu.arc.t | 24 ++++++++++++++++++++++++ 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") + )) -- cgit 1.4.1-2-gfad0