Saturday, April 28, 2007

Round Robin

Writing your own scheduler can be illuminating :). I realized that it is one thing to conceptually "understand" the algorithms involved and entirely another to come up with your own source from scratch. Trouble is *other* things get in the way.

1. Debugging debugging debugging. How are you going to test your code on bare metal? A simulator can be a life saver here. I'm working on a hand wired C501 dev board using SDCC and JSIM.

2. Target Architecture -
Maintaining stack integrity. How is the stack pointer incremented? Can it address all of memory? Probably not. Is the high byte/word pushed before or after the low one during a function call? How big is your stack anyway?
I mean do you have to swap the stack to RAM too during a context switch?

Registers - How many registers do you have to push? The one I'm working with has 32 general purpose ones. Only bank '0' (zero) is used most of the time.
Timer - Mine has 3. What mode to use? How fast is the context switch? what is the %time slice per process?
Can you get by with setjmp and longjmp? I saw lightweight threading support added using just setjmp and longjmp.

3. Organizing code - Since you are pretty much juggling around with stacks, I got burnt breaking up my code into functions. So I'm exclusively relying on 'C' style macros for any semblance of organization. Its hairy to say the least. The actual data structures and algorithms are pretty simple.

Just in case you were wondering mine is a round robin scheduler. The code is here.