1.5.7 Synchronization Primitives
The Cortex-M4F instruction set includes pairs of synchronization primitives. These primitives provide a nonblocking mechanism that a thread or process can use to obtain exclusive access to a memory location. Software can use these primitives to perform an ensured read-modify-write memory update sequence or for a semaphore mechanism.
NOTE
The available pairs of synchronization primitives are available only for single processor use and should not be used with multiprocessor systems.
A pair of synchronization primitives consists of:
- A Load-Exclusive instruction, which is used to read the value of a memory location and requests exclusive access to that location.
- A Store-Exclusive instruction, which is used to try to write to the same memory location and returns a status bit to a register. If this status bit is clear, it indicates that the thread or process gained exclusive access to the memory and the write succeeds. If this status bit is set, it indicates that the thread or process did not gain exclusive access to the memory and no write was performed.
The pairs of Load-Exclusive and Store-Exclusive instructions are:
- The word instructions LDREX and STREX
- The halfword instructions LDREXH and STREXH
- The byte instructions LDREXB and STREXB
Software must use a Load-Exclusive instruction with the corresponding Store-Exclusive instruction.
To perform an exclusive read-modify-write of a memory location, software must:
- Use a Load-Exclusive instruction to read the value of the location.
- Modify the value, as required.
- Use a Store-Exclusive instruction to try to write the new value back to the memory location.
- Test the returned status bit.
If the status bit is clear, the read-modify-write completed successfully. If the status bit is set, no write was performed, which indicates that the value returned at step 1 might be out of date. The software must retry the entire read-modify-write sequence.
Software can use the synchronization primitives to implement a semaphore as follows:
- Use a Load-Exclusive instruction to read from the semaphore address to check whether the semaphore is free.
- If the semaphore is free, use a Store-Exclusive instruction to write the claim value to the semaphore address.
- If the returned status bit from Step 2 indicates that the Store-Exclusive succeeded, then the software has claimed the semaphore. However, if the Store-Exclusive instruction failed, another process might have claimed the semaphore after the software performed Step 1.
The Cortex-M4F includes an exclusive access monitor that tags the fact that the processor has executed a Load-Exclusive instruction. The processor removes its exclusive access tag if:
- It executes a CLREX instruction.
- It executes a Store-Exclusive instruction, regardless of whether the write succeeds.
- An exception occurs, which means the processor can resolve semaphore conflicts between different threads.
For more information about the synchronization primitive instructions, see the Cortex-M4 instruction set chapter in the Arm Cortex-M4 Devices Generic User Guide.