RTOS Engineer at WHIS
Last updated 10th May 2024
Used as Lightweight Binary Semaphores Within an RTOS
A binary semaphore is a semaphore that has a maximum count of 1, hence the ‘binary’ name. A task can only ‘take’ the semaphore if it is available, and the semaphore is only available if its count is 1.
From the RTOS point of view, binary semaphores are essentially a special case of a traditional RTOS queue. Although no data is actually sent through the queue, the queue control structure is still necessary. Using task notifications in place of a binary semaphore, removes the need for the queue control structure, therefore saving RAM and simplifying the internal RTOS processes.
From an application point of view, the creation of the Binary Semaphore is no longer necessary and the calls to ‘take’ and ‘give’ the semaphore are replaced with calls to ‘take’ and ‘give’ the Task Notification. Typical uses are shown in Figure 1 and Figure 2.
Figure 1: Using RTOS Task Notifications as a Binary Semaphore Between Tasks
Figure 2: Using RTOS Task Notifications as a Binary Semaphore Between an ISR and a Task.
Performance Benefits and Usage Restrictions
Task Notifications have speed & RAM size advantages over other RTOS features with equivalent functionality. However, these benefits require some use case limitations:
- RTOS Task Notifications can only be used when there is only one task that can be the recipient of the event. However this condition is met in the majority of real world applications.
- Where an RTOS Task Notification is used in place of a Queue: While a receiving task can wait for a notification in the Blocked state (so not consuming any CPU time), a sending task cannot wait in the Blocked state for a Send to complete if the Send cannot complete immediately.
Used as a Lightweight Counting Semaphore
An RTOS Counting Semaphore is a semaphore that can have a count value of zero up to a maximum value set when the semaphore is created. A task can only ‘take’ the semaphore if it is available, and the semaphore is only available if its count is greater than zero.
As with Binary Semaphores, from the RTOS point of view, Counting Semaphores are essentially a special case of a traditional RTOS queue. Although no data is actually sent through the queue, the queue control structure is still necessary. Using Task Notifications in place of a Counting Semaphore, removes the need for the queue control structure, therefore saving RAM and simplifying the internal RTOS processes.
When a Task Notification is used in place of a Counting Semaphore, the receiving task’s notification value is used in place of the Counting Semaphore’s count value, and is therefore incremented whenever the Task Notification is ‘given’ and decremented whenever the Task Notification is ‘taken’. The example in Figure 3. uses the receiving task’s notification value as a counting semaphore.
Figure 3: Using RTOS Task Notifications as a Counting Semaphore Between an ISR and a Task
Used as a Lightweight Event Group in an RTOS
An Event Group is a set of binary flags (or bits), to each of which the application writer can assign a meaning. The task notification value can be used as such an Event Group, allowing the RTOS task to enter the Blocked state to wait for one or more flags within the group to become active as shown in Figure 4.
Figure 4: Using RTOS Task Notifications as an Event Group
Used as a Lightweight Mailbox (Queue)
RTOS Task Notifications can be used to send data to a task, but in a much more restricted way than can be achieved with an RTOS queue because:
- Only 32-bit values can be sent;
- The value is saved as the receiving task’s notification value, and there can only be one notification value at any one time.
Hence the phrase ‘lightweight mailbox’ is used in preference to ‘lightweight queue’. The task’s notification value is the mailbox value. Figure 5 demonstrates the use of task notifications as a lightweight mailbox.
Figure 5: Using RTOS Task Notifications as a Lightweight Mailbox
Ask Us a Question
For pricing, licensing, or any other sales or product related questions, please contact us.
Related Topics
Further Your knowledge of RTOS with our RTOS Resource Centre