Semaphores
MPC5510 Microcontroller Family Reference Manual, Rev. 1
18-10
Freescale Semiconductor
Preliminary
18.4
Functional Description
Multi-processor systems require a function that can be used to safely and easily provide a locking
mechanism that is then used by system software to control access to shared data structures, shared
hardware resources, and etc. These gating mechanisms are used by the software to serialize (and
synchronize) writes to shared data and/or resources to prevent race conditions and preserve memory
coherency between processes and processors.
For example, if processor X enters a section of code where shared data values are to be updated or read
coherently, it must first acquire a semaphore. This locks, or closes, a software gate. After the gate has been
locked, a properly-architected software system does not allow other processes (or processors) to execute
the same code segment or modify the shared data structure protected by the gate, that is, other
processes/processors are locked out. Many software implementations include a spin-wait loop within the
lock function until the locking of the gate is accomplished. After the lock has been obtained, processor X
continues execution and updates the data values protected by the particular lock. After the updates are
complete, processor X unlocks (or opens) the software gate, allowing other processes/processors access to
the updated data values.
There are three important rules that must be followed for a correctly-implemented system solution:
•
All writes to shared data values or shared hardware resources must be protected by a gate variable.
•
After a processor locks a gate, accesses to the shared data or resources by other
processes/processors must be blocked. This is enforced by software conventions.
•
The processor that locks a particular gate is the only processor that can unlock, or open, that gate.
Information in the hardware gate identifying the locking processor can be useful for system-level
debugging.
The Hennessy/Patterson text on computer architecture offers this description of software gating:
“One of the major requirements of a shared-memory architecture multiprocessor is being able to
coordinate processes that are working on a common task. Typically, a programmer will use
lock
variables
to synchronize the processes.
The difficulty for the architect of a multiprocessor is to provide a mechanism to decide which
processor gets the lock and to provide the operation that locks a variable. Arbitration is easy for
shared-bus multiprocessors, since the bus is the only path to memory. The processor that gets the
bus locks out all the other processors from memory. If the CPU and bus provide an atomic swap
operation, programmers can create locks with the proper semantics. The adjective
atomic
is key,
RSTNTN
Reset Notification Number. This 8-bit field specifies the specific IRQ notification state machine to be reset. This
field is updated by the second write.
If RSTNTN < 64, then reset the single IRQ notification machine defined by RSTNTN, else reset all the
notifications.
RSTNDP
Reset Notification Data Pattern. This write-only field is accessed with the specified data patterns on the two
consecutive writes to enable the notification reset mechanism. For the first write, RSTNDP = 0x47 while the
second write requires RSTNDP = 0xb8.
Table 18-6. SEMA4_RSTGT Field Descriptions
Field
Description