Arm Community
Site
Search
User
Site
Search
User
Support forums
Arm Development Studio forum
InterlockedExchangeAdd equivalent
Jump...
Cancel
Locked
Locked
Replies
9 replies
Subscribers
118 subscribers
Views
6146 views
Users
0 members are here
Options
Share
More actions
Cancel
Related
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
Doug Price
over 12 years ago
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
Doug Price
over 12 years ago
Note: This was originally posted on 11th February 2013 at
http://forums.arm.com
Thanks so much for your help. For anyone else that may be interested, here's what I came up with.
long myExchangeAdd( volatile long *pTarget, long nValue )
{
#if defined( _WIN32 )
return InterlockedExchangeAdd( pTarget, nValue );
#else
int nResult, nModified;
asm volatile
(
"1:" "\t@ loop label" "\n\t"
"LDREX %0,[%2]" "\t@ load value" "\n\t"
"ADD %0,%0,%3" "\t@ increment it" "\n\t"
"STREX %1,%0,[%2]" "\t@ attempt store" "\n\t"
"CMP %1,#0" "\t@ if not atomic" "\n\t"
"BNE 1b" "\t@ then repeat" "\n\t"
: "=&r" (nResult), "=&r" (nModified)
: "r" (pTarget), "r" (nValue)
: "cc", "memory"
);
return nResult;
#endif
}
Cancel
Vote up
0
Vote down
Cancel
Reply
Doug Price
over 12 years ago
Note: This was originally posted on 11th February 2013 at
http://forums.arm.com
Thanks so much for your help. For anyone else that may be interested, here's what I came up with.
long myExchangeAdd( volatile long *pTarget, long nValue )
{
#if defined( _WIN32 )
return InterlockedExchangeAdd( pTarget, nValue );
#else
int nResult, nModified;
asm volatile
(
"1:" "\t@ loop label" "\n\t"
"LDREX %0,[%2]" "\t@ load value" "\n\t"
"ADD %0,%0,%3" "\t@ increment it" "\n\t"
"STREX %1,%0,[%2]" "\t@ attempt store" "\n\t"
"CMP %1,#0" "\t@ if not atomic" "\n\t"
"BNE 1b" "\t@ then repeat" "\n\t"
: "=&r" (nResult), "=&r" (nModified)
: "r" (pTarget), "r" (nValue)
: "cc", "memory"
);
return nResult;
#endif
}
Cancel
Vote up
0
Vote down
Cancel
Children
No data