<< Chapter < Page | Chapter >> Page > |
The DSP/BIOS real-time operating system provides for inter-task synchronization through the use of semaphores. The module that provides this function is the SEM module. This module will describe the basic use of SEM objects that are initialized in the configuration tool.
Semaphores provide a way for intertask communication and synchronization. They are used to coordinate access to a shared resource being accessed by multiple tasks. The SEM objects are counting semaphores which means they keep a record of the number of corresponding resources available.
There are two main functions for using a semaphore once it has been created.
SEM_pend
is used to wait for a semaphore. If the semaphore has a positive value, this function will simply decrement the semaphore and return. If the value of the semaphore is 0, the task calling
SEM_pend
is blocked and its state is changed to
TSK_BLOCKED . The timeout parameter to
SEM_pend
allows the task to wait until a timeout occurs, wait indefinitely (
SYS_FOREVER
), or not wait at all.
SEM_pend
's return value is used to indicate if the semaphore was signaled successfully.SEM_post
is used to signal a semaphore. If a particular task is waiting for the semaphore (in the
TSK_BLOCKED state),
SEM_post
removes the task from the semaphore queue and puts it on the ready queue (in the
TSK_READY state). In this case, the semaphore remains at 0. If no tasks are waiting,
SEM_post
simply increments the semaphore count and returns.To create an SEM object, open the configuration file, right click on Syncronization->SEM and select Insert SEM . You can right click on the object and select Properties to change its properties. The following figure shows a semaphore, SEM0, in the configuration tool with an initial value of 1.
As an example, suppose there are two tasks, TSK0 and TSK1, that are at the same priority. When program execution begins, TSK0 starts first. The semaphore, SEM0, is initialized to 1. The following code is used for the two tasks.
funTSK0(){
1 LOG_printf(&trace,"TSK0 Start");
2 SEM_pend(&SEM0,SYS_FOREVER);
3 LOG_printf(&trace,"TSK0 1");
4 SEM_pend(&SEM0);
11 LOG_printf(&trace,"TSK0 2");
12 SEM_post(&SEM0);
13 SEM_post(&SEM0);
14 LOG_printf(&trace,"TSK0 3");
15 SEM_post(&SEM0);
16 LOG_printf(&trace,"TSK0 Finish");
}funTSK1(){
5 LOG_printf(&trace,"TSK1 Start");
6 SEM_post(&SEM0);
7 LOG_printf(&trace,"TSK1 1");
8 SEM_post(&SEM0);
9 SEM_pend(&SEM0);
10 SEM_pend(&SEM0);
17 LOG_printf(&trace,"TSK1 2");
18 LOG_printf(&trace,"TSK1 Finish");
}
The following table shows the order of execution, the state of the two tasks, the value of the semaphore and any output printed. The Code Line # indicates which line of code is executed. The line in the table indicates the result after the line of code is executed. For instance, the line 1 indicates that the following line of code is executed:
Notification Switch
Would you like to follow the 'Ti dsp/bios lab' conversation and receive update notifications?