Converting cooperative OS to preemptive OS - Page 1
Forum section: electronics / general

author: blueskull
author_rank: Unknown
timestamp: March 07, 2020, 04:21:33 pm
content: Is it possible to convert a cooperative OS to a preemptive OS? If so, what is the general procedure?I'm working on a project with AllWinner Tina (F1C series), and I'd like to get an OS to run on it.Since it is a relatively less popular chip from China, there's no FreeRTOS or other Western RTOS ports for it, and I'm not interested in porting everything from scratch.There are three OSes that I can use right now, XBoot, RT-Thread and Linux.Linux has the advantage of supporting all peripherals, but that's a feature that I don't need, besides I want as fast as possible boot time, so I'll just skip that part. Booting XBoot takes shorter than u-boot itself, rest alone Linux.RT-Thread had a good port on this chip, but it was removed from official GIT and no future support will be given as RTT received funding from a VC, and part of the deal is RTT team must generate revenue, so their Tina port has been dumb down since then so they can sell proprietary products based on it.Besides, I'm really not fond of scons. I prefer Makefile, and I hate Python. I'm a religiously firm C user for the past 15 years.That leaves me with XBoot, an OS I had read inside out, and stripped it all the way down from 3.4MB to 230kB. I've got all drivers I needed to work, and I did quite some deep customization to it.I'd be very prefer if I can modify XBoot from cooperative to preemptive, but I don't know how.It has a scheduler already, and the libcpu part has context switching code already. I've also got the timer part done, so I can now generate ticks and call functions periodically. I think all I need is to call the scheduler periodically (which was called only upon yield(), as it was cooperative).What do you think? If I add a tracer in scheduler structure to indicate the current task, and call yield() with that current task as its current task parameter, in a timer callback (and use some locking mechanism to make sure the timer doesn't switch a task during a task is yielding), should it work?

author: PlainName
author_rank: Super Contributor
timestamp: March 07, 2020, 06:57:59 pm
content: The big issue with going that way is access to shared stuff. With co-operative you only worry about that when you call yield (or some OS function that might achieve the same). So, for instance, you might have a callable function to post a task event, and that function would be callable by any other task. With cooperative multitasking, once you're in that function you know nothing else is going to get in the way. As a trivial example, you could call asctime() without undue hassle.The same function in a pre-emptive multitasker would be very different because you could get a second task entering in just as you hit asctime(). The second task would also end up calling that and screw up the shared output string - asctime is not a reentrant-capable function. It's fixable by having, say, and access mutex so only one task can hit the dodgy bits at a time. If you were writing for a pre-emptive multitasker from the start then you'd put that stuff in as you write the function (or not use things like asctime()). The issue you have here is that you need to identify those dodgy things and deal with them having forgotten what the code does (or even that it exists).Other than that...So it's doable. How practical depends on the specific project.

author: PlainName
author_rank: Super Contributor
timestamp: March 08, 2020, 06:01:57 pm
content: QuoteEasy mistake to make

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 08, 2020, 07:09:22 pm
content: Good luck. I don't know XBoot, but converting a cooperative OS to fully preemptive, even if the base functions are all there, is going to give you many headaches IMO. I'm sure many of the base blocks are not designed to accomodate being preempted. You will probably need to modify all drivers as well. A lot of work. I don't know if it's worth it, but keep us posted.

author: rstofer
author_rank: Super Contributor
timestamp: June 09, 2020, 06:16:15 pm
content: What's an OS? To you...To me, Windows or Linux is an OS, some embedded chip running FreeRTOS as a real-time scheduler might be viewed differently.Does the OS include higher level code like a network stack? Are the tasks static (like an embedded system) or is there some kind of shell for command line stuff? Is there a user accessible file system?I think you need to list the features the system is to provide.I don't see much commonality between a preemptive OS and a cooperative OS.As to FreeRTOS, don't build what you can buy! There are people who specialize in porting FreeRTOS and they can probably do in a week what it would take a newcomer an entire year to get working. It would be cheaper to buy the port than to build it in-house.See "Getting Help" here:https://www.freertos.org/FreeRTOS-porting-guide.html

author: wizard69
author_rank: Super Contributor
timestamp: June 11, 2020, 04:37:13 am
content: Quote from: blueskull on March 07, 2020, 04:21:33 pmPorting is far easier than converting. You might even get help with porting as an open source project. Converting an cooperative OS is a massive headache.

author: coppice
author_rank: Super Contributor
timestamp: June 11, 2020, 05:10:10 am
content: Quote from: wizard69 on June 11, 2020, 04:37:13 amThere is a simple test that demonstrates this to be true. If it were a modest task to turn a cooperative multi-tasker into a pre-emptive multi-tasker there would be no cooperative multi-taskers around, as they are far less flexible. The reason both types exists is there is a gulf between them.

author: coppice
author_rank: Super Contributor
timestamp: June 11, 2020, 06:04:02 am
content: Quote from: blueskull on June 11, 2020, 05:25:35 amIt wasn't that hard to get some preliminary results because you started with something built, at its lowest levels, to be pre-emptive. However, getting from "some preliminary working results" to a polished platform is a long long road. It the road most travelled in software development, which is why software projects are notorious for delivering late.