Hi,
The code is:
1. ldrex r1, [r0] 2. add r1 3. strex r2, r1, [r0]
There are three tasks: A, B, C.
First: task A run line1, and it gets r1 as 0, now local monitor is "Exclusive Access State", and then A is pre-empted by B.
B runs line1, line2, line3, it write r0 as 1 ok. The Local monitor is "Open Access State".
C runs line1, the local monitor is "Exclusive Access State" now, and C is pre-empted by A.
A runs line2, line3, and it success, now r0 is 1.
As above, A and B both success, but r0 is 1 .
Can I say that : ldrex/strex is breaked by the third task ?
Normally a task switcher should clear the exclusive monitor. That's why B can write the one.C is preempted and therefore the exclusive monitor is cleared again.
But A should see that the write of (r1+1) will not succeed (r2 == 1).
Anyway your code misses the check of r2 and the branch back to line 1.