Why is the xadd instruction used to get atomic operation?

Mulyadi Santosa mulyadi.santosa at gmail.com
Thu Dec 15 10:10:54 EST 2011


Hi...

On Thu, Dec 15, 2011 at 11:05, Parmenides <mobile.parmenides at 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



More information about the Kernelnewbies mailing list