New proposal for an Arduino scheduler, applicable for ALL Arduino devices

Hi,

Let me introduce:
Arduino-MOS, a ultra lightweight cooperative multitasking scheduler for Arduino devices

I know, there are a lot of schedulers available, but this one is different :smiley:
MOS stands for Macro based Operating System.
It consists of only a few macros and can therefore be used on all Arduino devices.
It allows multitasking with blocking, cooperative tasks, but does not require additional memory.

So, give it a try...
The code with examples is available on github

/Joe

Looks lot like a sub-set of Protothreads. See, for instance, build hardware and write code for it: protothread and arduino, a first easy example.

This is difficult to actually use without some guidelines as the hidden goto's will require the programmer to save and restore any necessary context that might be needed.

GCC has some nice support for local labels.

Cheers!

PS: Here is a link to the Cosa implementation; Cosa/libraries/ProtoThread at master · mikaelpatel/Cosa · GitHub. It uses both objects, local labels and integrated with the Cosa Job Schedulers. Please see the acknowledgements, and references.

@kowalski

Thanks for your hints.
You are right, I should add a comment that task local variables will lose their value just like in the standard loop function.

/Joe

@kowalski

Looks lot like a sub-set of Protothreads. See, for instance, build hardware and write code for it: protothread and arduino, a first easy example.

You are right, there are great matches between the Protothreads and my solution.
Both go back to the article “Coroutines in C” from Simon Tatham (Coroutines in C)

I already worked with „Coroutines in C“ in 2003. I also tested the setjmp()/longjmp()
approach. My favorite solution was an assembler solution. I published an article (in German language) on my homepage in 2004 about "Coroutines in C".
I found it on the “way back machine”: http://web.archive.org/web/20070704161820/http://www.jstolberg.de/JoesCms/data/Programming/CoroutinenInC.php :smiley:

But I had forgotten it until I have started playing with Arduino devices last Year.

PS: To be honest: I am no C++ programmer and have some difficulties to understand your code, but it looks very clean and professional...

@JoeSto

It is fun that you mention a setjmp/longjmp implemented variant. That is actually the implementation method I used for a very simple scheduler:

This library allows a very simple method to start multiple setup() and loop() functions in the same sketch. The only requirement is that yield() or delay() are called at some point to context switch.

Please have a look. There are plenty of example sketches as some support classes for queues, events, semaphores, etc.

Cheers!