Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Include Page
css.SynchronizationResource Management.css
css.SynchronizationResource Management.css
Include Page
css.webworks.css
css.webworks.css

Anchor
105384310498911053843
1049891
Semaphores

Anchor
105384410498921053844
As defined in Chapter 13, “Resource Management” on page 211
1049892
A semaphore was originally a mechanical signaling mechanism. The railroad industry used the device to provide a form of mutual exclusion for railroads tracks shared by more than one train. In this form, the semaphore signaled trains by closing a set of mechanical arms to block a train from a section of track that was currently in use. When the track became available, the arm would swing up and the waiting train would then proceed.

Anchor
1049893
1049893
The notion of using a semaphore in software as a means of mutual exclusion was invented by the Dutch computer scientist Edgser Dijkstra in 1959. In computer software, a semaphore is a protocol mechanism offered by most multitasking kernels. Semaphores were , originally used to control access to shared resources, but now they are used for synchronization as described in Chapter 14, “Synchronization” on page 251. 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-1it is useful to describe how semaphores can be used to share resources. The pitfalls of semaphores will be discussed in a later section.

Anchor
105384510498951053845
1049895
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. Anchor10538461053846 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. Anchor10544311054431 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. Anchor10810121081012 Image Removed

...

Anchor10538491053849 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 A semaphore was originally a “lock mechanism” and code acquired the key to this lock to continue execution. Acquiring the key means that the executing task has permission to enter the section of otherwise locked code. Entering a section of locked code causes the task to wait until the key becomes available.

Anchor
1078200
1078200
Typically, two types of semaphores exist: binary semaphores and counting semaphores. As its name implies, a binary semaphore can only take two values: 0 or 1. A counting semaphore allows for 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 can be changed as needed. Along with the semaphore’s value, µC/OS-III also keeps track of tasks waiting for the semaphore’s availability.

Anchor
1049897
1049897
Only tasks are allowed to use semaphores when semaphores are used for sharing resources; ISRs are not allowed.

Anchor
1049898
1049898
A semaphore is a kernel object defined by the OS_SEM data type, which is defined by the structure os_sem (see os.h). The application can have any number of semaphores (limited only by the amount of RAM available).

Anchor
1049899
1049899
There are a number of operations the application is able to perform on semaphores, summarized in Table 13-2. In this chapter, only three functions used most often are discussed: OSSemCreate(), OSSemPend(), and OSSemPost(). The other Other functions are described in Appendix A, “µC/OS-III API Reference” on page 431. Also note that When semaphores are used for sharing resources, every semaphore function is callable must be called from a task , but only OSSemPost() can be called by and never from an ISR. The same limitation does not apply when using semaphores for signaling, as described later in Chapter 13.

Anchor
108131610795861081316
1079586
 

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

Anchor
105444910511401054449
1051140
Function Name

Table Cell (td)

Anchor
105445110511421054451
1051142
Operation

Table Row (tr)
Table Cell (td)

Anchor
105445310511441054453
1051144
OSSemCreate()

Table Cell (td)

Anchor
105445510511461054455
1051146
Create a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
105445710511481054457
1051148
OSSemDel()

Table Cell (td)

Anchor
105445910511501054459
1051150
Delete a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
105446110511521054461
1051152
OSSemPend()

Table Cell (td)

Anchor
105446310511541054463
1051154
Wait on a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
105446510511561054465
1051156
OSSemPendAbort()

Table Cell (td)

Anchor
105446710511581054467
1051158
Abort the wait on a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
105446910511601054469
1051160
OSSemPost()

Table Cell (td)

Anchor
105447110511621054471
1051162
Signal Release or signal a semaphore.

Table Row (tr)
Table Cell (td)

Anchor
105447310511641054473
1051164
OSSemSet()

Table Cell (td)

Anchor
105447510511661054475
1051166
Force the semaphore count to a desired value.

    1. Anchor
      105385710499081053857
      1049908
      Semaphore API summary

...