MDK-ARM Keil, MDK-ARM Datasheet - Page 32

KIT REALVIEW MCU DEVELOPMENT

MDK-ARM

Manufacturer Part Number
MDK-ARM
Description
KIT REALVIEW MCU DEVELOPMENT
Manufacturer
Keil
Type
Compiler and IDEr
Datasheets

Specifications of MDK-ARM

For Use With/related Products
ARM MCUs
Lead Free Status / RoHS Status
Lead free / RoHS Compliant
32
Chapter 2. Developing With an RTOS
Semaphores
Like events, semaphores are a method of synchronizing activity between two or
more tasks. Put simply, a semaphore is a container that holds a number of
tokens. As a task executes, it will reach an RTOS call to acquire a semaphore
token. If the semaphore contains one or more tokens, the task will continue
executing and the number of tokens in the semaphore will be decremented by
one. If there are currently no tokens in the semaphore, the task will be placed in
a WAITING state until a token becomes available. At any point in its execution,
a task may add a token to the semaphore causing its token count to increment by
one.
Semaphores are used to control
access to program resources.
Before a task can access a
resource, it must acquire a token.
If none is available, it waits.
When it is finished with the
resource, it must return the
token.
The diagram illustrates the use of
a semaphore to synchronize two
tasks. First, the semaphore must
be created and initialized with an
initial token count. In this case,
the semaphore is initialized with a single token. Both tasks run and reach a point
in their code where they will attempt to acquire a token from the semaphore. The
first task to reach this point will acquire the token from the semaphore and
continue execution. The second task will also attempt to acquire a token, but as
the semaphore is empty, it will halt execution and be placed into a WAITING
state until a semaphore token is available.
Meanwhile, the executing task can release a token back to the semaphore. When
this happens, the waiting task will acquire the token and leave the WAITING
state for the READY state. Once in the READY state, the scheduler will place
the task into the RUN state so that task execution can continue. Although
semaphores have a simple set of RTX API calls, they can be one of the more
difficult RTX objects to fully understand. In this section, we will first look at
how to add semaphores to an RTOS program and then go on to look at the most
useful semaphore applications.

Related parts for MDK-ARM