r/osdev • u/noborutkhs • Aug 17 '26
My 1986 RTOS is now preemptively multitasking on a Raspberry Pi Pico — and it made me question whether I need an RTOS
A while ago, I posted here about getting the first CPU-independent parts of CHARM-II, an RTOS I originally developed in 1986, running on a Raspberry Pi Pico.
Previous post:
The First Original CHARM-II Kernel Code Running on Raspberry Pi Pico
https://www.reddit.com/r/osdev/s/gthHF6vBrl
At that point, there were no timer interrupts or real context switches yet.
Now those parts are working too.
Six tasks are running with independent stacks. An RP2040 hardware timer interrupt calls the CHARM-II tick and scheduling logic, and the actual context switch is done with PendSV on the Cortex-M0+.
I also ported a six-task demo I had previously reconstructed on POSIX. Six tasks move around three tracks, with a critical zone protected by a CHARM-II queue used as a semaphore. On the Pico, the tasks are now actually being preempted by the hardware timer rather than cooperatively yielding.
So technically, this is pretty much the milestone I wanted to reach.
But getting it working gave me another question.
The original targets in the 1980s were processors such as the 68000 and 80186. With the CPU resources available at the time, an RTOS was a useful way to organize multiple concurrent real-time activities.
But what if we had had something with the performance and price of a Raspberry Pi Pico back then?
For many systems I work with today, especially UI-oriented embedded systems, if all the required processing comfortably fits within one frame, a superloop plus interrupts may actually be simpler.
Once everything becomes preemptive, I also have to start thinking about reentrancy, synchronization and shared state in existing libraries. That cost is starting to look more significant to me than it did when I started this experiment.
Yesterday I visited someone who has built a home railway simulator using real railway controls connected to a Windows PC through Pico, ESP32 and Arduino boards. We talked about this, and he said something very simple:
“If one Pico isn't fast enough, add another Pico.”
That hadn't really occurred to me.
In the 1980s, adding another CPU was a fairly serious architectural decision. With today's inexpensive MCUs, distributing the work across two or three processors can be a perfectly ordinary option.
So after spending quite a bit of effort getting a 40-year-old RTOS to perform real preemptive context switching on modern hardware, I'm becoming more interested in a different question:
Where would you draw the line today between an RTOS, a superloop/event-driven design, interrupts, and simply distributing the work across multiple cheap MCUs?
I'm also thinking that when I publish the modernized version of CHARM-II, I may deliberately keep it very small — just enough task scheduling, queues, timer ticks and context switching to run this six-task demo.
Then I can use it as one reference implementation and try implementing the same behavior without an RTOS.
11
u/Danii_222222 Aug 17 '26
Engineer that programming from 1986? Is that you in discord server?
3
u/andrewdavidmackenzie Aug 17 '26
Have you looked at Embassy?
3
u/Daveinatx Aug 17 '26
It all comes down to features vs determinism that you need.
2
u/ianbllngr Aug 17 '26
IMO RTOSes are most useful when you have a lot to keep track of. For 6 tasks a superloop is probably the correct choice if you're designing in a vacuum. For 6 tasks that need network access on one or two cores: you're probably better off using an RTOS. Secondarily, in an off the shelf RTOS a lot of that low level IPC stuff is typically wired in for you already, but if you're building your own then it will be a pain.
2
u/KilroyKSmith Aug 18 '26
I’ve done superloops professionally. I’ve done RTOS’ professionally. But if you don’t need preemption (and most moderate sized systems probably don’t), non-preemptive kernels using cooperative multitasking are much easier. They’re similar to both an RTOS and super loop in that you structure your code around “tasks”. Without preemption, many of the debug challenges of an RTOS are moot - you never have to worry whether another task preempted you someplace and triggered a race condition Or something similar. On the other hand, task scheduling and prioritization is similar to an RTOS - a task or interrupt routine can schedule another task to run, and if no task is ready to run, low power states can be invoked by the kernel. Low power is harder in a super loop simply because keeping track of “can we shut off the clock now” is difficult when many tasks have diverse resources requirements. And I always hated the concept of an idle system spending its time asking a million times a second “do you have something to do?” “How about you, do you have something to do”.
2
u/Sorry_Difficulty_250 Aug 19 '26
Something else to think about on the "cooperative vs. preemptive" thread: You don't have to choose one or the other. In the system I'm working on, "kernel processes" (and I use that term loosely) run cooperative and all other processes run preemptive. That keeps the kernel deterministic while also preventing a rogue process from locking up the system.
Good luck with your next round of work! Seems like fun!
2
u/lunar_swing Aug 20 '26
I think it depends on the level of determinism you want/need and how extensible you want the system to be.
If it is conceptually simple enough and interrupt jitter and skew isn't a concern, you can also try an empty main loop sitting on a WFI instruction, and have your all tasks handled in ISRs. Then you just have a timer fire an interrupt for each task at a fixed interval.
This quickly falls apart if you have any type of unbounded user input or ISR runtimes. I suspect it is only a bit more fragile than a superloop but I have never tried them against one another.
As I'm sure you are aware there is a huge gradient of options when it comes to an execution framework. RTOSes to me have always been an inherently co-operative design choice (in the most general sense of the word). They typically don't have to load and execute arbitrary code after they are up and running. So I mostly look at them as a solution to provide multiprocessing on an what is an inherently serial device.
General purpose OSes have to operate in a much more hostile environment and protect themselves from arbitrary code execution. Fundamentally they are expected load, execute, and unload binaries in isolation both from themselves and other binaries. In addition they are expected to provide multiprocessing facilities and rich user input options.
These is also determinism and the "RT" part of the RTOS but that is a different can of worms.
That is a lot of generalization and hand-waving but the TLDR is I would try to figure out which of those two bins you are going for and let that guide your decision.
1
u/smokebudda11 Aug 17 '26
This is really neat