R8C Family General RTOS Concepts
Task Synchronization & Intertask Communication
Task synchronization and intertask communications serves to enable information to be transmitted safely from one task to another. The service also makes it possible for tasks to coordinate and cooperate with one another.
Synchronization is essential for tasks to share mutually exclusive resources (devices, buffers, etc) and/or allow multiple concurrent tasks to be executed (e.g. Task A needs a result from task B, so task A can only run till task B produces it). Task synchronization is achieved using two types of mechanisms; 1) Event Objects and 2) Semaphores.
Event objects are used when task synchronization is required without resource sharing. They allow one or more tasks to keep waiting for a specified event to occur. An event object can exist in either of two states: triggered and non-triggered. An event object in a triggered state indicates that a waiting task may resume. In contrast, if the event object is in a non- triggered state, a waiting task will need to stay suspended.
Figure 12 Working Principle of Event Objects
Sharing of resource among tasks can be a problem when the coordination is not well done. For instance, if task A begins reading a set of data currently being updated by task B, task A might received corrupted data – mixture of new and existing data. To resolve this problem, RTOS kernels provide a semaphore object and associated semaphore services to ensure the integrity of data.
A semaphore has an associated resource count and a wait queue. The resource count indicates availability of resource. The wait queue manages the tasks waiting for resources from the semaphore. A semaphore functions like a key that define whether a task has the access to the resource. A task gets an access to the resource when it acquires the semaphore. The resource count of a semaphore determines the number of times the semaphore can be acquired. When a task acquires the semaphore, its count decrement by one. Likewise, its count increment by one when a task releases the semaphore. Generally. There are three types of semaphore:
Binary Semaphores (semaphore value of either 0 or 1 to indicate unavailability and availability respectively)
Counting Semaphores (semaphore value of 0 or greater indicating it can be acquired/released multiple times)
Mutually Exclusion Semaphores (semaphore value of 0 or 1 but lock count can be 0 or greater for recursive locking)
Figure 13 illustrates the types of semaphore.
Page 10 of 18