We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
int atomic_inc(volatile int *ptr){ int result, modified; asm volatile ("1:" "\t@ loop label" "\n\t" "LDREX %0,[%1]" "\t@ load value" "\n\t" "ADD %0,%0,#1" "\t@ increment it" "\n\t" "STREX %2,%0,[%1]" "\t@ attempt store" "\n\t" "CMP %2,#0" "\t@ if not atomic" "\n\t" "BNE 1b" "\t@ then repeat" "\n\t" : "=&r" (result) : "r" (ptr), "r" (modified) : "cc", "memory" ); return result;}