This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

atomic operations

Note: This was originally posted on 31st August 2010 at http://forums.arm.com

Hi all,

I have to migrate Windows Code to Linux for an ARM processor. The Windows code uses the InterlockedIncrement function of the Windows API. I found a way to create functions for Linux which behave the same way as the Windows functions but it seems that this only works on Intel processors. The function I use is __sync_fetch_and_add (type *ptr, type value, ...). [url="http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Atomic-Builtins.html"]http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gc...c-Builtins.html[/url]
Is there something similar for ARM?

thanks for your answers,
Chris from Austria
Parents
  • Note: This was originally posted on 1st October 2010 at http://forums.arm.com

    The following GCC code implements an atomic increment akin to InterlockIncrement:

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


    hth
    s.
Reply
  • Note: This was originally posted on 1st October 2010 at http://forums.arm.com

    The following GCC code implements an atomic increment akin to InterlockIncrement:

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


    hth
    s.
Children
No data