Download TMS320 DSP/BIOS User's Guide
Transcript
Semaphores 4.6 Semaphores DSP/BIOS provides a fundamental set of functions for intertask synchronization and communication based upon semaphores. Semaphores are often used to coordinate access to a shared resource among a set of competing tasks. The SEM module provides functions that manipulate semaphore objects accessed through handles of type SEM_Handle. SEM objects are counting semaphores that can be used for both task synchronization and mutual exclusion. Counting semaphores keep an internal count of the number of corresponding resources available. When count is greater than 0, tasks do not block when acquiring a semaphore. The functions SEM_create and SEM_delete are used to create and delete semaphore objects, respectively, as shown in Example 4-7. You can also use the Configuration Tool to create semaphore objects. See section 2.2.8, Creating, Referencing, and Deleting Dynamically-Created DSP/BIOS Objects, page 2-10, for a discussion of the benefits of creating objects with the Configuration Tool. Example 4-7. Creating and Deleting a Semaphore SEM_Handle SEM_create(count, attrs); Uns count; SEM_Attrs *attrs; Void SEM_delete(sem); SEM_Handle sem; The semaphore count is initialized to count when it is created. In general, count is set to the number of resources that the semaphore is synchronizing. SEM_pend waits for a semaphore. If the semaphore count is greater than 0, SEM_pend simply decrements the count and returns. Otherwise, SEM_pend waits for the semaphore to be posted by SEM_post. Note: When called within an HWI, the code sequence calling SEM_post or SEM_ipost must be either wrapped within an HWI_enter/HWI_exit pair or invoked by the HWI dispatcher. The timeout parameter to SEM_pend, as shown in Example 4-8, allows the task to wait until a timeout, to wait indefinitely (SYS_FOREVER), or to not wait at all (0). SEM_pend’s return value is used to indicate if the semaphore was acquired successfully. Thread Scheduling 4-49