Enhancement--"Weight"
Once the system was running, I got a lot of inquiries from my peers about adding prioritization but to me the concept of prioritization for this application was wasted and defeated the purpose of the system. (Why is beyond the scope of this article.) When you add prioritization in a multithreaded system, you need to add the concept of blocking, which requires adding another queue of blocked items, among others. That would start adding complexity I didn't want. I wanted simple! I wondered how I could add the concept of one task being more important than another task without starvation or complexity: I came up with the concept of "weighted" importance. If a task has more weight, it requires additional ticks during its share of the CPU time. This can be abused if you are not careful to the point where starvation occurs, so think carefully before adding too much weight to a task. This enhancement changed the task control block structure to the code in Listing 6.
View the full-size image
The two new items are Weight and Count, where Weight is the amount of clock ticks this task will run consecutively before letting the next task run and Count is the number of clock ticks that have run so far. The base task always has a weight of 1.
View the full-size image
Listing 7 is the task initialization with weighting where the second parameter is now the Weight of the task.
The TaskCreate function would set Count to zero and then assign the second parameter to the Weight member of the TCB. Of course, good design states that you have a maximum Weight allowed in the system and then do some error checking.
The timer tick ISR would simply bump the Count member of the task's TCB by one, see if it equals the Weight member. If not, just return. If it does equal the Weight then switch tasks. Listing 8 is the modified portion of the timer tick ISR.
View the full-size image