SEM Module
2-370
Description
The SEM module makes available a set of functions that manipulate
semaphore objects accessed through handles of type SEM_Handle.
Semaphores can be used for task synchronization and mutual exclusion.
Semaphores can be counting semaphores or binary semaphores. The
APIs for binary and counting semaphores cannot be mixed for a single
semaphore.
❏
Counting semaphores
keep track of the number of times the
semaphore has been posted with SEM_post. This is useful, for
example, if you have a group of resources that are shared between
tasks. Such tasks might call SEM_pend to see if a resource is
available before using one. SEM_pend and SEM_post are for use
with counting semaphores.
❏
Binary semaphores
can have only two states: available and
unavailable. They can be used to share a single resource between
tasks. They can also be used for a basic signaling mechanism, where
the semaphore can be posted multiple times and a subsequent call
to SEM_pendBinary clears the count and returns. Binary
semaphores do not keep track of the count; they simply track
whether the semaphore has been posted or not. SEM_pendBinary
and SEM_postBinary are for use with binary semaphores.
The MBX module uses a counting semaphore internally to manage the
count of free (or full) mailbox elements. Another example of a counting
semaphore is an ISR that might fill multiple buffers of data for
consumption by a task. After filling each buffer, the ISR puts the buffer on
a queue and calls SEM_post. The task waiting for the data calls
SEM_pend, which simply decrements the semaphore count and returns
or blocks if the count is 0. The semaphore count thus tracks the number
of full buffers available for the task. The GIO and SIO modules follow this
model and use counting semaphores.
The internal data structures used for binary and counting semaphores
are the same; the only change is whether semaphore values are
incremented and decremented or simply set to zero and non-zero.
SEM_pend and SEM_pendBinary are used to wait for a semaphore. The
timeout parameter allows the task to wait until a timeout, wait indefinitely,
or not wait at all. The return value is used to indicate if the semaphore
was signaled successfully.
SEM_post and SEM_postBinary are used to signal a semaphore. If a
task is waiting for the semaphore, SEM_post/SEM_postBinary removes
the task from the semaphore queue and puts it on the ready queue. If no