Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Include Page
css.Synchronization.css
css.Synchronization.css
Include Page
css.webworks.css
css.webworks.css

Anchor
1053843
1053843
Semaphores

Anchor
1053844
1053844
As defined in Chapter 13, “Resource Management” on page 211, a semaphore is a protocol mechanism offered by most multitasking kernels. Semaphores were originally used to control access to shared resources. However, better mechanisms exist to protect access to shared resources, as described in Chapter 12. Semaphores are best used to synchronize an ISR to a task, or synchronize a task with another task as shown in Figure 14-1.

Anchor
1053845
1053845
Note that the semaphore is drawn as a ?ag to indicate that it is used to signal the occurrence of an event. The initial value for the semaphore is typically zero (0), indicating the event has not yet occurred.

Anchor
1053846
1053846
The value “N” next to the flag indicates that the semaphore can accumulate events or credits. An ISR (or a task) can post (or signal) multiple times to a semaphore and the semaphore will remember how many times it was posted. It is possible to initialize the semaphore with a value other than zero, indicating that the semaphore initially contains that number of events.

Anchor
1054431
1054431
Also, the small hourglass close to the receiving task indicates that the task has an option to specify a timeout. This timeout indicates that the task is willing to wait for the semaphore to be signaled (or posted to) within a certain amount of time. If the semaphore is not signaled within that time, µC/OS-III resumes the task and returns an error code indicating that the task was made ready-to-run because of a timeout and not the semaphore was signaled.

Anchor
1081012
1081012
Image Added

    1. Anchor
      1054432
      1054432
      µC/OS-III Semaphore Services

Anchor
1053849
1053849
There are a number of operations to perform on semaphores as summarized in Table 14-1 and Figure 14-1. However, in this chapter, we will only discuss the three functions used most often: OSSemCreate(), OSSemPend(), and OSSemPost(). The other functions are described in Appendix A, “µC/OS-III API Reference” on page 431. Also note that every semaphore function is callable from a task, but only OSSemPost() can be called by an ISR.

Anchor
1081316
1081316
 

HTML Table
summary
classPlain_Table
Table Row (tr)
Table Cell (td)

Anchor
1054449
1054449
Function Name

Table Cell (td)

Anchor
1054451
1054451
Operation

Table Row (tr)
Table Cell (td)

Anchor
1054453
1054453
OSSemCreate()

Table Cell (td)

Anchor
1054455
1054455
Create a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
1054457
1054457
OSSemDel()

Table Cell (td)

Anchor
1054459
1054459
Delete a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
1054461
1054461
OSSemPend()

Table Cell (td)

Anchor
1054463
1054463
Wait on a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
1054465
1054465
OSSemPendAbort()

Table Cell (td)

Anchor
1054467
1054467
Abort the wait on a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
1054469
1054469
OSSemPost()

Table Cell (td)

Anchor
1054471
1054471
Signal a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
1054473
1054473
OSSemSet()

Table Cell (td)

Anchor
1054475
1054475
Force the semaphore count to a desired value.

    1. Anchor
      1053857
      1053857
      Semaphore API summary

Anchor
1053858
1053858
When used for synchronization, a semaphore keeps track of how many times it was signaled using a counter. The counter can take values between 0 and 255, 65,535, or 4,294,967,295, depending on whether the semaphore mechanism is implemented using 8, 16, or 32 bits, respectively. For µC/OS-III, the maximum value of a semaphore is determined by the data type OS_SEM_CTR (see os_type.h), which is changeable, as needed (assuming access to µC/OS-III’s source code). Along with the semaphore’s value, µC/OS-III keeps track of tasks waiting for the semaphore to be signaled.