I found a couple of places online which state that CLREX "must" be called whenever an interrupt routine is entered, which I don't understand. The docs for CLREX state (added the numbering for easier reference):
(1) Clears the local record of the executing processor that an address has had a request for an exclusive access.
(2) Use the
CLREX
instruction to return a closely-coupled exclusive access monitor to its open-access state. This removes the requirement for a dummy store to memory.(3) It is implementation-defined whether
CLREX
also clears the global record of the executing processor that an address has had a request for an exclusive access.
I don't understand pretty much anything here.
I had the impression that writing something along the lines the example in the docs was enough to guarantee atomicity:
MOV r1, #0x1 ; load the ‘lock taken’ value
try: <---\
LDREX r0, [LockAddr] ; load the lock value |
CMP r0, #0 ; is the lock free? |
STREXEQ r0, r1, [LockAddr] ; try and claim the lock |
CMPEQ r0, #0 ; did this succeed? |
BNE try ; no - try again ------------/
.... ; yes - we have the lock
Why should the "local record" need to be cleared? I thought that
LDREX
/STREX
are enough to guarantee atomic access to an address from several interrupts? I.e. GCC for ARM compiles all C11 atomic functions usingLDREX
/STREX
and I don't seeCLREX
being called anywhere.What "requirement for a dummy store" is the second paragraph referring to?
What is the difference between the global record and a local record? Is global record needed for multi-core scenarios?
SWP
(and some other restrictions). I suspect it is for cleanliness when an LDREX/STREX pair are broken by an interrupt. - but I don't understand the reason again, theLDREX
/STREX
are explicitly created to solve the issue of different interrupts breaking in between. – Hengswp
is deprecated and cannot ensure the stated interrupt latency, so I don't see what could be "taken out of context". The accepted answer states thatCLREX
is required for multithreaded scenarios where threads can be preempted and explains how this can happen even on a single-core system, while your comment said it's for "cleanliness". – Heng