This section presents more complex design patterns for synchronization and communication. Multiple synchronization primitives can be found in a single design pattern.
15.7.1 Data Transfer with Flow Control
Task-to-task communication commonly involves data transfer. One task is a producer, and the other is a data consumer. Data processing takes time, and the consumer task might not be able to consume the data as fast as the producer can produce it. The producer can potentially overflow the communication channel if a higher priority task preempts the consumer task. Therefore, the consumer task might need to control the rate at which the producer task generates the data. This process is accomplished through a counting semaphore, as shown in Figure 15.17. In this case, the counting semaphore is a permission to produce data.
Figure 15.17: Using counting semaphores for flow control.
The data buffer in this design pattern is different from an RTOS-supplied message queue. Typically, a message queue has a built-in flow control mechanism. Assume that this message buffer is a custom data transfer mechanism that is not supplied by the RTOS.
As shown in Figure 15.17, task #1 is the data producer, while task #2 is the consumer. Task #1 can introduce data into the buffer as long as the task can successfully acquire the counting semaphore. The counting semaphore may be initialized to a value less than the maximum allowable token value. Task #2 can increase the token value with the give operation and may decrease the token value by the take operation depending on how fast the task can consume data. Listing 15.2 shows the pseudo code for this design pattern.
Listing 15.2: Pseudo code for data transfer with flow control.
data producing task
Acquire(Counting_Semaphore)
Produce data into msgQueue
data consuming task
Consume data from MsgQueue
Give(Counting_Semaphore)
15.7.2 Asynchronous Data Reception from Multiple Data Communication Channels
Commonly, a daemon task receives data from multiple input sources, which implies that data arrives on multiple message queues. A task cannot block and wait for data on multiple message queues. Therefore, in such cases, multiple sources may use a single semaphore to signal the arrival of data. A task cannot block and wait on multiple semaphores either.
The task blocks and waits on the semaphore. Each ISR inserts data in the corresponding message queue followed by a give operation on the semaphore.
As shown in Figure 15.18, a single interrupt lock is sufficient to protect against multiple interrupt sources, as long as the masked interrupt level covers these sources. Both the interrupt service routines use a single semaphore as the signal channel.
Figure 15.18: Task waiting on multiple input sources.
Listing 15.3 shows the code that the task runs when multiple input message queues are present. Note that the semaphore used in this case is a binary semaphore.
Listing 15.3: Pseudo code for task waiting on multiple input sources.
while (Get(Binary_Semaphore))
disable(interrupts)
for (each msgQueue)
get msgQueueLength
for (msgQueueLength)
remove a message
enable(interrupts)
process the message
disable(interrupts)
endfor
endfor
enable(interrupts)
end while
Some RTOS kernels do not have the event-register object. Implementing the event register using the common basic primitives found in the majority of the RTOS kernels can be quite useful when porting applications from one RTOS to another.
The event-register object can be implemented using a shared variable, an interrupt lock, and a semaphore. The shared variable stores and retrieves the events. The interrupt lock guards the shared variable because ISRs can generate events through the event register. The semaphore blocks the task wanting to receive desired events.
Event_Receive(wanted_events) {
task_cb.wanted_events = wanted_events
While (TRUE)
Get(task_cb.event_semaphore)
disable(interrupts)
events = wanted_events XOR task_cb.recvd_events
task_cb.wanted_events = task_cb.wanted_event AND (NOT events)
enable(interrupts)
If (events is not empty)
return (events)
endIf
EndWhile
}
The variable task_cb refers to the task control block, in which the kernel keeps its private, task-specific information. Note that the unwanted events are not cleared because the task can call event_receive some time later.
Event_Send(events) {
disable(interrupts)
task_cb.recvd_events = task_cb.recvd_events OR events
enable(interrupts)
Give(task_cb.event_semaphore)
}