Why is the xadd instruction used to get atomic operation?
Parmenides
mobile.parmenides at gmail.com
Wed Dec 14 23:05:43 EST 2011
Hi,
xadd is used in atomic function atomic_add_return() like this:
188static __inline__ int atomic_add_return(int i, atomic_t *v)
189{
190 int __i;
... ... ...
195 /* Modern 486+ processor */
196 __i = i;
197 __asm__ __volatile__(
198 LOCK "xaddl %0, %1;"
199 :"=r"(i)
200 :"m"(v->counter), "0"(i));
201 return i + __i;
202
... ... ...
211}
The xadd will store v->counter into i, and store v->counter+i into
v->counter. I wonder why we bother to obtain v->counter atomically. I
try to modify the asm with the following code:
static __inline__ int atomic_add_return(int i, atomic_t *v)
{
__asm__ __volatile__(
LOCK "addl %1, %0;"
: "m"(v->counter)
: "=r"(i), "m"(v->counter));
return v->counter;
}
Does my code may cause some problems concerning atomic operation?
More information about the Kernelnewbies
mailing list