June 6, 2023


Your Partner in the Digital Era

Programming embedded units: RTOS – efficient blocking

In this fourth lesson in the Real-Time Running Method (RTOS) series, you will see how to substitute horribly inefficient polling hold off with productive thread blocking.

In this fourth lesson in the Serious-Time Operating Technique (RTOS) collection, you’ll see how to swap the horribly inefficient polling hold off with economical thread blocking. Exclusively, in this lesson 25, you will include a blocking OS_delay() function to the MiROS RTOS [1] and discover the far-reaching implications of thread blocking on the RTOS structure.

Lesson 25 – RTOS Portion-4: Economical blocking of threads

Successful Blocking by Switching Context

The standard notion for steering clear of wasteful polling, as in the BSP_delay() perform, is to switch the CPU context absent from the waiting around thread and then change the context back again following the hold off has elapsed. This sort of a waiting around system is referred to as blocking, and its most important benefit is the a lot extra effective use of the CPU because a blocked thread consumes no CPU cycles at all. As an alternative, the CPU can operate other threads that actually have a little something useful to do.

From the application developer’s stage of look at, the habits stays the same—the OS_hold off() perform does not return until finally the asked for hold off has elapsed. Nevertheless, the OS_hold off() implementation causes an upheaval in the inside RTOS design.

Threads States

A single immediate consequence of the blocking mechanism is that the RTOS must “know” to exclude all blocked threads from the scheduling, which means that threads need a new state “blocked” alongside the previously current state “ready.”

A really efficient way to stand for a dynamic life cycle of objects, like RTOS threads, is by a condition machine. In the point out diagram revealed in the video, states are depicted as “blobs,” and point out alterations, identified as transitions, as “arrows.” The gain of these types of a representation is that it forces you to take into account all possible states and transitions. For case in point, a call to OS_delay() will cause a changeover to the “blocked” condition. But you can also clearly see that a different RTOS service is desired to changeover out of the “blocked” condition. That provider, recognized as OS_tick(), ought to be invoked from an interrupt (SysTick in the video) for the reason that the blocked thread is unresponsive and cannot do anything.

The Idle Thread

A state machine illustration of the thread life cycle also provokes inquiries like: Can all threads at the same time be in the “blocked” condition? It turns out they can, but what ought to the scheduler do when all threads are excluded from scheduling?

The answer is to add a unique idle thread, which the CPU can change to when all other threads are blocked. This idle thread can not block, so its life cycle has no “blocked” point out. The primary use of the idle condition is to set the CPU and peripherals into a very low-energy rest mode to conserve power (e.g., in battery-run devices). In truth, supplying the idle thread as a single, centralized location to perform electricity administration is just one of the RTOS’s sizeable gains.

End Notes

Even even though you’ve carried out thread blocking only for the time delay in this lesson, the blocking mechanism is essential to all other RTOS expert services for conversation and synchronization among the threads.

At this position, your MiROS RTOS [1] implements a spherical-robin scheduler with blocking, which around corresponds to the state-of-the-art time-sharing programs of the early 1960s. In the subsequent lesson, you are going to finally discover what the phrase “Real-Time” in the RTOS title indicates, and you are going to convey the MiROS RTOS into the 1970s by applying preemptive precedence-based mostly scheduling. Remain tuned!

[1] Miro Samek, MiROS (Nominal Serious-Time Functioning Process), GitHub

Dr. Miro M. Samek is the creator of the open up supply QP authentic-time embedded frameworks and the freeware QM graphical design-primarily based structure tool. He is also the founder and CEO of Quantum Leaps — the company of modern-day embedded software package centered on lively objects and hierarchical point out devices as effectively as equipment for visual modeling, automated code generation, and device testing of deeply embedded software package. Miro teaches the well known YouTube “Modern Embedded Programs Programming” video course on which this report series is dependent.

Associated Contents: