Why is the xadd instruction used to get atomic operation?
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?
Hi... On Thu, Dec 15, 2011 at 11:05, Parmenides <mobile.parmenides@gmail.com> wrote:
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?
Wikipedia explains about it: http://en.wikipedia.org/wiki/Fetch-and-add Although I am not also so clear, it says something related to returning original value memory location. Maybe that's needed since we pass v by reference, not by value -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
participants (2)
-
Mulyadi Santosa -
Parmenides