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

InterlockedExchangeAdd equivalent

Note: This was originally posted on 11th February 2013 at http://forums.arm.com

I'm porting a Windows application to an ARM® Cortex-A9 based system running Ubuntu 12.04 and I need something equivalent the Windows function InterlockedExchangeAdd().  Is there a function like this available for ARM?  If not, can anyone provide some equivalent assembly code?
Parents
  • Note: This was originally posted on 13th February 2013 at http://forums.arm.com

    Here's a slight modification since InterlockedExchangeAdd actually returns the previous value

    long myExchangeAdd( volatile long *pTarget, long nValue )
    {
    #if defined( _WIN32 )
    return InterlockedExchangeAdd( pTarget, nValue );
    #else
    int nOldValue, nNewValue, nModified;

    asm volatile
    (
         "1: LDREX %0,[%3]     @load value\n"
         " ADD   %1,%0,%4   @increment it\n"
         " STREX %2,%1,[%3] @attempt store\n"
         " CMP   %2,#0           @if not atomic\n"
         " BNE   1b                   @then repeat\n"
         : "=&r" (nOldValue), "=&r" (nNewValue), "=&r" (nModified)
         : "r" (pTarget), "r" (nValue)
         : "cc", "memory"
    );

    return nOldValue;
    #endif
    }
Reply
  • Note: This was originally posted on 13th February 2013 at http://forums.arm.com

    Here's a slight modification since InterlockedExchangeAdd actually returns the previous value

    long myExchangeAdd( volatile long *pTarget, long nValue )
    {
    #if defined( _WIN32 )
    return InterlockedExchangeAdd( pTarget, nValue );
    #else
    int nOldValue, nNewValue, nModified;

    asm volatile
    (
         "1: LDREX %0,[%3]     @load value\n"
         " ADD   %1,%0,%4   @increment it\n"
         " STREX %2,%1,[%3] @attempt store\n"
         " CMP   %2,#0           @if not atomic\n"
         " BNE   1b                   @then repeat\n"
         : "=&r" (nOldValue), "=&r" (nNewValue), "=&r" (nModified)
         : "r" (pTarget), "r" (nValue)
         : "cc", "memory"
    );

    return nOldValue;
    #endif
    }
Children
No data