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;}